binary-tree-typed 2.4.5 → 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -84
- package/dist/cjs/index.cjs +1476 -404
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +1473 -401
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +1476 -404
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +1473 -401
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +380 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +487 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +956 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +816 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +610 -31
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +326 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3781 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3607 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2874 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3528 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +429 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +393 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +473 -89
- package/dist/types/data-structures/heap/heap.d.ts +581 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +646 -47
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +596 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +793 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +499 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +593 -71
- package/dist/types/data-structures/queue/queue.d.ts +463 -42
- package/dist/types/data-structures/stack/stack.d.ts +384 -32
- package/dist/types/data-structures/trie/trie.d.ts +470 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/binary-tree-typed.js +1469 -397
- package/dist/umd/binary-tree-typed.js.map +1 -1
- package/dist/umd/binary-tree-typed.min.js +5 -5
- package/dist/umd/binary-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
|
@@ -205,84 +205,6 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
205
205
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
206
206
|
*
|
|
207
207
|
* @example
|
|
208
|
-
* // basic BinaryTree creation and insertion
|
|
209
|
-
* // Create a BinaryTree with entries
|
|
210
|
-
* const entries: [number, string][] = [
|
|
211
|
-
* [6, 'six'],
|
|
212
|
-
* [1, 'one'],
|
|
213
|
-
* [2, 'two'],
|
|
214
|
-
* [7, 'seven'],
|
|
215
|
-
* [5, 'five'],
|
|
216
|
-
* [3, 'three'],
|
|
217
|
-
* [4, 'four'],
|
|
218
|
-
* [9, 'nine'],
|
|
219
|
-
* [8, 'eight']
|
|
220
|
-
* ];
|
|
221
|
-
*
|
|
222
|
-
* const tree = new BinaryTree(entries);
|
|
223
|
-
*
|
|
224
|
-
* // Verify size
|
|
225
|
-
* console.log(tree.size); // 9;
|
|
226
|
-
*
|
|
227
|
-
* // Add new element
|
|
228
|
-
* tree.set(10, 'ten');
|
|
229
|
-
* console.log(tree.size); // 10;
|
|
230
|
-
* @example
|
|
231
|
-
* // BinaryTree get and has operations
|
|
232
|
-
* const tree = new BinaryTree(
|
|
233
|
-
* [
|
|
234
|
-
* [5, 'five'],
|
|
235
|
-
* [3, 'three'],
|
|
236
|
-
* [7, 'seven'],
|
|
237
|
-
* [1, 'one'],
|
|
238
|
-
* [4, 'four'],
|
|
239
|
-
* [6, 'six'],
|
|
240
|
-
* [8, 'eight']
|
|
241
|
-
* ],
|
|
242
|
-
* { isMapMode: false }
|
|
243
|
-
* );
|
|
244
|
-
*
|
|
245
|
-
* // Check if key exists
|
|
246
|
-
* console.log(tree.has(5)); // true;
|
|
247
|
-
* console.log(tree.has(10)); // false;
|
|
248
|
-
*
|
|
249
|
-
* // Get value by key
|
|
250
|
-
* console.log(tree.get(3)); // 'three';
|
|
251
|
-
* console.log(tree.get(7)); // 'seven';
|
|
252
|
-
* console.log(tree.get(100)); // undefined;
|
|
253
|
-
*
|
|
254
|
-
* // Get node structure
|
|
255
|
-
* const node = tree.getNode(5);
|
|
256
|
-
* console.log(node?.key); // 5;
|
|
257
|
-
* console.log(node?.value); // 'five';
|
|
258
|
-
* @example
|
|
259
|
-
* // BinaryTree level-order traversal
|
|
260
|
-
* const tree = new BinaryTree([
|
|
261
|
-
* [1, 'one'],
|
|
262
|
-
* [2, 'two'],
|
|
263
|
-
* [3, 'three'],
|
|
264
|
-
* [4, 'four'],
|
|
265
|
-
* [5, 'five'],
|
|
266
|
-
* [6, 'six'],
|
|
267
|
-
* [7, 'seven']
|
|
268
|
-
* ]);
|
|
269
|
-
*
|
|
270
|
-
* // Binary tree maintains level-order insertion
|
|
271
|
-
* // Complete binary tree structure
|
|
272
|
-
* console.log(tree.size); // 7;
|
|
273
|
-
*
|
|
274
|
-
* // Verify all keys are present
|
|
275
|
-
* console.log(tree.has(1)); // true;
|
|
276
|
-
* console.log(tree.has(4)); // true;
|
|
277
|
-
* console.log(tree.has(7)); // true;
|
|
278
|
-
*
|
|
279
|
-
* // Iterate through tree
|
|
280
|
-
* const keys: number[] = [];
|
|
281
|
-
* for (const [key] of tree) {
|
|
282
|
-
* keys.push(key);
|
|
283
|
-
* }
|
|
284
|
-
* console.log(keys.length); // 7;
|
|
285
|
-
* @example
|
|
286
208
|
* // determine loan approval using a decision tree
|
|
287
209
|
* // Decision tree structure
|
|
288
210
|
* const loanDecisionTree = new BinaryTree<string>(
|
|
@@ -633,7 +555,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
633
555
|
* @param key - The key to validate.
|
|
634
556
|
* @returns True if the key is valid, false otherwise.
|
|
635
557
|
*/
|
|
636
|
-
isValidKey(key:
|
|
558
|
+
isValidKey(key: unknown): key is K {
|
|
637
559
|
if (key === null) return true;
|
|
638
560
|
return isComparable(key);
|
|
639
561
|
}
|
|
@@ -644,6 +566,41 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
644
566
|
*
|
|
645
567
|
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
646
568
|
* @returns True if the addition was successful, false otherwise.
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
* @example
|
|
597
|
+
* // Add a single node
|
|
598
|
+
* const tree = new BinaryTree<number>();
|
|
599
|
+
* tree.add(1);
|
|
600
|
+
* tree.add(2);
|
|
601
|
+
* tree.add(3);
|
|
602
|
+
* console.log(tree.size); // 3;
|
|
603
|
+
* console.log(tree.has(1)); // true;
|
|
647
604
|
*/
|
|
648
605
|
add(
|
|
649
606
|
keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
@@ -658,6 +615,61 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
658
615
|
* @param keyNodeOrEntry - The key, node, or entry to set or update.
|
|
659
616
|
* @param [value] - The value, if providing just a key.
|
|
660
617
|
* @returns True if the addition was successful, false otherwise.
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
|
|
650
|
+
* @example
|
|
651
|
+
* // basic BinaryTree creation and insertion
|
|
652
|
+
* // Create a BinaryTree with entries
|
|
653
|
+
* const entries: [number, string][] = [
|
|
654
|
+
* [6, 'six'],
|
|
655
|
+
* [1, 'one'],
|
|
656
|
+
* [2, 'two'],
|
|
657
|
+
* [7, 'seven'],
|
|
658
|
+
* [5, 'five'],
|
|
659
|
+
* [3, 'three'],
|
|
660
|
+
* [4, 'four'],
|
|
661
|
+
* [9, 'nine'],
|
|
662
|
+
* [8, 'eight']
|
|
663
|
+
* ];
|
|
664
|
+
*
|
|
665
|
+
* const tree = new BinaryTree(entries);
|
|
666
|
+
*
|
|
667
|
+
* // Verify size
|
|
668
|
+
* console.log(tree.size); // 9;
|
|
669
|
+
*
|
|
670
|
+
* // Add new element
|
|
671
|
+
* tree.set(10, 'ten');
|
|
672
|
+
* console.log(tree.size); // 10;
|
|
661
673
|
*/
|
|
662
674
|
set(
|
|
663
675
|
keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -721,6 +733,41 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
721
733
|
*
|
|
722
734
|
* @param keysNodesEntriesOrRaws - An iterable of items to set.
|
|
723
735
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
* @example
|
|
767
|
+
* // Bulk add
|
|
768
|
+
* const tree = new BinaryTree<number>();
|
|
769
|
+
* tree.addMany([1, 2, 3, 4, 5]);
|
|
770
|
+
* console.log(tree.size); // 5;
|
|
724
771
|
*/
|
|
725
772
|
addMany(
|
|
726
773
|
keysNodesEntriesOrRaws: Iterable<
|
|
@@ -737,6 +784,34 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
737
784
|
* @param keysNodesEntriesOrRaws - An iterable of items to set or update.
|
|
738
785
|
* @param [values] - An optional parallel iterable of values.
|
|
739
786
|
* @returns An array of booleans indicating the success of each individual `set` operation.
|
|
787
|
+
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
* @example
|
|
811
|
+
* // Set multiple entries
|
|
812
|
+
* const tree = new BinaryTree<number, string>();
|
|
813
|
+
* tree.setMany([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
814
|
+
* console.log(tree.size); // 3;
|
|
740
815
|
*/
|
|
741
816
|
setMany(
|
|
742
817
|
keysNodesEntriesOrRaws: Iterable<
|
|
@@ -772,6 +847,42 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
772
847
|
* @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`).
|
|
773
848
|
*
|
|
774
849
|
* @param anotherTree - The tree to merge.
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
* @example
|
|
881
|
+
* // Combine trees
|
|
882
|
+
* const t1 = new BinaryTree<number>([1, 2]);
|
|
883
|
+
* const t2 = new BinaryTree<number>([3, 4]);
|
|
884
|
+
* t1.merge(t2);
|
|
885
|
+
* console.log(t1.size); // 4;
|
|
775
886
|
*/
|
|
776
887
|
merge(anotherTree: BinaryTree<K, V, R>) {
|
|
777
888
|
this.setMany(anotherTree, []);
|
|
@@ -800,6 +911,44 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
800
911
|
*
|
|
801
912
|
* @param keyNodeEntryRawOrPredicate - The node to delete.
|
|
802
913
|
* @returns An array containing deletion results (for compatibility with self-balancing trees).
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
|
|
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
|
+
* @example
|
|
947
|
+
* // Remove a node
|
|
948
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
949
|
+
* tree.delete(3);
|
|
950
|
+
* console.log(tree.has(3)); // false;
|
|
951
|
+
* console.log(tree.size); // 4;
|
|
803
952
|
*/
|
|
804
953
|
delete(
|
|
805
954
|
keyNodeEntryRawOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
|
|
@@ -865,6 +1014,37 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
865
1014
|
return deletedResult;
|
|
866
1015
|
}
|
|
867
1016
|
|
|
1017
|
+
/**
|
|
1018
|
+
* Search by predicate
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
* @example
|
|
1043
|
+
* // Search by predicate
|
|
1044
|
+
* const tree = new BinaryTree<number>([5, 3, 7, 1, 9]);
|
|
1045
|
+
* const found = tree.search(n => n!.key > 5, true);
|
|
1046
|
+
* console.log(found.length); // >= 1;
|
|
1047
|
+
*/
|
|
868
1048
|
search(
|
|
869
1049
|
keyNodeEntryOrPredicate:
|
|
870
1050
|
| K
|
|
@@ -963,6 +1143,40 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
963
1143
|
* @param [startNode=this._root] - The node to start the search from.
|
|
964
1144
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
965
1145
|
* @returns An array of matching nodes.
|
|
1146
|
+
|
|
1147
|
+
|
|
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
|
+
* @example
|
|
1176
|
+
* // Get nodes by condition
|
|
1177
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1178
|
+
* const nodes = tree.getNodes(node => node.key > 3);
|
|
1179
|
+
* console.log(nodes.length); // 2;
|
|
966
1180
|
*/
|
|
967
1181
|
getNodes(
|
|
968
1182
|
keyNodeEntryOrPredicate:
|
|
@@ -1000,6 +1214,40 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1000
1214
|
* @param [startNode=this._root] - The node to start the search from.
|
|
1001
1215
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
1002
1216
|
* @returns The first matching node, or undefined if not found.
|
|
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
|
+
* @example
|
|
1248
|
+
* // Get node by key
|
|
1249
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'child']]);
|
|
1250
|
+
* console.log(tree.getNode(2)?.value); // 'child';
|
|
1003
1251
|
*/
|
|
1004
1252
|
getNode(
|
|
1005
1253
|
keyNodeEntryOrPredicate:
|
|
@@ -1030,6 +1278,43 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1030
1278
|
* @param [startNode=this._root] - The node to start searching from (if not in Map mode).
|
|
1031
1279
|
* @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
|
|
1032
1280
|
* @returns The associated value, or undefined.
|
|
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
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
* @example
|
|
1314
|
+
* // Retrieve value by key
|
|
1315
|
+
* const tree = new BinaryTree<number, string>([[1, 'root'], [2, 'left'], [3, 'right']]);
|
|
1316
|
+
* console.log(tree.get(2)); // 'left';
|
|
1317
|
+
* console.log(tree.get(99)); // undefined;
|
|
1033
1318
|
*/
|
|
1034
1319
|
override get(
|
|
1035
1320
|
keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -1052,6 +1337,66 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1052
1337
|
* @param [startNode] - The node to start the search from.
|
|
1053
1338
|
* @param [iterationType] - The traversal method.
|
|
1054
1339
|
* @returns True if a matching node exists, false otherwise.
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
* @example
|
|
1373
|
+
* // BinaryTree get and has operations
|
|
1374
|
+
* const tree = new BinaryTree(
|
|
1375
|
+
* [
|
|
1376
|
+
* [5, 'five'],
|
|
1377
|
+
* [3, 'three'],
|
|
1378
|
+
* [7, 'seven'],
|
|
1379
|
+
* [1, 'one'],
|
|
1380
|
+
* [4, 'four'],
|
|
1381
|
+
* [6, 'six'],
|
|
1382
|
+
* [8, 'eight']
|
|
1383
|
+
* ],
|
|
1384
|
+
* { isMapMode: false }
|
|
1385
|
+
* );
|
|
1386
|
+
*
|
|
1387
|
+
* // Check if key exists
|
|
1388
|
+
* console.log(tree.has(5)); // true;
|
|
1389
|
+
* console.log(tree.has(10)); // false;
|
|
1390
|
+
*
|
|
1391
|
+
* // Get value by key
|
|
1392
|
+
* console.log(tree.get(3)); // 'three';
|
|
1393
|
+
* console.log(tree.get(7)); // 'seven';
|
|
1394
|
+
* console.log(tree.get(100)); // undefined;
|
|
1395
|
+
*
|
|
1396
|
+
* // Get node structure
|
|
1397
|
+
* const node = tree.getNode(5);
|
|
1398
|
+
* console.log(node?.key); // 5;
|
|
1399
|
+
* console.log(node?.value); // 'five';
|
|
1055
1400
|
*/
|
|
1056
1401
|
override has(
|
|
1057
1402
|
keyNodeEntryOrPredicate?:
|
|
@@ -1089,6 +1434,41 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1089
1434
|
/**
|
|
1090
1435
|
* Clears the tree of all nodes and values.
|
|
1091
1436
|
* @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
|
|
1437
|
+
|
|
1438
|
+
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
|
|
1445
|
+
|
|
1446
|
+
|
|
1447
|
+
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
|
|
1461
|
+
|
|
1462
|
+
|
|
1463
|
+
|
|
1464
|
+
|
|
1465
|
+
|
|
1466
|
+
|
|
1467
|
+
* @example
|
|
1468
|
+
* // Remove all nodes
|
|
1469
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
1470
|
+
* tree.clear();
|
|
1471
|
+
* console.log(tree.isEmpty()); // true;
|
|
1092
1472
|
*/
|
|
1093
1473
|
clear() {
|
|
1094
1474
|
this._clearNodes();
|
|
@@ -1100,6 +1480,39 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1100
1480
|
* @remarks Time O(1), Space O(1)
|
|
1101
1481
|
*
|
|
1102
1482
|
* @returns True if the tree has no nodes, false otherwise.
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
|
|
1491
|
+
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
|
|
1495
|
+
|
|
1496
|
+
|
|
1497
|
+
|
|
1498
|
+
|
|
1499
|
+
|
|
1500
|
+
|
|
1501
|
+
|
|
1502
|
+
|
|
1503
|
+
|
|
1504
|
+
|
|
1505
|
+
|
|
1506
|
+
|
|
1507
|
+
|
|
1508
|
+
|
|
1509
|
+
|
|
1510
|
+
|
|
1511
|
+
|
|
1512
|
+
|
|
1513
|
+
* @example
|
|
1514
|
+
* // Check empty
|
|
1515
|
+
* console.log(new BinaryTree().isEmpty()); // true;
|
|
1103
1516
|
*/
|
|
1104
1517
|
isEmpty(): boolean {
|
|
1105
1518
|
return this._size === 0;
|
|
@@ -1125,6 +1538,41 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1125
1538
|
* @param [startNode=this._root] - The node to start checking from.
|
|
1126
1539
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
1127
1540
|
* @returns True if it's a valid BST, false otherwise.
|
|
1541
|
+
|
|
1542
|
+
|
|
1543
|
+
|
|
1544
|
+
|
|
1545
|
+
|
|
1546
|
+
|
|
1547
|
+
|
|
1548
|
+
|
|
1549
|
+
|
|
1550
|
+
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
|
|
1558
|
+
|
|
1559
|
+
|
|
1560
|
+
|
|
1561
|
+
|
|
1562
|
+
|
|
1563
|
+
|
|
1564
|
+
|
|
1565
|
+
|
|
1566
|
+
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
|
|
1571
|
+
* @example
|
|
1572
|
+
* // Check BST property
|
|
1573
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
1574
|
+
* // BinaryTree doesn't guarantee BST order
|
|
1575
|
+
* console.log(typeof tree.isBST()); // 'boolean';
|
|
1128
1576
|
*/
|
|
1129
1577
|
isBST(
|
|
1130
1578
|
startNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
@@ -1176,6 +1624,43 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1176
1624
|
* @param dist - The node to find the depth of.
|
|
1177
1625
|
* @param [startNode=this._root] - The node to measure depth from (defaults to root).
|
|
1178
1626
|
* @returns The depth (0 if `dist` is `startNode`).
|
|
1627
|
+
|
|
1628
|
+
|
|
1629
|
+
|
|
1630
|
+
|
|
1631
|
+
|
|
1632
|
+
|
|
1633
|
+
|
|
1634
|
+
|
|
1635
|
+
|
|
1636
|
+
|
|
1637
|
+
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
|
|
1641
|
+
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
|
|
1646
|
+
|
|
1647
|
+
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
|
|
1653
|
+
|
|
1654
|
+
|
|
1655
|
+
|
|
1656
|
+
|
|
1657
|
+
|
|
1658
|
+
|
|
1659
|
+
* @example
|
|
1660
|
+
* // Get depth of a node
|
|
1661
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1662
|
+
* const node = tree.getNode(4);
|
|
1663
|
+
* console.log(tree.getDepth(node!)); // 2;
|
|
1179
1664
|
*/
|
|
1180
1665
|
getDepth(
|
|
1181
1666
|
dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
@@ -1201,6 +1686,42 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1201
1686
|
* @param [startNode=this._root] - The node to start measuring from.
|
|
1202
1687
|
* @param [iterationType=this.iterationType] - The traversal method.
|
|
1203
1688
|
* @returns The height ( -1 for an empty tree, 0 for a single-node tree).
|
|
1689
|
+
|
|
1690
|
+
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
|
|
1694
|
+
|
|
1695
|
+
|
|
1696
|
+
|
|
1697
|
+
|
|
1698
|
+
|
|
1699
|
+
|
|
1700
|
+
|
|
1701
|
+
|
|
1702
|
+
|
|
1703
|
+
|
|
1704
|
+
|
|
1705
|
+
|
|
1706
|
+
|
|
1707
|
+
|
|
1708
|
+
|
|
1709
|
+
|
|
1710
|
+
|
|
1711
|
+
|
|
1712
|
+
|
|
1713
|
+
|
|
1714
|
+
|
|
1715
|
+
|
|
1716
|
+
|
|
1717
|
+
|
|
1718
|
+
|
|
1719
|
+
|
|
1720
|
+
|
|
1721
|
+
* @example
|
|
1722
|
+
* // Get tree height
|
|
1723
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
1724
|
+
* console.log(tree.getHeight()); // 2;
|
|
1204
1725
|
*/
|
|
1205
1726
|
getHeight(
|
|
1206
1727
|
startNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
@@ -1466,6 +1987,46 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1466
1987
|
return y;
|
|
1467
1988
|
}
|
|
1468
1989
|
|
|
1990
|
+
/**
|
|
1991
|
+
* Depth-first search traversal
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
|
|
2003
|
+
|
|
2004
|
+
|
|
2005
|
+
|
|
2006
|
+
|
|
2007
|
+
|
|
2008
|
+
|
|
2009
|
+
|
|
2010
|
+
|
|
2011
|
+
|
|
2012
|
+
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
|
|
2016
|
+
|
|
2017
|
+
|
|
2018
|
+
|
|
2019
|
+
|
|
2020
|
+
|
|
2021
|
+
|
|
2022
|
+
|
|
2023
|
+
|
|
2024
|
+
* @example
|
|
2025
|
+
* // Depth-first search traversal
|
|
2026
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2027
|
+
* const inOrder = tree.dfs(node => node.key, 'IN');
|
|
2028
|
+
* console.log(inOrder); // [4, 2, 5, 1, 3];
|
|
2029
|
+
*/
|
|
1469
2030
|
dfs(): (K | undefined)[];
|
|
1470
2031
|
|
|
1471
2032
|
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(
|
|
@@ -1511,6 +2072,68 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1511
2072
|
return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
|
|
1512
2073
|
}
|
|
1513
2074
|
|
|
2075
|
+
/**
|
|
2076
|
+
* BinaryTree level-order traversal
|
|
2077
|
+
|
|
2078
|
+
|
|
2079
|
+
|
|
2080
|
+
|
|
2081
|
+
|
|
2082
|
+
|
|
2083
|
+
|
|
2084
|
+
|
|
2085
|
+
|
|
2086
|
+
|
|
2087
|
+
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
|
|
2091
|
+
|
|
2092
|
+
|
|
2093
|
+
|
|
2094
|
+
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
|
|
2098
|
+
|
|
2099
|
+
|
|
2100
|
+
|
|
2101
|
+
|
|
2102
|
+
|
|
2103
|
+
|
|
2104
|
+
|
|
2105
|
+
|
|
2106
|
+
|
|
2107
|
+
|
|
2108
|
+
|
|
2109
|
+
* @example
|
|
2110
|
+
* // BinaryTree level-order traversal
|
|
2111
|
+
* const tree = new BinaryTree([
|
|
2112
|
+
* [1, 'one'],
|
|
2113
|
+
* [2, 'two'],
|
|
2114
|
+
* [3, 'three'],
|
|
2115
|
+
* [4, 'four'],
|
|
2116
|
+
* [5, 'five'],
|
|
2117
|
+
* [6, 'six'],
|
|
2118
|
+
* [7, 'seven']
|
|
2119
|
+
* ]);
|
|
2120
|
+
*
|
|
2121
|
+
* // Binary tree maintains level-order insertion
|
|
2122
|
+
* // Complete binary tree structure
|
|
2123
|
+
* console.log(tree.size); // 7;
|
|
2124
|
+
*
|
|
2125
|
+
* // Verify all keys are present
|
|
2126
|
+
* console.log(tree.has(1)); // true;
|
|
2127
|
+
* console.log(tree.has(4)); // true;
|
|
2128
|
+
* console.log(tree.has(7)); // true;
|
|
2129
|
+
*
|
|
2130
|
+
* // Iterate through tree
|
|
2131
|
+
* const keys: number[] = [];
|
|
2132
|
+
* for (const [key] of tree) {
|
|
2133
|
+
* keys.push(key);
|
|
2134
|
+
* }
|
|
2135
|
+
* console.log(keys.length); // 7;
|
|
2136
|
+
*/
|
|
1514
2137
|
bfs(): (K | undefined)[];
|
|
1515
2138
|
|
|
1516
2139
|
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(
|
|
@@ -1595,6 +2218,44 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1595
2218
|
return ans;
|
|
1596
2219
|
}
|
|
1597
2220
|
|
|
2221
|
+
/**
|
|
2222
|
+
* Get leaf nodes
|
|
2223
|
+
|
|
2224
|
+
|
|
2225
|
+
|
|
2226
|
+
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
|
|
2230
|
+
|
|
2231
|
+
|
|
2232
|
+
|
|
2233
|
+
|
|
2234
|
+
|
|
2235
|
+
|
|
2236
|
+
|
|
2237
|
+
|
|
2238
|
+
|
|
2239
|
+
|
|
2240
|
+
|
|
2241
|
+
|
|
2242
|
+
|
|
2243
|
+
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
|
|
2247
|
+
|
|
2248
|
+
|
|
2249
|
+
|
|
2250
|
+
|
|
2251
|
+
|
|
2252
|
+
|
|
2253
|
+
* @example
|
|
2254
|
+
* // Get leaf nodes
|
|
2255
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2256
|
+
* const leafKeys = tree.leaves(node => node.key);
|
|
2257
|
+
* console.log(leafKeys.length); // > 0;
|
|
2258
|
+
*/
|
|
1598
2259
|
leaves(): (K | undefined)[];
|
|
1599
2260
|
|
|
1600
2261
|
leaves<C extends NodeCallback<BinaryTreeNode<K, V>>>(
|
|
@@ -1655,6 +2316,44 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1655
2316
|
return leaves;
|
|
1656
2317
|
}
|
|
1657
2318
|
|
|
2319
|
+
/**
|
|
2320
|
+
* Level-order grouping
|
|
2321
|
+
|
|
2322
|
+
|
|
2323
|
+
|
|
2324
|
+
|
|
2325
|
+
|
|
2326
|
+
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
|
|
2330
|
+
|
|
2331
|
+
|
|
2332
|
+
|
|
2333
|
+
|
|
2334
|
+
|
|
2335
|
+
|
|
2336
|
+
|
|
2337
|
+
|
|
2338
|
+
|
|
2339
|
+
|
|
2340
|
+
|
|
2341
|
+
|
|
2342
|
+
|
|
2343
|
+
|
|
2344
|
+
|
|
2345
|
+
|
|
2346
|
+
|
|
2347
|
+
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
* @example
|
|
2351
|
+
* // Level-order grouping
|
|
2352
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4, 5]);
|
|
2353
|
+
* const levels = tree.listLevels(node => node.key);
|
|
2354
|
+
* console.log(levels[0]); // [1];
|
|
2355
|
+
* console.log(levels[1].sort()); // [2, 3];
|
|
2356
|
+
*/
|
|
1658
2357
|
listLevels(): (K | undefined)[][];
|
|
1659
2358
|
|
|
1660
2359
|
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(
|
|
@@ -1732,6 +2431,43 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1732
2431
|
return levelsNodes;
|
|
1733
2432
|
}
|
|
1734
2433
|
|
|
2434
|
+
/**
|
|
2435
|
+
* Morris traversal (O(1) space)
|
|
2436
|
+
|
|
2437
|
+
|
|
2438
|
+
|
|
2439
|
+
|
|
2440
|
+
|
|
2441
|
+
|
|
2442
|
+
|
|
2443
|
+
|
|
2444
|
+
|
|
2445
|
+
|
|
2446
|
+
|
|
2447
|
+
|
|
2448
|
+
|
|
2449
|
+
|
|
2450
|
+
|
|
2451
|
+
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
|
|
2455
|
+
|
|
2456
|
+
|
|
2457
|
+
|
|
2458
|
+
|
|
2459
|
+
|
|
2460
|
+
|
|
2461
|
+
|
|
2462
|
+
|
|
2463
|
+
|
|
2464
|
+
|
|
2465
|
+
* @example
|
|
2466
|
+
* // Morris traversal (O(1) space)
|
|
2467
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2468
|
+
* const result = tree.morris(node => node.key, 'IN');
|
|
2469
|
+
* console.log(result.length); // 3;
|
|
2470
|
+
*/
|
|
1735
2471
|
morris(): (K | undefined)[];
|
|
1736
2472
|
|
|
1737
2473
|
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(
|
|
@@ -1855,6 +2591,42 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1855
2591
|
* @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.
|
|
1856
2592
|
*
|
|
1857
2593
|
* @returns A new, cloned instance of the tree.
|
|
2594
|
+
|
|
2595
|
+
|
|
2596
|
+
|
|
2597
|
+
|
|
2598
|
+
|
|
2599
|
+
|
|
2600
|
+
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
|
|
2604
|
+
|
|
2605
|
+
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
|
|
2609
|
+
|
|
2610
|
+
|
|
2611
|
+
|
|
2612
|
+
|
|
2613
|
+
|
|
2614
|
+
|
|
2615
|
+
|
|
2616
|
+
|
|
2617
|
+
|
|
2618
|
+
|
|
2619
|
+
|
|
2620
|
+
|
|
2621
|
+
|
|
2622
|
+
|
|
2623
|
+
|
|
2624
|
+
* @example
|
|
2625
|
+
* // Deep copy
|
|
2626
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2627
|
+
* const copy = tree.clone();
|
|
2628
|
+
* copy.delete(1);
|
|
2629
|
+
* console.log(tree.has(1)); // true;
|
|
1858
2630
|
*/
|
|
1859
2631
|
clone(): this {
|
|
1860
2632
|
const out = this._createInstance<K, V, R>();
|
|
@@ -1869,6 +2641,41 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1869
2641
|
* @param predicate - A function to test each [key, value] pair.
|
|
1870
2642
|
* @param [thisArg] - `this` context for the predicate.
|
|
1871
2643
|
* @returns A new, filtered tree.
|
|
2644
|
+
|
|
2645
|
+
|
|
2646
|
+
|
|
2647
|
+
|
|
2648
|
+
|
|
2649
|
+
|
|
2650
|
+
|
|
2651
|
+
|
|
2652
|
+
|
|
2653
|
+
|
|
2654
|
+
|
|
2655
|
+
|
|
2656
|
+
|
|
2657
|
+
|
|
2658
|
+
|
|
2659
|
+
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
|
|
2663
|
+
|
|
2664
|
+
|
|
2665
|
+
|
|
2666
|
+
|
|
2667
|
+
|
|
2668
|
+
|
|
2669
|
+
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
|
|
2673
|
+
|
|
2674
|
+
* @example
|
|
2675
|
+
* // Filter nodes by condition
|
|
2676
|
+
* const tree = new BinaryTree<number>([1, 2, 3, 4]);
|
|
2677
|
+
* const result = tree.filter((_, key) => key > 2);
|
|
2678
|
+
* console.log(result.size); // 2;
|
|
1872
2679
|
*/
|
|
1873
2680
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this {
|
|
1874
2681
|
const out = this._createInstance<K, V, R>();
|
|
@@ -1888,6 +2695,41 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1888
2695
|
* @param [options] - Options for the new tree.
|
|
1889
2696
|
* @param [thisArg] - `this` context for the callback.
|
|
1890
2697
|
* @returns A new, mapped tree.
|
|
2698
|
+
|
|
2699
|
+
|
|
2700
|
+
|
|
2701
|
+
|
|
2702
|
+
|
|
2703
|
+
|
|
2704
|
+
|
|
2705
|
+
|
|
2706
|
+
|
|
2707
|
+
|
|
2708
|
+
|
|
2709
|
+
|
|
2710
|
+
|
|
2711
|
+
|
|
2712
|
+
|
|
2713
|
+
|
|
2714
|
+
|
|
2715
|
+
|
|
2716
|
+
|
|
2717
|
+
|
|
2718
|
+
|
|
2719
|
+
|
|
2720
|
+
|
|
2721
|
+
|
|
2722
|
+
|
|
2723
|
+
|
|
2724
|
+
|
|
2725
|
+
|
|
2726
|
+
|
|
2727
|
+
|
|
2728
|
+
* @example
|
|
2729
|
+
* // Transform to new tree
|
|
2730
|
+
* const tree = new BinaryTree<number, number>([[1, 10], [2, 20]]);
|
|
2731
|
+
* const mapped = tree.map((v, key) => [key, (v ?? 0) + 1] as [number, number]);
|
|
2732
|
+
* console.log([...mapped.values()]); // contains 11;
|
|
1891
2733
|
*/
|
|
1892
2734
|
map<MK = K, MV = V, MR = any>(
|
|
1893
2735
|
cb: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
@@ -1940,6 +2782,40 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
1940
2782
|
*
|
|
1941
2783
|
* @param [options] - Options to control the output.
|
|
1942
2784
|
* @param [startNode=this._root] - The node to start printing from.
|
|
2785
|
+
|
|
2786
|
+
|
|
2787
|
+
|
|
2788
|
+
|
|
2789
|
+
|
|
2790
|
+
|
|
2791
|
+
|
|
2792
|
+
|
|
2793
|
+
|
|
2794
|
+
|
|
2795
|
+
|
|
2796
|
+
|
|
2797
|
+
|
|
2798
|
+
|
|
2799
|
+
|
|
2800
|
+
|
|
2801
|
+
|
|
2802
|
+
|
|
2803
|
+
|
|
2804
|
+
|
|
2805
|
+
|
|
2806
|
+
|
|
2807
|
+
|
|
2808
|
+
|
|
2809
|
+
|
|
2810
|
+
|
|
2811
|
+
|
|
2812
|
+
|
|
2813
|
+
|
|
2814
|
+
|
|
2815
|
+
* @example
|
|
2816
|
+
* // Display tree
|
|
2817
|
+
* const tree = new BinaryTree<number>([1, 2, 3]);
|
|
2818
|
+
* expect(() => tree.print()).not.toThrow();
|
|
1943
2819
|
*/
|
|
1944
2820
|
override print(
|
|
1945
2821
|
options?: BinaryTreePrintOptions,
|
|
@@ -2258,7 +3134,6 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2258
3134
|
node: BinaryTreeNode<K, V> | null | undefined,
|
|
2259
3135
|
options: BinaryTreePrintOptions
|
|
2260
3136
|
): NodeDisplayLayout {
|
|
2261
|
-
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
2262
3137
|
const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
|
|
2263
3138
|
|
|
2264
3139
|
// Iterative post-order: compute display layout bottom-up using an explicit stack.
|
|
@@ -2527,7 +3402,7 @@ export class BinaryTree<K = any, V = any, R = any>
|
|
|
2527
3402
|
* @param p - The item to check.
|
|
2528
3403
|
* @returns True if it's a function.
|
|
2529
3404
|
*/
|
|
2530
|
-
protected _isPredicate(p:
|
|
3405
|
+
protected _isPredicate(p: unknown): p is NodePredicate<BinaryTreeNode<K, V>> {
|
|
2531
3406
|
return typeof p === 'function';
|
|
2532
3407
|
}
|
|
2533
3408
|
|