ITEC 224: Programming Assignment 4
Due: Midnight Friday 12/12/03

Problem Specification (100 points)

 

Write a Java program that does the following:

  1. Reads 0 or more integers from standard input. 
  2. Writes these integers, in ascending order (or more precisely, non-descending order), to standard output.

 

Each line of the input will contain a single integer.  Each integer will be within the range of a Java int.  Use System.in and a buffered reader to read the input.  Each line of the output will contain a single integer.  Use System.out.println to write to standard output.

Store and sort the integers using a public class called Heap that implements a heap data structure.  Your heap class should have the following public operations:

 

  • Heap() - creates a new, empty heap
  • void add(int i) - adds the int i to the heap
  • int smallest() - returns the smallest value in the heap (without changing the heap)
  • void remove() - removes the smallest value from the heap
  • int size() - returns the number of elements currently in the heap
  • boolean isEmpty() - returns true if the heap is empty and false otherwise

 

Note that the heap can contain duplicates.  Preconditions for the heap operations are as follows:

 

  • smallest: the heap is not empty
  • remove: the heap is not empty

 

Your heap should be a binary tree implemented with linked nodes.  Each node should have a parent link in addition to its two child links.  You will want to read the section on tree-based heaps on page 600 of your text.  In particular, this section discusses how to use the size of a heap to locate the heap's last element or its first open location.

When testing, you can either supply test data at the keyboard (remembering to end the input with a
^D at the beginning of a line), or by feeding the contents of a file to it (e.g. HeapSorter < test_data) where the use of the redirection symbol < causes the contents of the file test_data to be fed into the program as if they had been typed in from the keyboard.

Put your main program in a class called HeapSorter in file
HeapSorter.java.  Your Heap class should be the file Heap.java

 

What to submit

 

  • Source code ...  // please include javadoc as a comment in your source code
  • All the softcopy should be submitted at

 \\neelix\dropbox\ITEC\ITEC224\hlee3\Submissions\ITEC224-02-fall\<your_RU_id>\RU04