java.lang.Object | +--Heap
This class implements a priority queue as a heap contained in an array. This class is a generic implementation of the (generic) PriorityQueue interface in that the array used to implement the heap is an array of references to objects of type Object. So the array (i.e. the heap) can hold any kind of objects.
However, it is assumed that all of the objects in any particular instance of Heap are of the same type, and that a Comparer class has been defined for those objects. A reference to an object from this Comparer class is passed as a parameter to the constructor for the heap instance. The instance needs to remember this reference since the (private) heapify methods will need to use the comparer.
See "Algorithms in Java: Third Edition" by Robert Sedgewick, page 386, for another outline of this class.
Constructor Summary | |
Heap(int size,
Comparer comparator)
This constructor creates an empty array based heap, where the array for the heap can hold size number of (references to) objects. |
|
Heap(java.lang.Object[] inputArray,
Comparer comparator)
This constructor creates an array based heap and then populates the heap with the items in the input array. |
Method Summary | |
java.lang.Object |
getMin()
Implement the getMin() method from the PriorityQueue interface. |
void |
insert(java.lang.Object item)
Implement the insert() method from the PriorityQueue interface. |
boolean |
isEmpty()
Implement the isEmpty() method from the PriorityQueue interface. |
java.lang.Object |
removeMin()
Implement the removeMin() method from the PriorityQueue interface. |
int |
size()
Implement the size() method from the PriorityQueue interface. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public Heap(int size, Comparer comparator)
size
number of (references to) objects. This constructor runs in constant amount of time, i.e., it is O(1).
size
- The size of the array (of references) used to implement the heap.comparator
- Determines how items in the heap will be ordered.public Heap(java.lang.Object[] inputArray, Comparer comparator)
inputArray
- An array of (references to) objects that are put in the constructed heap.comparator
- Determines how items in the heap will be ordered.Method Detail |
public java.lang.Object getMin()
getMin()
method from the PriorityQueue interface. This method will run in constant time.
getMin
in interface PriorityQueue
public void insert(java.lang.Object item)
insert()
method from the PriorityQueue interface. This method will run in O(ln(n)) time (because of the need to reheapify the heap).
insert
in interface PriorityQueue
public boolean isEmpty()
isEmpty()
method from the PriorityQueue interface. This method will run in constant time.
isEmpty
in interface PriorityQueue
public java.lang.Object removeMin()
removeMin()
method from the PriorityQueue interface. This method will run in O(ln(n)) time (because of the need to reheapify the heap).
removeMin
in interface PriorityQueue
public int size()
size()
method from the PriorityQueue interface. Notice that this is the size of the heap contained in the array used to implement the heap. It is not the size of the array.
size
in interface PriorityQueue