Class PriorityQueue

java.lang.Object
  |
  +--PriorityQueue

public class PriorityQueue
extends java.lang.Object

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
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

ROOT

private static final int ROOT
The index of the root node

data

private java.lang.Object[] data
Contains all of the data in this PriorityQueue

priorities

private double[] priorities
The priorities associated with each element in the array of data

index

private int index
The index of the most recently inserted item
Constructor Detail

PriorityQueue

public PriorityQueue()
Constructs a new, initially empty, PriorityQueue

PriorityQueue

public PriorityQueue(int depth)
Constructs a new, initially empty PriorityQueue with a specified depth
Parameters:
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 used
Method Detail

isEmpty

public boolean isEmpty()
Tests if this PriorityQueue contains any items
Returns:
true if and only if there are no items in the PriorityQueue; false otherwise

size

public int size()
The number of items in this PriorityQueue
Returns:
the number of items in the PriorityQueue.

insert

public void insert(java.lang.Object datum,
                   double priority)
Adds a new item to this PriorityQueue. There must be a priority associated with the item to be added.
Parameters:
datum - the item to be placed in this PriorityQueue
priority - the new item's priority

remove

public java.lang.Object remove()
Removes and returns the item with the smallest priority from this PriorityQueue
Returns:
the item removed from this PriorityQueue
Throws:
java.util.NoSuchElementException - if the PriorityQueue is empty

peekPriority

public double peekPriority()
Returns the priority of the smallest priority item in this PriorityQueue
Returns:
the priority of the smallest-priority item in this PriorityQueue
Throws:
java.util.NoSuchElementException - if the PriorityQueue is empty

peek

public java.lang.Object peek()
Returns the item with the smallest priority in this PriorityQueue. Note: the item is not removed.
Returns:
the smallest-priority item in this PriorityQueue
Throws:
java.util.NoSuchElementException - if the PriorityQueue is empty

heapifyUp

private void heapifyUp()
Moves the item at the current index up until it gets to the correct location in the heap

heapifyDown

private void heapifyDown(int ind)
Restores the heap ordering property at the specified index by pushing it downwards to its correct location
Parameters:
ind - the index at which to heapify down

hasLeftChild

private boolean hasLeftChild(int node)
Returns true iff the specified node has a left child

hasRightChild

private boolean hasRightChild(int node)
Returns true iff the specified node has a right child

leftChild

private int leftChild(int node)
Returns the index of the left child of the specified node
Parameters:
node - the node for which the left child is being sought

rightChild

private int rightChild(int node)
Returns the index of the right child of the specified node
Parameters:
node - the node for which the right child is being sought

parent

private int parent(int node)
Returns the index of the parent of the specified node
Parameters:
node - the node for which the parent is being sought

swap

private void swap(int node1,
                  int node2)
Swaps all of the values for two node indices
Parameters:
node1 - one of the two indices to swap
node2 - one of the two indices to swap

findMinPriority

private int findMinPriority(int node1,
                            int node2)
Returns the index of the node with the lower priority
Parameters:
node1 - one of the nodes for which to compare priorities
node2 - one of the nodes for which to compare priorities

doubleArrays

private void doubleArrays()
Doubles the size of arrays for priorities and data