public class HeapPriorityQueue<K,V> extends AbstractPriorityQueue<K,V>
AbstractPriorityQueue.PQEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected ArrayList<Entry<K,V>> |
heap
primary collection of priority queue entries
|
Constructor and Description |
---|
HeapPriorityQueue()
Creates an empty priority queue based on the natural ordering of its keys.
|
HeapPriorityQueue(Comparator<K> comp)
Creates an empty priority queue using the given comparator to order keys.
|
HeapPriorityQueue(K[] keys,
V[] values)
Creates a priority queue initialized with the respective
key-value pairs.
|
Modifier and Type | Method and Description |
---|---|
protected void |
downheap(int j)
Moves the entry at index j lower, if necessary, to restore the heap property.
|
protected boolean |
hasLeft(int j) |
protected boolean |
hasRight(int j) |
protected void |
heapify()
Performs a bottom-up construction of the heap in linear time.
|
Entry<K,V> |
insert(K key,
V value)
Inserts a key-value pair and return the entry created.
|
protected int |
left(int j) |
static void |
main(String[] args) |
Entry<K,V> |
min()
Returns (but does not remove) an entry with minimal key.
|
protected int |
parent(int j) |
Entry<K,V> |
removeMin()
Removes and returns an entry with minimal key.
|
protected int |
right(int j) |
int |
size()
Returns the number of items in the priority queue.
|
protected void |
swap(int i,
int j)
Exchanges the entries at indices i and j of the array list.
|
protected void |
upheap(int j)
Moves the entry at index j higher, if necessary, to restore the heap property.
|
checkKey, compare, isEmpty
public HeapPriorityQueue()
public HeapPriorityQueue(Comparator<K> comp)
comp
- comparator defining the order of keys in the priority queuepublic HeapPriorityQueue(K[] keys, V[] values)
keys
- an array of the initial keys for the priority queuevalues
- an array of the initial values for the priority queueprotected int parent(int j)
protected int left(int j)
protected int right(int j)
protected boolean hasLeft(int j)
protected boolean hasRight(int j)
protected void swap(int i, int j)
protected void upheap(int j)
protected void downheap(int j)
protected void heapify()
public int size()
public Entry<K,V> min()
public Entry<K,V> insert(K key, V value) throws IllegalArgumentException
key
- the key of the new entryvalue
- the associated value of the new entryIllegalArgumentException
- if the key is unacceptable for this queuepublic Entry<K,V> removeMin()
public static void main(String[] args)