CS 201 - Homework 7 - Heaps

Due: Wednesday 11/7 at 8am

This assignment consists of three related programming problems. When you have completed the assignment, please submit your three programs (HeapTest.java, IntVectorHeap.java, and IntSkewHeap.java) using the HW 7 submission page.

Lab exercises:

During lab we will go through the instructions below to help you get started. I will also provide more explanation about the java.awt.Graphics commands you'll use for problem 1.

Be sure to follow the instructions (posted on the course page) on how to enable Java applets in your web browser. If you have trouble, come see me.

All three problems relate to an applet that visualizes operations on heaps of integers. Here is a small and a big version that you can run. Note: you can also run any applet from the command line using a program called "appletviewer". This program takes the applet's URL as a command-line parameter. For instance, to run the above applet, type:

   appletviewer http://www.cs.middlebury.edu/~schar/courses/cs201-f18/examples/Heaps/hw7Sample/HeapTest.html
To get started, unzip the file hw7.zip in your cs201 directory, which will create a directory hw7 containing the following files:
   HeapTest.html       IntHeap.java        IntTree.java        RandomHeap.java
   HeapTest.java       IntSkewHeap.java    IntVectorHeap.java
The file IntHeap.java contains an interface for heaps of integers which is listed below. The files IntVectorHeap.java, IntSkewHeap.java, and RandomHeap.java all contain implementations of this interface, and HeapTest.java contains code for the actual applet.

Import the hw7 directory into Eclipse as usual. Since this program is an Applet, running it is a bit different: right-click on the file HeapTest.java and select "Run As -> Java Applet". This should open a window showing your applet, however, it might be too small. Instead of having to resize it each time, simply close the window, then go to "Run -> Run Configurations...", open the "Parameters" tab, and change the Width and Height to 600 (or bigger). Now you're all set! The next time you should be able to run the program via the green triangle button as usual.

Note that you can also run the applet from the command line (after you've compiled it), using the supplied file HeapTest.html. If you want to try this, cd to your hw7 directory and type

   appletviewer HeapTest.html
In this assignment, you will have to add code to the files HeapTest.java, IntSkewHeap.java, and IntVectorHeap.java. I strongly recommend that you solve problem 1 before starting on problems 2 and 3 (otherwise it will be hard to debug your code for the latter two problems).

The interface IntHeap.java

// Interface for heaps of integers
// CS 201 HW 7

public interface IntHeap {

    // returns true iff heap has no values
    public boolean isEmpty();

    // adds value to heap
    public void add(int value);

    // removes and returns the minimum int at the root of the heap
    // pre: !isEmpty()
    public int remove();

    // returns the minimum int at the root of the heap ( = Bailey's getFirst())
    // pre: !isEmpty()
    public int value();

    // removes all elements from the heap
    public void clear();

    // The following two methods left() and right() are provided to
    // enable printing of the heap.  They are not part of a
    // "traditional" heap interface, which should hide the actual heap
    // structure.  Only the methods isEmpty(), value(), left(), and
    // right() should be used on sub-heaps returned by left() and
    // right().  The methods add() and remove() should NOT be used on
    // sub-heaps!

    // returns left "sub-heap" of heap
    // pre: !isEmpty()
    public IntHeap left();

    // returns right "sub-heap" of heap
    // pre: !isEmpty()
    public IntHeap right();


Problem 1: Drawing the heap

The file HeapTest.java contains the code to set up the applet, and to respond to the buttons. It also contains the definition of a class HeapCanvas, which can display an IntHeap. Your job is to complete the method drawHeap in this class:
    public static void drawHeap(Graphics g, IntHeap h, 
                                int l, int r, int y, int dy, int rad);

    // Draws the heap h at height y with root centered between l and r.
    // The next level is drawn at height y+dy.
    // Each leaf is drawn as a circle with radius rad.
The picture below illustrates the role of the parameters of drawHeap, for a heap with value 5 at its root:

You will need to use drawing commands such as

 g.drawLine(x1, y1, x2, y2);
 g.drawOval(x, y, width, height);
 g.fillOval(x, y, width, height);
For more information and other commands, see the documentation on java.awt.Graphics. Test your implementation after you successfully compile the file HeapTest.java. Clicking on the "add" button creates a random tree (not really a heap) in the lower panel, which your method should display.

Problem 2: Vector-based heaps of integers

Complete the definitions of add() and remove() in the file IntVectorHeap.java. This involves adding and modifying code from Bailey's VectorHeap.java.

Once your code works, you should see a heap using complete trees in the top half of the applet (as in the sample applet).

Problem 3: Skew heaps of integers

Complete the definitions of add() and remove() in the file IntSkewHeap.java. This involves adding and modifying code from Bailey's SkewHeap.java.

Note: Bailey's object-oriented implementation has two 'merge' methods. Ignore the first one. You only need the second (static) one.

To test your code, comment out the line

        hc2 = new HeapCanvas(new RandomHeap(), "Random 'Heap'");
in HeapTest.java, and replace it with the line
        hc2 = new HeapCanvas(new IntSkewHeap(), "Skew Heap");
You should then see a skew heap in the bottom half of the applet (as in the sample applet).