-
Notifications
You must be signed in to change notification settings - Fork 17
/
js-min-heap.js
187 lines (148 loc) · 6.21 KB
/
js-min-heap.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// https://en.wikipedia.org/wiki/Heap_(data_structure)
// https://en.wikipedia.org/wiki/Binary_heap
// https://www.g ee ksforgeeks.org/binary-heap/
// https://courses.cs.washington.edu/courses/cse373/18wi/files/slides/lecture-14-ann.pdf
// https://users.cs.duke.edu/~reif/courses/alglectures/skiena.lectures/lecture4.pdf
// https://github.com/manoharreddyporeddy/math-advanced-data-structures-and-algorithms
/*
min heap1 min heap2 min heap3 MAX heap1
10 10 1 100
/ \ / \ / \ / \
20 100 15 30 2 3 19 36
/ / \ / \ / \ / \ / \ / \
30 40 50 100 40 17 19 36 7 17 3 25 1
/ \ / \
25 100 2 7
*/
// A class for Min Heap
class MinHeap {
// Constructor: Builds a heap from a given array a[] of given size
constructor(capacity) {
// console.log("constructor", capacity);
this.INT_MIN = -2147483648;
this.size = 0; // Current number of elements in min heap
this.capacity = capacity; // maximum possible size of min heap
this.a = []; // new Array(capacity); // pointer to array of elements in heap
// console.log("size, capacity, a - ", this.size, this.capacity, this.a);
}
parent(i) {
return Math.trunc((i - 1) / 2);
}
leftChild(i) {
return 2 * i + 1;
}
rightChild(i) {
return 2 * i + 2;
}
// Inserts a new key 'k'
insertKey(k) {
if (this.size == this.capacity) {
console.log("\nOverflow: Could not insertKey\n");
return;
}
// First insert the new key at the end
this.size++;
let i = this.size - 1;
this.a[i] = k;
// Fix the min heap property if it is violated
while (i != 0 && this.a[this.parent(i)] > this.a[i]) {
[this.a[i], this.a[this.parent(i)]] = [
this.a[this.parent(i)],
this.a[i],
];
i = this.parent(i);
}
return null;
}
// Decreases key value of key at index i to new_val
// Decreases value of key at index 'i' to new_val. It is assumed that
// new_val is smaller than this.a[i].
decreaseKey(i, new_val) {
this.a[i] = new_val;
while (i != 0 && this.a[this.parent(i)] > this.a[i]) {
[this.a[i], this.a[this.parent(i)]] = [
this.a[this.parent(i)],
this.a[i],
];
i = this.parent(i);
}
return null;
}
// to extract the root which is the minimum element
// Method to remove minimum element (or root) from min heap
extractMin() {
if (this.size <= 0) return INT_MAX;
if (this.size == 1) {
this.size--;
return this.a[0];
}
// Store the minimum value, and remove it from heap
let root = this.a[0];
this.a[0] = this.a[this.size - 1];
this.size--;
this.MinHeapify(0);
return root;
}
// Returns the minimum key (key at root) from min heap
getMin() {
// console.log(this.a[0], ", arr =", this.a);
return this.a[0];
}
// Deletes a key stored at index i
// This function deletes key at index i. It first reduced value to minus
// infinite, then calls extractMin()
deleteKeyAt(i) {
this.decreaseKey(i, this.INT_MIN);
this.extractMin();
return null;
}
// // to heapify a subtree with the root at given index
// A recursive method to heapify a subtree with the root at given index
// This method assumes that the subtrees are already heapified
MinHeapify(i) {
let l = this.leftChild(i);
let r = this.rightChild(i);
let smallest = i;
if (l < this.size && this.a[l] < this.a[i]) smallest = l;
if (r < this.size && this.a[r] < this.a[smallest]) smallest = r;
if (smallest != i) {
[this.a[i], this.a[smallest]] = [this.a[smallest], this.a[i]];
this.MinHeapify(smallest);
}
}
test() {
// new MinHeap().test();
let o1 = new MinHeap(11);
o1.insertKey(3);
console.log("insertKey 3, ", o1.a, ", ", o1.getMin());
o1.insertKey(2);
console.log("insertKey 2, ", o1.a, ", ", o1.getMin());
o1.deleteKeyAt(1);
console.log("deleteKeyAt 1 , ", o1.a, ", ", o1.getMin());
o1.insertKey(15);
console.log("insertKey 15, ", o1.a, ", ", o1.getMin());
o1.insertKey(5);
console.log("insertKey 5, ", o1.a, ", ", o1.getMin());
o1.insertKey(4);
console.log("insertKey 4, ", o1.a, ", ", o1.getMin());
o1.insertKey(45);
console.log("insertKey 45, ", o1.a, ", ", o1.getMin());
console.log("extractMin ", o1.extractMin(), "", o1.a, ",", o1.getMin());
o1.decreaseKey(2, 1);
console.log("decreaseKey 2,1 , ", o1.a, ", ", o1.getMin());
/*
// new MinHeap().test();
// operation parameters, arr, getMin
insertKey 3, [ 3 ] , 3
insertKey 2, [ 2, 3 ] , 2
deleteKeyAt 1 , [ 2, 2 ] , 2
insertKey 15, [ 2, 15 ] , 2
insertKey 5, [ 2, 15, 5 ] , 2
insertKey 4, [ 2, 4, 5, 15 ] , 2
insertKey 45, [ 2, 4, 5, 15, 45 ] , 2
extractMin 2 [ 4, 15, 5, 45, 45 ] , 4
decreaseKey 2,1 , [ 1, 15, 4, 45, 45 ] , 1
*/
}
}
// new MinHeap().test();