无界堆和跳表实现的优先队列
1. 无界堆基于的优先队列
1.1 顺序堆的 removeMin 方法
顺序堆的 removeMin 方法用于移除堆中优先级最小的元素。以下是该方法的代码:
public T removeMin() {
int bottom = --next;
T item = heap[ROOT].item;
heap[ROOT] = heap[bottom];
if (bottom == ROOT) {
return item;
}
int child = 0;
int parent = ROOT;
while (parent < heap.length / 2) {
int left = parent * 2; int right = (parent * 2) + 1;
if (left >= next) {
return item;
} else if (right >= next || heap[left].score < heap[right].score) {
child = left;
} else {
child = right;
}
if (heap[child].score < heap[parent].score) {
swap
超级会员免费看
订阅专栏 解锁全文
171万+

被折叠的 条评论
为什么被折叠?



