salvo.jesus.util
Class Heap

java.lang.Object
  extended by salvo.jesus.util.Heap
All Implemented Interfaces:
java.io.Serializable

public class Heap
extends java.lang.Object
implements java.io.Serializable

The Heap class implements a heap data structure, also called a priority queue.

Author:
Jesus M. Salvo Jr.
See Also:
Serialized Form

Field Summary
(package private)  java.util.Vector binarytree
          To hold the actual storage of the Heap class.
(package private)  HeapNodeComparator comparator
          HeapNodeComparator to compare two heap nodes in the heap.
 
Constructor Summary
Heap()
          Creates an instance of a Heap.
Heap(HeapNodeComparator comparator)
          Creates an instance of a Heap, with a specified HeapNodeComparator.
 
Method Summary
 void clear()
          Clears the heap, removing all nodes in the heap.
 HeapNode contains(java.lang.Object object, java.util.Comparator heapnodeobjectcomparator)
          Determines if the given object is encapsulated by one of the nodes in the heap.
private  int downHeap(int index)
          Move the node, specified by the index, down in the heap until the heap condition is satisfied.
 void insert(HeapNode node)
          Add a new item into the heap
 boolean isEmpty()
          Checks if the heap is empty
 HeapNode remove()
          Remove the item with the highest priority from the heap.
 void setPriority(HeapNode node, double priority)
          Sets the priority of a specific node in the heap, thereby also forcing to fixup the heap to satisfy the heap condition.
 java.lang.String toString()
          Returns a String representation of the Heap.
private  int upHeap(int index)
          Move the node, specified by the index, up in the heap until heap condition is satisfied.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

binarytree

java.util.Vector binarytree
To hold the actual storage of the Heap class.


comparator

HeapNodeComparator comparator
HeapNodeComparator to compare two heap nodes in the heap.

Constructor Detail

Heap

public Heap()
Creates an instance of a Heap. Using this constructor will make use of the default HeapNodeComparator. The HeapNodeConstructor created will have priorities sorted in such a way that priorities that are numerically higher are at the top of the heap.


Heap

public Heap(HeapNodeComparator comparator)
Creates an instance of a Heap, with a specified HeapNodeComparator. This way, you can control if nodes with numerically higher priorities are at the top or bottom of the heap. To have nodes with numerically lower priorities at the top of the heap, create a HeapNodeComparator with a constructor parameter that is > 0.

Parameters:
comapartor - The HeapNodeComparator object to be used in comparing the priorities of the nodes of the heap.
Method Detail

insert

public void insert(HeapNode node)
Add a new item into the heap


remove

public HeapNode remove()
Remove the item with the highest priority from the heap.


setPriority

public void setPriority(HeapNode node,
                        double priority)
Sets the priority of a specific node in the heap, thereby also forcing to fixup the heap to satisfy the heap condition.

Parameters:
node - The HeapNode object whose priority is to be changed
priority - The new priority that will be assigned to the heapnode.

clear

public void clear()
Clears the heap, removing all nodes in the heap.


isEmpty

public boolean isEmpty()
Checks if the heap is empty


contains

public HeapNode contains(java.lang.Object object,
                         java.util.Comparator heapnodeobjectcomparator)
Determines if the given object is encapsulated by one of the nodes in the heap. If it is, then the HeapNode encapsulating the object is returned.


upHeap

private int upHeap(int index)
Move the node, specified by the index, up in the heap until heap condition is satisfied.

Parameters:
index - 0-based index of the node we want to move up the heap.
Returns:
The new index of the node that we have moved up the heap.

downHeap

private int downHeap(int index)
Move the node, specified by the index, down in the heap until the heap condition is satisfied.

Parameters:
index - 0-based index of the node we want to move down the heap.
Returns:
The new index of the node that we have moved down the heap.

toString

public java.lang.String toString()
Returns a String representation of the Heap.

Overrides:
toString in class java.lang.Object