Basic Algorithms, v22.0310, Fall'98: Homework 5

Due date: November 23

SUBMISSION INSTRUCTIONS

Put your applet class in your public directory on acf5, as you did with homework 3, and send the source code for your program to the grader.

IMPLEMENTATION OF PRIORITY QUEUE AS ARRAYS

This assignment is to write a program that will perform heapsort on a list of words that will be read in from a file. This requires an implementation of priority queues using binary trees stored in an array (see section 6.3 of the book).

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:

  1. If v is the root of T, then p(v) = 0.
  2. If v is the left child of node u, then p(v) = 2p(u) + 1.
  3. If v is the right child of node u, then p(v) = 2p(u) + 2.
Here p(v) represents the index of node v in the array.

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 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.

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 compareTo() method in class String (java.lang.String) to compare the strings.

Once the heap is built (all items have been added), sort it into alphabetical order using heapsort, as described in section 6.3.3.

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.)

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:

      NY                     // insert "NY" 
      NJ  NY                 // insert "NJ"  
      IL  NY  NJ             // insert "IL"  
      ...

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.