Assignment 4 -- due Thursday, October 3 at the beginning of lab
CS 5521 Fall Semester, 2013
20 Points

Topics: Binary Heaps and their implementation of min priority queues

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:
minHeapify, (a utility routine), insert, minimum, extractMin, and decreaseKey. You can represent binary heaps by a triple consisting of an array of integers, the heap's size, heapSize, and its length length. Then all a constructor does is does is allocate such a triple with heapSize equal to zero, length set to an appropriate value, and an array of size length+1. You will also need to use the 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 construct an empty heap (done in the command interpreter) i key insert node with value key into the heap m return the minimum value in the heap e extract node with minimum key from the heap k i key decrease the key at index i to the value key 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', 'm', 'e', and 'k' commands correspond to the constructor, insert, minimum, extractMin, and decreaseKey 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 (mostly given to you below). 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. Any work you do 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 (this is already done in the command interpreter)
c

# insert 4 items, printing the heap after each insert
i 491
S
i 122
S
i 245
S
i 474
S

# The following command tests minimum()
m

# The following command tests decreaseKey(), decreaseing 491 to 111
k 4 111
S

# extract mins (quitely after 1st) until empty, printing heap after each one
e
S
-
e
S
e
S
e
S

# attempt to extract min from empty heap
e

# end of test

```
The following is the correct output for heaptest1:
```
# create empty heap (this is already done in the command interpreter)

# 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

# The following command tests minimum()

Minimum = 122
# The following command tests decreaseKey(), decreaseing 491 to 111

Structure of heap (rotated 90 degrees to left):

245
111
122
474

# extract mins (quitely after 1st) until empty, printing heap after each one

Minimum extracted = 111
Structure of heap (rotated 90 degrees to left):

245
122
474

Structure of heap (rotated 90 degrees to left):

245
474

Structure of heap (rotated 90 degrees to left):

474

Structure of heap (rotated 90 degrees to left):

# attempt to extract min from empty heap

Heap empty, can't extract minimum.

# 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
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 153. 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/cs5521f13/assignments/a4/assignment.html
Page Author: Doug Dunham