-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeap.ts
343 lines (290 loc) · 10.9 KB
/
Heap.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* HeapTypes
*
* the types of Heap that can be initialised - MAX and/or MIN
*/
enum HeapTypes {
MAX = 'MAX',
MIN = 'MIN',
};
/**
* ChildNodeTypes
*
* the Child Nodes that a Heap Node can have (LEFT and/or RIGHT)
*/
enum ChildNodeTypes {
LEFT,
RIGHT,
};
/**
* IHeap
*
* the interface specifying methods for Heap to implement
*/
interface IHeap<T> {
insert(element: T): void;
delete(): T;
heapifyUp(): void;
heapifyDown(): void;
}
/**
* Heap - implements the Heap data structure
*/
class Heap<T> extends Array<T> implements IHeap<T> {
type: string;
insertComparisonCounter: number = 0;
deleteComparisonCounter: number = 0;
constructor(type: string, items?: T[]) {
super(...items);
(<any>Object).setPrototypeOf(this, Object.create(Heap.prototype));
this.type = type;
}
/**
* getParentNodeIndex
*
* gets the index of the current item's parent
*
* @param currentIndex the position of the current item whose parent index is needed
* @return number
*/
getParentNodeIndex(currentIndex: number): number {
return Math.floor((currentIndex - 1) / 2);
}
/**
* getChildNodeIndex
*
* gets the index of the current item's child node as
* specified by the childNodeType argument (LEFT or RIGHT)
*
* @param currentIndex the position of the current item whose child node index is needed
* @param childNodeType the type of the current item's child node that is needed - LEFT or RIGHT
* @return number
*/
getChildNodeIndex(currentIndex: number, childNodeType: ChildNodeTypes): number {
return childNodeType === ChildNodeTypes.LEFT
? (currentIndex * 2) + 1
: (currentIndex * 2) + 2;
}
/**
* isHeapTypeConditionSatisfiedForHeapifyUp
*
* determines whether the Heap's (MIN or MAX) condition is satisified
*
* @param currentIndex the index of the current item
* @return boolean
*/
isHeapTypeConditionSatisfiedForHeapifyUp(currentIndex: number): boolean {
const parentNodeIndex = this.getParentNodeIndex(currentIndex);
return this.type === HeapTypes.MAX
? this[parentNodeIndex] >= this[currentIndex]
: this[parentNodeIndex] <= this[currentIndex]
}
/**
* isHeapTypeConditionSatisfiedForHeapifyUp
*
* determines whether the Heap's (MIN or MAX) condition is satisified
*
* @param currentIndex the index of the current item
* @return boolean
*/
isHeapTypeConditionSatisfiedForHeapifyDown(currentIndex: number): boolean {
const childNodeIndex = this.getChildNodeIndexThatSatisfiesHeapCondition(currentIndex);
return this.type === HeapTypes.MAX
? this[childNodeIndex] <= this[currentIndex]
: this[childNodeIndex] >= this[currentIndex]
}
swapItems(firstIndex: number, secondIndex: number) {
const temp: T = this[firstIndex];
this[firstIndex] = this[secondIndex];
this[secondIndex] = temp;
}
/**
* swapCurrentItemWithParentItem
*
* swaps the current item with its parent item and returns the parent's index
*
* @param currentIndex the index of the current item
* @return number
*/
swapCurrentItemWithParentItem(currentIndex: number): number {
const parentNodeIndex = this.getParentNodeIndex(currentIndex);
this.swapItems(currentIndex, parentNodeIndex);
return parentNodeIndex;
}
/**
* getChildNodeIndexThatSatisfiesHeapCondition
* gets the node index of the child node that satisfies the Heap's condition
*
* @param currentIndex the index of the current item
*
* @return number
*/
getChildNodeIndexThatSatisfiesHeapCondition(currentIndex: number): number {
const leftChildNodeIndex = this.getChildNodeIndex(currentIndex, ChildNodeTypes.LEFT);
const rightChildNodeIndex = this.getChildNodeIndex(currentIndex, ChildNodeTypes.RIGHT);
const childNodeIndex = this.type === 'MAX'
? (
this[leftChildNodeIndex] >= this[rightChildNodeIndex]
? leftChildNodeIndex : rightChildNodeIndex
) : (
this[leftChildNodeIndex] <= this[rightChildNodeIndex]
? leftChildNodeIndex : rightChildNodeIndex
);
return childNodeIndex;
}
/**
* swapCurrentItemWithChildItem
* swaps the current item with its qualified child item and returns the child's index
*
* @param currentIndex the index of the current item
*
* @return number
*/
swapCurrentItemWithChildItem(currentIndex: number): number {
const childNodeIndex = this.getChildNodeIndexThatSatisfiesHeapCondition(currentIndex);
this.swapItems(currentIndex, childNodeIndex);
return childNodeIndex;
}
/**
* heapifyUp
*
* rearranges the Heap to satisfy its heap condition by
* taking the inserted item to its final position
*
* @return void
*/
heapifyUp(): void {
let currentIndex = this.length - 1;
while (!this.isHeapTypeConditionSatisfiedForHeapifyUp(currentIndex) && !(currentIndex <= 0)) {
currentIndex = this.swapCurrentItemWithParentItem(currentIndex);
this.insertComparisonCounter += 1;
}
}
/**
* heapifyDown
*
* rearranges the Heap to satisfy its heap condition by
* taking the root item to its final position
*
* @return void
*/
heapifyDown(): void {
let currentIndex = 0;
while (!this.isHeapTypeConditionSatisfiedForHeapifyDown(currentIndex) && !(currentIndex > length - 2)) {
currentIndex = this.swapCurrentItemWithChildItem(currentIndex);
this.deleteComparisonCounter += 1;
}
}
/**
* insert
*
* inserts a new item into the heap
*
* @param element the new element to insert into the heap
* @return void
*/
insert(element: T): void {
this[this.length] = element;
this.heapifyUp();
}
delete(): T {
const rootIndex = 0;
const leafIndex = this.length - 1;
this.swapItems(leafIndex, rootIndex);
this.heapifyDown();
return this[leafIndex];
}
}
// ==================================================================
// SAMPLE RUN OF THE DATA STRUCTURE
// ==================================================================
function runSample() {
// INSERT
console.log("\nGenerate MAX Heap and MIN Heap for the entries 50, 5, 12, 27, 43, 4, 10, 1\n");
// Max Heap
const maxHeap: Heap<number> = new Heap<number>("MAX");
maxHeap.insert(50);
// parentPosition = Math.floor((0 - 1) / 2) => null ===== [50]
maxHeap.insert(5);
// parentPosition = Math.floor((1 - 1) / 2) => 0 ===== [50, 5]
maxHeap.insert(12);
// parentPosition = Math.floor((2 - 1) / 2) => 0 ===== [50, 5, 12]
maxHeap.insert(27);
// parentPosition = Math.floor((3 - 1) / 2) => 1 - swap with 5 ===== [50, 27, 12, 5]
maxHeap.insert(43);
// parentPosition = Math.floor((4 - 1) / 2) => 1 - swap with 27 ===== [50, 43, 12, 5, 27]
maxHeap.insert(4);
// parentPosition = Math.floor((5 - 1) / 2) => 2 ===== [50, 43, 12, 5, 27, 4]
maxHeap.insert(10);
// parentPosition = Math.floor((6 - 1) / 2) => 2 ===== [50, 43, 12, 5, 27, 4, 10]
maxHeap.insert(1);
// parentPosition = Math.floor((7 - 1) / 2) => 3 ===== [50, 43, 12, 5, 27, 4, 10, 1]
console.log(`MAX HEAP: ${maxHeap}`); // [50, 43, 12, 5, 27, 4, 10, 1]
console.log('Max Heap generation for all the entries did not do more than (n * log n) comparisons where n = 8, therefore, at most 24 comparisons - O(n * log n) = O(24).');
console.log(`For all the entries, it did ${maxHeap.insertComparisonCounter} comparisons >>>>> ${maxHeap.insertComparisonCounter} <= 24 is ${maxHeap.insertComparisonCounter <= 24 } as expected.\n\n`);
//===================================================================
// Min Heap
const minHeap: Heap<number> = new Heap<number>("MIN");
minHeap.insert(50);
// parentPosition = Math.floor((0 - 1) / 2) => null
minHeap.insert(5);
// parentPosition = Math.floor((1 - 1) / 2) => 0 - swap with 50 ===== [5, 50]
minHeap.insert(12);
// parentPosition = Math.floor((2 - 1) / 2) => 0 ===== [5, 50, 12]
minHeap.insert(27);
// parentPosition = Math.floor((3 - 1) / 2) => 1 - swap with 50 ===== [5, 27, 12, 50]
minHeap.insert(43);
// parentPosition = Math.floor((4 - 1) / 2) => 1 ===== [5, 27, 12, 50, 43]
minHeap.insert(4);
// parentPosition = Math.floor((5 - 1) / 2) => 2 - swap with 12 ===== [5, 27, 4, 50, 43, 12]
// parentPosition = Math.floor((2 - 1) / 2) => 0 - swap with 5 ===== [4, 27, 5, 50, 43, 12]
minHeap.insert(10);
// parentPosition = Math.floor((6 - 1) / 2) => 2 ===== [4, 27, 5, 50, 43, 12, 10]
minHeap.insert(1);
// parentPosition = Math.floor((7 - 1) / 2) => 3 - swap with 50 ===== [4, 27, 5, 1, 43, 12, 10, 50]
// parentPosition = Math.floor((3 - 1) / 2) => 1 - swap with 27 ===== [4, 1, 5, 27, 43, 12, 10, 50]
// parentPosition = Math.floor((1 - 1) / 2) => 0 - swap with 4 ===== [1, 4, 5, 27, 43, 12, 10, 50]
console.log(`MIN HEAP: ${minHeap}`); // [1, 4, 5, 27, 43, 12, 10, 50]
console.log('Min Heap generation for all the entries did not do more than (n * log n) comparisons where n = 8, therefore, at most 24 comparisons - O(n * log n) = O(24).');
console.log(`For all the entries, it did ${minHeap.insertComparisonCounter} comparisons >>>>> ${minHeap.insertComparisonCounter} <= 24 is ${minHeap.insertComparisonCounter <= 24 } as expected\n\n`);
// ==================================================================
//DELETE
console.log("\nDelete from MAX Heap and MIN Heap generated above\n");
console.log('MAX HEAP: [50, 43, 12, 5, 27, 4, 10, 1]');
// Heap = [50, 43, 12, 5, 27, 4, 10, 1]
const deleted: number = maxHeap.delete();
// Run 1
// current index = next current index = root index = 0
// current item is always the item in the current index
// leaf item = 1
// swap leaf and current item ===== [1, 43, 12, 5, 27, 4, 10, 50]
// left child index = (0 * 2) + 1 = 1
// right child index = (0 * 2) + 2 = 2
// left child = Heap[1] = 43
// right child = Heap[2] = 12
// max child = max(left child, right child) = 43
// next current index = index of max child = 1
// swap current item and max child ===== [43, 1, 12, 5, 27, 4, 10, 50]
// Run 2
// Heap = [43, 1, 12, 5, 27, 4, 10, 50]
// current index = next current index = 1
// left child index = (1 * 2) + 1 = 3
// right child index = (1 * 2) + 2 = 4
// left child = Heap[3] = 5
// right child = Heap[4] = 27
// max child = max(left child, right child) = 27
// next current index = index of max child = 4
// swap current item and max child ===== [43, 27, 12, 5, 1, 4, 10, 50]
// Run 3
// Heap = [43, 27, 12, 5, 1, 4, 10, 50]
// current index = next current index = 4
// left child index = (4 * 2) + 1 = 9
// right child index = (4 * 2) + 2 = 10
// left child = Heap[9] = undefined
// right child = Heap[10] = undefined
// max child = max(left child, right child) = undefined
// next current index = index of max child = undefined
// since no next current index item, stop and return [43, 27, 12, 5, 1, 4, 10, 50]
console.log(`DELETED FROM MAX HEAP: ${deleted}`); // 50
console.log(`MAX HEAP AFTER DELETE: ${maxHeap}\n`); // [43, 27, 12, 5, 1, 4, 10, 50]
}