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.
Files changed (94) hide show
  1. package/README.md +0 -84
  2. package/dist/cjs/index.cjs +1476 -404
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +1473 -401
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +1476 -404
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +1473 -401
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/base/index.d.ts +1 -0
  11. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
  26. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  27. package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
  28. package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
  29. package/dist/types/data-structures/heap/heap.d.ts +581 -99
  30. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  31. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  32. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
  33. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
  34. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
  35. package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
  36. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  37. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  39. package/dist/types/data-structures/queue/deque.d.ts +593 -71
  40. package/dist/types/data-structures/queue/queue.d.ts +463 -42
  41. package/dist/types/data-structures/stack/stack.d.ts +384 -32
  42. package/dist/types/data-structures/trie/trie.d.ts +470 -48
  43. package/dist/types/interfaces/graph.d.ts +1 -1
  44. package/dist/types/types/common.d.ts +2 -2
  45. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  46. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  47. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  48. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  49. package/dist/types/types/utils/validate-type.d.ts +4 -4
  50. package/dist/umd/binary-tree-typed.js +1469 -397
  51. package/dist/umd/binary-tree-typed.js.map +1 -1
  52. package/dist/umd/binary-tree-typed.min.js +5 -5
  53. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  54. package/package.json +2 -2
  55. package/src/data-structures/base/index.ts +1 -0
  56. package/src/data-structures/base/iterable-element-base.ts +4 -5
  57. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  58. package/src/data-structures/base/linear-base.ts +3 -3
  59. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  60. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  61. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  62. package/src/data-structures/binary-tree/bst.ts +840 -35
  63. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  64. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  65. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  66. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  67. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  68. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  69. package/src/data-structures/graph/abstract-graph.ts +4 -4
  70. package/src/data-structures/graph/directed-graph.ts +429 -47
  71. package/src/data-structures/graph/map-graph.ts +59 -1
  72. package/src/data-structures/graph/undirected-graph.ts +393 -59
  73. package/src/data-structures/hash/hash-map.ts +476 -92
  74. package/src/data-structures/heap/heap.ts +581 -99
  75. package/src/data-structures/heap/max-heap.ts +46 -0
  76. package/src/data-structures/heap/min-heap.ts +59 -0
  77. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  78. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  79. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  80. package/src/data-structures/matrix/matrix.ts +584 -12
  81. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  82. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  83. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  84. package/src/data-structures/queue/deque.ts +592 -70
  85. package/src/data-structures/queue/queue.ts +463 -42
  86. package/src/data-structures/stack/stack.ts +384 -32
  87. package/src/data-structures/trie/trie.ts +470 -48
  88. package/src/interfaces/graph.ts +1 -1
  89. package/src/types/common.ts +2 -2
  90. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  91. package/src/types/data-structures/heap/heap.ts +1 -0
  92. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  93. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  94. package/src/types/utils/validate-type.ts +4 -4
