-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaps.js
266 lines (232 loc) · 7.06 KB
/
heaps.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// refresh heaps here https://www.youtube.com/watch?v=WCm3TqScBM8
// minheap for numbers
class MinHeap {
heap = [];
insert(number) {
if (this.heap.length === 0) {
this.heap[0] = number;
return;
}
const indexForElement = this.heap.length;
// put the element at the last empty space
this.heap[indexForElement] = number;
// compare the element with its parent and move up if required
this.compareAndMoveUp(indexForElement);
}
compareAndMoveUp(index) {
const number = this.heap[index];
const parentIndex = Math.floor((index - 1) / 2);
const hasParent = parentIndex >= 0;
if (hasParent) {
const parent = this.heap[parentIndex];
const isSmallerThanParent = number < parent;
if (isSmallerThanParent) {
// swap them
this.heap[parentIndex] = number;
this.heap[index] = parent;
this.compareAndMoveUp(parentIndex);
}
}
}
deleteMin() {
const rootIndex = 0;
const rootNumber = this.heap[rootIndex];
const lastLeafNumberIndex = this.heap.length - 1;
const lastLeafNumber = this.heap[lastLeafNumberIndex];
// replace root with the last leaf
this.heap[rootIndex] = lastLeafNumber;
this.heap[lastLeafNumberIndex] = rootNumber;
// delete last leaf now
this.heap.length = this.heap.length - 1;
// now we need to do heapify
this.compareAndMoveDown(rootIndex);
return rootNumber;
}
compareAndMoveDown(index) {
const parent = this.heap[index];
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
const lChild = this.heap[leftChildIndex];
const rChild = this.heap[rightChildIndex];
const lChildIsSmaller = lChild < parent;
if (lChildIsSmaller) {
// swap them
this.heap[leftChildIndex] = parent;
this.heap[index] = lChild;
this.compareAndMoveDown(leftChildIndex);
return;
}
const rChildIsSmaller = rChild < parent;
if (rChildIsSmaller) {
// swap them
this.heap[rightChildIndex] = parent;
this.heap[index] = rChild;
this.compareAndMoveDown(rightChildIndex);
return;
}
}
minimum() {
return this.heap[0];
}
log() {
console.log(JSON.stringify(this.heap));
}
}
const minheap = new MinHeap();
minheap.insert(3);
minheap.insert(5);
minheap.insert(2);
minheap.insert(1);
minheap.insert(-1);
minheap.insert(0);
minheap.insert(10);
minheap.insert(12);
minheap.insert(-12);
minheap.log();
// min heap for objects
class MinHeapForObjects {
heap = [];
constructor(hash, accessor) {
if (!hash) {
throw new Error('Please provide a hash map of id and its value;');
}
this.hash = hash;
this.accessor = accessor;
}
getValue(id) {
if (this.accessor) {
return this.hash[id][this.accessor];
}
return this.hash[id];
}
insert({ id }) {
if (this.heap.length === 0) {
this.heap[0] = id;
return;
}
const indexForElement = this.heap.length;
// put the element at the last empty space
this.heap[indexForElement] = id;
// compare the element with its parent and move up if required
this.compareAndMoveUp(indexForElement);
}
compareAndMoveUp(index) {
const numberId = this.heap[index];
const number = this.getValue(numberId);
const parentIndex = Math.floor((index - 1) / 2);
const hasParent = parentIndex >= 0;
if (hasParent) {
const parentId = this.heap[parentIndex];
const parent = this.getValue(this.heap[parentIndex]);
const isSmallerThanParent = number < parent;
if (isSmallerThanParent) {
// swap them
this.heap[parentIndex] = numberId;
this.heap[index] = parentId;
this.compareAndMoveUp(parentIndex);
}
}
}
modify(id, value) {
this.hash[id] = value;
this.modified(id);
}
modified(id) {
this.delete(id);
this.insert({ id });
}
delete(id) {
const index = this.heap.findIndex((e) => e === id);
const lastLeafNumberIndex = this.heap.length - 1;
const lastLeafNumberId = this.heap[lastLeafNumberIndex];
const lastLeafNumber = this.getValue(lastLeafNumberId);
if (!index) {
return;
}
// replace this node with the last leaf
this.heap[index] = lastLeafNumberId;
this.heap[lastLeafNumberIndex] = id;
// delete last leaf now
this.heap.length = this.heap.length - 1;
// now we need to do heapify
this.compareAndMoveDown(index);
}
deleteMin() {
const rootIndex = 0;
const rootNumberId = this.heap[rootIndex];
const rootNumber = this.getValue(rootNumberId);
const lastLeafNumberIndex = this.heap.length - 1;
const lastLeafNumberId = this.heap[lastLeafNumberIndex];
const lastLeafNumber = this.getValue(lastLeafNumberId);
// replace root with the last leaf
this.heap[rootIndex] = lastLeafNumberId;
this.heap[lastLeafNumberIndex] = rootNumberId;
// delete last leaf now
this.heap.length = this.heap.length - 1;
// now we need to do heapify
this.compareAndMoveDown(rootIndex);
return rootNumber;
}
compareAndMoveDown(index) {
const parentId = this.heap[index];
const parent = this.getValue(parentId);
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
const lChildId = this.heap[leftChildIndex];
const lChild = this.getValue(lChildId);
const rChildId = this.heap[rightChildIndex];
const rChild = this.getValue(rChildId);
const lChildIsSmaller = lChild < parent;
if (lChildIsSmaller) {
// swap them
this.heap[leftChildIndex] = parentId;
this.heap[index] = lChildId;
this.compareAndMoveDown(leftChildIndex);
return;
}
const rChildIsSmaller = rChild < parent;
if (rChildIsSmaller) {
// swap them
this.heap[rightChildIndex] = parentId;
this.heap[index] = rChildId;
this.compareAndMoveDown(rightChildIndex);
return;
}
}
minimum() {
return this.heap[0];
}
log() {
console.log(JSON.stringify(this.heap));
}
}
const hashmap = {
qui_cupidatat_aute_fugiat_mollit_dolore_pariatur_et_culpa_magnabc: 3,
temabc: 5,
veniam_irure_do_sint_abc: -2,
proident_laboris_elit_in_commodo_fugiat_eu_eabc: 6,
non_minim_reprehenderit_quis_nabc: 23,
deserunabc: 04,
occaecat_est_excepteur_dolore_sunt_consequat_consequat_deserunt_anim_lorem_desabc: 2,
iabc: 33,
do_sunt_enim_proident_ullamco_minimabc: 5,
};
const minheapObj = new MinHeapForObjects(hashmap);
minheapObj.insert({
id: 'qui_cupidatat_aute_fugiat_mollit_dolore_pariatur_et_culpa_magnabc',
});
minheapObj.insert({ id: 'temabc' });
minheapObj.insert({ id: 'veniam_irure_do_sint_abc' });
minheapObj.insert({ id: 'proident_laboris_elit_in_commodo_fugiat_eu_eabc' });
minheapObj.insert({ id: 'non_minim_reprehenderit_quis_nabc' });
minheapObj.insert({ id: 'deserunabc' });
minheapObj.insert({
id:
'occaecat_est_excepteur_dolore_sunt_consequat_consequat_deserunt_anim_lorem_desabc',
});
minheapObj.insert({ id: 'iabc' });
minheapObj.insert({ id: 'do_sunt_enim_proident_ullamco_minimabc' });
minheapObj.log();
hashmap.temabc = -333;
minheapObj.modified('temabc');
minheapObj.log();