|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object structure5.VectorHeap<E>
public class VectorHeap<E extends java.lang.Comparable<E>>
This class implements a priority queue based on a traditional array-based heap. Most heap operations, including insert and remove, execute in logarithmic time, but the minimum element can be returned in constant time.
Example usage:
To print out a list of programmers sorted by age we could use the following:
public static void main(String[] argv){ //initialize a new fib heap VectorHeap programmers = newVectorHeap()
; //add programmers and their ages to heap //ages current of 7/22/2002 //add programmers and their ages to heap //ages current of 7/22/2002 programmers.add(new ComparableAssociation(new Integer(22), "Evan"))
; programmers.add(new ComparableAssociation(new Integer(19), "Chris")); programmers.add(new ComparableAssociation(new Integer(20), "Shimon")); programmers.add(new ComparableAssociation(new Integer(21), "Diane")); programmers.add(new ComparableAssociation(new Integer(21), "Lida")); programmers.add(new ComparableAssociation(new Integer(20), "Rob")); programmers.add(new ComparableAssociation(new Integer(20), "Sean")); //print out programmers while(!programmers.isEmpty()
){ ComparableAssociation p = (ComparableAssociation)programmers.remove()
; System.out.println(p.getValue() + " is " + p.getKey() + " years old."); } }
Field Summary | |
---|---|
protected Vector<E> |
data
The data, kept in heap order. |
Constructor Summary | |
---|---|
VectorHeap()
Construct a new priority queue |
|
VectorHeap(Vector<E> v)
Construct a new priority queue from an unordered vector |
Method Summary | |
---|---|
void |
add(E value)
Add a value to the priority queue. |
void |
clear()
Remove all the elements from the queue. |
E |
getFirst()
Fetch lowest valued (highest priority) item from queue. |
boolean |
isEmpty()
Determine if the queue is empty. |
protected static int |
left(int i)
Returns left child index. |
protected static int |
parent(int i)
Returns parent index |
protected void |
percolateUp(int leaf)
Moves node upward to appropriate position within heap. |
protected void |
pushDownRoot(int root)
Moves node downward, into appropriate position within subheap. |
E |
remove()
Returns the minimum value from the queue. |
protected static int |
right(int i)
Returns right child index. |
int |
size()
Determine the size of the queue. |
java.lang.String |
toString()
Construct a string representation of the heap. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected Vector<E extends java.lang.Comparable<E>> data
Constructor Detail |
---|
public VectorHeap()
public VectorHeap(Vector<E> v)
Method Detail |
---|
protected static int parent(int i)
i
- a node index
protected static int left(int i)
i
- a node index
protected static int right(int i)
i
- a node index
public E getFirst()
getFirst
in interface PriorityQueue<E extends java.lang.Comparable<E>>
public E remove()
remove
in interface PriorityQueue<E extends java.lang.Comparable<E>>
public void add(E value)
add
in interface PriorityQueue<E extends java.lang.Comparable<E>>
value
- The value to be added.public boolean isEmpty()
isEmpty
in interface PriorityQueue<E extends java.lang.Comparable<E>>
protected void percolateUp(int leaf)
leaf
- Index of the node in the heap.protected void pushDownRoot(int root)
root
- Index of the root of the subheap.public int size()
size
in interface PriorityQueue<E extends java.lang.Comparable<E>>
public void clear()
clear
in interface PriorityQueue<E extends java.lang.Comparable<E>>
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |