Class Heap

java.lang.Object
  |
  +--Heap
All Implemented Interfaces:
PriorityQueue

public class Heap
extends java.lang.Object
implements PriorityQueue

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

Heap

public 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. This constructor runs in constant amount of time, i.e., it is O(1).

Parameters:
size - The size of the array (of references) used to implement the heap.
comparator - Determines how items in the heap will be ordered.

Heap

public 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. This constructor takes O(n*ln(n)) time to run, where n is the length of the input array. This method will run in constant time.

Parameters:
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

getMin

public java.lang.Object getMin()
Implement the getMin() method from the PriorityQueue interface. This method will run in constant time.

Specified by:
getMin in interface PriorityQueue

insert

public void insert(java.lang.Object item)
Implement the insert() method from the PriorityQueue interface. This method will run in O(ln(n)) time (because of the need to reheapify the heap).

Specified by:
insert in interface PriorityQueue

isEmpty

public boolean isEmpty()
Implement the isEmpty() method from the PriorityQueue interface. This method will run in constant time.

Specified by:
isEmpty in interface PriorityQueue

removeMin

public java.lang.Object removeMin()
Implement the removeMin() method from the PriorityQueue interface. This method will run in O(ln(n)) time (because of the need to reheapify the heap).

Specified by:
removeMin in interface PriorityQueue

size

public int size()
Implement the 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.

Specified by:
size in interface PriorityQueue