无界优先级队列的实现与分析
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
无界优先级队列:堆与跳表实现分析
超级会员免费看
订阅专栏 解锁全文
590

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