@@ -125,84 +125,6 @@ export declare class BinaryTreeNode<K = any, V = any> {
125
125
  * 5. Leaf Nodes: Nodes without children are leaves.
126
126
  *
127
127
  * @example
128
- * // basic BinaryTree creation and insertion
129
- * // Create a BinaryTree with entries
130
- * const entries: [number, string][] = [
131
- * [6, 'six'],
132
- * [1, 'one'],
133
- * [2, 'two'],
134
- * [7, 'seven'],
135
- * [5, 'five'],
136
- * [3, 'three'],
137
- * [4, 'four'],
138
- * [9, 'nine'],
139
- * [8, 'eight']
140
- * ];
141
- *
142
- * const tree = new BinaryTree(entries);
143
- *
144
- * // Verify size
145
- * console.log(tree.size); // 9;
146
- *
147
- * // Add new element
148
- * tree.set(10, 'ten');
149
- * console.log(tree.size); // 10;
150
- * @example
151
- * // BinaryTree get and has operations
152
- * const tree = new BinaryTree(
153
- * [
154
- * [5, 'five'],
155
- * [3, 'three'],
156
- * [7, 'seven'],
157
- * [1, 'one'],
158
- * [4, 'four'],
159
- * [6, 'six'],
160
- * [8, 'eight']
161
- * ],
162
- * { isMapMode: false }
163
- * );
164
- *
165
- * // Check if key exists
166
- * console.log(tree.has(5)); // true;
167
- * console.log(tree.has(10)); // false;
168
- *
169
- * // Get value by key
170
- * console.log(tree.get(3)); // 'three';
171
- * console.log(tree.get(7)); // 'seven';
172
- * console.log(tree.get(100)); // undefined;
173
- *
174
- * // Get node structure
175
- * const node = tree.getNode(5);
176
- * console.log(node?.key); // 5;
177
- * console.log(node?.value); // 'five';
178
- * @example
179
- * // BinaryTree level-order traversal
180
- * const tree = new BinaryTree([
181
- * [1, 'one'],
182
- * [2, 'two'],
183
- * [3, 'three'],
184
- * [4, 'four'],
185
- * [5, 'five'],
186
- * [6, 'six'],
187
- * [7, 'seven']
188
- * ]);
189
- *
190
- * // Binary tree maintains level-order insertion
191
- * // Complete binary tree structure
192
- * console.log(tree.size); // 7;
193
- *
194
- * // Verify all keys are present
195
- * console.log(tree.has(1)); // true;
196
- * console.log(tree.has(4)); // true;
197
- * console.log(tree.has(7)); // true;
198
- *
199
- * // Iterate through tree
200
- * const keys: number[] = [];
201
- * for (const [key] of tree) {
202
- * keys.push(key);
203
- * }
204
- * console.log(keys.length); // 7;
205
- * @example
206
128
  * // determine loan approval using a decision tree
207
129
  * // Decision tree structure
208
130
  * const loanDecisionTree = new BinaryTree<string>(
@@ -428,13 +350,48 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
428
350
  * @param key - The key to validate.
429
351
  * @returns True if the key is valid, false otherwise.
430
352
  */
431
- isValidKey(key: any): key is K;
353
+ isValidKey(key: unknown): key is K;
432
354
  /**
433
355
  * Adds a new node to the tree.
434
356
  * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
435
357
  *
436
358
  * @param keyNodeOrEntry - The key, node, or entry to add.
437
359
  * @returns True if the addition was successful, false otherwise.
360
+
361
+
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
+ * @example
388
+ * // Add a single node
389
+ * const tree = new BinaryTree<number>();
390
+ * tree.add(1);
391
+ * tree.add(2);
392
+ * tree.add(3);
393
+ * console.log(tree.size); // 3;
394
+ * console.log(tree.has(1)); // true;
438
395
  */
439
396
  add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
440
397
  /**
@@ -444,6 +401,61 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
444
401
  * @param keyNodeOrEntry - The key, node, or entry to set or update.
445
402
  * @param [value] - The value, if providing just a key.
446
403
  * @returns True if the addition was successful, false otherwise.
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
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+ * @example
437
+ * // basic BinaryTree creation and insertion
438
+ * // Create a BinaryTree with entries
439
+ * const entries: [number, string][] = [
440
+ * [6, 'six'],
441
+ * [1, 'one'],
442
+ * [2, 'two'],
443
+ * [7, 'seven'],
444
+ * [5, 'five'],
445
+ * [3, 'three'],
446
+ * [4, 'four'],
447
+ * [9, 'nine'],
448
+ * [8, 'eight']
449
+ * ];
450
+ *
451
+ * const tree = new BinaryTree(entries);
452
+ *
453
+ * // Verify size
454
+ * console.log(tree.size); // 9;
455
+ *
456
+ * // Add new element
457
+ * tree.set(10, 'ten');
458
+ * console.log(tree.size); // 10;
447
459
  */
448
460
  set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
449
461
  /**
@@ -452,6 +464,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
452
464
  *
453
465
  * @param keysNodesEntriesOrRaws - An iterable of items to set.
454
466
  * @returns An array of booleans indicating the success of each individual `set` operation.
467
+
468
+
469
+
470
+
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
+ * @example
498
+ * // Bulk add
499
+ * const tree = new BinaryTree<number>();
500
+ * tree.addMany([1, 2, 3, 4, 5]);
501
+ * console.log(tree.size); // 5;
455
502
  */
456
503
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>): boolean[];
457
504
  /**
@@ -461,6 +508,34 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
461
508
  * @param keysNodesEntriesOrRaws - An iterable of items to set or update.
462
509
  * @param [values] - An optional parallel iterable of values.
463
510
  * @returns An array of booleans indicating the success of each individual `set` operation.
511
+
512
+
513
+
514
+
515
+
516
+
517
+
518
+
519
+
520
+
521
+
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+
534
+ * @example
535
+ * // Set multiple entries
536
+ * const tree = new BinaryTree<number, string>();
537
+ * tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
538
+ * console.log(tree.size); // 3;
464
539
  */
465
540
  setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
466
541
  /**
@@ -468,6 +543,42 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
468
543
  * @remarks Time O(N * M), same as `setMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `set`).
469
544
  *
470
545
  * @param anotherTree - The tree to merge.
546
+
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
+ * @example
577
+ * // Combine trees
578
+ * const t1 = new BinaryTree<number>([1, 2]);
579
+ * const t2 = new BinaryTree<number>([3, 4]);
580
+ * t1.merge(t2);
581
+ * console.log(t1.size); // 4;
471
582
  */
472
583
  merge(anotherTree: BinaryTree<K, V, R>): void;
473
584
  /**
@@ -484,8 +595,77 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
484
595
  *
485
596
  * @param keyNodeEntryRawOrPredicate - The node to delete.
486
597
  * @returns An array containing deletion results (for compatibility with self-balancing trees).
598
+
599
+
600
+
601
+
602
+
603
+
604
+
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
630
+ * @example
631
+ * // Remove a node
632
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
633
+ * tree.delete(3);
634
+ * console.log(tree.has(3)); // false;
635
+ * console.log(tree.size); // 4;
487
636
  */
488
637
  delete(keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
638
+ /**
639
+ * Search by predicate
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+
649
+
650
+
651
+
652
+
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+
663
+ * @example
664
+ * // Search by predicate
665
+ * const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
666
+ * const found = tree.search(n => n!.key > 5, true);
667
+ * console.log(found.length); // >= 1;
668
+ */
489
669
  search(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean): (K | undefined)[];
490
670
  search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne: boolean, callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
491
671
  /**
@@ -497,6 +677,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
497
677
  * @param [startNode=this._root] - The node to start the search from.
498
678
  * @param [iterationType=this.iterationType] - The traversal method.
499
679
  * @returns An array of matching nodes.
680
+
681
+
682
+
683
+
684
+
685
+
686
+
687
+
688
+
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+ * @example
710
+ * // Get nodes by condition
711
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
712
+ * const nodes = tree.getNodes(node => node.key > 3);
713
+ * console.log(nodes.length); // 2;
500
714
  */
501
715
  getNodes(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V>[];
502
716
  /**
@@ -507,6 +721,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
507
721
  * @param [startNode=this._root] - The node to start the search from.
508
722
  * @param [iterationType=this.iterationType] - The traversal method.
509
723
  * @returns The first matching node, or undefined if not found.
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
+ * // Get node by key
756
+ * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
757
+ * console.log(tree.getNode(2)?.value); // 'child';
510
758
  */
511
759
  getNode(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
512
760
  /**
@@ -517,6 +765,43 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
517
765
  * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
518
766
  * @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
519
767
  * @returns The associated value, or undefined.
768
+
769
+
770
+
771
+
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
+ * @example
801
+ * // Retrieve value by key
802
+ * const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
803
+ * console.log(tree.get(2)); // 'left';
804
+ * console.log(tree.get(99)); // undefined;
520
805
  */
521
806
  get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
522
807
  /**
@@ -527,11 +812,106 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
527
812
  * @param [startNode] - The node to start the search from.
528
813
  * @param [iterationType] - The traversal method.
529
814
  * @returns True if a matching node exists, false otherwise.
815
+
816
+
817
+
818
+
819
+
820
+
821
+
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
+ * @example
848
+ * // BinaryTree get and has operations
849
+ * const tree = new BinaryTree(
850
+ * [
851
+ * [5, 'five'],
852
+ * [3, 'three'],
853
+ * [7, 'seven'],
854
+ * [1, 'one'],
855
+ * [4, 'four'],
856
+ * [6, 'six'],
857
+ * [8, 'eight']
858
+ * ],
859
+ * { isMapMode: false }
860
+ * );
861
+ *
862
+ * // Check if key exists
863
+ * console.log(tree.has(5)); // true;
864
+ * console.log(tree.has(10)); // false;
865
+ *
866
+ * // Get value by key
867
+ * console.log(tree.get(3)); // 'three';
868
+ * console.log(tree.get(7)); // 'seven';
869
+ * console.log(tree.get(100)); // undefined;
870
+ *
871
+ * // Get node structure
872
+ * const node = tree.getNode(5);
873
+ * console.log(node?.key); // 5;
874
+ * console.log(node?.value); // 'five';
530
875
  */
531
876
  has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
532
877
  /**
533
878
  * Clears the tree of all nodes and values.
534
879
  * @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
880
+
881
+
882
+
883
+
884
+
885
+
886
+
887
+
888
+
889
+
890
+
891
+
892
+
893
+
894
+
895
+
896
+
897
+
898
+
899
+
900
+
901
+
902
+
903
+
904
+
905
+
906
+
907
+
908
+
909
+
910
+ * @example
911
+ * // Remove all nodes
912
+ * const tree = new BinaryTree<number>([1, 2, 3]);
913
+ * tree.clear();
914
+ * console.log(tree.isEmpty()); // true;
535
915
  */
536
916
  clear(): void;
537
917
  /**
@@ -539,6 +919,39 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
539
919
  * @remarks Time O(1), Space O(1)
540
920
  *
541
921
  * @returns True if the tree has no nodes, false otherwise.
922
+
923
+
924
+
925
+
926
+
927
+
928
+
929
+
930
+
931
+
932
+
933
+
934
+
935
+
936
+
937
+
938
+
939
+
940
+
941
+
942
+
943
+
944
+
945
+
946
+
947
+
948
+
949
+
950
+
951
+
952
+ * @example
953
+ * // Check empty
954
+ * console.log(new BinaryTree().isEmpty()); // true;
542
955
  */
543
956
  isEmpty(): boolean;
544
957
  /**
@@ -556,6 +969,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
556
969
  * @param [startNode=this._root] - The node to start checking from.
557
970
  * @param [iterationType=this.iterationType] - The traversal method.
558
971
  * @returns True if it's a valid BST, false otherwise.
972
+
973
+
974
+
975
+
976
+
977
+
978
+
979
+
980
+
981
+
982
+
983
+
984
+
985
+
986
+
987
+
988
+
989
+
990
+
991
+
992
+
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
1001
+
1002
+ * @example
1003
+ * // Check BST property
1004
+ * const tree = new BinaryTree<number>([1, 2, 3]);
1005
+ * // BinaryTree doesn't guarantee BST order
1006
+ * console.log(typeof tree.isBST()); // 'boolean';
559
1007
  */
560
1008
  isBST(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
561
1009
  /**
@@ -565,6 +1013,43 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
565
1013
  * @param dist - The node to find the depth of.
566
1014
  * @param [startNode=this._root] - The node to measure depth from (defaults to root).
567
1015
  * @returns The depth (0 if `dist` is `startNode`).
1016
+
1017
+
1018
+
1019
+
1020
+
1021
+
1022
+
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
+ * @example
1049
+ * // Get depth of a node
1050
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1051
+ * const node = tree.getNode(4);
1052
+ * console.log(tree.getDepth(node!)); // 2;
568
1053
  */
569
1054
  getDepth(dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): number;
570
1055
  /**
@@ -574,6 +1059,42 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
574
1059
  * @param [startNode=this._root] - The node to start measuring from.
575
1060
  * @param [iterationType=this.iterationType] - The traversal method.
576
1061
  * @returns The height ( -1 for an empty tree, 0 for a single-node tree).
1062
+
1063
+
1064
+
1065
+
1066
+
1067
+
1068
+
1069
+
1070
+
1071
+
1072
+
1073
+
1074
+
1075
+
1076
+
1077
+
1078
+
1079
+
1080
+
1081
+
1082
+
1083
+
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+
1090
+
1091
+
1092
+
1093
+
1094
+ * @example
1095
+ * // Get tree height
1096
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1097
+ * console.log(tree.getHeight()); // 2;
577
1098
  */
578
1099
  getHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
579
1100
  /**
@@ -607,17 +1128,232 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
607
1128
  * @returns The successor node, or null/undefined if none exists.
608
1129
  */
609
1130
  getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined;
1131
+ /**
1132
+ * Depth-first search traversal
1133
+
1134
+
1135
+
1136
+
1137
+
1138
+
1139
+
1140
+
1141
+
1142
+
1143
+
1144
+
1145
+
1146
+
1147
+
1148
+
1149
+
1150
+
1151
+
1152
+
1153
+
1154
+
1155
+
1156
+
1157
+
1158
+
1159
+
1160
+
1161
+
1162
+
1163
+
1164
+
1165
+ * @example
1166
+ * // Depth-first search traversal
1167
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1168
+ * const inOrder = tree.dfs(node => node.key, 'IN');
1169
+ * console.log(inOrder); // [4, 2, 5, 1, 3];
1170
+ */
610
1171
  dfs(): (K | undefined)[];
611
1172
  dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
612
1173
  dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[];
1174
+ /**
1175
+ * BinaryTree level-order traversal
1176
+
1177
+
1178
+
1179
+
1180
+
1181
+
1182
+
1183
+
1184
+
1185
+
1186
+
1187
+
1188
+
1189
+
1190
+
1191
+
1192
+
1193
+
1194
+
1195
+
1196
+
1197
+
1198
+
1199
+
1200
+
1201
+
1202
+
1203
+
1204
+
1205
+
1206
+
1207
+
1208
+ * @example
1209
+ * // BinaryTree level-order traversal
1210
+ * const tree = new BinaryTree([
1211
+ * [1, 'one'],
1212
+ * [2, 'two'],
1213
+ * [3, 'three'],
1214
+ * [4, 'four'],
1215
+ * [5, 'five'],
1216
+ * [6, 'six'],
1217
+ * [7, 'seven']
1218
+ * ]);
1219
+ *
1220
+ * // Binary tree maintains level-order insertion
1221
+ * // Complete binary tree structure
1222
+ * console.log(tree.size); // 7;
1223
+ *
1224
+ * // Verify all keys are present
1225
+ * console.log(tree.has(1)); // true;
1226
+ * console.log(tree.has(4)); // true;
1227
+ * console.log(tree.has(7)); // true;
1228
+ *
1229
+ * // Iterate through tree
1230
+ * const keys: number[] = [];
1231
+ * for (const [key] of tree) {
1232
+ * keys.push(key);
1233
+ * }
1234
+ * console.log(keys.length); // 7;
1235
+ */
613
1236
  bfs(): (K | undefined)[];
614
1237
  bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
615
1238
  bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
1239
+ /**
1240
+ * Get leaf nodes
1241
+
1242
+
1243
+
1244
+
1245
+
1246
+
1247
+
1248
+
1249
+
1250
+
1251
+
1252
+
1253
+
1254
+
1255
+
1256
+
1257
+
1258
+
1259
+
1260
+
1261
+
1262
+
1263
+
1264
+
1265
+
1266
+
1267
+
1268
+
1269
+
1270
+
1271
+ * @example
1272
+ * // Get leaf nodes
1273
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1274
+ * const leafKeys = tree.leaves(node => node.key);
1275
+ * console.log(leafKeys.length); // > 0;
1276
+ */
616
1277
  leaves(): (K | undefined)[];
617
1278
  leaves<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
1279
+ /**
1280
+ * Level-order grouping
1281
+
1282
+
1283
+
1284
+
1285
+
1286
+
1287
+
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1295
+
1296
+
1297
+
1298
+
1299
+
1300
+
1301
+
1302
+
1303
+
1304
+
1305
+
1306
+
1307
+
1308
+
1309
+
1310
+ * @example
1311
+ * // Level-order grouping
1312
+ * const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
1313
+ * const levels = tree.listLevels(node => node.key);
1314
+ * console.log(levels[0]); // [1];
1315
+ * console.log(levels[1].sort()); // [2, 3];
1316
+ */
618
1317
  listLevels(): (K | undefined)[][];
619
1318
  listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
620
1319
  listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
1320
+ /**
1321
+ * Morris traversal (O(1) space)
1322
+
1323
+
1324
+
1325
+
1326
+
1327
+
1328
+
1329
+
1330
+
1331
+
1332
+
1333
+
1334
+
1335
+
1336
+
1337
+
1338
+
1339
+
1340
+
1341
+
1342
+
1343
+
1344
+
1345
+
1346
+
1347
+
1348
+
1349
+
1350
+
1351
+ * @example
1352
+ * // Morris traversal (O(1) space)
1353
+ * const tree = new BinaryTree<number>([1, 2, 3]);
1354
+ * const result = tree.morris(node => node.key, 'IN');
1355
+ * console.log(result.length); // 3;
1356
+ */
621
1357
  morris(): (K | undefined)[];
622
1358
  morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
623
1359
  /**
@@ -625,6 +1361,42 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
625
1361
  * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `set`, and `set` is O(M)). Space O(N) for the new tree and the BFS queue.
626
1362
  *
627
1363
  * @returns A new, cloned instance of the tree.
1364
+
1365
+
1366
+
1367
+
1368
+
1369
+
1370
+
1371
+
1372
+
1373
+
1374
+
1375
+
1376
+
1377
+
1378
+
1379
+
1380
+
1381
+
1382
+
1383
+
1384
+
1385
+
1386
+
1387
+
1388
+
1389
+
1390
+
1391
+
1392
+
1393
+
1394
+ * @example
1395
+ * // Deep copy
1396
+ * const tree = new BinaryTree<number>([1, 2, 3]);
1397
+ * const copy = tree.clone();
1398
+ * copy.delete(1);
1399
+ * console.log(tree.has(1)); // true;
628
1400
  */
629
1401
  clone(): this;
630
1402
  /**
@@ -634,6 +1406,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
634
1406
  * @param predicate - A function to test each [key, value] pair.
635
1407
  * @param [thisArg] - `this` context for the predicate.
636
1408
  * @returns A new, filtered tree.
1409
+
1410
+
1411
+
1412
+
1413
+
1414
+
1415
+
1416
+
1417
+
1418
+
1419
+
1420
+
1421
+
1422
+
1423
+
1424
+
1425
+
1426
+
1427
+
1428
+
1429
+
1430
+
1431
+
1432
+
1433
+
1434
+
1435
+
1436
+
1437
+
1438
+
1439
+ * @example
1440
+ * // Filter nodes by condition
1441
+ * const tree = new BinaryTree<number>([1, 2, 3, 4]);
1442
+ * const result = tree.filter((_, key) => key > 2);
1443
+ * console.log(result.size); // 2;
637
1444
  */
638
1445
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
639
1446
  /**
@@ -647,6 +1454,41 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
647
1454
  * @param [options] - Options for the new tree.
648
1455
  * @param [thisArg] - `this` context for the callback.
649
1456
  * @returns A new, mapped tree.
1457
+
1458
+
1459
+
1460
+
1461
+
1462
+
1463
+
1464
+
1465
+
1466
+
1467
+
1468
+
1469
+
1470
+
1471
+
1472
+
1473
+
1474
+
1475
+
1476
+
1477
+
1478
+
1479
+
1480
+
1481
+
1482
+
1483
+
1484
+
1485
+
1486
+
1487
+ * @example
1488
+ * // Transform to new tree
1489
+ * const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
1490
+ * const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
1491
+ * console.log([...mapped.values()]); // contains 11;
650
1492
  */
651
1493
  map<MK = K, MV = V, MR = any>(cb: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BinaryTree<MK, MV, MR>;
652
1494
  /**
@@ -664,6 +1506,40 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
664
1506
  *
665
1507
  * @param [options] - Options to control the output.
666
1508
  * @param [startNode=this._root] - The node to start printing from.
1509
+
1510
+
1511
+
1512
+
1513
+
1514
+
1515
+
1516
+
1517
+
1518
+
1519
+
1520
+
1521
+
1522
+
1523
+
1524
+
1525
+
1526
+
1527
+
1528
+
1529
+
1530
+
1531
+
1532
+
1533
+
1534
+
1535
+
1536
+
1537
+
1538
+
1539
+ * @example
1540
+ * // Display tree
1541
+ * const tree = new BinaryTree<number>([1, 2, 3]);
1542
+ * expect(() => tree.print()).not.toThrow();
667
1543
  */
668
1544
  print(options?: BinaryTreePrintOptions, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
669
1545
  protected _dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRight?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldProcessRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean): ReturnType<C>[];
@@ -785,7 +1661,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
785
1661
  * @param p - The item to check.
786
1662
  * @returns True if it's a function.
787
1663
  */
788
- protected _isPredicate(p: any): p is NodePredicate<BinaryTreeNode<K, V>>;
1664
+ protected _isPredicate(p: unknown): p is NodePredicate<BinaryTreeNode<K, V>>;
789
1665
  /**
790
1666
  * (Protected) Extracts the key from a key, node, or entry.
791
1667
  * @remarks Time O(1)