CMPS 144L
Lab Activity on Max-Heaps

                   
Name __________________________
A Max-Heap
                                0:37 
                                /  \
                               /    \
                              /      \
                             /        \
                            /          \
                           /            \
                          /              \
                         /                \
                        /                  \
                       /                    \
                      /                      \
                     /                        \
                    /                          \
                   /                            \
                 1:30                           2:26 
                 /  \                           /  \
                /    \                         /    \
               /      \                       /      \
              /        \                     /        \
             /          \                   /          \
            /            \                 /            \
         3:15           4:24             5:20          6:12
          /\             /\               /\            /\
         /  \           /  \             /  \          /  \
        /    \         /    \           /    \        /    \
       /      \       /      \         /      \      /      \
      /        \     /        \       /        \    /        \
    7:8       8:2   9:5     10:17  11:16    12:13 13:3      14:0
    / \       /  
   /   \     / 
 15:6 16:4 17:0
A max-heap (in which each node is labeled by its ID and its key) is shown to the right. Space in which to provide answers to the following problems is found below.

1. Show the contents of an array that represents the pictured heap, in accord with the standard scheme by which a complete binary tree is mapped to an array.

Because the size of a heap can grow and shrink over time, but an array's length remains fixed, an array will typically have a length that exceeds the current size of the heap that it represents. Thus, a numNodes variable is also needed in the concrete representation. (Here, we suppose that the array has length 20, even though the heap has only 18 elements.)

2. Carry out the following operations in succession, for each one showing how the heap has changed as a result. (Here it suffices to show only the nodes along the path where changes have occurred, perhaps with a little surrounding context.)

(a) insert(18)       (b) insert(32)

3. Revert back to the original heap, as pictured, and carry out the following operations in succession, for each one showing how the heap has changed (just as you did in the previous problem).

(a) deleteMax()       (b) deleteMax()

Answers:

     0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19    numNodes
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+   +------+
1. |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |      |
   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |      |
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+   +------+

Use the rest of the page (and back, if needed) for your answers to Problems 2 and 3.