Package algs24
Class IndexMaxPQ<K extends Comparable<? super K>>
java.lang.Object
algs24.IndexMaxPQ<K>
public class IndexMaxPQ<K extends Comparable<? super K>>
extends Object
implements Iterable<Integer>
The
IndexMaxPQ class represents an indexed priority queue of generic keys.
It supports the usual insert and delete-the-maximum
operations, along with delete and change-the-key
methods. In order to let the client refer to items on the priority queue,
an integer between 0 and NMAX-1 is associated with each key—the client
uses this integer to specify which key to delete or change.
It also supports methods for peeking at the maximum key,
testing if the priority queue is empty, and iterating through
the keys.
The insert, delete-the-maximum, delete, change-key, decrease-key, and increase-key operations take logarithmic time. The is-empty, size, max-index, max-key, and key-of operations take constant time. Construction takes time proportional to the specified capacity.
This implementation uses a binary heap along with an array to associate keys with integers in the given range.
For additional documentation, see Section 2.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionIndexMaxPQ(int NMAX) Create an empty indexed priority queue with indices between0andNMAX-1. -
Method Summary
Modifier and TypeMethodDescriptionvoidDeprecated.Replaced by changeKey()voidChange the key associated with index i to the specified value.booleancontains(int i) Is i an index on the priority queue?voiddecreaseKey(int i, K key) Decrease the key associated with index i to the specified value.voiddelete(int i) Delete the key associated with index i.intdelMax()Delete a maximal key and return its associated index.private voidexch(int i, int j) voidincreaseKey(int i, K key) Increase the key associated with index i to the specified value.voidAssociate key with index i.booleanisEmpty()Is the priority queue empty?iterator()Return an iterator that iterates over all of the elements on the priority queue in descending order.keyOf(int i) Return the key associated with index i.private booleanless(int i, int j) static voidintmaxIndex()Return the index associated with a maximal key.maxKey()Return a minimal key.private voidsink(int k) intsize()Return the number of keys on the priority queue.private voidswim(int k) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
N
-
pq
-
qp
-
keys
-
-
Constructor Details
-
IndexMaxPQ
Create an empty indexed priority queue with indices between0andNMAX-1.- Throws:
IllegalArgumentException- ifNMAX < 0
-
-
Method Details
-
isEmpty
Is the priority queue empty? -
contains
Is i an index on the priority queue?- Throws:
IndexOutOfBoundsException- unless(0 <= i < NMAX)
-
size
Return the number of keys on the priority queue. -
insert
Associate key with index i.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXIllegalArgumentException- if there already is an item associated with index i.
-
maxIndex
Return the index associated with a maximal key.- Throws:
NoSuchElementException- if priority queue is empty.
-
maxKey
Return a minimal key.- Throws:
NoSuchElementException- if priority queue is empty.
-
delMax
Delete a maximal key and return its associated index.- Throws:
NoSuchElementException- if priority queue is empty.
-
keyOf
Return the key associated with index i.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXNoSuchElementException- no key is associated with index i
-
change
Deprecated.Replaced by changeKey()Change the key associated with index i to the specified value.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXNoSuchElementException- no key is associated with index i
-
changeKey
Change the key associated with index i to the specified value.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXNoSuchElementException- no key is associated with index i
-
increaseKey
Increase the key associated with index i to the specified value.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXIllegalArgumentException- if key ≤ key associated with index iNoSuchElementException- no key is associated with index i
-
decreaseKey
Decrease the key associated with index i to the specified value.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXIllegalArgumentException- if key ≥ key associated with index iNoSuchElementException- no key is associated with index i
-
delete
Delete the key associated with index i.- Throws:
IndexOutOfBoundsException- unless0 <= i < NMAXNoSuchElementException- no key is associated with index i
-
less
-
exch
-
swim
-
sink
-
iterator
Return an iterator that iterates over all of the elements on the priority queue in descending order.The iterator doesn't implement
remove()since it's optional.- Specified by:
iteratorin interfaceIterable<K extends Comparable<? super K>>
-
main
-