-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-heap.js
More file actions
78 lines (62 loc) · 1.55 KB
/
Copy pathbinary-heap.js
File metadata and controls
78 lines (62 loc) · 1.55 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
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
76
77
78
class BinaryHeap {
constructor() {
this.values = [];
}
insert(val) {
this.values.push(val);
if (!this.values.length) return this;
let index = this.values.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (this.values[index] <= this.values[parentIndex]) break;
[this.values[parentIndex], this.values[index]] = [
this.values[index],
this.values[parentIndex],
];
index = parentIndex;
}
}
extractMax() {
const max = this.values[0];
const end = this.values.pop();
this.values[0] = end;
if (this.values.length === 1) return this.values.pop();
let idx = 0;
let length = this.values.length;
let element = this.values[idx];
while (idx < length) {
let leftIdx = idx * 2 + 1;
let rightIdx = idx * 2 + 2;
let left, right;
let swap = null;
if (leftIdx < length) {
left = this.values[leftIdx];
if (left > element) {
swap = leftIdx;
}
}
if (rightIdx < length) {
right = this.values[rightIdx];
if (
(swap === null && right > element) ||
(swap !== null && right > left)
) {
swap = rightIdx;
}
}
if (swap === null) break;
this.values[idx] = this.values[swap];
this.values[swap] = element;
idx = swap;
}
return max;
}
}
const bh = new BinaryHeap();
// bh.insert(10)
// bh.insert(11)
// bh.insert(9)
// bh.insert(8)
// bh.insert(20)
// bh.insert(25)
// bh.insert(17)