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.
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.
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.