stack-typed 1.48.2 → 1.48.4

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 (40) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
  2. package/dist/data-structures/binary-tree/avl-tree.js +7 -7
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +89 -87
  4. package/dist/data-structures/binary-tree/binary-tree.js +67 -58
  5. package/dist/data-structures/binary-tree/bst.d.ts +28 -47
  6. package/dist/data-structures/binary-tree/bst.js +54 -57
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +7 -7
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  10. package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
  11. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  12. package/dist/data-structures/graph/abstract-graph.js +4 -0
  13. package/dist/data-structures/graph/directed-graph.d.ts +25 -7
  14. package/dist/data-structures/graph/directed-graph.js +58 -12
  15. package/dist/data-structures/graph/undirected-graph.d.ts +25 -6
  16. package/dist/data-structures/graph/undirected-graph.js +70 -7
  17. package/dist/interfaces/binary-tree.d.ts +6 -6
  18. package/dist/types/common.d.ts +11 -8
  19. package/dist/types/common.js +6 -1
  20. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  21. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  22. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  23. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  24. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  25. package/package.json +2 -2
  26. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  27. package/src/data-structures/binary-tree/binary-tree.ts +147 -136
  28. package/src/data-structures/binary-tree/bst.ts +86 -82
  29. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  30. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  31. package/src/data-structures/graph/abstract-graph.ts +5 -0
  32. package/src/data-structures/graph/directed-graph.ts +61 -12
  33. package/src/data-structures/graph/undirected-graph.ts +75 -7
  34. package/src/interfaces/binary-tree.ts +5 -6
  35. package/src/types/common.ts +11 -8
  36. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  37. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  38. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  39. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  40. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
