CMPS 144L: Programming Lab Activity on Max-Heaps

An instance of the MaxHeapOfInt class represents, as its name suggests, a max-heap containing integers. Three of its methods are stubbed: getMax(), siftUp(), and siftDown().

To complete getMax() should be a no-brainer.

It is recommended that you next try to complete siftUp(). To test its correctness, use the HeapTester application by choosing to start with an empty heap and then trying to do some insertions (by invoking the Insert value into heap option). (The insert() method makes use of siftUp().)

After getting siftUp() to work, focus your attention upon siftDown(), which is similar in concept but somewhat trickier to implement.

A good way to test the correctness of siftDown() is to choose either the Load some values into a new heap option or the Delete max value from heap option.


Dialog with HeapTester:

$ java HeapTester
Welcome to the Heap Tester program.

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 3
Enter a number to insert: 5

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 3
Enter a number to insert: 9

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 3
Enter a number to insert: 0

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 6
0:9
   1:5
   2:0

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 3
Enter a number to insert: 12

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 3
Enter a number to insert: -5

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 6
0:12
   1:9
      3:5
      4:-5
   2:0

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 4
12

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 2
Enter (one or more) values to load into a new heap: 12 0 17 19 -3 45 21 18 6 9

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 6
0:45
   1:19
      3:18
         7:0
         8:6
      4:9
         9:-3
   2:21
      5:17
      6:12

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 5

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 6
0:21
   1:19
      3:18
         7:0
         8:6
      4:9
   2:17
      5:-3
      6:12

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 5

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 6
0:19
   1:18
      3:6
         7:0
      4:9
   2:17
      5:-3
      6:12

(1) Create new empty heap
(2) Load some values into a new heap
(3) Insert value into heap
(4) Show max value in heap
(5) Delete max value from heap
(6) Display heap
(7) Quit
> 7

Goodbye from the Heap Tester program.