Assignment 11 -- Due Monday, November 23 (at the beginning of lab)
CS 4521 Fall Semester, 2009
20 Points

Topic: Implementation of Binomial Heaps

The assignment consists of writing code and demonstrating binomial heap operations as described in Chapter 19 of the text. Implementing binomial heaps will require coding the operations:

The only algorithm not in the text is BINOMIAL-HEAP-MERGE, which you wrote in a previous assignment (the decrease-key and delete operations are not required yet). On page 468, for BINOMIAL-HEAP-EXTRACT-MIN the text assumes, but does not provide, algorithms for: For the former, BINOMIAL-HEAP-MINIMUM on page 462 is a good model, but you must provide the latter. If you don't already have working implementations of these linked list operations, then you may want to test them carefully in isolation before using them in BINOMIAL-HEAP-EXTRACT-MIN.

You can use a modified version of the command interpreter for binary heaps (in Assignment 4); as in that assignment, i, m, and e insert, return the minimum key value, and extract the minimum; the merge and union operations are tested indirectly. Also, you need to implement the routine ShowBinomialHeap which displays the binomial heap structure graphically (see below), rotated 90 degrees as usual.

Test Example

The test files are the same as for binary heaps. Here is the correct output for heaptest2:
+-----------------------------------------------------------------+
|# construct empty heap (this is already done in the command interpreter)
|								  |
|# insert 20 keys						  |
|								  |
|# show heap structure						  |
|								  |
|Structure of binomial heap (rotated 90 degrees counterclockwise):|
|								  |
|    1     2							  |
|                                                                 |
|          3     4						  |
|								  |
|          5     6                                                |
|						                  |
|                7     8					  |
|								  |
|          9     10                                               |
| 						                  |
|                11    12					  |
|								  |
|                13    14                                         |
|					                          |
|                      15    16					  |
|								  |
|								  |
|    17    18                                                     |
|							          |
|          19    20						  |
|								  |
|								  |
|# do 10 extract mins with print key flag off                     |
|								  |
|# show heap structure						  |
|								  |
|Structure of binomial heap (rotated 90 degrees counterclockwise):|
|								  |
|    13    14                                                     |
|							          |
|          15    16						  |
|								  |
|          17    18                                               |
|						                  |
|                19    20					  |
|								  |
|								  |
|    11    12							  |
|								  |
|								  |
|# do 10 extract mins with print key flag on                      |
|								  |
|Minimum extracted: 11                                            |
|Minimum extracted: 12                                            |
|Minimum extracted: 13                                            |
|Minimum extracted: 14                                            |
|Minimum extracted: 15                                            |
|Minimum extracted: 16                                            |
|Minimum extracted: 17                                            |
|Minimum extracted: 18                                            |
|Minimum extracted: 19                                            |
|Minimum extracted: 20                                            |
|								  |
|# show heap structure (should be empty)			  |
|								  |
|Structure of binomial heap (rotated 90 degrees counterclockwise):|
|								  |
|								  |
|								  |
|# do an extract min from empty heap                              |
|Heap Empty							  |
|								  |
|# quit								  |
+-----------------------------------------------------------------+

Displaying The Structure Of A Binomial Heap

One way to do this is to let ShowBinomialHeap be a non-recursive "driver" that calls a recursive routine ShowHeap to display a binomial heap rotated 90 degrees. So ShowHeap could be something like:
  ShowHeap( x, depth )
     if ( sibling[x] != NIL ) then      |> Do sibling list first
        ShowHeap( sibling[x], depth ) 

     |> May have to print a blank line here

     if ( ( child[x] != NIL ) or ( p[x] = NIL ) ) then
        print key[x] shifted 6*depth + 4 spaces     |> First key on a line
        if ( child[x] = NIL ) print a blank line    |> Special case
     else
        print key[x] shifted 6 spaces, then a blank line  |> Last key on line

     if ( child[x] != NIL ) then        |> Take care of children last
        ShowHeap( child[x], depth + 1 )
And ShowBinomialHeap would be something like:
  ShowBinomialHeap(H)
     print "Stucture of binomial heap (rotated 90 degrees ccwise):"
     if ( head[H] = NIL ) then print "Empty heap"
                          else ShowHeap( head[H], 0 )
You may have to adjust the formatting to get good looking output.

Helpful code: Here is a .h file for the Binomial Heap (BHeap) class binomheap.h. and skeleton code for the BHeap class implementation binomheap.cpp. Here is a command interpreter: cmdint.cpp. Here is a "Makefile" that puts it all together: Makefile. When you download these files, be sure to remove the ".txt" suffixes (by moving them to the same file name without the ".txt"). In addition, as I mentioned in the email, a careful reading to the requirements of the merge() operation show that it should return a pointer to the merged list, not a binomial heap as was presented in class. Here is the (correct) pseudocode for merge() that returns a pointer: merge() pseudocode.

What To Hand In

Hand in the output like the one given above for each of the test files: Identify them clearly and draw in the links between nodes printed by your ShowBinomialHeap() operation.

Also hand in the following:

Extra Credit (up to 4 points)

Implement the decrease-key and delete operations, and test them with test files that show those operations work correctly. To do this, you should also implement a search(k) operation that returns a pointer, x, to the node with key k. Then to test decrease-key in your command interpreter, first search for the original key value k-orig with search(k-orig) to get x, then call decrease-key(x, k-new) where k-new is the decreased value of the key. Similarly, to test delete for a node with key value k, first search(k) to get x, and then call delete(x).


Page URL: http://www.d.umn.edu /~ddunham/cs4521f09/assignments/a11/assignment.html
Page Author: Doug Dunham
Last Modified: Thursday, 19-Nov-2009 22:30:27 CST
Comments to: ddunham@d.umn.edu