data-structure-typed 2.6.0 → 2.6.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.
Files changed (80) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  4. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  5. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  6. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  7. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  8. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  9. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  10. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  11. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  12. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  13. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  14. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  15. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  16. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  17. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
  18. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  19. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  20. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  21. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  22. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  23. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  24. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  25. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  26. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  27. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  28. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  29. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  30. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  31. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  32. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  33. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  34. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  35. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  36. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  37. package/package.json +45 -46
  38. package/src/common/error.ts +15 -32
  39. package/src/common/index.ts +0 -3
  40. package/src/data-structures/base/iterable-element-base.ts +0 -3
  41. package/src/data-structures/base/linear-base.ts +2 -36
  42. package/src/data-structures/binary-tree/avl-tree.ts +31 -529
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
  44. package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
  45. package/src/data-structures/binary-tree/bst.ts +158 -1082
  46. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
  47. package/src/data-structures/binary-tree/segment-tree.ts +73 -351
  48. package/src/data-structures/binary-tree/tree-map.ts +462 -5124
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
  51. package/src/data-structures/binary-tree/tree-set.ts +338 -4836
  52. package/src/data-structures/graph/abstract-graph.ts +98 -167
  53. package/src/data-structures/graph/directed-graph.ts +137 -562
  54. package/src/data-structures/graph/map-graph.ts +0 -3
  55. package/src/data-structures/graph/undirected-graph.ts +132 -511
  56. package/src/data-structures/hash/hash-map.ts +154 -582
  57. package/src/data-structures/heap/heap.ts +200 -795
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
  59. package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
  60. package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
  61. package/src/data-structures/matrix/matrix.ts +179 -518
  62. package/src/data-structures/matrix/navigator.ts +0 -1
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  65. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  66. package/src/data-structures/queue/deque.ts +214 -882
  67. package/src/data-structures/queue/queue.ts +102 -625
  68. package/src/data-structures/stack/stack.ts +76 -505
  69. package/src/data-structures/trie/trie.ts +98 -628
  70. package/src/types/common.ts +0 -10
  71. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  72. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  73. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  74. package/src/types/data-structures/hash/hash-map.ts +0 -3
  75. package/src/types/data-structures/hash/index.ts +0 -1
  76. package/src/types/data-structures/matrix/navigator.ts +0 -2
  77. package/src/types/utils/utils.ts +0 -7
  78. package/src/types/utils/validate-type.ts +0 -7
  79. package/src/utils/number.ts +0 -2
  80. package/src/utils/utils.ts +0 -5
@@ -5,7 +5,6 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
-
9
8
  import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
10
9
  import { IterableElementBase } from '../base';
11
10
  import { ERR, raise } from '../../common';
@@ -157,15 +156,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
157
156
  * @param [options] - Options such as comparator and toElementFn.
158
157
  * @returns New Heap instance.
159
158
  */
160
-
161
159
  constructor(elements: Iterable<E | R> = [], options?: HeapOptions<E, R>) {
162
160
  super(options);
163
-
164
161
  if (options) {
165
162
  const { comparator } = options;
166
163
  if (comparator) this._comparator = comparator;
167
164
  }
168
-
169
165
  this.addMany(elements as Iterable<E | R>);
170
166
  }
171
167
 
@@ -176,7 +172,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
176
172
  * @remarks Time O(1), Space O(1)
177
173
  * @returns Internal elements array.
178
174
  */
179
-
180
175
  get elements(): E[] {
181
176
  return this._elements;
182
177
  }
@@ -185,59 +180,16 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
185
180
  * Get the number of elements.
186
181
  * @remarks Time O(1), Space O(1)
187
182
  * @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
-
221
-
222
-
223
-
224
-
225
-
226
-
227
-
228
-
229
-
230
- * @example
231
- * // Track heap capacity
232
- * const heap = new Heap<number>();
233
- * console.log(heap.size); // 0;
234
- * heap.add(10);
235
- * heap.add(20);
236
- * console.log(heap.size); // 2;
237
- * heap.poll();
238
- * console.log(heap.size); // 1;
183
+ * @example
184
+ * // Track heap capacity
185
+ * const heap = new Heap<number>();
186
+ * console.log(heap.size); // 0;
187
+ * heap.add(10);
188
+ * heap.add(20);
189
+ * console.log(heap.size); // 2;
190
+ * heap.poll();
191
+ * console.log(heap.size); // 1;
239
192
  */
