类 IndexMaxPQ<Key extends java.lang.Comparable<Key>>
- java.lang.Object
-
- edu.princeton.cs.algs4.IndexMaxPQ<Key>
-
- 类型参数:
Key- the generic type of key on this priority queue
- 所有已实现的接口:
java.lang.Iterable<java.lang.Integer>
public class IndexMaxPQ<Key extends java.lang.Comparable<Key>> extends java.lang.Object implements java.lang.Iterable<java.lang.Integer>TheIndexMaxPQclass 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 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 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 logarithmic time. The is-empty, size, max-index, max-key, contains, 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.
-
-
构造器概要
构造器 构造器 说明 IndexMaxPQ(int maxN)Initializes an empty indexed priority queue with indices between0andmaxN - 1.
-
方法概要
修饰符和类型 方法 说明 voidchange(int i, Key key)已过时。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 on the priority queue associated with indexi.intdelMax()Removes a maximum 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)Associate key with index i.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 descending order.KeykeyOf(int i)Returns the key associated with indexi.static voidmain(java.lang.String[] args)Unit tests theIndexMaxPQdata type.intmaxIndex()Returns an index associated with a maximum key.KeymaxKey()Returns a maximum key.intsize()Returns the number of keys on this priority queue.
-
-
-
方法详细资料
-
isEmpty
public boolean isEmpty()
Returns true if this priority queue is empty.- 返回:
trueif this priority queue is empty;falseotherwise
-
contains
public boolean contains(int i)
Isian index on this priority queue?- 参数:
i- an index- 返回:
trueifiis an index on this priority queue;falseotherwise- 抛出:
java.lang.IllegalArgumentException- unless0 <= i < maxN
-
size
public int size()
Returns the number of keys on this priority queue.- 返回:
- the number of keys on this priority queue
-
insert
public void insert(int i, Key key)Associate key with index i.- 参数:
i- an indexkey- the key to associate with indexi- 抛出:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.lang.IllegalArgumentException- if there already is an item associated with indexi
-
maxIndex
public int maxIndex()
Returns an index associated with a maximum key.- 返回:
- an index associated with a maximum key
- 抛出:
java.util.NoSuchElementException- if this priority queue is empty
-
maxKey
public Key maxKey()
Returns a maximum key.- 返回:
- a maximum key
- 抛出:
java.util.NoSuchElementException- if this priority queue is empty
-
delMax
public int delMax()
Removes a maximum key and returns its associated index.- 返回:
- an index associated with a maximum key
- 抛出:
java.util.NoSuchElementException- if this priority queue is empty
-
keyOf
public Key keyOf(int i)
Returns the key associated with indexi.- 参数:
i- the index of the key to return- 返回:
- the key associated with index
i - 抛出:
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.- 参数:
i- the index of the key to changekey- change the key associated with indexito this key- 抛出:
java.lang.IllegalArgumentException- unless0 <= i < maxN
-
change
@Deprecated public void change(int i, Key key)已过时。Replaced bychangeKey(int, Key).Change the key associated with indexito the specified value.- 参数:
i- the index of the key to changekey- change the key associated with indexito this key- 抛出:
java.lang.IllegalArgumentException- unless0 <= i < maxN
-
increaseKey
public void increaseKey(int i, Key key)Increase the key associated with indexito the specified value.- 参数:
i- the index of the key to increasekey- increase the key associated with indexito this key- 抛出:
java.lang.IllegalArgumentException- unless0 <= i < maxNjava.lang.IllegalArgumentException- ifkey <= keyOf(i)java.util.NoSuchElementException- no key is associated with indexi
-
decreaseKey
public void decreaseKey(int i, Key key)Decrease the key associated with indexito the specified value.- 参数:
i- the index of the key to decreasekey- decrease the key associated with indexito this key- 抛出:
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 on the priority queue associated with indexi.- 参数:
i- the index of the key to remove- 抛出:
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 descending order. The iterator doesn't implementremove()since it's optional.
-
main
public static void main(java.lang.String[] args)
Unit tests theIndexMaxPQdata type.- 参数:
args- the command-line arguments
-
-