-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapBasedImplementation.js
113 lines (95 loc) · 2.25 KB
/
HeapBasedImplementation.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
107
108
109
110
111
112
113
// Elements in this heap would be <value, key>
class HeapBasedImplementation {
constructor() {
this.heap = [];
}
findParent(index) {
if (index === 0) return index;
return Math.floor((index - 1) / 2);
}
getLeft(index) {
return 2 * index + 1;
}
getRight(index) {
return 2 * index + 2;
}
getParent(index) {
if (index > 0) {
return Math.floor((index - 1) / 2);
}
return 0;
}
swap(arr, index1, index2) {
const argArr = [...arr];
const temp = argArr[index1];
argArr[index1] = argArr[index2];
argArr[index2] = temp;
return argArr;
}
/**
* This takes O(logn) time.
* It percolates the element with minimum priority
* to top of heap.
*/
minHeapify(arr, index) {
const left = this.getLeft(index);
const right = this.getRight(index);
let smallest = index;
if (left < arr.length && arr[left].key < arr[index].key) {
smallest = left;
}
if (right < arr.length && arr[right].key < arr[smallest].key) {
smallest = right;
}
if (smallest !== index) {
arr = this.swap(arr, smallest, index);
return this.minHeapify(arr, smallest);
}
return arr;
}
heapExtractMax() {
const { value } = this.heap.shift();
this.heap = this.minHeapify(this.heap, 0);
return value;
}
dequeue() {
if (this.heap.length > 0) {
return this.heapExtractMax();
}
return null;
}
heapDecreaseKey(arr, i, element) {
const { key, value } = element;
if (arr[i].key < key) {
throw new Error("new key is bigger than current key");
}
arr[i] = { key, value };
while (i > 0 && arr[this.getParent(i)].key > arr[i].key) {
arr = this.swap(arr, this.getParent(i), i);
i = this.getParent(i);
}
return arr;
}
minHeapInsert(arr, newElement) {
arr.push({
value: null,
key: Infinity
});
arr = this.heapDecreaseKey(arr, arr.length - 1, newElement);
return arr;
}
queue(value, key = 5) {
const newElement = { value, key };
this.heap = this.minHeapInsert(this.heap, newElement);
}
peek() {
if (this.heap.length > 0) {
return this.heap[0].value;
}
return null;
}
clear() {
this.heap = [];
}
}
module.exports = HeapBasedImplementation;