-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaxBinaryHeap.js
106 lines (83 loc) · 2.42 KB
/
maxBinaryHeap.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
A max binary heap is a data structure that uses an array (under the hood) to mimic a binary tree.
It has a variety of use cases including (notably) priority queues.
*/
class MaxBinaryHeap {
constructor() {
this.values = [];
}
insert(newValue) {
this.values.push(newValue);
return this._bubbleUpValue();
}
remove() {
if (this.values.length <= 1) {
const value = this.values[0];
this.values = [];
return value;
}
const lastIndex = this.values.length - 1;
this._swapValues(0, lastIndex);
const value = this.values.pop();
this._bubbleDownValue();
return value;
}
_swapValues(indexOne, indexTwo) {
[this.values[indexOne], this.values[indexTwo]] = [
this.values[indexTwo],
this.values[indexOne],
];
}
_getChildIndices(currentIndex) {
const baseMultiplier = currentIndex * 2;
const leftChildIndex = baseMultiplier + 1;
const rightChildIndex = baseMultiplier + 2;
return [leftChildIndex, rightChildIndex];
}
_getParentIndex(childIndex) {
return Math.max(Math.floor((childIndex - 1) / 2), 0);
}
_bubbleUpValue() {
let index = this.values.length - 1;
const value = this.values[index];
while (true) {
const parentIndex = this._getParentIndex(index);
if (this.values[parentIndex] < value) {
this._swapValues(index, parentIndex);
index = parentIndex;
} else {
break;
}
}
return index;
}
_bubbleDownValue() {
let index = 0;
while (true) {
const currentValue = this.values[index];
const [rightChildIndex, leftChildIndex] = this._getChildIndices(index);
const rightValue = this.values[rightChildIndex];
const leftValue = this.values[leftChildIndex];
if (
leftValue &&
rightValue &&
leftValue > currentValue &&
rightValue > currentValue
) {
const largestChildIndex =
leftValue > rightValue ? leftChildIndex : rightChildIndex;
this._swapValues(index, largestChildIndex);
index = largestChildIndex;
} else if (leftValue && leftValue > currentValue) {
this._swapValues(index, leftChildIndex);
index = leftChildIndex;
} else if (rightValue && rightValue > currentValue) {
this._swapValues(index, rightChildIndex);
index = rightChildIndex;
} else {
break;
}
}
}
}
module.exports = MaxBinaryHeap;