@@ -83,17 +83,24 @@ class BinaryTree extends base_1.IterablePairBase {
83
83
  constructor(elements, options) {
84
84
  super();
85
85
  this.iterationType = types_1.IterationType.ITERATIVE;
86
+ this._extractor = (key) => Number(key);
86
87
  this._defaultOneParamCallback = (node) => node.key;
87
88
  if (options) {
88
- const { iterationType } = options;
89
+ const { iterationType, extractor } = options;
89
90
  if (iterationType) {
90
91
  this.iterationType = iterationType;
91
92
  }
93
+ if (extractor) {
94
+ this._extractor = extractor;
95
+ }
92
96
  }
93
97
  this._size = 0;
94
98
  if (elements)
95
99
  this.addMany(elements);
96
100
  }
101
+ get extractor() {
102
+ return this._extractor;
103
+ }
97
104
  get root() {
98
105
  return this._root;
99
106
  }
@@ -102,7 +109,7 @@ class BinaryTree extends base_1.IterablePairBase {
102
109
  }
103
110
  /**
104
111
  * Creates a new instance of BinaryTreeNode with the given key and value.
105
- * @param {BTNKey} key - The key for the new node.
112
+ * @param {K} key - The key for the new node.
106
113
  * @param {V} value - The value for the new node.
107
114
  * @returns {N} - The newly created BinaryTreeNode.
108
115
  */
@@ -121,7 +128,7 @@ class BinaryTree extends base_1.IterablePairBase {
121
128
  }
122
129
  /**
123
130
  * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
124
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
131
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V,N>`.
125
132
  * @returns a boolean value indicating whether the exemplar is an instance of the class N.
126
133
  */
127
134
  isNode(exemplar) {
@@ -130,7 +137,7 @@ class BinaryTree extends base_1.IterablePairBase {
130
137
  /**
131
138
  * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
132
139
  * object.
133
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
140
+ * @param exemplar - BTNodeExemplar<K, V,N> - A generic type representing the exemplar parameter of the
134
141
  * function. It can be any type.
135
142
  * @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
136
143
  */
@@ -156,7 +163,7 @@ class BinaryTree extends base_1.IterablePairBase {
156
163
  else if (this.isNode(exemplar)) {
157
164
  node = exemplar;
158
165
  }
159
- else if (this.isNodeKey(exemplar)) {
166
+ else if (this.isNotNodeInstance(exemplar)) {
160
167
  node = this.createNode(exemplar);
161
168
  }
162
169
  else {
@@ -166,7 +173,7 @@ class BinaryTree extends base_1.IterablePairBase {
166
173
  }
167
174
  /**
168
175
  * The function checks if a given value is an entry in a binary tree node.
169
- * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
176
+ * @param kne - BTNodeExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
170
177
  * two type parameters V and N, representing the value and node type respectively.
171
178
  * @returns a boolean value.
172
179
  */
@@ -234,7 +241,7 @@ class BinaryTree extends base_1.IterablePairBase {
234
241
  * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
235
242
  * current instance, and returns an array of the inserted nodes.
236
243
  * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
237
- * `BTNodeExemplar<V, N>` objects.
244
+ * `BTNodeExemplar<K, V,N>` objects.
238
245
  * @returns The function `addMany` returns an array of values, where each value is either of type
239
246
  * `N`, `null`, or `undefined`.
240
247
  */
@@ -334,11 +341,11 @@ class BinaryTree extends base_1.IterablePairBase {
334
341
  * Space Complexity: O(1)
335
342
  *
336
343
  * The function calculates the depth of a given node in a binary tree.
337
- * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
338
- * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
344
+ * @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
345
+ * the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
339
346
  * `undefined`.
340
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
341
- * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
347
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
348
+ * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
342
349
  * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
343
350
  * @returns the depth of the `distNode` relative to the `beginRoot`.
344
351
  */
@@ -365,9 +372,9 @@ class BinaryTree extends base_1.IterablePairBase {
365
372
  *
366
373
  * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
367
374
  * iterative traversal.
368
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
375
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
369
376
  * starting node of the binary tree from which we want to calculate the height. It can be of type
370
- * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
377
+ * `K`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
371
378
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
372
379
  * height of the tree using a recursive approach or an iterative approach. It can have two possible
373
380
  * values:
@@ -412,9 +419,9 @@ class BinaryTree extends base_1.IterablePairBase {
412
419
  *
413
420
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
414
421
  * recursive or iterative approach.
415
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
422
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
416
423
  * starting node of the binary tree from which we want to calculate the minimum height. It can be of
417
- * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
424
+ * type `K`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
418
425
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
419
426
  * to calculate the minimum height of a binary tree. It can have two possible values:
420
427
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -475,8 +482,8 @@ class BinaryTree extends base_1.IterablePairBase {
475
482
  *
476
483
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
477
484
  * height of the tree.
478
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
479
- * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
485
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
486
+ * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
480
487
  * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
481
488
  * @returns a boolean value.
482
489
  */
@@ -501,7 +508,7 @@ class BinaryTree extends base_1.IterablePairBase {
501
508
  * matches the identifier. If set to true, the function will stop iterating once it finds a matching
502
509
  * node and return that node. If set to false (default), the function will continue iterating and
503
510
  * return all nodes that match the identifier.
504
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
511
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
505
512
  * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
506
513
  * it is `null` or `undefined`, an empty array will be returned.
507
514
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
@@ -558,8 +565,8 @@ class BinaryTree extends base_1.IterablePairBase {
558
565
  * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
559
566
  * function should return a boolean value indicating whether the node should be included in the
560
567
  * result or not.
561
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
562
- * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
568
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
569
+ * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
563
570
  * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
564
571
  * @param iterationType - The `iterationType` parameter is a variable that determines the type of
565
572
  * iteration to be performed on the binary tree. It is used to specify whether the iteration should
@@ -584,7 +591,7 @@ class BinaryTree extends base_1.IterablePairBase {
584
591
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
585
592
  * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
586
593
  * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
587
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
594
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
588
595
  * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
589
596
  * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
590
597
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
@@ -608,7 +615,7 @@ class BinaryTree extends base_1.IterablePairBase {
608
615
  *
609
616
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
610
617
  * recursive or iterative iteration.
611
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
618
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
612
619
  * It is used to find the node with the matching key value.
613
620
  * @param iterationType - The `iterationType` parameter is used to determine whether the search for
614
621
  * the node with the given key should be performed iteratively or recursively. It has two possible
@@ -652,7 +659,7 @@ class BinaryTree extends base_1.IterablePairBase {
652
659
  /**
653
660
  * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
654
661
  * key, otherwise it returns the key itself.
655
- * @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
662
+ * @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
656
663
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
657
664
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
658
665
  * type of iteration to be used when searching for a node by key. It has a default value of
@@ -661,7 +668,7 @@ class BinaryTree extends base_1.IterablePairBase {
661
668
  * itself if it is not a valid node key.
662
669
  */
663
670
  ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
664
- return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
671
+ return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
665
672
  }
666
673
  /**
667
674
  * Time Complexity: O(n)
@@ -676,8 +683,8 @@ class BinaryTree extends base_1.IterablePairBase {
676
683
  * the binary tree. It is used to determine whether a node matches the given identifier. The callback
677
684
  * function should return a value that can be compared to the identifier to determine if it is a
678
685
  * match.
679
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
680
- * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
686
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
687
+ * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
681
688
  * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
682
689
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
683
690
  * be performed when searching for a node in the binary tree. It is an optional parameter with a
@@ -715,8 +722,8 @@ class BinaryTree extends base_1.IterablePairBase {
715
722
  *
716
723
  * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
717
724
  * structure, with the option to reverse the order of the nodes.
718
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
719
- * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
725
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
726
+ * starting node from which you want to find the path to the root. It can be of type `K`, `N`,
720
727
  * `null`, or `undefined`.
721
728
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
722
729
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
@@ -748,8 +755,8 @@ class BinaryTree extends base_1.IterablePairBase {
748
755
  *
749
756
  * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
750
757
  * iteratively.
751
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
752
- * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
758
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
759
+ * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `N` (a
753
760
  * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
754
761
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
755
762
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
@@ -788,8 +795,8 @@ class BinaryTree extends base_1.IterablePairBase {
788
795
  *
789
796
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
790
797
  * iteratively.
791
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
792
- * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
798
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
799
+ * starting node from which we want to find the rightmost node. It can be of type `K`, `N`,
793
800
  * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
794
801
  * current object.
795
802
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
@@ -829,7 +836,7 @@ class BinaryTree extends base_1.IterablePairBase {
829
836
  * Space Complexity: O(1)
830
837
  *
831
838
  * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
832
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
839
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
833
840
  * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
834
841
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
835
842
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
@@ -845,9 +852,10 @@ class BinaryTree extends base_1.IterablePairBase {
845
852
  const dfs = (cur, min, max) => {
846
853
  if (!cur)
847
854
  return true;
848
- if (cur.key <= min || cur.key >= max)
855
+ const numKey = this.extractor(cur.key);
856
+ if (numKey <= min || numKey >= max)
849
857
  return false;
850
- return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
858
+ return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
851
859
  };
852
860
  return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
853
861
  }
@@ -860,9 +868,10 @@ class BinaryTree extends base_1.IterablePairBase {
860
868
  curr = curr.left;
861
869
  }
862
870
  curr = stack.pop();
863
- if (!curr || prev >= curr.key)
871
+ const numKey = this.extractor(curr.key);
872
+ if (!curr || prev >= numKey)
864
873
  return false;
865
- prev = curr.key;
874
+ prev = numKey;
866
875
  curr = curr.right;
867
876
  }
868
877
  return true;
@@ -897,8 +906,8 @@ class BinaryTree extends base_1.IterablePairBase {
897
906
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
898
907
  * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
899
908
  * returns a value of any type.
900
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
901
- * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
909
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
910
+ * starting node or key from which the subtree traversal should begin. It can be of type `K`,
902
911
  * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
903
912
  * the default value.
904
913
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -980,13 +989,13 @@ class BinaryTree extends base_1.IterablePairBase {
980
989
  return this.isRealNode(node) || node === null;
981
990
  }
982
991
  /**
983
- * The function "isNodeKey" checks if a potential key is a number.
992
+ * The function "isNotNodeInstance" checks if a potential key is a number.
984
993
  * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
985
994
  * data type.
986
995
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
987
996
  */
988
- isNodeKey(potentialKey) {
989
- return typeof potentialKey === 'number';
997
+ isNotNodeInstance(potentialKey) {
998
+ return !(potentialKey instanceof BinaryTreeNode);
990
999
  }
991
1000
  /**
992
1001
  * Time complexity: O(n)
@@ -1000,7 +1009,7 @@ class BinaryTree extends base_1.IterablePairBase {
1000
1009
  * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1001
1010
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1002
1011
  * nodes are traversed during the depth-first search. It can have one of the following values:
1003
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1012
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1004
1013
  * for the depth-first search traversal. It can be specified as a key, a node object, or
1005
1014
  * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1006
1015
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@@ -1126,7 +1135,7 @@ class BinaryTree extends base_1.IterablePairBase {
1126
1135
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1127
1136
  * the breadth-first search traversal. It takes a single parameter, which is the current node being
1128
1137
  * visited, and returns a value of any type.
1129
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1138
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1130
1139
  * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1131
1140
  * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1132
1141
  * the class is used as
@@ -1200,9 +1209,9 @@ class BinaryTree extends base_1.IterablePairBase {
1200
1209
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1201
1210
  * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1202
1211
  * returns a value of any type.
1203
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1212
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1204
1213
  * starting node for traversing the tree. It can be either a node object (`N`), a key value
1205
- * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1214
+ * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1206
1215
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1207
1216
  * performed on the tree. It can have two possible values:
1208
1217
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
@@ -1262,7 +1271,7 @@ class BinaryTree extends base_1.IterablePairBase {
1262
1271
  }
1263
1272
  /**
1264
1273
  * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1265
- * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1274
+ * @param {K | N | null | undefined} node - The `node` parameter can be of type `K`, `N`,
1266
1275
  * `null`, or `undefined`.
1267
1276
  * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1268
1277
  */
@@ -1285,7 +1294,7 @@ class BinaryTree extends base_1.IterablePairBase {
1285
1294
  }
1286
1295
  /**
1287
1296
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1288
- * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1297
+ * @param {K | N | null} [x] - The parameter `x` can be of type `K`, `N`, or `null`.
1289
1298
  * @returns the successor of the given node or key. The successor is the node that comes immediately
1290
1299
  * after the given node in the inorder traversal of the binary tree.
1291
1300
  */
@@ -1314,7 +1323,7 @@ class BinaryTree extends base_1.IterablePairBase {
1314
1323
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1315
1324
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1316
1325
  * following values:
1317
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1326
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1318
1327
  * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1319
1328
  * the root of the tree. If no value is provided, the default value is the root of the tree.
1320
1329
  * @returns The function `morris` returns an array of values that are the result of invoking the
@@ -1480,7 +1489,7 @@ class BinaryTree extends base_1.IterablePairBase {
1480
1489
  return newTree;
1481
1490
  }
1482
1491
  // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1483
- // // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1492
+ // // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
1484
1493
  // // const newTree = this.createTree();
1485
1494
  // // for (const [key, value] of this) {
1486
1495
  // // newTree.add(key, callback([key, value], this));
@@ -1490,7 +1499,7 @@ class BinaryTree extends base_1.IterablePairBase {
1490
1499
  //
1491
1500
  /**
1492
1501
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1493
- * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
1502
+ * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
1494
1503
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
1495
1504
  * following types:
1496
1505
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
@@ -1524,23 +1533,23 @@ class BinaryTree extends base_1.IterablePairBase {
1524
1533
  const stack = [];
1525
1534
  let current = node;
1526
1535
  while (current || stack.length > 0) {
1527
- while (current && !isNaN(current.key)) {
1536
+ while (current && !isNaN(this.extractor(current.key))) {
1528
1537
  stack.push(current);
1529
1538
  current = current.left;
1530
1539
  }
1531
1540
  current = stack.pop();
1532
- if (current && !isNaN(current.key)) {
1541
+ if (current && !isNaN(this.extractor(current.key))) {
1533
1542
  yield [current.key, current.value];
1534
1543
  current = current.right;
1535
1544
  }
1536
1545
  }
1537
1546
  }
1538
1547
  else {
1539
- if (node.left && !isNaN(node.key)) {
1548
+ if (node.left && !isNaN(this.extractor(node.key))) {
1540
1549
  yield* this[Symbol.iterator](node.left);
1541
1550
  }
1542
1551
  yield [node.key, node.value];
1543
- if (node.right && !isNaN(node.key)) {
1552
+ if (node.right && !isNaN(this.extractor(node.key))) {
1544
1553
  yield* this[Symbol.iterator](node.right);
1545
1554
  }
1546
1555
  }
@@ -1555,12 +1564,12 @@ class BinaryTree extends base_1.IterablePairBase {
1555
1564
  else if (node === undefined && !isShowUndefined) {
1556
1565
  return emptyDisplayLayout;
1557
1566
  }
1558
- else if (node !== null && node !== undefined && isNaN(node.key) && !isShowRedBlackNIL) {
1567
+ else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
1559
1568
  return emptyDisplayLayout;
1560
1569
  }
1561
1570
  else if (node !== null && node !== undefined) {
1562
1571
  // Display logic of normal nodes
1563
- const key = node.key, line = isNaN(key) ? 'S' : key.toString(), width = line.length;
1572
+ const key = node.key, line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(), width = line.length;
1564
1573
  return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
1565
1574
  }
1566
1575
  else {
@@ -1647,7 +1656,7 @@ class BinaryTree extends base_1.IterablePairBase {
1647
1656
  * If the parent node is null, the function also returns undefined.
1648
1657
  */
1649
1658
  _addTo(newNode, parent) {
1650
- if (this.isNodeKey(parent))
1659
+ if (this.isNotNodeInstance(parent))
1651
1660
  parent = this.getNode(parent);
1652
1661
  if (parent) {
1653
1662
  // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.
@@ -5,13 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, BTNodeExemplar, Comparator } from '../../types';
9
- import { CP, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNodeExemplar } from '../../types';
9
+ import { BSTVariant, CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
- export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
12
+ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
13
13
  parent?: N;
14
- constructor(key: BTNKey, value?: V);
14
+ constructor(key: K, value?: V);
15
15
  protected _left?: N;
16
16
  /**
17
17
  * Get the left child node.
@@ -42,7 +42,7 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
42
42
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
43
43
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
44
44
  */
45
- export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
45
+ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>> extends BinaryTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
46
46
  /**
47
47
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
48
48
  * the tree with optional elements and options.
@@ -51,19 +51,20 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
51
51
  * @param [options] - The `options` parameter is an optional object that can contain additional
52
52
  * configuration options for the binary search tree. It can have the following properties:
53
53
  */
54
- constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BSTOptions>);
54
+ constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>);
55
55
  protected _root?: N;
56
56
  get root(): N | undefined;
57
- comparator: Comparator<BTNKey>;
57
+ protected _variant: BSTVariant;
58
+ get variant(): BSTVariant;
58
59
  /**
59
60
  * The function creates a new binary search tree node with the given key and value.
60
- * @param {BTNKey} key - The key parameter is the key value that will be associated with
61
+ * @param {K} key - The key parameter is the key value that will be associated with
61
62
  * the new node. It is used to determine the position of the node in the binary search tree.
62
63
  * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
63
64
  * represents the value associated with the node in a binary search tree.
64
65
  * @returns a new instance of the BSTNode class with the specified key and value.
65
66
  */
66
- createNode(key: BTNKey, value?: V): N;
67
+ createNode(key: K, value?: V): N;
67
68
  /**
68
69
  * The function creates a new binary search tree with the specified options.
69
70
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
@@ -71,20 +72,20 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
71
72
  * that defines various options for creating a binary search tree.
72
73
  * @returns a new instance of the BST class with the specified options.
73
74
  */
74
- createTree(options?: Partial<BSTOptions>): TREE;
75
+ createTree(options?: Partial<BSTOptions<K>>): TREE;
75
76
  /**
76
77
  * The function checks if an exemplar is an instance of BSTNode.
77
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
78
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
78
79
  * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
79
80
  */
80
- isNode(exemplar: BTNodeExemplar<V, N>): exemplar is N;
81
+ isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
81
82
  /**
82
83
  * The function `exemplarToNode` takes an exemplar and returns a corresponding node if the exemplar
83
84
  * is valid, otherwise it returns undefined.
84
- * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<V, N>`.
85
+ * @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
85
86
  * @returns a variable `node` which is of type `N` or `undefined`.
86
87
  */
87
- exemplarToNode(exemplar: BTNodeExemplar<V, N>): N | undefined;
88
+ exemplarToNode(exemplar: BTNodeExemplar<K, V, N>): N | undefined;
88
89
  /**
89
90
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
90
91
  * Space Complexity: O(1) - Constant space is used.
@@ -99,7 +100,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
99
100
  * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
100
101
  * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
101
102
  */
102
- add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
103
+ add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>): N | undefined;
103
104
  /**
104
105
  * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
105
106
  * Space Complexity: O(k) - Additional space is required for the sorted array.
@@ -120,27 +121,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
120
121
  * tree instance.
121
122
  * @returns The `addMany` function returns an array of `N` or `undefined` values.
122
123
  */
123
- addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
124
- /**
125
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
126
- * Space Complexity: O(n) - Additional space is required for the sorted array.
127
- */
128
- /**
129
- * Time Complexity: O(log n) - Average case for a balanced tree.
130
- * Space Complexity: O(1) - Constant space is used.
131
- *
132
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
133
- * leftmost node if the comparison result is greater than.
134
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
135
- * type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
136
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
137
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
138
- * be performed. It can have one of the following values:
139
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
140
- * the key of the leftmost node if the comparison result is greater than, and the key of the
141
- * rightmost node otherwise. If no node is found, it returns 0.
142
- */
143
- lastKey(beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): BTNKey;
124
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
144
125
  /**
145
126
  * Time Complexity: O(log n) - Average case for a balanced tree.
146
127
  * Space Complexity: O(1) - Constant space is used.
@@ -151,7 +132,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
151
132
  *
152
133
  * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
153
134
  * either recursive or iterative methods.
154
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
135
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
155
136
  * It is used to identify the node that we want to retrieve.
156
137
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
157
138
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
@@ -159,7 +140,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
159
140
  * @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
160
141
  * found in the binary tree. If no node is found, it returns `undefined`.
161
142
  */
162
- getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
143
+ getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
163
144
  /**
164
145
  * Time Complexity: O(log n) - Average case for a balanced tree.
165
146
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -167,13 +148,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
167
148
  /**
168
149
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
169
150
  * otherwise it returns the key itself.
170
- * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
151
+ * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
171
152
  * `undefined`.
172
153
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
173
154
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
174
155
  * @returns either a node object (N) or undefined.
175
156
  */
176
- ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
157
+ ensureNode(key: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
177
158
  /**
178
159
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
179
160
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -190,14 +171,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
190
171
  * first node that matches the identifier. If set to true, the function will return an array
191
172
  * containing only the first matching node. If set to false (default), the function will continue
192
173
  * searching for all nodes that match the identifier and return an array containing
193
- * @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
174
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
194
175
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
195
176
  * traversal will start from the root of the tree.
196
177
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
197
178
  * performed on the binary tree. It can have two possible values:
198
179
  * @returns The method returns an array of nodes (`N[]`).
199
180
  */
200
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
181
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): N[];
201
182
  /**
202
183
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
203
184
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -215,7 +196,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
215
196
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
216
197
  * `CP`, which is a custom type representing the comparison operator. The possible values for
217
198
  * `lesserOrGreater` are
218
- * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
199
+ * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
219
200
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
220
201
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
221
202
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -223,7 +204,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
223
204
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
224
205
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
225
206
  */
226
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
207
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<K, N>, iterationType?: IterationType): ReturnType<C>[];
227
208
  /**
228
209
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
229
210
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -267,10 +248,10 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
267
248
  /**
268
249
  * The function compares two values using a comparator function and returns whether the first value
269
250
  * is greater than, less than, or equal to the second value.
270
- * @param {BTNKey} a - The parameter "a" is of type BTNKey.
271
- * @param {BTNKey} b - The parameter "b" in the above code represents a BTNKey.
251
+ * @param {K} a - The parameter "a" is of type K.
252
+ * @param {K} b - The parameter "b" in the above code represents a K.
272
253
  * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
273
254
  * than), CP.lt (less than), or CP.eq (equal).
274
255
  */
275
- protected _compare(a: BTNKey, b: BTNKey): CP;
256
+ protected _compare(a: K, b: K): CP;
276
257
  }