Package edu.princeton.cs.algs4
Class IndexMaxPQ<Key extends Comparable<Key>>
- Object
-
- edu.princeton.cs.algs4.IndexMaxPQ<Key>
-
- Type Parameters:
Key
- the generic type of key on this priority queue
public class IndexMaxPQ<Key extends Comparable<Key>> extends Object implements Iterable<Integer>
TheIndexMaxPQ
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 between0
andmaxN - 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 a maximum key, testing if the priority queue is empty, and iterating through the keys.This implementation uses a binary heap along with an array to associate keys with integers in the given range. The insert, delete-the-maximum, delete, change-key, decrease-key, and increase-key operations take Θ(log n) time in the worst case, where n is the number of elements in the priority queue. Construction takes time proportional to the specified capacity.
For additional documentation, see Section 2.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description IndexMaxPQ(int maxN)
Initializes an empty indexed priority queue with indices between0
andmaxN - 1
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
changeKey(int i, Key key)
Change the key associated with indexi
to the specified value.boolean
contains(int i)
Isi
an index on this priority queue?void
decreaseKey(int i, Key key)
Decrease the key associated with indexi
to the specified value.void
delete(int i)
Remove the key on the priority queue associated with indexi
.int
delMax()
Removes a maximum key and returns its associated index.void
increaseKey(int i, Key key)
Increase the key associated with indexi
to the specified value.void
insert(int i, Key key)
Associate key with index i.boolean
isEmpty()
Returns true if this priority queue is empty.Iterator<Integer>
iterator()
Returns an iterator that iterates over the keys on the priority queue in descending order.Key
keyOf(int i)
Returns the key associated with indexi
.static void
main(String[] args)
Unit tests theIndexMaxPQ
data type.int
maxIndex()
Returns an index associated with a maximum key.Key
maxKey()
Returns a maximum key.int
size()
Returns the number of keys on this priority queue.-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
IndexMaxPQ
public IndexMaxPQ(int maxN)
Initializes an empty indexed priority queue with indices between0
andmaxN - 1
.- Parameters:
maxN
- the keys on this priority queue are index from0
tomaxN - 1
- Throws:
IllegalArgumentException
- ifmaxN < 0
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returns true if this priority queue is empty.- Returns:
true
if this priority queue is empty;false
otherwise
-
contains
public boolean contains(int i)
Isi
an index on this priority queue?- Parameters:
i
- an index- Returns:
true
ifi
is an index on this priority queue;false
otherwise- Throws:
IllegalArgumentException
- unless0 <= i < maxN
-
size
public int size()
Returns the number of keys on this priority queue.- Returns:
- the number of keys on this priority queue
-
insert
public void insert(int i, Key key)
Associate key with index i.- Parameters:
i
- an indexkey
- the key to associate with indexi
- Throws:
IllegalArgumentException
- unless0 <= i < maxN
IllegalArgumentException
- if there already is an item associated with indexi
-
maxIndex
public int maxIndex()
Returns an index associated with a maximum key.- Returns:
- an index associated with a maximum key
- Throws:
NoSuchElementException
- if this priority queue is empty
-
maxKey
public Key maxKey()
Returns a maximum key.- Returns:
- a maximum key
- Throws:
NoSuchElementException
- if this priority queue is empty
-
delMax
public int delMax()
Removes a maximum key and returns its associated index.- Returns:
- an index associated with a maximum key
- Throws:
NoSuchElementException
- if this priority queue is empty
-
keyOf
public Key keyOf(int i)
Returns the key associated with indexi
.- Parameters:
i
- the index of the key to return- Returns:
- the key associated with index
i
- Throws:
IllegalArgumentException
- unless0 <= i < maxN
NoSuchElementException
- no key is associated with indexi
-
changeKey
public void changeKey(int i, Key key)
Change the key associated with indexi
to the specified value.- Parameters:
i
- the index of the key to changekey
- change the key associated with indexi
to this key- Throws:
IllegalArgumentException
- unless0 <= i < maxN
-
increaseKey
public void increaseKey(int i, Key key)
Increase the key associated with indexi
to the specified value.- Parameters:
i
- the index of the key to increasekey
- increase the key associated with indexi
to this key- Throws:
IllegalArgumentException
- unless0 <= i < maxN
IllegalArgumentException
- ifkey <= keyOf(i)
NoSuchElementException
- no key is associated with indexi
-
decreaseKey
public void decreaseKey(int i, Key key)
Decrease the key associated with indexi
to the specified value.- Parameters:
i
- the index of the key to decreasekey
- decrease the key associated with indexi
to this key- Throws:
IllegalArgumentException
- unless0 <= i < maxN
IllegalArgumentException
- ifkey >= keyOf(i)
NoSuchElementException
- no key is associated with indexi
-
delete
public void delete(int i)
Remove the key on the priority queue associated with indexi
.- Parameters:
i
- the index of the key to remove- Throws:
IllegalArgumentException
- unless0 <= i < maxN
NoSuchElementException
- no key is associated with indexi
-
iterator
public Iterator<Integer> iterator()
Returns an iterator that iterates over the keys on the priority queue in descending order. The iterator doesn't implementremove()
since it's optional.- Specified by:
iterator
in interfaceIterable<Key extends Comparable<Key>>
- Returns:
- an iterator that iterates over the keys in descending order
-
main
public static void main(String[] args)
Unit tests theIndexMaxPQ
data type.- Parameters:
args
- the command-line arguments
-
-