Lab - Building Heaps

Overview

Welcome to another lab without any code.

We have discussed two different ways for constructing a heap from scratch. The first method is to simply insert items one-by-one using our standard heap insertion routine (based on "up-heap bubbling"). The second method was to build the heap in a bottom-up fashion, based using a sequence of calls to the "down-heap bubbling" routine. In this lab, we are going to use an applet to examine these two different methods.

The Applet

Our applet works as follows. It creates two ballanced binary trees, for use as heaps, containing a certain number N of internal nodes (the default is N=10). The key values used are chosen at random, although both trees are set using the same sequence of keys. At the beginning of the program most internal nodes will be drawn in yellow, as we will only care to examine nodes of the tree a little at a time.

The heap nearer to the top of the screen will be used to examine the standard method of inserting key after key. Initially, we can imagine inserting the first item at the root. Then we will insert the second item. Then the third. And so on until all N items are inserted. When the applet begins, the root node will be drawn in red. This is because it is the first one to be processed. Whenever you click on the "Process next item" button, that node will be turned black, and the next node to be processed will be changed from yellow to red. Your job will be to actually perform the required up-heap swaps to make the insertions work correctly. In particular, when the current item is drawn in red, you may click on its parent if you wish to have the current item swap places with its parent. You may continue to do these swaps until you are happy with the placement. At that point you can click on the "Process next item" button. When you have processed all items, you will hopefully have a valid heap - that is, each node's value should be at least as great as its parent's value.

The heap nearer to the bottom of the screen will be used to examine the bottom-up method for building a heap. It will process the nodes in the opposite order, starting with the rightmost internal node of the bottom level. For this tree, you will always have the option, should you choose, to swap the current (red) node downward as far as you wish. In particular, if you click on a child of the red node, the item stored at that child will be swapped with the red node's item. Again, you may do as many swaps as you see fit, and when you are happy, you can click on the "Process next item" button to continue. The last node to be processed will be the root of the tree. When you are finished with that node, you will hopefully have a valid heap - that is, each node's value should be at least as great as its parent's value.

When you feel that you have both heaps correct, you may click on the "Ready to Validate" button. If they were indeed correct, the phrase "CONGRATULATIONS" will appear. If there were any problems, the program will display these problems.

Should you ever decide that you wish to start over with new trees, there is a button near the top of the screen which will re-initialize (using a new set of random items).

Completing the Lab

To get full credit for completing this lab, you must do the following:

Running the Applet

There are three different ways to start the applet:

  1. The applet can be viewed with a web browser by going to the URL www.cs.luc.edu/~mhg/demos/BuildHeap/

  2. The applet can be viewed from a DOS window using the appletviewer program which is part of Sun's JDK. To do this from the department labs, open a DOS window from the "Programming (java) --> jdk (dos)" menu, and do the following:
    DOSprompt>  e:
    DOSprompt>  cd prof_pub\mhg\comp271\labs\heaps\
    DOSprompt>  appletviewer Heaps.html
    

  3. If you wish, you can actually download the sourcecode yourself and then compile them in your favorite Java environment. The files can be downloaded from the web page or found on the department network, at:
       e:\prof_pub\mhg\comp271\labs\heaps\files\
    


Last modified: 17 October 2002