-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinBinaryHeap.js
63 lines (55 loc) · 1.81 KB
/
MinBinaryHeap.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
// [a, b, b, c, c, c, c]
// left child index - (index * 2) + 1
// right child index -> (index * 2) + 2
// parent index -> Math.floor((index - 1) / 2) ;
class MinBinaryHeap {
constructor() {
this.values = [];
}
insert(value) {
this.values.push(value);
let currentIndex = this.values.length - 1;
while (currentIndex > 0) {
let parentIndex = Math.floor((currentIndex - 1) / 2);
if (this.values[currentIndex] < this.values[parentIndex]) {
// swap
let parent = this.values[parentIndex];
this.values[parentIndex] = this.values[currentIndex];
this.values[currentIndex] = parent;
currentIndex = parentIndex;
} else break;
}
}
extractMin() {
[this.values[0], this.values[this.values.length - 1]] = [this.values[this.values.length - 1], this.values[0]];
const max = this.values.pop();
let currentIndex = 0;
while (currentIndex < this.values.length) {
let current = this.values[currentIndex];
let leftChildIndex = currentIndex * 2 + 1;
let rightChildIndex = leftChildIndex + 1;
let rightChild = this.values[rightChildIndex];
let leftChild = this.values[leftChildIndex];
if (leftChild < current && rightChild < current) {
if (leftChild < rightChild) {
this.values[currentIndex] = leftChild;
this.values[leftChildIndex] = current;
currentIndex = leftChildIndex;
} else {
this.values[currentIndex] = rightChild;
this.values[rightChildIndex] = current;
currentIndex = rightChildIndex;
}
} else if (leftChild < current) {
this.values[currentIndex] = leftChild;
this.values[leftChildIndex] = current;
currentIndex = leftChildIndex;
} else if (rightChild < current) {
this.values[currentIndex] = rightChild;
this.values[rightChildIndex] = current;
currentIndex = rightChildIndex;
} else break;
}
return max;
}
}