Your goal in this lab is to implement a heap. Unlike the heap described in the book, your heap must allow elements of any comparable type and must be a min-heap.
Define the class MinHeap
. Give it this public
interface:
E
representing the element type. Elements must be
comparable in order to position them within the heap.public
and final
array named items
that holds the elements
of the heap. Normally instance variables should be private, but the
unit tests check your operations by accessing this array. The
final
modifier means that the variable items
can only be
assigned once. It doesn’t mean the individual elements of the
array are constant.int
. Your
heap will never have more than the indicated number of elements, and it
will never need to resize.size
method that returns an int
representing the number of
items currently in the heap.isEmpty
method that returns true
if and only if the heap
contains no items.enqueue
method that accepts a new item as a parameter. If
the heap is full, this method throws an IllegalStateException
.dequeue
method that removes and returns the minimum item. This
method must assign null
to the vacated array entry. If the heap is
empty, this method must throw a NoSuchElementException
.peek
method that returns, but does not remove, the minimum item.
If the heap is empty, this method must throw a NoSuchElementException
.Test your implementation locally, either using a main method, a driver
class, or unit tests. Once you are confident that your code is
correct, submit MinHeap.java
through Gradescope.