CMPS 144L: Lab Activity on Max-Heaps

A Max-Heap
                         37 
                        /  \
                       /    \
                      /      \
                     /        \
                    /          \
                   /            \
                  /              \
                 /                \
                /                  \
               /                    \
             30                      26 
            /  \                    /  \
           /    \                  /    \
          /      \                /      \
         /        \              /        \
       15          24          20          12
       /\          /\          /\          /\
      /  \        /  \        /  \        /  \
     /    \      /    \      /    \      /    \
    /      \    /      \    /      \    /      \
   8        2  5       17  16      13  3        0
  / \      /  
 /   \    / 
6     4  0

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

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 fewer elements.)

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

         +----+
         |    |
numNodes |    |
         |    |
         +----+ 

2. Carry out the following operations in succession, for each one showing how the heap —and thus the array that represents it— has changed as a result. (Here it suffices to identify which locations in the array have been modified and what their new values are.)

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

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

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