The goal of this project is to understand the heap data structure and its use in implementing the heapsort algorithm and the priority queue ADT.
This project is found in the book as Project 2.4 on page 172. The project description (both here and in the book) are pretty sparse, but the accompanying book section on heaps and priority queues extensively goes through the code and algorithms you are to implement. Also, this project includes two parts at the end that do not appear in the book's description of this project.
Make a folder for this project and copy the starter code from
/homes/tvandrun/Public/cs345/heap
and make a new
project for it.
The code is organized into typical folders adt
,
impl
, and test
,
and the additional folder expr
.
Heap
classFinish the implementation of the heap data structure
by writing the methods Heap.decreaseKey()
and Heap.increaseKey()
.
Test using test.HeapTest
.
Finish the implementation of heap sort by
writing the missing loops in the two sort()
.
Note that one of the sort methods is for integers
and sorts them least to greatest;
the other is for objects of some generic type E
and sorts them based on a given comparator.
Test using test.HeapSortTest
.
HeapPriorityQueue
classFinish the implementation of HeapPriorityQueue
by writing the methods extractMax()
and insert()
.
Test using test.HPQTest
.
To take us back around to stacks and queues, consider how a priority queue can be used to implement a queue: As each element is entered into the priority queue, it is assigned a priority based on its time of arrival.
The class PQQueue
has two instance
variables--- a HeapPriorityQueue
and a Map
.
The Map
associates elements in the queue (which are
also keys in the priority queue) with integer priorities.
We'll give the priority queue a comparator that
uses this map to look up a key's priority.
The real trick to all this is determining how to assign priorities and how to write the comparator to determine realtive priorities of keys.
Implement what's left in the PQQueue
class,
including the constructor.
The constructor will need to instantiate the
HeapPriorityQueue
class, passing a comparator to its constructor.
Write this comparator as an anonymous inner class.
This this using test.PQQTest
.
Finally, do the same thing as in the previous section, but implement
a stack (PQStack
).
The only real difference is how you calculate priorities.
Test using test.PQSTest
.
Copy the files you modified
(Heap
, HeapSorter
,
HeapPriorityQueue
, PQQueue
,
and PQStack
to your turn-in folder
/cslab/class/cs345/(your id)/heap
.
To keep up with the course, this should be finished by Feb 13.