edu.princeton.cs.algs4
Class IndexMinPQ<Key extends java.lang.Comparable<Key>>
- java.lang.Object
-
- edu.princeton.cs.algs4.IndexMinPQ<Key>
-
- Type Parameters:
Key- the generic type of key on this priority queue
- All Implemented Interfaces:
- java.lang.Iterable<java.lang.Integer>
public class IndexMinPQ<Key extends java.lang.Comparable<Key>> extends java.lang.Object implements java.lang.Iterable<java.lang.Integer>TheIndexMinPQclass represents an indexed priority queue of generic keys. It supports the usual insert and delete-the-minimum operations, along with delete and change-the-key methods. In order to let the client refer to keys on the priority queue, an integer between0andmaxN - 1is 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 minimum 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-minimum, delete, change-key, decrease-key, and increase-key operations take logarithmic time. The is-empty, size, min-index, min-key, and key-of operations take constant time. 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.
-
-
Constructor Summary
Constructors Constructor and Description IndexMinPQ(int maxN)Initializes an empty indexed priority queue with indices between0andmaxN - 1.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description voidchange(int i, Key key)Deprecated.Replaced bychangeKey(int, Key).voidchangeKey(int i, Key key)Change the key associated with indexito the specified value.booleancontains(int i)Isian index on this priority queue?voiddecreaseKey(int i, Key key)Decrease the key associated with indexito the specified value.voiddelete(int i)Remove the key associated with indexi.intdelMin()Removes a minimum key and returns its associated index.voidincreaseKey(int i, Key key)Increase the key associated with indexito the specified value.voidinsert(int i, Key key)Associates key with indexi.booleanisEmpty()Returns true if this priority queue is empty.java.util.Iterator<java.lang.Integer>iterator()Returns an iterator that iterates over the keys on the priority queue in ascending order.KeykeyOf(int i)Returns the key associated with indexi.static voidmain(java.lang.String[] args)Unit tests theIndexMinPQdata type.intminIndex()Returns an index associated with a minimum key.KeyminKey()Returns a minimum key.intsize()Returns the number of keys on this priority queue.
-
-
-
Constructor Detail
-
IndexMinPQ
public IndexMinPQ(int maxN)
Initializes an empty indexed priority queue with indices between0andmaxN - 1.- Parameters:
maxN- the keys on this priority queue are index from0maxN - 1- Throws:
java.lang.IllegalArgumentException- ifmaxN < 0
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returns true if this priority queue is empty.- Returns:
trueif this priority queue is empty;falseotherwise
-
contains
public boolean contains(int i)
Isian index on this priority queue?- Parameters:
i- an index- Returns:
trueifiis an index on this priority queue;falseotherwise- Throws:
java.lang.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)Associates key with indexi.- Parameters:
i- an indexkey- the key to associate with indexi- Throws:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.lang.IllegalArgumentException- if there already is an item associated with indexi
-
minIndex
public int minIndex()
Returns an index associated with a minimum key.- Returns:
- an index associated with a minimum key
- Throws:
java.util.NoSuchElementException- if this priority queue is empty
-
minKey
public Key minKey()
Returns a minimum key.- Returns:
- a minimum key
- Throws:
java.util.NoSuchElementException- if this priority queue is empty
-
delMin
public int delMin()
Removes a minimum key and returns its associated index.- Returns:
- an index associated with a minimum key
- Throws:
java.util.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:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.util.NoSuchElementException- no key is associated with indexi
-
changeKey
public void changeKey(int i, Key key)Change the key associated with indexito the specified value.- Parameters:
i- the index of the key to changekey- change the key associated with indexito this key- Throws:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.util.NoSuchElementException- no key is associated with indexi
-
change
@Deprecated public void change(int i, Key key)Deprecated. Replaced bychangeKey(int, Key).Change the key associated with indexito the specified value.- Parameters:
i- the index of the key to changekey- change the key associated with indexito this key- Throws:
java.lang.IllegalArgumentException- unless0 <= i < maxN
-
decreaseKey
public void decreaseKey(int i, Key key)Decrease the key associated with indexito the specified value.- Parameters:
i- the index of the key to decreasekey- decrease the key associated with indexito this key- Throws:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.lang.IllegalArgumentException- ifkey >= keyOf(i)java.util.NoSuchElementException- no key is associated with indexi
-
increaseKey
public void increaseKey(int i, Key key)Increase the key associated with indexito the specified value.- Parameters:
i- the index of the key to increasekey- increase the key associated with indexito this key- Throws:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.lang.IllegalArgumentException- ifkey <= keyOf(i)java.util.NoSuchElementException- no key is associated with indexi
-
delete
public void delete(int i)
Remove the key associated with indexi.- Parameters:
i- the index of the key to remove- Throws:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.util.NoSuchElementException- no key is associated with indexi
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
Returns an iterator that iterates over the keys on the priority queue in ascending order. The iterator doesn't implementremove()since it's optional.- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Integer>- Returns:
- an iterator that iterates over the keys in ascending order
-
main
public static void main(java.lang.String[] args)
Unit tests theIndexMinPQdata type.- Parameters:
args- the command-line arguments
-
-
DataMelt 3.0 © DataMelt by jWork.ORG