-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcourse-max-binary-heap.js
69 lines (66 loc) · 1.71 KB
/
course-max-binary-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
class MaxBinaryHeap {
constructor() {
this.values = [];
}
insert(val) {
this.values.push(val);
let pInx = this.values.length - 1;
let nInx = Math.floor((pInx - 1) / 2);
// 33 41
// (n - 1) / 2
// 6 => (5 - 1) / 2 => 2
// 2 => (2 - 1) / 2 => 0.5
let next = true;
while (next) {
next = false;
if (this.values[nInx] < val) {
let tmp = this.values[pInx];
this.values[pInx] = this.values[nInx];
this.values[nInx] = tmp;
next = true;
}
pInx = nInx;
nInx = Math.floor((nInx - 1) / 2);
}
return this.values;
}
extractMax() {
const swap = (i0, i1) => {
let tmp = this.values[i0];
this.values[i0] = this.values[i1];
this.values[i1] = tmp;
};
let last = this.values.pop();
this.values[0] = last;
let inx = 0;
let lChildInx = 2 * inx + 1;
let rChildInx = 2 * inx + 2;
while (true) {
if (this.values[inx] < this.values[lChildInx]) {
if (this.values[inx] < this.values[rChildInx]) {
if (this.values[lChildInx] > this.values[rChildInx]) {
swap(inx, lChildInx);
inx = lChildInx;
} else {
swap(inx, rChildInx);
inx = rChildInx;
}
} else {
swap(inx, lChildInx);
}
} else if (this.values[inx] > this.values[rChildInx]) {
swap(inx, rChildInx);
inx = rChildInx;
} else {
break;
}
rChildInx = 2 * lChildInx + 2;
lChildInx = 2 * lChildInx + 1;
}
return this.values;
}
}
const maxBinaryHeap = new MaxBinaryHeap();
maxBinaryHeap.values = [41, 39, 33, 18, 27, 12];
// maxBinaryHeap.insert(55);
maxBinaryHeap.extractMax();