的困惑,我的一些方法,如变化的FPGA实现(INT K A有点困惑, Item item) and delete(int i)。我已经得到了优先queue.But的想法,当涉及到索引优先级队列索引优先级队列
变化(INT K,项项)是具有k相关联的项目改变为项目
删除(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, n--);
swim(index);
sink(index);
keys[i] = null;
qp[i] = -1;
}
private void swim(int k) {
while (k > 1 && greater(k/2, k)) {
exch(k, k/2);
k = k/2;
}
}
private void sink(int k) {
while (2*k <= n) {
int j = 2*k;
if (j < n && greater(j, j+1)) j++;
if (!greater(k, j)) break;
exch(k, j);
k = j;
}
}
private int maxN; // maximum number of elements on PQ
private int n; // number of elements on PQ
private int[] pq; // binary heap using 1-based indexing
private int[] qp; // inverse of pq - qp[pq[i]] = pq[qp[i]] = i
private Key[] keys; // keys[i] = priority of i
我明白了接收器和游泳的操作。但是为什么在方法删除(int i)和chang eKey(int i,Key key)有语句swim(qp[i]/index);
和sink(qp[i]/index);
究竟发生了什么?
而且我也想知道优先级队列和索引优先级队列,什么之间的所有元素作风建设是存储在索引优先级队列?索引或元素的二叉堆?
IndexPrioriryQueue(或任何你命名的类)不是标准类。可以分享这个类的其余代码吗?或者您使用的API的名称? – Asoub
@Asoub的[代码](http://algs4.cs.princeton.edu/24pq/IndexMaxPQ.java.html)似乎有从[算法](http://algs4.cs.princeton.edu/24pq未来/)书。 –