Assignment 4 -- due Wednesday, February 14 at the beginning of lab
CS 4521 Spring Semester, 2007
20 Points

Topics: Binary Heaps

The assignment: consists of two parts: in the first part, you are asked to implement binary heaps, and in the second part you will prove facts about them.

Part 1: Implementing and testing binary heaps (14 points)

Implement min-heaps as described in Chapter 6 of the text. This will require coding the operations:
CREATE-HEAP, MIN-HEAPIFY, HEAP-EXTRACT-MIN, and MIN-HEAP-INSERT. Of the four operations, all but CREATE-HEAP are described in the text and lecture notes. I suggest that you represent binary heaps by a pair consisting of a 1000-element array of integers and the heap's size, heap-size. Then all CREATE-HEAP does is allocate such a pair with heap-size equal to zero. You will also need to code a ShowBinaryHeap() operation, discussed below, for displaying the structure of a binary heap.
Note: For this lab you will use the min-heap property:
      "The key value for any node in the heap is less than or equal to the key value for its children (if any)."
Thus, the node with minimum key is the root. This is opposite of the max-heap property illustrated in the text.

You will test your implementation using test files containing the following heap commands:

c create an empty heap
i key insert node with key key into the heap
d delete node with minimum key from the heap i.e. extract min
S Show (display) the heap structure
+ turn on print key flag for extract min
- turn off print key flag for extract min
# echo comment line

The 'c', 'i', and 'd' commands correspond to the CREATE-HEAP, MIN-HEAP-INSERT, and HEAP-EXTRACT-MIN operations. The 'S' command "Shows" the heap structure; it is implemented by the ShowBinaryHeap(), discussed below. The '+' and '-' commands turn on and off, respectively, a flag indicating whether the key of the extracted node is to be printed on the screen (useful for debugging). Finally, if a command line begins with a '#', that line is interpreted as a comment and simply echoed to standard output. Lines beginning with any other character (e.g. a blank) are ignored.

To do this, you need to write a simple command interpreter that reads and parses these commands. It need not be super robust or full of features; it only needs to be able to minimally interpret these commands and dispatch control to the appropriate procedure. Your work on this interpreter won't be wasted, because it can be used again for the testing of other data structures in the course. Thus the code for this interpreter should be in a separate module from the code for your heap operations.

You will test your implementation on these command files: heaptest1, heaptest2, and heaptest3
Here are the commands in heaptest1:



+---------------------------------------------------------------+
| # create empty heap						|
| c								|
| 								|
| # insert 4 items, printing the heap after each insert		|
| i 491								|
| S								|
| i 122								|
| S								|
| i 245								|
| S								|
| i 474								|
| S								|
| 								|
| # Extract mins until empty, printing the heap after each delete
| -							  	|
| d								|
| S								|
| d								|
| S								|
| d								|
| S								|
| d								|
| S								|
| 								|
| # Extract min from empty heap					|
| d								|
| 								|
| # end of test							|
+---------------------------------------------------------------+
The following is the correct output for heaptest1:
+---------------------------------------------------------------+
| 								|
| # create empty heap						|
| 								|
| # insert 4 items, printing the heap after each insert		|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|     491							|
| 								|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|     122							|
|           491							|
| 								|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|           245							|
|     122							|
|           491							|
| 								|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|           245							|
|     122							|
|           474							|
|                 491						|
| 								|
| 								|
| # Extract mins until empty, printing the heap after each delete
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|           491							|
|     245							|
|           474							|
| 								|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|     474							|
|           491							|
| 								|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
|     491							|
| 								|
| 								|
| Structure of heap (rotated 90 degrees to left):		|
| 								|
| 								|
| 								|
| # Extract min from empty heap					|
| Heap Empty							|
| 								|
| # end of test							|
| 								|
+---------------------------------------------------------------+

The ShowBinaryHeap() routine.
It is quite easy to use recursion to display the structure of the heap if the it is oriented on the page (or screen) so that the root is at the far left and it branches off toward the right. The algorithm is:
ShowBinaryHeap(i, depth)   |> i is the index of a subheap with given depth
  if i <= heap-size
    then ShowBinaryHeap(RIGHT(i), depth+1)
         print A[i] right-justified in a field of (depth * 6) + 4
         ShowBinaryHeap(LEFT(i), depth+1)
The procedure is initially called with ShowBinaryHeap(1,0).

Helpful skeleton code: Here is a Makefile, a "command interpreter" cmdint.cpp, a .h file for the BinHeap class binheap.h, and skeleton code for the BinHeap class implementation binheap.cpp. When you download these files, be sure to remove the ".txt" suffixes (by moving them to the same file name without the ".txt").

What To Hand In: (1) Clearly marked scripts of heaptest1, heaptest2 and heaptest3 demonstrating your min-heap implementation. Be sure to hand-draw the heap links. And (2) well documented code for your min-heap implementation.


Part 2: Proving facts about binary heaps (6 points)

  1. (1 point) Exercise 6.1-2, page 129. Hint: you may assume that 2kn < 2k+1 for some integer k ≥ 0. Subhint: (easy question) what is the relation between k and the height, h?
  2. (5 points) Prove (by induction) that the sum of the heights of all nodes in a complete binary tree/heap of height h (and thus having 2h+1 - 1 nodes) is 2h+1 - h - 2.



Page URL: http://www.d.umn.edu /~ddunham/cs4521s07/assignments/a4/assignment.html
Page Author: Doug Dunham
Last Modified: Tuesday, 06-Feb-2007 15:54:40 CST
Comments to: ddunham@d.umn.edu