// 删除堆中最大的元素并返回这个最大的元素 public T delMax() { Tmax= data[1]; exch(1, maxPriorityQueueLength); maxPriorityQueueLength--; sink(1); return max; }
// 使用上浮算法,使索引k处的元素能在堆中处于一个正确的位置 privatevoidswim(int k) { while (k>1) { if (less(k/2, k)) { exch(k/2, k); } k = k/2; } }
// 使用下沉算法,使索引k处的元素能在堆中处于一个正确的位置 privatevoidsink(int k) { while (2*k<maxPriorityQueueLength) { int max; if (2*k+1<=maxPriorityQueueLength) { if (less(2*k, 2*k+1)) { max = 2*k+1; } else { max = 2*k; } } else { max = 2*k; }