binary-tree-typed 2.4.5 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -84
- package/dist/cjs/index.cjs +1476 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1473 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1476 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1473 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/binary-tree-typed.js +1469 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -26,105 +26,6 @@ import { ERR } from '../../common';
|
|
|
26
26
|
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
|
27
27
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prime's minimum-spanning tree algorithm, which use heaps to improve performance.
|
|
28
28
|
* @example
|
|
29
|
-
* // basic Heap creation and add operation
|
|
30
|
-
* // Create a min heap (default)
|
|
31
|
-
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
32
|
-
*
|
|
33
|
-
* // Verify size
|
|
34
|
-
* console.log(minHeap.size); // 6;
|
|
35
|
-
*
|
|
36
|
-
* // Add new element
|
|
37
|
-
* minHeap.add(4);
|
|
38
|
-
* console.log(minHeap.size); // 7;
|
|
39
|
-
*
|
|
40
|
-
* // Min heap property: smallest element at root
|
|
41
|
-
* const min = minHeap.peek();
|
|
42
|
-
* console.log(min); // 1;
|
|
43
|
-
* @example
|
|
44
|
-
* // Heap with custom comparator (MaxHeap behavior)
|
|
45
|
-
* interface Task {
|
|
46
|
-
* id: number;
|
|
47
|
-
* priority: number;
|
|
48
|
-
* name: string;
|
|
49
|
-
* }
|
|
50
|
-
*
|
|
51
|
-
* // Custom comparator for max heap behavior (higher priority first)
|
|
52
|
-
* const tasks: Task[] = [
|
|
53
|
-
* { id: 1, priority: 5, name: 'Email' },
|
|
54
|
-
* { id: 2, priority: 3, name: 'Chat' },
|
|
55
|
-
* { id: 3, priority: 8, name: 'Alert' }
|
|
56
|
-
* ];
|
|
57
|
-
*
|
|
58
|
-
* const maxHeap = new Heap(tasks, {
|
|
59
|
-
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
60
|
-
* });
|
|
61
|
-
*
|
|
62
|
-
* console.log(maxHeap.size); // 3;
|
|
63
|
-
*
|
|
64
|
-
* // Peek returns highest priority task
|
|
65
|
-
* const topTask = maxHeap.peek();
|
|
66
|
-
* console.log(topTask?.priority); // 8;
|
|
67
|
-
* console.log(topTask?.name); // 'Alert';
|
|
68
|
-
* @example
|
|
69
|
-
* // Heap for event processing with priority
|
|
70
|
-
* interface Event {
|
|
71
|
-
* id: number;
|
|
72
|
-
* type: 'critical' | 'warning' | 'info';
|
|
73
|
-
* timestamp: number;
|
|
74
|
-
* message: string;
|
|
75
|
-
* }
|
|
76
|
-
*
|
|
77
|
-
* // Custom priority: critical > warning > info
|
|
78
|
-
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
79
|
-
*
|
|
80
|
-
* const eventHeap = new Heap<Event>([], {
|
|
81
|
-
* comparator: (a: Event, b: Event) => {
|
|
82
|
-
* const priorityA = priorityMap[a.type];
|
|
83
|
-
* const priorityB = priorityMap[b.type];
|
|
84
|
-
* return priorityB - priorityA; // Higher priority first
|
|
85
|
-
* }
|
|
86
|
-
* });
|
|
87
|
-
*
|
|
88
|
-
* // Add events in random order
|
|
89
|
-
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
90
|
-
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
91
|
-
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
92
|
-
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
93
|
-
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
94
|
-
*
|
|
95
|
-
* console.log(eventHeap.size); // 5;
|
|
96
|
-
*
|
|
97
|
-
* // Process events by priority (critical first)
|
|
98
|
-
* const processedOrder: Event[] = [];
|
|
99
|
-
* while (eventHeap.size > 0) {
|
|
100
|
-
* const event = eventHeap.poll();
|
|
101
|
-
* if (event) {
|
|
102
|
-
* processedOrder.push(event);
|
|
103
|
-
* }
|
|
104
|
-
* }
|
|
105
|
-
*
|
|
106
|
-
* // Verify critical events came first
|
|
107
|
-
* console.log(processedOrder[0].type); // 'critical';
|
|
108
|
-
* console.log(processedOrder[1].type); // 'critical';
|
|
109
|
-
* console.log(processedOrder[2].type); // 'warning';
|
|
110
|
-
* console.log(processedOrder[3].type); // 'info';
|
|
111
|
-
* console.log(processedOrder[4].type); // 'info';
|
|
112
|
-
*
|
|
113
|
-
* // Verify O(log n) operations
|
|
114
|
-
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
115
|
-
*
|
|
116
|
-
* // Add - O(log n)
|
|
117
|
-
* newHeap.add(2);
|
|
118
|
-
* console.log(newHeap.size); // 5;
|
|
119
|
-
*
|
|
120
|
-
* // Poll - O(log n)
|
|
121
|
-
* const removed = newHeap.poll();
|
|
122
|
-
* console.log(removed); // 1;
|
|
123
|
-
*
|
|
124
|
-
* // Peek - O(1)
|
|
125
|
-
* const top = newHeap.peek();
|
|
126
|
-
* console.log(top); // 2;
|
|
127
|
-
* @example
|
|
128
29
|
* // Use Heap to solve top k problems
|
|
129
30
|
* function topKElements(arr: number[], k: number): number[] {
|
|
130
31
|
* const heap = new Heap<number>([], { comparator: (a, b) => b - a }); // Max heap
|
|
@@ -239,6 +140,12 @@ import { ERR } from '../../common';
|
|
|
239
140
|
* ['Task5', 4]
|
|
240
141
|
* ]);
|
|
241
142
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap;
|
|
143
|
+
* @example
|
|
144
|
+
* // Get all elements as array
|
|
145
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
146
|
+
* const arr = heap.toArray();
|
|
147
|
+
* console.log(arr.length); // 5;
|
|
148
|
+
* console.log(arr.sort()); // [1, 2, 3, 4, 5];
|
|
242
149
|
*/
|
|
243
150
|
export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
244
151
|
protected _equals: (a: E, b: E) => boolean = Object.is;
|
|
@@ -278,6 +185,47 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
278
185
|
* Get the number of elements.
|
|
279
186
|
* @remarks Time O(1), Space O(1)
|
|
280
187
|
* @returns Heap size.
|
|
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
|
+
* @example
|
|
221
|
+
* // Track heap capacity
|
|
222
|
+
* const heap = new Heap<number>();
|
|
223
|
+
* console.log(heap.size); // 0;
|
|
224
|
+
* heap.add(10);
|
|
225
|
+
* heap.add(20);
|
|
226
|
+
* console.log(heap.size); // 2;
|
|
227
|
+
* heap.poll();
|
|
228
|
+
* console.log(heap.size); // 1;
|
|
281
229
|
*/
|
|
282
230
|
|
|
283
231
|
get size(): number {
|
|
@@ -332,6 +280,53 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
332
280
|
* @remarks Time O(1) amortized, Space O(1)
|
|
333
281
|
* @param element - Element to insert.
|
|
334
282
|
* @returns True.
|
|
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
|
+
* @example
|
|
316
|
+
* // basic Heap creation and add operation
|
|
317
|
+
* // Create a min heap (default)
|
|
318
|
+
* const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
|
|
319
|
+
*
|
|
320
|
+
* // Verify size
|
|
321
|
+
* console.log(minHeap.size); // 6;
|
|
322
|
+
*
|
|
323
|
+
* // Add new element
|
|
324
|
+
* minHeap.add(4);
|
|
325
|
+
* console.log(minHeap.size); // 7;
|
|
326
|
+
*
|
|
327
|
+
* // Min heap property: smallest element at root
|
|
328
|
+
* const min = minHeap.peek();
|
|
329
|
+
* console.log(min); // 1;
|
|
335
330
|
*/
|
|
336
331
|
|
|
337
332
|
add(element: E): boolean {
|
|
@@ -344,6 +339,41 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
344
339
|
* @remarks Time O(N log N), Space O(1)
|
|
345
340
|
* @param elements - Iterable of elements or raw values.
|
|
346
341
|
* @returns Array of per-element success flags.
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
* @example
|
|
372
|
+
* // Add multiple elements
|
|
373
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
374
|
+
* heap.addMany([5, 3, 7, 1]);
|
|
375
|
+
* console.log(heap.peek()); // 1;
|
|
376
|
+
* console.log(heap.size); // 4;
|
|
347
377
|
*/
|
|
348
378
|
|
|
349
379
|
addMany(elements: Iterable<E | R>): boolean[] {
|
|
@@ -364,6 +394,63 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
364
394
|
* Remove and return the top element.
|
|
365
395
|
* @remarks Time O(log N), Space O(1)
|
|
366
396
|
* @returns Top element or undefined.
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
* @example
|
|
430
|
+
* // Heap with custom comparator (MaxHeap behavior)
|
|
431
|
+
* interface Task {
|
|
432
|
+
* id: number;
|
|
433
|
+
* priority: number;
|
|
434
|
+
* name: string;
|
|
435
|
+
* }
|
|
436
|
+
*
|
|
437
|
+
* // Custom comparator for max heap behavior (higher priority first)
|
|
438
|
+
* const tasks: Task[] = [
|
|
439
|
+
* { id: 1, priority: 5, name: 'Email' },
|
|
440
|
+
* { id: 2, priority: 3, name: 'Chat' },
|
|
441
|
+
* { id: 3, priority: 8, name: 'Alert' }
|
|
442
|
+
* ];
|
|
443
|
+
*
|
|
444
|
+
* const maxHeap = new Heap(tasks, {
|
|
445
|
+
* comparator: (a: Task, b: Task) => b.priority - a.priority
|
|
446
|
+
* });
|
|
447
|
+
*
|
|
448
|
+
* console.log(maxHeap.size); // 3;
|
|
449
|
+
*
|
|
450
|
+
* // Peek returns highest priority task
|
|
451
|
+
* const topTask = maxHeap.peek();
|
|
452
|
+
* console.log(topTask?.priority); // 8;
|
|
453
|
+
* console.log(topTask?.name); // 'Alert';
|
|
367
454
|
*/
|
|
368
455
|
|
|
369
456
|
poll(): E | undefined {
|
|
@@ -381,6 +468,97 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
381
468
|
* Get the current top element without removing it.
|
|
382
469
|
* @remarks Time O(1), Space O(1)
|
|
383
470
|
* @returns Top element or undefined.
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
* @example
|
|
504
|
+
* // Heap for event processing with priority
|
|
505
|
+
* interface Event {
|
|
506
|
+
* id: number;
|
|
507
|
+
* type: 'critical' | 'warning' | 'info';
|
|
508
|
+
* timestamp: number;
|
|
509
|
+
* message: string;
|
|
510
|
+
* }
|
|
511
|
+
*
|
|
512
|
+
* // Custom priority: critical > warning > info
|
|
513
|
+
* const priorityMap = { critical: 3, warning: 2, info: 1 };
|
|
514
|
+
*
|
|
515
|
+
* const eventHeap = new Heap<Event>([], {
|
|
516
|
+
* comparator: (a: Event, b: Event) => {
|
|
517
|
+
* const priorityA = priorityMap[a.type];
|
|
518
|
+
* const priorityB = priorityMap[b.type];
|
|
519
|
+
* return priorityB - priorityA; // Higher priority first
|
|
520
|
+
* }
|
|
521
|
+
* });
|
|
522
|
+
*
|
|
523
|
+
* // Add events in random order
|
|
524
|
+
* eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
|
|
525
|
+
* eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
|
|
526
|
+
* eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
|
|
527
|
+
* eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
|
|
528
|
+
* eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
|
|
529
|
+
*
|
|
530
|
+
* console.log(eventHeap.size); // 5;
|
|
531
|
+
*
|
|
532
|
+
* // Process events by priority (critical first)
|
|
533
|
+
* const processedOrder: Event[] = [];
|
|
534
|
+
* while (eventHeap.size > 0) {
|
|
535
|
+
* const event = eventHeap.poll();
|
|
536
|
+
* if (event) {
|
|
537
|
+
* processedOrder.push(event);
|
|
538
|
+
* }
|
|
539
|
+
* }
|
|
540
|
+
*
|
|
541
|
+
* // Verify critical events came first
|
|
542
|
+
* console.log(processedOrder[0].type); // 'critical';
|
|
543
|
+
* console.log(processedOrder[1].type); // 'critical';
|
|
544
|
+
* console.log(processedOrder[2].type); // 'warning';
|
|
545
|
+
* console.log(processedOrder[3].type); // 'info';
|
|
546
|
+
* console.log(processedOrder[4].type); // 'info';
|
|
547
|
+
*
|
|
548
|
+
* // Verify O(log n) operations
|
|
549
|
+
* const newHeap = new Heap<number>([5, 3, 7, 1]);
|
|
550
|
+
*
|
|
551
|
+
* // Add - O(log n)
|
|
552
|
+
* newHeap.add(2);
|
|
553
|
+
* console.log(newHeap.size); // 5;
|
|
554
|
+
*
|
|
555
|
+
* // Poll - O(log n)
|
|
556
|
+
* const removed = newHeap.poll();
|
|
557
|
+
* console.log(removed); // 1;
|
|
558
|
+
*
|
|
559
|
+
* // Peek - O(1)
|
|
560
|
+
* const top = newHeap.peek();
|
|
561
|
+
* console.log(top); // 2;
|
|
384
562
|
*/
|
|
385
563
|
|
|
386
564
|
peek(): E | undefined {
|
|
@@ -391,6 +569,42 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
391
569
|
* Check whether the heap is empty.
|
|
392
570
|
* @remarks Time O(1), Space O(1)
|
|
393
571
|
* @returns True if size is 0.
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
* @example
|
|
603
|
+
* // Check if heap is empty
|
|
604
|
+
* const heap = new Heap<number>([], { comparator: (a, b) => a - b });
|
|
605
|
+
* console.log(heap.isEmpty()); // true;
|
|
606
|
+
* heap.add(1);
|
|
607
|
+
* console.log(heap.isEmpty()); // false;
|
|
394
608
|
*/
|
|
395
609
|
|
|
396
610
|
isEmpty(): boolean {
|
|
@@ -401,6 +615,41 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
401
615
|
* Remove all elements.
|
|
402
616
|
* @remarks Time O(1), Space O(1)
|
|
403
617
|
* @returns void
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
* @example
|
|
649
|
+
* // Remove all elements
|
|
650
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
651
|
+
* heap.clear();
|
|
652
|
+
* console.log(heap.isEmpty()); // true;
|
|
404
653
|
*/
|
|
405
654
|
|
|
406
655
|
clear(): void {
|
|
@@ -424,6 +673,34 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
424
673
|
* @remarks Time O(N), Space O(1)
|
|
425
674
|
* @param element - Element to search for.
|
|
426
675
|
* @returns True if found.
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
* @example
|
|
700
|
+
* // Check element existence
|
|
701
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
702
|
+
* console.log(heap.has(1)); // true;
|
|
703
|
+
* console.log(heap.has(99)); // false;
|
|
427
704
|
*/
|
|
428
705
|
|
|
429
706
|
override has(element: E): boolean {
|
|
@@ -436,6 +713,40 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
436
713
|
* @remarks Time O(N), Space O(1)
|
|
437
714
|
* @param element - Element to delete.
|
|
438
715
|
* @returns True if an element was removed.
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
|
|
729
|
+
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
* @example
|
|
746
|
+
* // Remove specific element
|
|
747
|
+
* const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
|
|
748
|
+
* heap.delete(4);
|
|
749
|
+
* console.log(heap.toArray().includes(4)); // false;
|
|
439
750
|
*/
|
|
440
751
|
|
|
441
752
|
delete(element: E): boolean {
|
|
@@ -504,6 +815,34 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
504
815
|
* @remarks Time O(N), Space O(H)
|
|
505
816
|
* @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
|
|
506
817
|
* @returns Array of visited elements.
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
* @example
|
|
842
|
+
* // Depth-first traversal
|
|
843
|
+
* const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
|
|
844
|
+
* const result = heap.dfs('IN');
|
|
845
|
+
* console.log(result.length); // 3;
|
|
507
846
|
*/
|
|
508
847
|
|
|
509
848
|
dfs(order: DFSOrderPattern = 'PRE'): E[] {
|
|
@@ -549,6 +888,43 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
549
888
|
* Return all elements in ascending order by repeatedly polling.
|
|
550
889
|
* @remarks Time O(N log N), Space O(N)
|
|
551
890
|
* @returns Sorted array of elements.
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
|
|
923
|
+
* @example
|
|
924
|
+
* // Sort elements using heap
|
|
925
|
+
* const heap = new Heap<number>([5, 1, 3, 2, 4]);
|
|
926
|
+
* const sorted = heap.sort();
|
|
927
|
+
* console.log(sorted); // [1, 2, 3, 4, 5];
|
|
552
928
|
*/
|
|
553
929
|
|
|
554
930
|
sort(): E[] {
|
|
@@ -566,6 +942,43 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
566
942
|
* Deep clone this heap.
|
|
567
943
|
* @remarks Time O(N), Space O(N)
|
|
568
944
|
* @returns A new heap with the same elements.
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
|
|
959
|
+
|
|
960
|
+
|
|
961
|
+
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
|
|
974
|
+
|
|
975
|
+
* @example
|
|
976
|
+
* // Create independent copy
|
|
977
|
+
* const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
|
|
978
|
+
* const copy = heap.clone();
|
|
979
|
+
* copy.poll();
|
|
980
|
+
* console.log(heap.size); // 3;
|
|
981
|
+
* console.log(copy.size); // 2;
|
|
569
982
|
*/
|
|
570
983
|
|
|
571
984
|
clone(): this {
|
|
@@ -580,6 +993,41 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
580
993
|
* @param callback - Predicate (element, index, heap) → boolean to keep element.
|
|
581
994
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
582
995
|
* @returns A new heap with the kept elements.
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
* @example
|
|
1027
|
+
* // Filter elements
|
|
1028
|
+
* const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
|
|
1029
|
+
* const evens = heap.filter(x => x % 2 === 0);
|
|
1030
|
+
* console.log(evens.size); // 2;
|
|
583
1031
|
*/
|
|
584
1032
|
|
|
585
1033
|
filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
|
|
@@ -604,6 +1052,40 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
|
604
1052
|
* @param options - Options for the output heap, including comparator for EM.
|
|
605
1053
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
606
1054
|
* @returns A new heap with mapped elements.
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
* @example
|
|
1085
|
+
* // Transform elements
|
|
1086
|
+
* const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
|
|
1087
|
+
* const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
|
|
1088
|
+
* console.log(doubled.peek()); // 2;
|
|
607
1089
|
*/
|
|
608
1090
|
|
|
609
1091
|
map<EM, RM>(
|