我已经了解了优先级队列.但是当涉及到索引优先级队列时,我对某些方法的实现有点困惑,例如change(int k,Item item)和delete(int i).
change(int k,Item item)是将与k关联的项目更改为item
delete(int i)是删除k及其关联项
public void changeKey(int i, Key key) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
keys[i] = key;
swim(qp[i]);
sink(qp[i]);
}
public void delete(int i) {
if (i < 0 || i >= maxN) throw new IndexOutOfBoundsException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
int index = qp[i];
exch(index, …Run Code Online (Sandbox Code Playgroud)