-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority-queue-heap.js
75 lines (64 loc) · 1.52 KB
/
priority-queue-heap.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
66
67
68
69
70
71
72
73
74
75
class PriorityQueue {
constructor(comparator, max) {
this.comparator = comparator;
this.max = max;
this.size = 0;
this.data = [null];
}
swap (i, j) {
const temp = this.data[i];
this.data[i] = this.data[j];
this.data[j] = temp;
}
bubbleUp(index) {
const comparator = (i, j) => this.comparator(this.data[i], this.data[j]);
while (index > 1 && comparator(index, Math.floor(index / 2)) > 0) {
const parent = Math.floor(index / 2);
this.swap(index, parent);
index = parent;
}
}
bubbleDown() {
let index = 1;
const comparator = (i, j) => this.comparator(this.data[i], this.data[j]);
while (index < this.size) {
const leftIndex = index * 2;
const rightIndex = leftIndex + 1;
if (comparator(index, leftIndex) < 0) {
this.swap(index, leftIndex);
index = leftIndex;
continue;
}
if (comparator(index, rightIndex) < 0) {
this.swap(index, rightIndex);
index = rightIndex;
continue;
}
break;
}
}
add(val) {
this.size++;
const index = this.size;
this.data[index] = val;
this.bubbleUp(index);
}
peek() {
return this.data[1];
}
remove() {
const result = this.data[1];
this.swap(1, this.size);
this.data.length = this.size;
this.size--;
this.bubbleDown();
return result;
}
}
const pq = new PriorityQueue((a, b) => a - b);
const addAll = (nums) => {
nums.forEach((num) => {
pq.add(num);
});
};
addAll([5, 6, 7, 9]);