-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinaryHeap.js
83 lines (69 loc) · 1.89 KB
/
BinaryHeap.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
class BinaryHeap {
constructor() {
this.heap = [];
this.heap.push({});
}
size = () => {
return this.heap.length - 1;
};
isEmpty = () => {
return this.size() === 0;
};
swim = (index) => {
let parentIndex = this.getParent(index);
while (index > 1 && this.less(index, parentIndex)) {
this.exchange(index, parentIndex);
index = parentIndex;
parentIndex = this.getParent(index);
}
};
sink = (index) => {
let bestChild = this.getBestChild(index);
while (bestChild <= this.size() && this.less(bestChild, index)) {
this.exchange(index, bestChild);
index = bestChild;
bestChild = this.getBestChild(index);
}
};
insert = (item) => {
this.heap.push(item);
this.swim(this.size());
};
remove = () => {
if (this.isEmpty())
throw new Error();
this.exchange(1, this.size());
let returnVal = this.heap.pop();
this.sink(1);
return returnVal;
};
exchange = (i, j) => {
let temp = this.heap[i];
this.heap[i] = this.heap[j];
this.heap[j] = temp;
};
less = (i, j) => {
return this.heap[i].key < this.heap[j].key;
};
getParent = (index) => {
return Math.floor(index/2);
};
getLeftChild = (index) => {
return 2 * index;
}
getBestChild = (index) => {
let leftChild = this.getLeftChild(index);
if (leftChild + 1 > this.size() || this.less(leftChild, leftChild + 1)) {
return leftChild;
}
return leftChild + 1;
}
toString = () => {
let keys = [];
for (let i = 1; i <= this.size(); i++) {
keys.push(this.heap[i].key);
}
return keys.join(',');
}
}
module.exports = BinaryHeap;