data-structure-typed 2.4.5 → 2.5.0

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.
Files changed (71) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +15 -5
  3. package/dist/cjs/index.cjs +10240 -2079
  4. package/dist/cjs-legacy/index.cjs +10305 -2135
  5. package/dist/esm/index.mjs +10241 -2078
  6. package/dist/esm-legacy/index.mjs +10306 -2134
  7. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  12. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
  13. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  14. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  16. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  17. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
  19. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  20. package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
  21. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  22. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  23. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  24. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  28. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  29. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  30. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  31. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  32. package/dist/types/data-structures/queue/deque.d.ts +272 -65
  33. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  34. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  35. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  36. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  37. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  38. package/dist/umd/data-structure-typed.js +10308 -2133
  39. package/dist/umd/data-structure-typed.min.js +4 -4
  40. package/package.json +5 -4
  41. package/src/data-structures/base/iterable-element-base.ts +4 -5
  42. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
  44. package/src/data-structures/binary-tree/binary-tree.ts +454 -79
  45. package/src/data-structures/binary-tree/bst.ts +359 -34
  46. package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
  47. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  48. package/src/data-structures/binary-tree/tree-map.ts +1403 -6
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
  51. package/src/data-structures/binary-tree/tree-set.ts +1250 -9
  52. package/src/data-structures/graph/directed-graph.ts +229 -47
  53. package/src/data-structures/graph/map-graph.ts +59 -1
  54. package/src/data-structures/graph/undirected-graph.ts +213 -59
  55. package/src/data-structures/hash/hash-map.ts +241 -77
  56. package/src/data-structures/heap/heap.ts +301 -99
  57. package/src/data-structures/heap/max-heap.ts +46 -0
  58. package/src/data-structures/heap/min-heap.ts +59 -0
  59. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  60. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  61. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  62. package/src/data-structures/matrix/matrix.ts +424 -12
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  65. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  66. package/src/data-structures/queue/deque.ts +287 -65
  67. package/src/data-structures/queue/queue.ts +223 -42
  68. package/src/data-structures/stack/stack.ts +184 -32
  69. package/src/data-structures/trie/trie.ts +225 -43
  70. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  71. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
@@ -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,27 @@ 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
+ * @example
201
+ * // Track heap capacity
202
+ * const heap = new Heap<number>();
203
+ * console.log(heap.size); // 0;
204
+ * heap.add(10);
205
+ * heap.add(20);
206
+ * console.log(heap.size); // 2;
207
+ * heap.poll();
208
+ * console.log(heap.size); // 1;
281
209
  */
282
210
 
