-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority-queue.js
More file actions
50 lines (44 loc) · 1.29 KB
/
priority-queue.js
File metadata and controls
50 lines (44 loc) · 1.29 KB
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
/**
* Min Priority Queue (smallest value dequeued first).
*/
class PriorityQueue {
constructor(comparator = (a, b) => a - b) {
this._heap = [];
this._cmp = comparator;
}
enqueue(val) {
this._heap.push(val);
this._bubbleUp(this._heap.length - 1);
}
dequeue() {
if (!this._heap.length) return undefined;
const top = this._heap[0];
const last = this._heap.pop();
if (this._heap.length) { this._heap[0] = last; this._siftDown(0); }
return top;
}
peek() { return this._heap[0]; }
get size() { return this._heap.length; }
isEmpty() { return this._heap.length === 0; }
_bubbleUp(i) {
while (i > 0) {
const parent = (i - 1) >> 1;
if (this._cmp(this._heap[i], this._heap[parent]) < 0) {
[this._heap[i], this._heap[parent]] = [this._heap[parent], this._heap[i]];
i = parent;
} else break;
}
}
_siftDown(i) {
const n = this._heap.length;
while (true) {
let min = i, l = 2*i+1, r = 2*i+2;
if (l < n && this._cmp(this._heap[l], this._heap[min]) < 0) min = l;
if (r < n && this._cmp(this._heap[r], this._heap[min]) < 0) min = r;
if (min === i) break;
[this._heap[i], this._heap[min]] = [this._heap[min], this._heap[i]];
i = min;
}
}
}
module.exports = { PriorityQueue };