240
-
241
193
  get size(): number {
242
194
  return this.elements.length;
243
195
  }
@@ -247,11 +199,19 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
247
199
  * @remarks Time O(1), Space O(1)
248
200
  * @returns Last element or undefined.
249
201
  */
250
-
251
202
  get leaf(): E | undefined {
252
203
  return this.elements[this.size - 1] ?? undefined;
253
204
  }
254
205
 
206
+ /**
207
+ * Get the comparator used to order elements.
208
+ * @remarks Time O(1), Space O(1)
209
+ * @returns Comparator function.
210
+ */
211
+ get comparator() {
212
+ return this._comparator;
213
+ }
214
+
255
215
  /**
256
216
  * Create a heap of the same class from an iterable.
257
217
  * @remarks Time O(N), Space O(N)
@@ -262,7 +222,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
262
222
  * @param [options] - Heap options including comparator.
263
223
  * @returns A new heap instance of this class.
264
224
  */
265
-
266
225
  static from<T, R = any, S extends Heap<T, R> = Heap<T, R>>(
267
226
  this: new (elements?: Iterable<T | R>, options?: HeapOptions<T, R>) => S,
268
227
  elements?: Iterable<T | R>,
@@ -280,7 +239,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
280
239
  * @param options - Heap options including comparator.
281
240
  * @returns A new Heap built from elements.
282
241
  */
283
-
284
242
  static heapify<EE = any, RR = any>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR> {
285
243
  return new Heap<EE, RR>(elements, options);
286
244
  }
@@ -290,65 +248,22 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
290
248
  * @remarks Time O(log N) amortized, Space O(1)
291
249
  * @param element - Element to insert.
292
250
  * @returns True.
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
- * @example
336
- * // basic Heap creation and add operation
337
- * // Create a min heap (default)
338
- * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
339
- *
340
- * // Verify size
341
- * console.log(minHeap.size); // 6;
342
- *
343
- * // Add new element
344
- * minHeap.add(4);
345
- * console.log(minHeap.size); // 7;
346
- *
347
- * // Min heap property: smallest element at root
348
- * const min = minHeap.peek();
349
- * console.log(min); // 1;
251
+ * @example
252
+ * // basic Heap creation and add operation
253
+ * // Create a min heap (default)
254
+ * const minHeap = new Heap([5, 3, 7, 1, 9, 2]);
255
+ *
256
+ * // Verify size
257
+ * console.log(minHeap.size); // 6;
258
+ *
259
+ * // Add new element
260
+ * minHeap.add(4);
261
+ * console.log(minHeap.size); // 7;
262
+ *
263
+ * // Min heap property: smallest element at root
264
+ * const min = minHeap.peek();
265
+ * console.log(min); // 1;
350
266
  */
351
-
352
267
  add(element: E): boolean {
353
268
  this._elements.push(element);
354
269
  return this._bubbleUp(this.elements.length - 1);
@@ -359,53 +274,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
359
274
  * @remarks Time O(N log N), Space O(1)
360
275
  * @param elements - Iterable of elements or raw values.
361
276
  * @returns Array of per-element success flags.
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
- * @example
402
- * // Add multiple elements
403
- * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
404
- * heap.addMany([5, 3, 7, 1]);
405
- * console.log(heap.peek()); // 1;
406
- * console.log(heap.size); // 4;
277
+ * @example
278
+ * // Add multiple elements
279
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
280
+ * heap.addMany([5, 3, 7, 1]);
281
+ * console.log(heap.peek()); // 1;
282
+ * console.log(heap.size); // 4;
407
283
  */
408
-
409
284
  addMany(elements: Iterable<E | R>): boolean[] {
410
285
  const flags: boolean[] = [];
411
286
  for (const el of elements) {
@@ -424,101 +299,59 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
424
299
  * Remove and return the top element.
425
300
  * @remarks Time O(log N), Space O(1)
426
301
  * @returns Top element or undefined.
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
- * @example
466
- * // Heap with custom comparator (MaxHeap behavior)
467
- * interface Task {
468
- * id: number;
469
- * priority: number;
470
- * name: string;
471
- * }
472
- *
473
- * // Custom comparator for max heap behavior (higher priority first)
474
- * const tasks: Task[] = [
475
- * { id: 1, priority: 5, name: 'Email' },
476
- * { id: 2, priority: 3, name: 'Chat' },
477
- * { id: 3, priority: 8, name: 'Alert' }
478
- * ];
479
- *
480
- * const maxHeap = new Heap(tasks, {
481
- * comparator: (a: Task, b: Task) => b.priority - a.priority
482
- * });
483
- *
484
- * console.log(maxHeap.size); // 3;
485
- *
486
- * // Peek returns highest priority task
487
- * const topTask = maxHeap.peek();
488
- * console.log(topTask?.priority); // 8;
489
- * console.log(topTask?.name); // 'Alert';
302
+ * @example
303
+ * // Heap with custom comparator (MaxHeap behavior)
304
+ * interface Task {
305
+ * id: number;
306
+ * priority: number;
307
+ * name: string;
308
+ * }
309
+ *
310
+ * // Custom comparator for max heap behavior (higher priority first)
311
+ * const tasks: Task[] = [
312
+ * { id: 1, priority: 5, name: 'Email' },
313
+ * { id: 2, priority: 3, name: 'Chat' },
314
+ * { id: 3, priority: 8, name: 'Alert' }
315
+ * ];
316
+ *
317
+ * const maxHeap = new Heap(tasks, {
318
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
319
+ * });
320
+ *
321
+ * console.log(maxHeap.size); // 3;
322
+ *
323
+ * // Peek returns highest priority task
324
+ * const topTask = maxHeap.peek();
325
+ * console.log(topTask?.priority); // 8;
326
+ * console.log(topTask?.name); // 'Alert';
490
327
  */
491
-
492
328
  /**
493
329
  * @deprecated Use `pop` instead. Will be removed in a future major version.
494
-
495
-
496
-
497
- * @example
498
- * // Heap with custom comparator (MaxHeap behavior)
499
- * interface Task {
500
- * id: number;
501
- * priority: number;
502
- * name: string;
503
- * }
504
- *
505
- * // Custom comparator for max heap behavior (higher priority first)
506
- * const tasks: Task[] = [
507
- * { id: 1, priority: 5, name: 'Email' },
508
- * { id: 2, priority: 3, name: 'Chat' },
509
- * { id: 3, priority: 8, name: 'Alert' }
510
- * ];
511
- *
512
- * const maxHeap = new Heap(tasks, {
513
- * comparator: (a: Task, b: Task) => b.priority - a.priority
514
- * });
515
- *
516
- * console.log(maxHeap.size); // 3;
517
- *
518
- * // Peek returns highest priority task
519
- * const topTask = maxHeap.peek();
520
- * console.log(topTask?.priority); // 8;
521
- * console.log(topTask?.name); // 'Alert';
330
+ * @example
331
+ * // Heap with custom comparator (MaxHeap behavior)
332
+ * interface Task {
333
+ * id: number;
334
+ * priority: number;
335
+ * name: string;
336
+ * }
337
+ *
338
+ * // Custom comparator for max heap behavior (higher priority first)
339
+ * const tasks: Task[] = [
340
+ * { id: 1, priority: 5, name: 'Email' },
341
+ * { id: 2, priority: 3, name: 'Chat' },
342
+ * { id: 3, priority: 8, name: 'Alert' }
343
+ * ];
344
+ *
345
+ * const maxHeap = new Heap(tasks, {
346
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
347
+ * });
348
+ *
349
+ * console.log(maxHeap.size); // 3;
350
+ *
351
+ * // Peek returns highest priority task
352
+ * const topTask = maxHeap.peek();
353
+ * console.log(topTask?.priority); // 8;
354
+ * console.log(topTask?.name); // 'Alert';
522
355
  */
523
356
  poll(): E | undefined {
524
357
  return this.pop();
@@ -544,109 +377,66 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
544
377
  * Get the current top element without removing it.
545
378
  * @remarks Time O(1), Space O(1)
546
379
  * @returns Top element or undefined.
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
- * @example
590
- * // Heap for event processing with priority
591
- * interface Event {
592
- * id: number;
593
- * type: 'critical' | 'warning' | 'info';
594
- * timestamp: number;
595
- * message: string;
596
- * }
597
- *
598
- * // Custom priority: critical > warning > info
599
- * const priorityMap = { critical: 3, warning: 2, info: 1 };
600
- *
601
- * const eventHeap = new Heap<Event>([], {
602
- * comparator: (a: Event, b: Event) => {
603
- * const priorityA = priorityMap[a.type];
604
- * const priorityB = priorityMap[b.type];
605
- * return priorityB - priorityA; // Higher priority first
606
- * }
607
- * });
608
- *
609
- * // Add events in random order
610
- * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
611
- * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
612
- * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
613
- * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
614
- * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
615
- *
616
- * console.log(eventHeap.size); // 5;
617
- *
618
- * // Process events by priority (critical first)
619
- * const processedOrder: Event[] = [];
620
- * while (eventHeap.size > 0) {
621
- * const event = eventHeap.poll();
622
- * if (event) {
623
- * processedOrder.push(event);
624
- * }
625
- * }
626
- *
627
- * // Verify critical events came first
628
- * console.log(processedOrder[0].type); // 'critical';
629
- * console.log(processedOrder[1].type); // 'critical';
630
- * console.log(processedOrder[2].type); // 'warning';
631
- * console.log(processedOrder[3].type); // 'info';
632
- * console.log(processedOrder[4].type); // 'info';
633
- *
634
- * // Verify O(log n) operations
635
- * const newHeap = new Heap<number>([5, 3, 7, 1]);
636
- *
637
- * // Add - O(log n)
638
- * newHeap.add(2);
639
- * console.log(newHeap.size); // 5;
640
- *
641
- * // Poll - O(log n)
642
- * const removed = newHeap.poll();
643
- * console.log(removed); // 1;
644
- *
645
- * // Peek - O(1)
646
- * const top = newHeap.peek();
647
- * console.log(top); // 2;
380
+ * @example
381
+ * // Heap for event processing with priority
382
+ * interface Event {
383
+ * id: number;
384
+ * type: 'critical' | 'warning' | 'info';
385
+ * timestamp: number;
386
+ * message: string;
387
+ * }
388
+ *
389
+ * // Custom priority: critical > warning > info
390
+ * const priorityMap = { critical: 3, warning: 2, info: 1 };
391
+ *
392
+ * const eventHeap = new Heap<Event>([], {
393
+ * comparator: (a: Event, b: Event) => {
394
+ * const priorityA = priorityMap[a.type];
395
+ * const priorityB = priorityMap[b.type];
396
+ * return priorityB - priorityA; // Higher priority first
397
+ * }
398
+ * });
399
+ *
400
+ * // Add events in random order
401
+ * eventHeap.add({ id: 1, type: 'info', timestamp: 100, message: 'User logged in' });
402
+ * eventHeap.add({ id: 2, type: 'critical', timestamp: 101, message: 'Server down' });
403
+ * eventHeap.add({ id: 3, type: 'warning', timestamp: 102, message: 'High memory' });
404
+ * eventHeap.add({ id: 4, type: 'info', timestamp: 103, message: 'Cache cleared' });
405
+ * eventHeap.add({ id: 5, type: 'critical', timestamp: 104, message: 'Database error' });
406
+ *
407
+ * console.log(eventHeap.size); // 5;
408
+ *
409
+ * // Process events by priority (critical first)
410
+ * const processedOrder: Event[] = [];
411
+ * while (eventHeap.size > 0) {
412
+ * const event = eventHeap.poll();
413
+ * if (event) {
414
+ * processedOrder.push(event);
415
+ * }
416
+ * }
417
+ *
418
+ * // Verify critical events came first
419
+ * console.log(processedOrder[0].type); // 'critical';
420
+ * console.log(processedOrder[1].type); // 'critical';
421
+ * console.log(processedOrder[2].type); // 'warning';
422
+ * console.log(processedOrder[3].type); // 'info';
423
+ * console.log(processedOrder[4].type); // 'info';
424
+ *
425
+ * // Verify O(log n) operations
426
+ * const newHeap = new Heap<number>([5, 3, 7, 1]);
427
+ *
428
+ * // Add - O(log n)
429
+ * newHeap.add(2);
430
+ * console.log(newHeap.size); // 5;
431
+ *
432
+ * // Poll - O(log n)
433
+ * const removed = newHeap.poll();
434
+ * console.log(removed); // 1;
435
+ *
436
+ * // Peek - O(1)
437
+ * const top = newHeap.peek();
438
+ * console.log(top); // 2;
648
439
  */
649
-
650
440
  peek(): E | undefined {
651
441
  return this.elements[0];
652
442
  }
@@ -655,54 +445,13 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
655
445
  * Check whether the heap is empty.
656
446
  * @remarks Time O(1), Space O(1)
657
447
  * @returns True if size is 0.
658
-
659
-
660
-
661
-
662
-
663
-
664
-
665
-
666
-
667
-
668
-
669
-
670
-
671
-
672
-
673
-
674
-
675
-
676
-
677
-
678
-
679
-
680
-
681
-
682
-
683
-
684
-
685
-
686
-
687
-
688
-
689
-
690
-
691
-
692
-
693
-
694
-
695
-
696
-
697
-
698
- * @example
699
- * // Check if heap is empty
700
- * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
701
- * console.log(heap.isEmpty()); // true;
702
- * heap.add(1);
703
- * console.log(heap.isEmpty()); // false;
448
+ * @example
449
+ * // Check if heap is empty
450
+ * const heap = new Heap<number>([], { comparator: (a, b) => a - b });
451
+ * console.log(heap.isEmpty()); // true;
452
+ * heap.add(1);
453
+ * console.log(heap.isEmpty()); // false;
704
454
  */
705
-
706
455
  isEmpty(): boolean {
707
456
  return this.size === 0;
708
457
  }
@@ -711,104 +460,27 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
711
460
  * Remove all elements.
712
461
  * @remarks Time O(1), Space O(1)
713
462
  * @returns void
714
-
715
-
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
-
746
-
747
-
748
-
749
-
750
-
751
-
752
-
753
-
754
- * @example
755
- * // Remove all elements
756
- * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
757
- * heap.clear();
758
- * console.log(heap.isEmpty()); // true;
463
+ * @example
464
+ * // Remove all elements
465
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
466
+ * heap.clear();
467
+ * console.log(heap.isEmpty()); // true;
759
468
  */
760
-
761
469
  clear(): void {
762
470
  this._elements = [];
763
471
  }
764
472
 
765
-
766
-
767
473
  /**
768
474
  * Check if an equal element exists in the heap.
769
475
  * @remarks Time O(N), Space O(1)
770
476
  * @param element - Element to search for.
771
477
  * @returns True if found.
772
-
773
-
774
-
775
-
776
-
777
-
778
-
779
-
780
-
781
-
782
-
783
-
784
-
785
-
786
-
787
-
788
-
789
-
790
-
791
-
792
-
793
-
794
-
795
-
796
-
797
-
798
-
799
-
800
-
801
-
802
-
803
-
804
-
805
- * @example
806
- * // Check element existence
807
- * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
808
- * console.log(heap.has(1)); // true;
809
- * console.log(heap.has(99)); // false;
478
+ * @example
479
+ * // Check element existence
480
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
481
+ * console.log(heap.has(1)); // true;
482
+ * console.log(heap.has(99)); // false;
810
483
  */
811
-
812
484
  override has(element: E): boolean {
813
485
  for (const el of this.elements) if (this._equals(el, element)) return true;
814
486
  return false;
@@ -819,52 +491,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
819
491
  * @remarks Time O(N), Space O(1)
820
492
  * @param element - Element to delete.
821
493
  * @returns True if an element was removed.
822
-
823
-
824
-
825
-
826
-
827
-
828
-
829
-
830
-
831
-
832
-
833
-
834
-
835
-
836
-
837
-
838
-
839
-
840
-
841
-
842
-
843
-
844
-
845
-
846
-
847
-
848
-
849
-
850
-
851
-
852
-
853
-
854
-
855
-
856
-
857
-
858
-
859
-
860
-
861
- * @example
862
- * // Remove specific element
863
- * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
864
- * heap.delete(4);
865
- * console.log(heap.toArray().includes(4)); // false;
494
+ * @example
495
+ * // Remove specific element
496
+ * const heap = new Heap<number>([3, 1, 4, 1, 5], { comparator: (a, b) => a - b });
497
+ * heap.delete(4);
498
+ * console.log(heap.toArray().includes(4)); // false;
866
499
  */
867
-
868
500
  delete(element: E): boolean {
869
501
  let index = -1;
870
502
  for (let i = 0; i < this.elements.length; i++) {
@@ -926,7 +558,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
926
558
  * @param equals - Equality predicate (a, b) → boolean.
927
559
  * @returns This heap.
928
560
  */
929
-
930
561
  setEquality(equals: (a: E, b: E) => boolean): this {
931
562
  this._equals = equals;
932
563
  return this;
@@ -937,46 +568,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
937
568
  * @remarks Time O(N), Space O(H)
938
569
  * @param [order] - Traversal order: 'PRE' | 'IN' | 'POST'.
939
570
  * @returns Array of visited elements.
940
-
941
-
942
-
943
-
944
-
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
- * @example
974
- * // Depth-first traversal
975
- * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
976
- * const result = heap.dfs('IN');
977
- * console.log(result.length); // 3;
571
+ * @example
572
+ * // Depth-first traversal
573
+ * const heap = new Heap<number>([3, 1, 2], { comparator: (a, b) => a - b });
574
+ * const result = heap.dfs('IN');
575
+ * console.log(result.length); // 3;
978
576
  */
979
-
980
577
  dfs(order: DFSOrderPattern = 'PRE'): E[] {
981
578
  const result: E[] = [];
982
579
  const _dfs = (index: number) => {
@@ -1007,7 +604,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1007
604
  * @remarks Time O(N), Space O(1)
1008
605
  * @returns Array of per-node results from fixing steps.
1009
606
  */
1010
-
1011
607
  fix(): boolean[] {
1012
608
  const results: boolean[] = [];
1013
609
  for (let i = Math.floor(this.size / 2) - 1; i >= 0; i--) {
@@ -1020,55 +616,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1020
616
  * Return all elements in ascending order by repeatedly polling.
1021
617
  * @remarks Time O(N log N), Space O(N)
1022
618
  * @returns Sorted array of elements.
1023
-
1024
-
1025
-
1026
-
1027
-
1028
-
1029
-
1030
-
1031
-
1032
-
1033
-
1034
-
1035
-
1036
-
1037
-
1038
-
1039
-
1040
-
1041
-
1042
-
1043
-
1044
-
1045
-
1046
-
1047
-
1048
-
1049
-
1050
-
1051
-
1052
-
1053
-
1054
-
1055
-
1056
-
1057
-
1058
-
1059
-
1060
-
1061
-
1062
-
1063
-
1064
-
1065
- * @example
1066
- * // Sort elements using heap
1067
- * const heap = new Heap<number>([5, 1, 3, 2, 4]);
1068
- * const sorted = heap.sort();
1069
- * console.log(sorted); // [1, 2, 3, 4, 5];
619
+ * @example
620
+ * // Sort elements using heap
621
+ * const heap = new Heap<number>([5, 1, 3, 2, 4]);
622
+ * const sorted = heap.sort();
623
+ * console.log(sorted); // [1, 2, 3, 4, 5];
1070
624
  */
1071
-
1072
625
  sort(): E[] {
1073
626
  const visited: E[] = [];
1074
627
  const cloned = this._createInstance();
@@ -1084,55 +637,14 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1084
637
  * Deep clone this heap.
1085
638
  * @remarks Time O(N), Space O(N)
1086
639
  * @returns A new heap with the same elements.
1087
-
1088
-
1089
-
1090
-
1091
-
1092
-
1093
-
1094
-
1095
-
1096
-
1097
-
1098
-
1099
-
1100
-
1101
-
1102
-
1103
-
1104
-
1105
-
1106
-
1107
-
1108
-
1109
-
1110
-
1111
-
1112
-
1113
-
1114
-
1115
-
1116
-
1117
-
1118
-
1119
-
1120
-
1121
-
1122
-
1123
-
1124
-
1125
-
1126
-
1127
- * @example
1128
- * // Create independent copy
1129
- * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
1130
- * const copy = heap.clone();
1131
- * copy.poll();
1132
- * console.log(heap.size); // 3;
1133
- * console.log(copy.size); // 2;
640
+ * @example
641
+ * // Create independent copy
642
+ * const heap = new Heap<number>([3, 1, 4], { comparator: (a, b) => a - b });
643
+ * const copy = heap.clone();
644
+ * copy.poll();
645
+ * console.log(heap.size); // 3;
646
+ * console.log(copy.size); // 2;
1134
647
  */
1135
-
1136
648
  clone(): this {
1137
649
  const next = this._createInstance();
1138
650
  for (const x of this.elements) next.add(x);
@@ -1145,53 +657,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1145
657
  * @param callback - Predicate (element, index, heap) → boolean to keep element.
1146
658
  * @param [thisArg] - Value for `this` inside the callback.
1147
659
  * @returns A new heap with the kept elements.
1148
-
1149
-
1150
-
1151
-
1152
-
1153
-
1154
-
1155
-
1156
-
1157
-
1158
-
1159
-
1160
-
1161
-
1162
-
1163
-
1164
-
1165
-
1166
-
1167
-
1168
-
1169
-
1170
-
1171
-
1172
-
1173
-
1174
-
1175
-
1176
-
1177
-
1178
-
1179
-
1180
-
1181
-
1182
-
1183
-
1184
-
1185
-
1186
-
1187
-
1188
- * @example
1189
- * // Filter elements
1190
- * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
1191
- * const evens = heap.filter(x => x % 2 === 0);
1192
- * console.log(evens.size); // 2;
660
+ * @example
661
+ * // Filter elements
662
+ * const heap = new Heap<number>([1, 2, 3, 4, 5], { comparator: (a, b) => a - b });
663
+ * const evens = heap.filter(x => x % 2 === 0);
664
+ * console.log(evens.size); // 2;
1193
665
  */
1194
-
1195
666
  filter(callback: ElementCallback<E, R, boolean>, thisArg?: unknown): this {
1196
667
  const out = this._createInstance();
1197
668
  let i = 0;
@@ -1214,52 +685,12 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1214
685
  * @param options - Options for the output heap, including comparator for EM.
1215
686
  * @param [thisArg] - Value for `this` inside the callback.
1216
687
  * @returns A new heap with mapped elements.
1217
-
1218
-
1219
-
1220
-
1221
-
1222
-
1223
-
1224
-
1225
-
1226
-
1227
-
1228
-
1229
-
1230
-
1231
-
1232
-
1233
-
1234
-
1235
-
1236
-
1237
-
1238
-
1239
-
1240
-
1241
-
1242
-
1243
-
1244
-
1245
-
1246
-
1247
-
1248
-
1249
-
1250
-
1251
-
1252
-
1253
-
1254
-
1255
-
1256
- * @example
1257
- * // Transform elements
1258
- * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
1259
- * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
1260
- * console.log(doubled.peek()); // 2;
688
+ * @example
689
+ * // Transform elements
690
+ * const heap = new Heap<number>([1, 2, 3], { comparator: (a, b) => a - b });
691
+ * const doubled = heap.map(x => x * 2, { comparator: (a, b) => a - b });
692
+ * console.log(doubled.peek()); // 2;
1261
693
  */
1262
-
1263
694
  map<EM, RM>(
1264
695
  callback: ElementCallback<E, R, EM>,
1265
696
  options: HeapOptions<EM, RM> & { comparator: Comparator<EM> },
@@ -1283,7 +714,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1283
714
  * @param [thisArg] - Value for `this` inside the callback.
1284
715
  * @returns A new heap with mapped elements.
1285
716
  */
1286
-
1287
717
  mapSame(callback: ElementCallback<E, R, E>, thisArg?: unknown): this {
1288
718
  const out = this._createInstance();
1289
719
  let i = 0;
@@ -1305,16 +735,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1305
735
 
1306
736
  protected readonly _comparator: Comparator<E> = this._DEFAULT_COMPARATOR;
1307
737
 
1308
- /**
1309
- * Get the comparator used to order elements.
1310
- * @remarks Time O(1), Space O(1)
1311
- * @returns Comparator function.
1312
- */
1313
-
1314
- get comparator() {
1315
- return this._comparator;
1316
- }
1317
-
1318
738
  protected *_getIterator(): IterableIterator<E> {
1319
739
  for (const element of this.elements) yield element;
1320
740
  }
@@ -1356,12 +776,8 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1356
776
  * @param [options] - Options to override comparator or toElementFn.
1357
777
  * @returns A like-kind empty heap instance.
1358
778
  */
1359
-
1360
779
  protected _createInstance(options?: HeapOptions<E, R>): this {
1361
- const Ctor = this.constructor as new (
1362
- elements?: Iterable<E> | Iterable<R>,
1363
- options?: HeapOptions<E, R>
1364
- ) => this;
780
+ const Ctor = this.constructor as new (elements?: Iterable<E> | Iterable<R>, options?: HeapOptions<E, R>) => this;
1365
781
  return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...(options ?? {}) });
1366
782
  }
1367
783
 
@@ -1374,7 +790,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1374
790
  * @param [options] - Options forwarded to the constructor.
1375
791
  * @returns A like-kind heap instance.
1376
792
  */
1377
-
1378
793
  protected _createLike<EM, RM>(
1379
794
  elements: Iterable<EM> | Iterable<RM> = [],
1380
795
  options?: HeapOptions<EM, RM>
@@ -1394,7 +809,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
1394
809
  * @param [options] - Options forwarded to the constructor.
1395
810
  * @returns An empty like-kind heap instance.
1396
811
  */
1397
-
1398
812
  protected _spawnLike<EM, RM>(options?: HeapOptions<EM, RM>): Heap<EM, RM> {
1399
813
  return this._createLike<EM, RM>([], options);
1400
814
  }
@@ -1428,13 +842,14 @@ export class FibonacciHeapNode<E> {
1428
842
  * @example examples will be generated by unit test
1429
843
  */
1430
844
  export class FibonacciHeap<E> {
845
+ protected readonly _comparator: Comparator<E>;
846
+
1431
847
  /**
1432
848
  * Create a FibonacciHeap.
1433
849
  * @remarks Time O(1), Space O(1)
1434
850
  * @param [comparator] - Comparator to order elements (min-heap by default).
1435
851
  * @returns New FibonacciHeap instance.
1436
852
  */
1437
-
1438
853
  constructor(comparator?: Comparator<E>) {
1439
854
  this.clear();
1440
855
  this._comparator = comparator || this._defaultComparator;
@@ -1448,12 +863,12 @@ export class FibonacciHeap<E> {
1448
863
  * @remarks Time O(1), Space O(1)
1449
864
  * @returns Root node or undefined.
1450
865
  */
1451
-
1452
866
  get root(): FibonacciHeapNode<E> | undefined {
1453
867
  return this._root;
1454
868
  }
1455
869
 
1456
870
  protected _size = 0;
871
+
1457
872
  get size(): number {
1458
873
  return this._size;
1459
874
  }
@@ -1465,13 +880,10 @@ export class FibonacciHeap<E> {
1465
880
  * @remarks Time O(1), Space O(1)
1466
881
  * @returns Min node or undefined.
1467
882
  */
1468
-
1469
883
  get min(): FibonacciHeapNode<E> | undefined {
1470
884
  return this._min;
1471
885
  }
1472
886
 
1473
- protected readonly _comparator: Comparator<E>;
1474
-
1475
887
  get comparator(): Comparator<E> {
1476
888
  return this._comparator;
1477
889
  }
@@ -1493,7 +905,6 @@ export class FibonacciHeap<E> {
1493
905
  * @param element - Element to insert.
1494
906
  * @returns True when the element is added.
1495
907
  */
1496
-
1497
908
  push(element: E): boolean {
1498
909
  const node = this.createNode(element);
1499
910
  node.left = node;
@@ -1514,7 +925,6 @@ export class FibonacciHeap<E> {
1514
925
  * @param [head] - Start node of the circular list.
1515
926
  * @returns Array of nodes from the list.
1516
927
  */
1517
-
1518
928
  consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[] {
1519
929
  const elements: FibonacciHeapNode<E>[] = [];
1520
930
  if (!head) return elements;
@@ -1536,7 +946,6 @@ export class FibonacciHeap<E> {
1536
946
  * @param node - Child node to insert.
1537
947
  * @returns void
1538
948
  */
1539
-
1540
949
  mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void {
1541
950
  if (!parent.child) parent.child = node;
1542
951
  else {
@@ -1556,7 +965,6 @@ export class FibonacciHeap<E> {
1556
965
  * @remarks Time O(log N) amortized, Space O(1)
1557
966
  * @returns Minimum element or undefined.
1558
967
  */
1559
-
1560
968
  pop(): E | undefined {
1561
969
  if (this._size === 0) return undefined;
1562
970
  const z = this.min!;
@@ -1585,7 +993,6 @@ export class FibonacciHeap<E> {
1585
993
  * @param heapToMerge - Another FibonacciHeap to meld into this one.
1586
994
  * @returns void
1587
995
  */
1588
-
1589
996
  merge(heapToMerge: FibonacciHeap<E>): void {
1590
997
  if (heapToMerge.size === 0) return;
1591
998
  if (this.root && heapToMerge.root) {
@@ -1653,7 +1060,6 @@ export class FibonacciHeap<E> {
1653
1060
  y: FibonacciHeapNode<E> | undefined,
1654
1061
  d: number,
1655
1062
  t: FibonacciHeapNode<E> | undefined;
1656
-
1657
1063
  for (const node of elements) {
1658
1064
  x = node;
1659
1065
  d = x.degree;
@@ -1670,7 +1076,6 @@ export class FibonacciHeap<E> {
1670
1076
  }
1671
1077
  A[d] = x;
1672
1078
  }
1673
-
1674
1079
  for (let i = 0; i < A.length; i++) {
1675
1080
  if (A[i] && (!this.min || this.comparator(A[i]!.element, this.min.element) <= 0)) this._min = A[i]!;
1676
1081
  }