Subject: homework clarifications

From: Kenneth Been <[email-masked]>
Date: Sun, 15 Nov 1998 12:04:23 -0500

Basic Algorithms Class:

There seems to be some confusion about what you are supposed to do for
homework 5.  I will try to clarify.

Define a PriorityQueue interface with the methods listed in section
6.1.3 (except with the simplifications described in the assignment
having to do with using the strings as both elements and keys; i.e., you
do not need minKey(), and insertItem() takes only one parameter).

Define a class Heap which implements PriorityQueue, as described in
section 6.3.  (Notice that you could also implement a PriorityQueue with
a sequence, as described in section 6.2, but this is not what you are
asked to do for the assignment.)
The Heap class should additionally have a method sort(), which is not
part of the PriorityQueue interface.

Notice that a heap is defined as a binary tree, but binary tree is
itself an abstract data type (ADT) (see section 5.3), so it can be
implemented in different ways, such as with a sequence (section 5.4.1)
or with a linked structure (section 5.4.2).

If we were to be completely rigorous about it, as the book is, then we
would consider sequence to be an ADT which can be implemented in several
ways, such as with an array or with a linked list (chapter 4).  Since we
want you to use an array, this is finally the end of the line for
implementations, because an array is a direct language feature, and not
an ADT, so it doesn't need to be "implemented".

But for this assignment you do not need to be so rigorous.  Within your
Heap class you will just declare an array as an instance variable, and
do manipulations directly on that array to implement the various methods
in the PriorityQueue interface.

So, your main program will read in a string s from the input, then call
heap.insertItem(s), then read in the next string.  When all the input
has been read, it will call heap.sort().  The Heap class will probably
also need a method (which could be private) called snapshot(), or
something like that, which will print out the current contents of the
heap to the screen, in the right location. The methods insertItem() and
sort() will call snapshot() at the appropriate times, as described in
the assignment.

(This means that the Heap will have to know about the graphics context
of the applet that created it--the "Graphics g" variable in the paint()
method of Applet.  There are different ways to do this.  One way would
be to have a Graphics instance variable g in the Heap class, then,
assuming the heap is created and used in the paint() method of the
applet, the applet can simply assign its own Graphics variable to the
heap's Graphics variable.  Or it could be set in the heap's
constructor.)

Ken