-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathindex.js
78 lines (66 loc) · 1.83 KB
/
index.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
class MinHeap {
constructor(collection) {
this.heap = [];
if (collection) {
collection.forEach((element) => {
this.add(element);
});
}
}
add(element) {
this.heap.push(element);
// check for the parent element & swap if required
// eslint-disable-next-line no-underscore-dangle
this.__traverseUpAndSwap(this.heap.length - 1);
}
getMin() {
return this.heap[0] !== undefined ? this.heap[0] : null;
}
remove() {
const min = this.heap[0] !== undefined ? this.heap[0] : null;
if (this.heap.length === 1) {
this.heap.pop();
}
if (this.heap.length > 1) {
this.heap[0] = this.heap[this.heap.length - 1];
this.heap.pop();
// eslint-disable-next-line no-underscore-dangle
this.__heapify(0);
}
return min;
}
destroy() {
this.heap = [];
}
// eslint-disable-next-line consistent-return
__traverseUpAndSwap(index) {
if (index <= 0) return null;
const parent = Math.floor(index / 2);
if (this.heap[parent] > this.heap[index]) {
const temp = this.heap[parent];
this.heap[parent] = this.heap[index];
this.heap[index] = temp;
// eslint-disable-next-line no-underscore-dangle
this.__traverseUpAndSwap(parent);
}
}
__heapify(index) {
const left = index * 2;
const right = index * 2 + 1;
let smallest = index;
if (this.heap.length > left && this.heap[smallest] > this.heap[left]) {
smallest = left;
}
if (this.heap.length > right && this.heap[smallest] > this.heap[right]) {
smallest = right;
}
if (smallest !== index) {
const tmp = this.heap[smallest];
this.heap[smallest] = this.heap[index];
this.heap[index] = tmp;
// eslint-disable-next-line no-underscore-dangle
this.__heapify(smallest);
}
}
}
module.exports = MinHeap;