Package algs24
Class MaxPQ<K extends Comparable<? super K>>
java.lang.Object
algs24.MaxPQ<K>
- All Implemented Interfaces:
- Iterable<K>
The 
MaxPQ class represents a priority queue of generic keys.
  It supports the usual insert and delete-the-maximum
  operations, along with methods for peeking at the maximum key,
  testing if the priority queue is empty, and iterating through
  the keys.
  The insert and delete-the-maximum operations take logarithmic amortized time. The max, size, and is-empty operations take constant time. Construction takes time proportional to the specified capacity or the number of items used to initialize the data structure.
This implementation uses a binary heap.
For additional documentation, see Section 2.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- 
Nested Class SummaryNested Classes
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprivate Comparator<? super K> static booleanA test client.private intprivate K[]
- 
Constructor SummaryConstructorsConstructorDescriptionMaxPQ()Create an empty priority queue.MaxPQ(int initCapacity) Create an empty priority queue with the given initial capacity.MaxPQ(int initCapacity, Comparator<? super K> comparator) MaxPQ(Comparator<? super K> comparator) Create an empty priority queue using the given comparator.Create a priority queue with the given items.
- 
Method SummaryModifier and TypeMethodDescriptiondelMax()Delete and return the largest key on the priority queue.private voidexch(int i, int j) voidAdd a new key to the priority queue.booleanisEmpty()Is the priority queue empty?private booleanprivate booleanisMaxHeap(int k) iterator()Return an iterator that iterates over all of the keys on the priority queue in descending order.private booleanless(int i, int j) static voidmax()Return the largest key on the priority queue.private voidresize(int capacity) (package private) voidshowHeap()private voidsink(int k) intsize()Return the number of items on the priority queue.private voidswim(int k) Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.IterableforEach, spliterator
- 
Field Details- 
pq
- 
N
- 
comparator
- 
DEBUGA test client.
 
- 
- 
Constructor Details- 
MaxPQ
- 
MaxPQCreate an empty priority queue with the given initial capacity.
- 
MaxPQCreate an empty priority queue using the given comparator.
- 
MaxPQpublic MaxPQ()Create an empty priority queue.
- 
MaxPQCreate a priority queue with the given items. Takes time proportional to the number of items using sink-based heap construction.
 
- 
- 
Method Details- 
resize
- 
isEmptyIs the priority queue empty?
- 
sizeReturn the number of items on the priority queue.
- 
maxReturn the largest key on the priority queue.- Throws:
- NoSuchElementException- if the priority queue is empty.
 
- 
insertAdd a new key to the priority queue.
- 
delMaxDelete and return the largest key on the priority queue.- Throws:
- NoSuchElementException- if the priority queue is empty.
 
- 
swim
- 
sink
- 
less
- 
exch
- 
isMaxHeap
- 
isMaxHeap
- 
iteratorReturn an iterator that iterates over all of the keys on the priority queue in descending order.The iterator doesn't implement remove()since it's optional.- Specified by:
- iteratorin interface- Iterable<K extends Comparable<? super K>>
 
- 
showHeapvoid showHeap()
- 
main
 
-