|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--PriorityQueue
Builds a MinHeap of Objects. This class may be especially useful to act as a timer of sorts in a simulation: the heap would contain "events" (the Objects in the heap) which would happen at a certain time (the priority). Removing the minimum value from the heap would be getting the event which would happen next.
There could be great improvements made to this class, including allowing for bulk-loading the PriorityQueue, and breaking ties in priority by using FIFO properties
Field Summary | |
private java.lang.Object[] |
data
Contains all of the data in this PriorityQueue |
private int |
index
The index of the most recently inserted item |
private double[] |
priorities
The priorities associated with each element in the array of data |
private static int |
ROOT
The index of the root node |
Constructor Summary | |
PriorityQueue()
Constructs a new, initially empty, PriorityQueue |
|
PriorityQueue(int depth)
Constructs a new, initially empty PriorityQueue with a specified depth |
Method Summary | |
private void |
doubleArrays()
Doubles the size of arrays for priorities and data |
private int |
findMinPriority(int node1,
int node2)
Returns the index of the node with the lower priority |
private boolean |
hasLeftChild(int node)
Returns true iff the specified node has a left child |
private boolean |
hasRightChild(int node)
Returns true iff the specified node has a right child |
private void |
heapifyDown(int ind)
Restores the heap ordering property at the specified index by pushing it downwards to its correct location |
private void |
heapifyUp()
Moves the item at the current index up until it gets to the correct location in the heap |
void |
insert(java.lang.Object datum,
double priority)
Adds a new item to this PriorityQueue. |
boolean |
isEmpty()
Tests if this PriorityQueue contains any items |
private int |
leftChild(int node)
Returns the index of the left child of the specified node |
private int |
parent(int node)
Returns the index of the parent of the specified node |
java.lang.Object |
peek()
Returns the item with the smallest priority in this PriorityQueue. |
double |
peekPriority()
Returns the priority of the smallest priority item in this PriorityQueue |
java.lang.Object |
remove()
Removes and returns the item with the smallest priority from this PriorityQueue |
private int |
rightChild(int node)
Returns the index of the right child of the specified node |
int |
size()
The number of items in this PriorityQueue |
private void |
swap(int node1,
int node2)
Swaps all of the values for two node indices |
Methods inherited from class java.lang.Object |
|
Field Detail |
private static final int ROOT
private java.lang.Object[] data
private double[] priorities
private int index
Constructor Detail |
public PriorityQueue()
public PriorityQueue(int depth)
depth
- the initial depth of the PriorityQueue. The
PriorityQueue may contain (2^depth) - 1 elements before it needs
to be resized. If depth is not positive, a default depth is usedMethod Detail |
public boolean isEmpty()
public int size()
public void insert(java.lang.Object datum, double priority)
datum
- the item to be placed in this PriorityQueuepriority
- the new item's prioritypublic java.lang.Object remove()
public double peekPriority()
public java.lang.Object peek()
private void heapifyUp()
private void heapifyDown(int ind)
ind
- the index at which to heapify downprivate boolean hasLeftChild(int node)
private boolean hasRightChild(int node)
private int leftChild(int node)
node
- the node for which the left child is being soughtprivate int rightChild(int node)
node
- the node for which the right child is being soughtprivate int parent(int node)
node
- the node for which the parent is being soughtprivate void swap(int node1, int node2)
node1
- one of the two indices to swapnode2
- one of the two indices to swapprivate int findMinPriority(int node1, int node2)
node1
- one of the nodes for which to compare prioritiesnode2
- one of the nodes for which to compare prioritiesprivate void doubleArrays()
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |