-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriority_queue.js
65 lines (58 loc) · 1.73 KB
/
priority_queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class PriorityQueue {
constructor() {
this.heap = [];
}
push(entry) {
this.heap.push(entry);
this.bubbleUp(this.heap.length - 1);
}
shift() {
if (this.isEmpty()) {
return null;
}
const min = this.heap[0];
const end = this.heap.pop();
if (this.heap.length > 0) {
this.heap[0] = end;
this.bubbleDown(0);
}
return min;
}
bubbleUp(index) {
const entry = this.heap[index];
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
const parent = this.heap[parentIndex];
if (entry.value.score >= parent.value.score) {
break;
}
this.heap[parentIndex] = entry;
this.heap[index] = parent;
index = parentIndex;
}
}
bubbleDown(index) {
const length = this.heap.length;
const entry = this.heap[index];
while (true) {
let leftChildIndex = 2 * index + 1;
let rightChildIndex = 2 * index + 2;
let swap = null;
if (leftChildIndex < length && this.heap[leftChildIndex].value.score < entry.value.score) {
swap = leftChildIndex;
}
if (rightChildIndex < length && this.heap[rightChildIndex].value.score < (swap === null ? entry.value.score : this.heap[swap].value.score)) {
swap = rightChildIndex;
}
if (swap === null) {
break;
}
this.heap[index] = this.heap[swap];
this.heap[swap] = entry;
index = swap;
}
}
isEmpty() {
return this.heap.length === 0;
}
}