COM 1201 Algorithms and Data Structures 2

Notes on Quiz #2 -- To be given Thursday, March 2nd

Winter 2000 -- Professor Futrelle
College of Computer Science, Northeastern U., Boston, MA

(Version of 2/29/00)

The notes below are essentially a version of the actual quiz in which the critical details that will appear on the actual quiz have been omitted. This should help you prepare. Omissions are denoted by question marks, etc. Please do not write me to clarify what I've left out. I'm giving you a lot here! If you have questions, just study your textbook and make up some examples and work through them until you're comfortable that you know how the code works and how it relates to the trees and/or arrays.

Question 1.This question deals with a heap (not a binary search tree!). Below, you are given code and you are to explain how it operates for a specific case.

  1. Your are to begin with the heap shown below . Draw and fill in the array corresponding to the heap.
    < A PICTURE OF A HEAP WILL APPEAR HERE.>
  2. Assume you are to insert a new item with key ??, or remove the maximum item using this code:
    CODE HERE
  3. The critical lines in the ??() code below are numbered ??.
    CODE FOR FIXUP OR FIXDOWN HERE
  4. The while loop in ??() is executed a number of times to accomplish the insert or remove.
  5. For each of the times the while loop executes, show the following pieces of information:
  6. What is the value of ?? that finally leads to the while condition failing and to the termination of the loop?

 

Question 2. This question deals with a binary search tree (BST) (not a heap!). It proceeds in much the same way that Question 1 did, in which you are given code and you are to explain how it operates for a specific case.

  1. Your are to begin with the BST shown below.
    < A PICTURE OF A BST WILL APPEAR HERE.>
  2. Assume you are to insert a new item with key ??, using this code:
    
    void insert(Item x)
      { Key v = x.key();
        if (head == 0) 
          { head = new node(x); return; }
        link p = head;
       L1: for (link q = p; q != 0; p = q ? q : p)
        L2:  q = (v < q->item.key()) ? q->l : q->r; 
        L3:if (v < p->item.key()) p->l = new node(x); 
                          else p->r = new node(x);
      }
    
    
  3. The three critical pieces in the insert() code above are numbered.
  4. The for loop in insert() is executed a number of times to accomplish the insert of the item ??.
  5. For each of the times the for loop executes, show the following three pieces of information: