Implement a priority queue class, and use this class to
implement a HeapSort algorithm that sorts Vector objects.
The priority queue is implemented as
a nearly-complete ternary tree, and is
encapsulated in a class (called TernaryHeap) that
extends the Vector
class. The tree used to represent the heap is stored in the Vector
such that the root of
the tree is stored at position 0
of the Vector, and,
for every
i = 0..(size()-1)
, the left, middle and
right
children of the element stored at position i
,
if they
exist,
are stored at positions 3i+1
, 3i+2
,
and 3i+3
,
respectively. For every element of the heap, the priority of this
element must be higher than or equal to that of its children. To
compare the priorities of two heap elements an instance of a class that
implements the PriorityComparator
interface
is used.
You should implement the following methods of the TernaryHeap class (described in the starter code):
buildTernaryHeap()
extractHighestPriority()
insertElement()
You must also implement the following method of the TernaryHeapSort class:
sort()
The worst-case time complexity of buildTernaryHeap()
should be O(n),
the worst-case time complexity of
extractHighestPriority()
and
insertElement()
should be O(logn),
and the worst-case time complexity of sort()
should be O(nlogn),
where n is the size of the heap.
To mark your assignment, we will test the above methods using a tester routine that is more thorough than the sample TernaryHeapTester class provided. The sample TernaryHeapTester class that we provide is very basic; running it successfully does not guarantee that your code is correct or will pass our marking tester routine. You are encouraged to derive your own test cases. Each method gets 5 marks if it works correctly, and gets 0 marks otherwise.
For more details, including exact specifications of the
methods that you should implement, see the starter code. All of your
work must be done inside the TernaryHeap
class and the TernaryHeapSort class,
where indicated by comments that start with: "// TODO:
".
You may also add appropriate private instance variables or helper
methods.
Submission Instructions