Package com.zybooks.dsaj.pq
Class HeapAdaptablePriorityQueue<K,V>
java.lang.Object
com.zybooks.dsaj.pq.AbstractPriorityQueue<K,V>
com.zybooks.dsaj.pq.HeapPriorityQueue<K,V>
com.zybooks.dsaj.pq.HeapAdaptablePriorityQueue<K,V>
- Type Parameters:
K- The key type (keys must be unique and comparable)V- The value type stored with each entry
- All Implemented Interfaces:
AdaptablePriorityQueue<K,,V> PriorityQueue<K,V>
public class HeapAdaptablePriorityQueue<K,V>
extends HeapPriorityQueue<K,V>
implements AdaptablePriorityQueue<K,V>
An implementation of an adaptable priority queue using an array-based heap.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classExtension of the PQEntry to include location information.Nested classes/interfaces inherited from class com.zybooks.dsaj.pq.AbstractPriorityQueue
AbstractPriorityQueue.PQEntry<K,V> -
Field Summary
Fields inherited from class com.zybooks.dsaj.pq.HeapPriorityQueue
heap -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty adaptable priority queue using natural ordering of keys.Creates an empty adaptable priority queue using the given comparator to order keys. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbubble(int j) Restores the heap property by moving the entry at index j upward/downward.protected AbstractPriorityQueue.PQEntry<K,V> createEntry(K key, V value) Factory function to create an entry storing key,value.voidRemoves the given entry from the priority queue.voidreplaceKey(Entry<K, V> entry, K key) Replaces the key of an entry.voidreplaceValue(Entry<K, V> entry, V value) Replaces the value of an entry.protected voidswap(int i, int j) Exchanges the entries at indices i and j of the array list.protected HeapAdaptablePriorityQueue.AdaptablePQEntry<K,V> Validates an entry to ensure it is location-aware.Methods inherited from class com.zybooks.dsaj.pq.HeapPriorityQueue
downheap, hasLeft, hasRight, heapify, insert, left, min, parent, removeMin, right, size, upheapMethods inherited from class com.zybooks.dsaj.pq.AbstractPriorityQueue
compare, isEmpty
-
Constructor Details
-
HeapAdaptablePriorityQueue
public HeapAdaptablePriorityQueue()Creates an empty adaptable priority queue using natural ordering of keys. -
HeapAdaptablePriorityQueue
Creates an empty adaptable priority queue using the given comparator to order keys.- Parameters:
comp- comparator defining the order of keys in the priority queue
-
-
Method Details
-
createEntry
Factory function to create an entry storing key,value.- Overrides:
createEntryin classAbstractPriorityQueue<K,V> - Parameters:
key- the keyvalue- the value- Returns:
- the new PQEntry
-
validate
protected HeapAdaptablePriorityQueue.AdaptablePQEntry<K,V> validate(Entry<K, V> entry) throws IllegalArgumentExceptionValidates an entry to ensure it is location-aware.- Parameters:
entry- an entry instance- Returns:
- the entry cast as an AdaptablePQEntry instance
- Throws:
IllegalArgumentException- if the given entry was not valid
-
swap
protected void swap(int i, int j) Exchanges the entries at indices i and j of the array list.- Overrides:
swapin classHeapPriorityQueue<K,V>
-
bubble
protected void bubble(int j) Restores the heap property by moving the entry at index j upward/downward. -
remove
Removes the given entry from the priority queue.- Specified by:
removein interfaceAdaptablePriorityQueue<K,V> - Parameters:
entry- an entry of this priority queue- Throws:
IllegalArgumentException- if e is not a valid entry for the priority queue.
-
replaceKey
Replaces the key of an entry.- Specified by:
replaceKeyin interfaceAdaptablePriorityQueue<K,V> - Parameters:
entry- an entry of this priority queuekey- the new key- Throws:
IllegalArgumentException- if e is not a valid entry for the priority queue.
-
replaceValue
Replaces the value of an entry.- Specified by:
replaceValuein interfaceAdaptablePriorityQueue<K,V> - Parameters:
entry- an entry of this priority queuevalue- the new value- Throws:
IllegalArgumentException- if e is not a valid entry for the priority queue.
-