public class PriorityQueue<E> extends java.lang.Object implements java.util.Iterator<E>, java.io.Serializable, java.lang.Cloneable, PriorityQueueInterface<E>
Constructor and Description |
---|
PriorityQueue() |
PriorityQueue(int capacity) |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E key,
double priority) |
Counter<E> |
asCounter()
Returns a counter whose keys are the elements in this priority queue, and
whose counts are the priorities in this queue.
|
PriorityQueue<E> |
clone()
Returns a clone of this priority queue.
|
double |
getPriority()
Gets the priority of the highest-priority element of the queue.
|
protected void |
grow(int newCapacity) |
boolean |
hasNext()
Returns true if the priority queue is non-empty
|
protected void |
heapifyDown(int loc) |
protected void |
heapifyUp(int loc) |
boolean |
isEmpty()
True if the queue is empty (size == 0).
|
protected int |
leftChild(int loc) |
static void |
main(java.lang.String[] args) |
E |
next()
Returns the element in the queue with highest priority, and pops it from
the queue.
|
protected int |
parent(int loc) |
E |
peek()
Returns the highest-priority element in the queue, but does not pop it.
|
void |
put(E key,
double priority)
Adds a key to the queue with the given priority.
|
void |
remove()
Not supported -- next() already removes the head of the queue.
|
protected void |
removeFirst() |
protected int |
rightChild(int loc) |
int |
size()
Number of elements in the queue.
|
protected void |
swap(int loc1,
int loc2) |
java.lang.String |
toString()
Returns a representation of the queue in decreasing priority order.
|
java.lang.String |
toString(int maxKeysToPrint,
boolean multiline)
Returns a representation of the queue in decreasing priority order,
displaying at most maxKeysToPrint elements and optionally printing
one element per line.
|
public PriorityQueue()
public PriorityQueue(int capacity)
protected void grow(int newCapacity)
protected int parent(int loc)
protected int leftChild(int loc)
protected int rightChild(int loc)
protected void heapifyUp(int loc)
protected void heapifyDown(int loc)
protected void swap(int loc1, int loc2)
protected void removeFirst()
public boolean hasNext()
PriorityQueueInterface
hasNext
in interface java.util.Iterator<E>
hasNext
in interface PriorityQueueInterface<E>
public E next()
PriorityQueueInterface
next
in interface java.util.Iterator<E>
next
in interface PriorityQueueInterface<E>
public void remove()
PriorityQueueInterface
remove
in interface java.util.Iterator<E>
remove
in interface PriorityQueueInterface<E>
public E peek()
PriorityQueueInterface
peek
in interface PriorityQueueInterface<E>
public double getPriority()
PriorityQueueInterface
getPriority
in interface PriorityQueueInterface<E>
public int size()
PriorityQueueInterface
size
in interface PriorityQueueInterface<E>
public boolean isEmpty()
PriorityQueueInterface
isEmpty
in interface PriorityQueueInterface<E>
public boolean add(E key, double priority)
public void put(E key, double priority)
PriorityQueueInterface
put
in interface PriorityQueueInterface<E>
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(int maxKeysToPrint, boolean multiline)
maxKeysToPrint
- maximum number of keys to printmultiline
- if is set to true, prints each element on new line. Prints elements in one line otherwise.public Counter<E> asCounter()
public PriorityQueue<E> clone()
clone
in class java.lang.Object
public static void main(java.lang.String[] args)