283
211
  get size(): number {
@@ -332,6 +260,33 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
332
260
  * @remarks Time O(1) amortized, Space O(1)
333
261
  * @param element - Element to insert.
334
262
  * @returns True.
263
+
264
+
265
+
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+
275
+ * @example
276
+ * // basic Heap creation and add operation
277
+ * // Create a min heap (default)
278
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
279
+ *
280
+ * // Verify size
281
+ * console.log(minHeap.size); // 6;
282
+ *
283
+ * // Add new element
284
+ * minHeap.add(4);
285
+ * console.log(minHeap.size); // 7;
286
+ *
287
+ * // Min heap property: smallest element at root
288
+ * const min = minHeap.peek();
289
+ * console.log(min); // 1;
335
290
  */
336
291
 
337
292
  add(element: E): boolean {
@@ -344,6 +299,21 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
344
299
  * @remarks Time O(N log N), Space O(1)
345
300
  * @param elements - Iterable of elements or raw values.
346
301
  * @returns Array of per-element success flags.
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ * @example
312
+ * // Add multiple elements
313
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
314
+ * heap.addMany([5, 3, 7, 1]);
315
+ * console.log(heap.peek()); // 1;
316
+ * console.log(heap.size); // 4;
347
317
  */
348
318
 
349
319
  addMany(elements: Iterable<E | R>): boolean[] {
@@ -364,6 +334,43 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
364
334
  * Remove and return the top element.
365
335
  * @remarks Time O(log N), Space O(1)
366
336
  * @returns Top element or undefined.
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+ * @example
350
+ * // Heap with custom comparator (MaxHeap behavior)
351
+ * interface Task {
352
+ * id: number;
353
+ * priority: number;
354
+ * name: string;
355
+ * }
356
+ *
357
+ * // Custom comparator for max heap behavior (higher priority first)
358
+ * const tasks: Task[] = [
359
+ * { id: 1, priority: 5, name: 'Email' },
360
+ * { id: 2, priority: 3, name: 'Chat' },
361
+ * { id: 3, priority: 8, name: 'Alert' }
362
+ * ];
363
+ *
364
+ * const maxHeap = new Heap(tasks, {
365
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
366
+ * });
367
+ *
368
+ * console.log(maxHeap.size); // 3;
369
+ *
370
+ * // Peek returns highest priority task
371
+ * const topTask = maxHeap.peek();
372
+ * console.log(topTask?.priority); // 8;
373
+ * console.log(topTask?.name); // 'Alert';
367
374
  */
368
375
 
369
376
  poll(): E | undefined {
@@ -381,6 +388,77 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
381
388
  * Get the current top element without removing it.
382
389
  * @remarks Time O(1), Space O(1)
383
390
  * @returns Top element or undefined.
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+ * @example
404
+ * // Heap for event processing with priority
405
+ * interface Event {
406
+ * id: number;
407
+ * type: 'critical' | 'warning' | 'info';
408
+ * timestamp: number;
409
+ * message: string;
410
+ * }
411
+ *
412
+ * // Custom priority: critical > warning > info
413
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
414
+ *
415
+ * const eventHeap = new Heap<Event>([], {
416
+ * comparator: (a: Event, b: Event) => {
417
+ * const priorityA = priorityMap[a.type];
418
+ * const priorityB = priorityMap[b.type];
419
+ * return priorityB - priorityA; // Higher priority first
420
+ * }
421
+ * });
422
+ *
423
+ * // Add events in random order
424
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
425
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
426
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
427
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
428
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
429
+ *
430
+ * console.log(eventHeap.size); // 5;
431
+ *
432
+ * // Process events by priority (critical first)
433
+ * const processedOrder: Event[] = [];
434
+ * while (eventHeap.size > 0) {
435
+ * const event = eventHeap.poll();
436
+ * if (event) {
437
+ * processedOrder.push(event);
438
+ * }
439
+ * }
440
+ *
441
+ * // Verify critical events came first
442
+ * console.log(processedOrder[0].type); // 'critical';
443
+ * console.log(processedOrder[1].type); // 'critical';
444
+ * console.log(processedOrder[2].type); // 'warning';
445
+ * console.log(processedOrder[3].type); // 'info';
446
+ * console.log(processedOrder[4].type); // 'info';
447
+ *
448
+ * // Verify O(log n) operations
449
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
450
+ *
451
+ * // Add - O(log n)
452
+ * newHeap.add(2);
453
+ * console.log(newHeap.size); // 5;
454
+ *
455
+ * // Poll - O(log n)
456
+ * const removed = newHeap.poll();
457
+ * console.log(removed); // 1;
458
+ *
459
+ * // Peek - O(1)
460
+ * const top = newHeap.peek();
461
+ * console.log(top); // 2;
384
462
  */
385
463
 
386
464
  peek(): E | undefined {
@@ -391,6 +469,22 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
391
469
  * Check whether the heap is empty.
392
470
  * @remarks Time O(1), Space O(1)
393
471
  * @returns True if size is 0.
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+ * @example
483
+ * // Check if heap is empty
484
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
485
+ * console.log(heap.isEmpty()); // true;
486
+ * heap.add(1);
487
+ * console.log(heap.isEmpty()); // false;
394
488
  */
395
489
 
396
490
  isEmpty(): boolean {
@@ -401,6 +495,21 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
401
495
  * Remove all elements.
402
496
  * @remarks Time O(1), Space O(1)
403
497
  * @returns void
498
+
499
+
500
+
501
+
502
+
503
+
504
+
505
+
506
+
507
+
508
+ * @example
509
+ * // Remove all elements
510
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
511
+ * heap.clear();
512
+ * console.log(heap.isEmpty()); // true;
404
513
  */
405
514
 
406
515
  clear(): void {
@@ -424,6 +533,14 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
424
533
  * @remarks Time O(N), Space O(1)
425
534
  * @param element - Element to search for.
426
535
  * @returns True if found.
536
+
537
+
538
+
539
+ * @example
540
+ * // Check element existence
541
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
542
+ * console.log(heap.has(1)); // true;
543
+ * console.log(heap.has(99)); // false;
427
544
  */
428
545
 
429
546
  override has(element: E): boolean {
@@ -436,6 +553,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
436
553
  * @remarks Time O(N), Space O(1)
437
554
  * @param element - Element to delete.
438
555
  * @returns True if an element was removed.
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+ * @example
566
+ * // Remove specific element
567
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
568
+ * heap.delete(4);
569
+ * console.log(heap.toArray().includes(4)); // false;
439
570
  */
440
571
 
441
572
  delete(element: E): boolean {
@@ -504,6 +635,14 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
504
635
  * @remarks Time O(N), Space O(H)
505
636
  * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
506
637
  * @returns Array of visited elements.
638
+
639
+
640
+
641
+ * @example
642
+ * // Depth-first traversal
643
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
644
+ * const result = heap.dfs('IN');
645
+ * console.log(result.length); // 3;
507
646
  */
508
647
 
509
648
  dfs(order: DFSOrderPattern = 'PRE'): E[] {
@@ -549,6 +688,23 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
549
688
  * Return all elements in ascending order by repeatedly polling.
550
689
  * @remarks Time O(N log N), Space O(N)
551
690
  * @returns Sorted array of elements.
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+ * @example
704
+ * // Sort elements using heap
705
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
706
+ * const sorted = heap.sort();
707
+ * console.log(sorted); // [1, 2, 3, 4, 5];
552
708
  */
553
709
 
554
710
  sort(): E[] {
@@ -566,6 +722,23 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
566
722
  * Deep clone this heap.
567
723
  * @remarks Time O(N), Space O(N)
568
724
  * @returns A new heap with the same elements.
725
+
726
+
727
+
728
+
729
+
730
+
731
+
732
+
733
+
734
+
735
+ * @example
736
+ * // Create independent copy
737
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
738
+ * const copy = heap.clone();
739
+ * copy.poll();
740
+ * console.log(heap.size); // 3;
741
+ * console.log(copy.size); // 2;
569
742
  */
570
743
 
571
744
  clone(): this {
@@ -580,6 +753,21 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
580
753
  * @param callback - Predicate (element, index, heap) → boolean to keep element.
581
754
  * @param [thisArg] - Value for `this` inside the callback.
582
755
  * @returns A new heap with the kept elements.
756
+
757
+
758
+
759
+
760
+
761
+
762
+
763
+
764
+
765
+
766
+ * @example
767
+ * // Filter elements
768
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
769
+ * const evens = heap.filter(x => x % 2 === 0);
770
+ * console.log(evens.size); // 2;
583
771
  */
584
772
 
585
773
  filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
@@ -604,6 +792,20 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
604
792
  * @param options - Options for the output heap, including comparator for EM.
605
793
  * @param [thisArg] - Value for `this` inside the callback.
606
794
  * @returns A new heap with mapped elements.
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+ * @example
805
+ * // Transform elements
806
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
807
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
808
+ * console.log(doubled.peek()); // 2;
607
809
  */
608
810
 
609
811
  map<EM, RM>(
@@ -23,6 +23,52 @@ import { ERR } from '../../common';
23
23
  * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
24
24
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum-spanning tree algorithm, which use heaps to improve performance.
25
25
  * @example
26
+ * // Find the K largest elements
27
+ * const data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
28
+ * const heap = new MaxHeap(data);
29
+ *
30
+ * // Extract top 3 elements
31
+ * const top3 = [];
32
+ * for (let i = 0; i < 3; i++) {
33
+ * top3.push(heap.poll());
34
+ * }
35
+ * console.log(top3); // [9, 6, 5];
36
+ * @example
37
+ * // Priority-based task processing
38
+ * interface Task {
39
+ * name: string;
40
+ * priority: number;
41
+ * }
42
+ *
43
+ * const heap = new MaxHeap<Task>([], {
44
+ * comparator: (a, b) => b.priority - a.priority
45
+ * });
46
+ *
47
+ * heap.add({ name: 'Low priority', priority: 1 });
48
+ * heap.add({ name: 'Critical fix', priority: 10 });
49
+ * heap.add({ name: 'Medium task', priority: 5 });
50
+ *
51
+ * // Highest priority first
52
+ * console.log(heap.poll()?.name); // 'Critical fix';
53
+ * console.log(heap.poll()?.name); // 'Medium task';
54
+ * console.log(heap.poll()?.name); // 'Low priority';
55
+ * @example
56
+ * // Real-time top score tracking
57
+ * const scores = new MaxHeap<number>();
58
+ *
59
+ * // Stream of scores coming in
60
+ * for (const score of [72, 85, 91, 68, 95, 78, 88]) {
61
+ * scores.add(score);
62
+ * }
63
+ *
64
+ * // Current highest score without removing
65
+ * console.log(scores.peek()); // 95;
66
+ * console.log(scores.size); // 7;
67
+ *
68
+ * // Remove top 2 scores
69
+ * console.log(scores.poll()); // 95;
70
+ * console.log(scores.poll()); // 91;
71
+ * console.log(scores.peek()); // 88;
26
72
  */
27
73
  export class MaxHeap<E = any, R = any> extends Heap<E, R> {
28
74
  /**
@@ -23,6 +23,65 @@ import { Heap } from './heap';
23
23
  * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
24
24
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
25
25
  * @example
26
+ * // Merge K sorted arrays
27
+ * const arrays = [
28
+ * [1, 4, 7],
29
+ * [2, 5, 8],
30
+ * [3, 6, 9]
31
+ * ];
32
+ *
33
+ * // Use min heap to merge: track (value, arrayIndex, elementIndex)
34
+ * const heap = new MinHeap<[number, number, number]>([], {
35
+ * comparator: (a, b) => a[0] - b[0]
36
+ * });
37
+ *
38
+ * // Initialize with first element of each array
39
+ * arrays.forEach((arr, i) => heap.add([arr[0], i, 0]));
40
+ *
41
+ * const merged: number[] = [];
42
+ * while (heap.size > 0) {
43
+ * const [val, arrIdx, elemIdx] = heap.poll()!;
44
+ * merged.push(val);
45
+ * if (elemIdx + 1 < arrays[arrIdx].length) {
46
+ * heap.add([arrays[arrIdx][elemIdx + 1], arrIdx, elemIdx + 1]);
47
+ * }
48
+ * }
49
+ *
50
+ * console.log(merged); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
51
+ * @example
52
+ * // Dijkstra-style shortest distance tracking
53
+ * // Simulating distance updates: (distance, nodeId)
54
+ * const heap = new MinHeap<[number, string]>([], {
55
+ * comparator: (a, b) => a[0] - b[0]
56
+ * });
57
+ *
58
+ * heap.add([0, 'start']);
59
+ * heap.add([10, 'A']);
60
+ * heap.add([5, 'B']);
61
+ * heap.add([3, 'C']);
62
+ *
63
+ * // Process nearest node first
64
+ * console.log(heap.poll()); // [0, 'start'];
65
+ * console.log(heap.poll()); // [3, 'C'];
66
+ * console.log(heap.poll()); // [5, 'B'];
67
+ * console.log(heap.poll()); // [10, 'A'];
68
+ * @example
69
+ * // Running median with min heap (upper half)
70
+ * const upperHalf = new MinHeap<number>();
71
+ *
72
+ * // Add larger numbers to min heap
73
+ * for (const n of [5, 8, 3, 9, 1]) {
74
+ * upperHalf.add(n);
75
+ * }
76
+ *
77
+ * // Smallest of the upper half is always accessible
78
+ * console.log(upperHalf.peek()); // 1;
79
+ * console.log(upperHalf.size); // 5;
80
+ *
81
+ * // Remove smallest repeatedly
82
+ * console.log(upperHalf.poll()); // 1;
83
+ * console.log(upperHalf.poll()); // 3;
84
+ * console.log(upperHalf.peek()); // 5;
26
85
  */
27
86
  export class MinHeap<E = any, R = any> extends Heap<E, R> {
28
87
  /**