Use the sequence-based implementation for the binary tree, as described in section 5.4.1, and implement the sequence as a simple array. Note, however, that since array indices in Java start from 0, the binary tree implementation will be slightly different from that in the book:
You must write a PriorityQueue interface
that specifies the methods of a
priority queue; as listed in section 6.1.3.
For simplicity, we will assume that an item is simply
given by a key and not as a pair of
(key, element).
Moreover, we use the datatype
The input file will contain a list of words, one per line.
Your program should not assume the number of words in the input
(you may only assume that there are at at most 50 words).
You should read each word into a String, and add it to the heap, which is
implemented as an array of Strings. You may use the
Once the heap is built (all items have been added), sort it into
alphabetical order using heapsort, as described in section 6.3.3.
This should be done after adding each element to the heap. It should
also be done on each iteration of the sorting; that is, after removing
the minimum element and restoring the heap property, then list the
current contents of the array.
This way you can follow the structure of the heap as it is being built
and as it is being sorted. The first three lines of the output
will look something like this:
The sorting should be done "in place". That is, without using an
auxiliary array. On each iteration, remove the minimum element, and
switch it with the last element in the part of the array that
currently contains the heap. Then, restore heap order, and the new
"current" heap will be reduced in size by one, and the right end of
the array will contain the lowest elements, in reverse sorted order.
Here is the first input data file.
For further testing, here
is a second input data file (this one has
duplicate keys).
You should download them and keep it on the same machine where your
applet class file resides. The applet can read it in by considering
it to be a resource on the web. It is important that the domain (in
this case cs.nyu.edu) that you give in the address of the input file
must be the exact same as the one that the applet is loaded from.
(This is because of the security restrictions that applets live
under.) A sample program
here from Ken Been
shows how an applet can read in files in this way.
Here is the corresponding html file.
You can copy
this code and modify it to make the directory point to the right
location.
String
will function as key values. So you do not need to
implement minKey()
,
since it would just be the same as minElement()
.
Also, the insertItem(k)
method will just take one parameter,
the key k
to be inserted.
compareTo()
method in class String
(java.lang.String) to compare the
strings.
INPUT AND OUTPUT
As output, you should list, to the screen, the current contents of the
array, at each step of the process. Listing the current contents
means write one line to the screen containing each string currently in
the array, in order as they appear in the array, and with space in
between each one. Then print the next line underneath it, and make
the columns line up. (Since it is an applet, you will use the
graphics drawString() method to print the strings to the screen.)
NY // insert "NY"
NJ NY // insert "NJ"
IL NY NJ // insert "IL"
...