Problem Specification (100 points)
Write a Java program that does the following:
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
Note that the heap can contain duplicates. Preconditions for
the heap operations are as follows:
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
\\neelix\dropbox\ITEC\ITEC224\hlee3\Submissions\ITEC224-02-fall\<your_RU_id>\RU04