Package com.zybooks.dsaj.pq
Class HeapPriorityQueue<K,V> 
java.lang.Object
com.zybooks.dsaj.pq.AbstractPriorityQueue<K,V>
 
com.zybooks.dsaj.pq.HeapPriorityQueue<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:
- PriorityQueue<K,- V> 
- Direct Known Subclasses:
- HeapAdaptablePriorityQueue
An implementation of a priority queue using an array-based heap.
- 
Nested Class SummaryNested classes/interfaces inherited from class com.zybooks.dsaj.pq.AbstractPriorityQueueAbstractPriorityQueue.PQEntry<K,V> 
- 
Field SummaryFields
- 
Constructor SummaryConstructorsConstructorDescriptionCreates 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.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voiddownheap(int j) Moves the entry at index j lower, if necessary, to restore the heap property.protected booleanhasLeft(int j) protected booleanhasRight(int j) protected voidheapify()Performs a bottom-up construction of the heap in linear time.Inserts a key-value pair and returns the newly created entry.protected intleft(int j) min()Returns (but does not remove) an entry with minimal key.protected intparent(int j) Removes and returns an entry with minimal key.protected intright(int j) intsize()Returns the number of items in the priority queue.protected voidswap(int i, int j) Exchanges the entries at indices i and j of the array list.protected voidupheap(int j) Moves the entry at index j higher, if necessary, to restore the heap property.Methods inherited from class com.zybooks.dsaj.pq.AbstractPriorityQueuecompare, createEntry, isEmpty
- 
Field Details- 
heapprimary collection of priority queue entries
 
- 
- 
Constructor Details- 
HeapPriorityQueuepublic HeapPriorityQueue()Creates an empty priority queue based on the natural ordering of its keys.
- 
HeapPriorityQueueCreates an empty priority queue using the given comparator to order keys.- Parameters:
- comp- comparator defining the order of keys in the priority queue
 
- 
HeapPriorityQueueCreates a priority queue initialized with the respective key-value pairs. The two arrays given will be paired element-by-element. They are presumed to have the same length. (If not, entries will be created only up to the length of the shorter of the arrays)- Parameters:
- keys- an array of the initial keys for the priority queue
- values- an array of the initial values for the priority queue
 
 
- 
- 
Method Details- 
parentprotected int parent(int j) 
- 
leftprotected int left(int j) 
- 
rightprotected int right(int j) 
- 
hasLeftprotected boolean hasLeft(int j) 
- 
hasRightprotected boolean hasRight(int j) 
- 
swapprotected void swap(int i, int j) Exchanges the entries at indices i and j of the array list.
- 
upheapprotected void upheap(int j) Moves the entry at index j higher, if necessary, to restore the heap property.
- 
downheapprotected void downheap(int j) Moves the entry at index j lower, if necessary, to restore the heap property.
- 
heapifyprotected void heapify()Performs a bottom-up construction of the heap in linear time.
- 
sizepublic int size()Returns the number of items in the priority queue.- Returns:
- number of items
 
- 
minReturns (but does not remove) an entry with minimal key.- Returns:
- entry having a minimal key (or null if empty)
 
- 
insertInserts a key-value pair and returns the newly created entry.- Parameters:
- key- the key of the new entry
- value- the associated value of the new entry
- Returns:
- the entry storing the new key-value pair
 
- 
removeMinRemoves and returns an entry with minimal key.- Returns:
- the removed entry (or null if empty)
 
 
-