public class HeapPriorityQueue<K,V> extends java.lang.Object implements PriorityQueue<K,V>
Modifier and Type | Class and Description |
---|---|
protected static class |
HeapPriorityQueue.MyEntry<K,V>
Inner class for heap entries.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Comparator<K> |
comp |
protected CompleteBinaryTree<Entry<K,V>> |
heap |
Constructor and Description |
---|
HeapPriorityQueue()
Creates an empty heap with the default comparator
|
HeapPriorityQueue(java.util.Comparator<K> c)
Creates an empty heap with the given comparator
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkKey(K key)
Determines whether a given key is valid
|
protected void |
downHeap(Position<Entry<K,V>> r)
Performs down-heap bubbling
|
Entry<K,V> |
insert(K k,
V x)
Inserts a key-value pair and returns the entry created
|
boolean |
isEmpty()
Returns whether the heap is empty
|
Entry<K,V> |
min()
Returns but does not remove an entry with minimum key
|
Entry<K,V> |
removeMin()
Removes and returns an entry with minimum key
|
void |
setComparator(java.util.Comparator<K> c)
Sets the comparator used for comparing items in the heap.
|
int |
size()
Returns the size of the heap
|
protected void |
swap(Position<Entry<K,V>> x,
Position<Entry<K,V>> y)
Swaps the entries of the two given positions
|
java.lang.String |
toString()
Text visualization for debugging purposes
|
protected void |
upHeap(Position<Entry<K,V>> v)
Performs up-heap bubbling
|
protected CompleteBinaryTree<Entry<K,V>> heap
protected java.util.Comparator<K> comp
public HeapPriorityQueue()
public HeapPriorityQueue(java.util.Comparator<K> c)
public void setComparator(java.util.Comparator<K> c) throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if priority queue is not emptypublic int size()
size
in interface PriorityQueue<K,V>
public boolean isEmpty()
isEmpty
in interface PriorityQueue<K,V>
public Entry<K,V> min() throws EmptyPriorityQueueException
min
in interface PriorityQueue<K,V>
EmptyPriorityQueueException
public Entry<K,V> insert(K k, V x) throws InvalidKeyException
insert
in interface PriorityQueue<K,V>
InvalidKeyException
public Entry<K,V> removeMin() throws EmptyPriorityQueueException
removeMin
in interface PriorityQueue<K,V>
EmptyPriorityQueueException
protected void checkKey(K key) throws InvalidKeyException
InvalidKeyException
protected void swap(Position<Entry<K,V>> x, Position<Entry<K,V>> y)
public java.lang.String toString()
toString
in class java.lang.Object