data-structure-typed 1.42.2 → 1.42.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 (57) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +12 -12
  3. package/benchmark/report.html +12 -12
  4. package/benchmark/report.json +104 -104
  5. package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
  6. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +5 -3
  7. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +57 -53
  9. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +116 -54
  10. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +42 -15
  12. package/dist/cjs/src/data-structures/binary-tree/bst.js +77 -21
  13. package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
  15. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +148 -180
  16. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/src/data-structures/binary-tree/tree-multiset.d.ts +10 -10
  18. package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js +20 -17
  19. package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js.map +1 -1
  20. package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
  21. package/dist/cjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  22. package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
  23. package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
  24. package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js.map +1 -1
  25. package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
  26. package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +5 -3
  27. package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +57 -53
  28. package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +117 -55
  29. package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +42 -15
  30. package/dist/mjs/src/data-structures/binary-tree/bst.js +79 -21
  31. package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
  32. package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +148 -184
  33. package/dist/mjs/src/data-structures/binary-tree/tree-multiset.d.ts +10 -10
  34. package/dist/mjs/src/data-structures/binary-tree/tree-multiset.js +19 -17
  35. package/dist/mjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  36. package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
  37. package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
  38. package/dist/umd/data-structure-typed.min.js +1 -1
  39. package/dist/umd/data-structure-typed.min.js.map +1 -1
  40. package/package.json +2 -2
  41. package/src/data-structures/binary-tree/avl-tree.ts +5 -4
  42. package/src/data-structures/binary-tree/binary-tree.ts +227 -158
  43. package/src/data-structures/binary-tree/bst.ts +100 -34
  44. package/src/data-structures/binary-tree/rb-tree.ts +227 -236
  45. package/src/data-structures/binary-tree/tree-multiset.ts +24 -23
  46. package/src/data-structures/graph/abstract-graph.ts +18 -14
  47. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  48. package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
  49. package/test/performance/reportor.ts +1 -0
  50. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -1
  51. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -38
  52. package/test/unit/data-structures/binary-tree/bst.test.ts +13 -0
  53. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +205 -159
  54. package/test/unit/data-structures/graph/directed-graph.test.ts +7 -8
  55. package/test/unit/data-structures/graph/salty-edges.json +875 -1
  56. package/test/unit/data-structures/graph/salty-vertexes.json +200 -1
  57. package/test/unit/data-structures/graph/undirected-graph.test.ts +15 -16
@@ -107,7 +107,7 @@ class BinaryTree {
107
107
  this.iterationType = iterationType;
108
108
  }
109
109
  }
110
- _root = null;
110
+ _root = undefined;
111
111
  /**
112
112
  * Get the root node of the binary tree.
113
113
  */
@@ -121,7 +121,6 @@ class BinaryTree {
121
121
  get size() {
122
122
  return this._size;
123
123
  }
124
- defaultOneParamCallback = (node) => node.key;
125
124
  /**
126
125
  * Creates a new instance of BinaryTreeNode with the given key and value.
127
126
  * @param {BTNKey} key - The key for the new node.
@@ -135,7 +134,7 @@ class BinaryTree {
135
134
  * Clear the binary tree, removing all nodes.
136
135
  */
137
136
  clear() {
138
- this._setRoot(null);
137
+ this._setRoot(undefined);
139
138
  this._size = 0;
140
139
  }
141
140
  /**
@@ -307,10 +306,10 @@ class BinaryTree {
307
306
  /**
308
307
  * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
309
308
  * specified root node.
310
- * @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
309
+ * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
311
310
  * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
312
311
  * of the node (`BTNKey`), or `null`.
313
- * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
312
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
314
313
  * starting node from which we want to calculate the depth. It can be either a node object or the key
315
314
  * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
316
315
  * node of the binary tree.
@@ -334,7 +333,7 @@ class BinaryTree {
334
333
  /**
335
334
  * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
336
335
  * iterative approach.
337
- * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
336
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
338
337
  * starting node from which the height of the binary tree is calculated. It can be either a node
339
338
  * object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
340
339
  * node is specified. If `
@@ -380,7 +379,7 @@ class BinaryTree {
380
379
  /**
381
380
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
382
381
  * recursive or iterative approach.
383
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
382
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
384
383
  * calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
385
384
  * not provided.
386
385
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
@@ -433,7 +432,7 @@ class BinaryTree {
433
432
  /**
434
433
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
435
434
  * height of the tree.
436
- * @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
435
+ * @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
437
436
  * either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
438
437
  * @returns The method is returning a boolean value.
439
438
  */
@@ -454,7 +453,7 @@ class BinaryTree {
454
453
  * first node that matches the identifier. If set to true, the function will return an array with
455
454
  * only one element (or an empty array if no matching node is found). If set to false (default), the
456
455
  * function will continue searching for all
457
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
456
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
458
457
  * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
459
458
  * tree.
460
459
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
@@ -585,7 +584,7 @@ class BinaryTree {
585
584
  /**
586
585
  * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
587
586
  * iterative traversal.
588
- * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
587
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
589
588
  * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
590
589
  * of a node (`BTNKey`), or `null` if the tree is empty.
591
590
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
@@ -619,8 +618,8 @@ class BinaryTree {
619
618
  /**
620
619
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
621
620
  * iteratively.
622
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
623
- * find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
621
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
622
+ * find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
624
623
  * or `null`. If it is `null`, it means there is no starting node
625
624
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
626
625
  * be performed when finding the rightmost node in a binary tree. It can have two possible values:
@@ -709,7 +708,7 @@ class BinaryTree {
709
708
  * subtree traversal. It takes a single argument, which is the current node being traversed, and
710
709
  * returns a value. The return values from each callback invocation will be collected and returned as
711
710
  * an array.
712
- * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
711
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
713
712
  * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
714
713
  * start from the root of the tree.
715
714
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -728,12 +727,12 @@ class BinaryTree {
728
727
  if (cur !== undefined) {
729
728
  ans.push(callback(cur));
730
729
  if (includeNull) {
731
- cur !== null && cur.left !== undefined && _traverse(cur.left);
732
- cur !== null && cur.right !== undefined && _traverse(cur.right);
730
+ cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
731
+ cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
733
732
  }
734
733
  else {
735
- cur !== null && cur.left && _traverse(cur.left);
736
- cur !== null && cur.right && _traverse(cur.right);
734
+ cur && cur.left && _traverse(cur.left);
735
+ cur && cur.right && _traverse(cur.right);
737
736
  }
738
737
  }
739
738
  };
@@ -746,18 +745,27 @@ class BinaryTree {
746
745
  if (cur !== undefined) {
747
746
  ans.push(callback(cur));
748
747
  if (includeNull) {
749
- cur !== null && cur.right !== undefined && stack.push(cur.right);
750
- cur !== null && cur.left !== undefined && stack.push(cur.left);
748
+ cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
749
+ cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
751
750
  }
752
751
  else {
753
- cur !== null && cur.right && stack.push(cur.right);
754
- cur !== null && cur.left && stack.push(cur.left);
752
+ cur && cur.right && stack.push(cur.right);
753
+ cur && cur.left && stack.push(cur.left);
755
754
  }
756
755
  }
757
756
  }
758
757
  }
759
758
  return ans;
760
759
  }
760
+ isNode(node) {
761
+ return node instanceof BinaryTreeNode && node.key.toString() !== 'NaN';
762
+ }
763
+ isNIL(node) {
764
+ return node instanceof BinaryTreeNode && node.key.toString() === 'NaN';
765
+ }
766
+ isNodeOrNull(node) {
767
+ return this.isNode(node) || node === null;
768
+ }
761
769
  /**
762
770
  * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
763
771
  * function on each node according to a specified order pattern.
@@ -766,7 +774,7 @@ class BinaryTree {
766
774
  * is `this.defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
767
775
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
768
776
  * nodes are visited during the depth-first search. There are three possible values for `pattern`:
769
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
777
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
770
778
  * search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
771
779
  * is `null`, an empty array will be returned.
772
780
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@@ -783,50 +791,50 @@ class BinaryTree {
783
791
  switch (pattern) {
784
792
  case 'in':
785
793
  if (includeNull) {
786
- if (node !== null && node.left !== undefined)
794
+ if (node && this.isNodeOrNull(node.left))
787
795
  _traverse(node.left);
788
- ans.push(callback(node));
789
- if (node !== null && node.right !== undefined)
796
+ this.isNodeOrNull(node) && ans.push(callback(node));
797
+ if (node && this.isNodeOrNull(node.right))
790
798
  _traverse(node.right);
791
799
  }
792
800
  else {
793
- if (node !== null && node.left)
801
+ if (node && node.left)
794
802
  _traverse(node.left);
795
- ans.push(callback(node));
796
- if (node !== null && node.right)
803
+ this.isNode(node) && ans.push(callback(node));
804
+ if (node && node.right)
797
805
  _traverse(node.right);
798
806
  }
799
807
  break;
800
808
  case 'pre':
801
809
  if (includeNull) {
802
- ans.push(callback(node));
803
- if (node !== null && node.left !== undefined)
810
+ this.isNodeOrNull(node) && ans.push(callback(node));
811
+ if (node && this.isNodeOrNull(node.left))
804
812
  _traverse(node.left);
805
- if (node !== null && node.right !== undefined)
813
+ if (node && this.isNodeOrNull(node.right))
806
814
  _traverse(node.right);
807
815
  }
808
816
  else {
809
- ans.push(callback(node));
810
- if (node !== null && node.left)
817
+ this.isNode(node) && ans.push(callback(node));
818
+ if (node && node.left)
811
819
  _traverse(node.left);
812
- if (node !== null && node.right)
820
+ if (node && node.right)
813
821
  _traverse(node.right);
814
822
  }
815
823
  break;
816
824
  case 'post':
817
825
  if (includeNull) {
818
- if (node !== null && node.left !== undefined)
826
+ if (node && this.isNodeOrNull(node.left))
819
827
  _traverse(node.left);
820
- if (node !== null && node.right !== undefined)
828
+ if (node && this.isNodeOrNull(node.right))
821
829
  _traverse(node.right);
822
- ans.push(callback(node));
830
+ this.isNodeOrNull(node) && ans.push(callback(node));
823
831
  }
824
832
  else {
825
- if (node !== null && node.left)
833
+ if (node && node.left)
826
834
  _traverse(node.left);
827
- if (node !== null && node.right)
835
+ if (node && node.right)
828
836
  _traverse(node.right);
829
- ans.push(callback(node));
837
+ this.isNode(node) && ans.push(callback(node));
830
838
  }
831
839
  break;
832
840
  }
@@ -838,7 +846,7 @@ class BinaryTree {
838
846
  const stack = [{ opt: 0, node: beginRoot }];
839
847
  while (stack.length > 0) {
840
848
  const cur = stack.pop();
841
- if (cur === undefined)
849
+ if (cur === undefined || this.isNIL(cur.node))
842
850
  continue;
843
851
  if (includeNull) {
844
852
  if (cur.node === undefined)
@@ -885,7 +893,7 @@ class BinaryTree {
885
893
  * @param callback - The `callback` parameter is a function that will be called for each node in the
886
894
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
887
895
  * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
888
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
896
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
889
897
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
890
898
  * will not be performed and an empty array will be returned.
891
899
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
@@ -905,9 +913,9 @@ class BinaryTree {
905
913
  const current = queue.shift();
906
914
  ans.push(callback(current));
907
915
  if (includeNull) {
908
- if (current && current.left !== undefined)
916
+ if (current && this.isNodeOrNull(current.left))
909
917
  queue.push(current.left);
910
- if (current && current.right !== undefined)
918
+ if (current && this.isNodeOrNull(current.right))
911
919
  queue.push(current.right);
912
920
  }
913
921
  else {
@@ -928,9 +936,9 @@ class BinaryTree {
928
936
  const current = queue.shift();
929
937
  ans.push(callback(current));
930
938
  if (includeNull) {
931
- if (current !== null && current.left !== undefined)
939
+ if (current && this.isNodeOrNull(current.left))
932
940
  queue.push(current.left);
933
- if (current !== null && current.right !== undefined)
941
+ if (current && this.isNodeOrNull(current.right))
934
942
  queue.push(current.right);
935
943
  }
936
944
  else {
@@ -950,7 +958,7 @@ class BinaryTree {
950
958
  * @param {C} callback - The `callback` parameter is a function that will be called on each node in
951
959
  * the tree. It takes a node as input and returns a value. The return type of the callback function
952
960
  * is determined by the generic type `C`.
953
- * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
961
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
954
962
  * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
955
963
  * from the root node of the binary tree.
956
964
  * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
@@ -970,9 +978,9 @@ class BinaryTree {
970
978
  levelsNodes[level] = [];
971
979
  levelsNodes[level].push(callback(node));
972
980
  if (includeNull) {
973
- if (node && node.left !== undefined)
981
+ if (node && this.isNodeOrNull(node.left))
974
982
  _recursive(node.left, level + 1);
975
- if (node && node.right !== undefined)
983
+ if (node && this.isNodeOrNull(node.right))
976
984
  _recursive(node.right, level + 1);
977
985
  }
978
986
  else {
@@ -993,9 +1001,9 @@ class BinaryTree {
993
1001
  levelsNodes[level] = [];
994
1002
  levelsNodes[level].push(callback(node));
995
1003
  if (includeNull) {
996
- if (node && node.right !== undefined)
1004
+ if (node && this.isNodeOrNull(node.right))
997
1005
  stack.push([node.right, level + 1]);
998
- if (node && node.left !== undefined)
1006
+ if (node && this.isNodeOrNull(node.left))
999
1007
  stack.push([node.left, level + 1]);
1000
1008
  }
1001
1009
  else {
@@ -1045,7 +1053,6 @@ class BinaryTree {
1045
1053
  }
1046
1054
  return y;
1047
1055
  }
1048
- // --- start additional methods ---
1049
1056
  /**
1050
1057
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
1051
1058
  * algorithm and returns an array of values obtained by applying a callback function to each node.
@@ -1055,7 +1062,7 @@ class BinaryTree {
1055
1062
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1056
1063
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1057
1064
  * following values:
1058
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
1065
+ * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
1059
1066
  * traversal. It specifies the root node of the tree from which the traversal should begin. If
1060
1067
  * `beginRoot` is `null`, an empty array will be returned.
1061
1068
  * @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
@@ -1144,6 +1151,7 @@ class BinaryTree {
1144
1151
  }
1145
1152
  return ans;
1146
1153
  }
1154
+ // --- start additional methods ---
1147
1155
  /**
1148
1156
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
1149
1157
  * either an iterative or recursive manner.
@@ -1184,6 +1192,7 @@ class BinaryTree {
1184
1192
  }
1185
1193
  }
1186
1194
  }
1195
+ defaultOneParamCallback = (node) => node.key;
1187
1196
  /**
1188
1197
  * Swap the data of two nodes in the binary tree.
1189
1198
  * @param {N} srcNode - The source node to swap.
@@ -1203,7 +1212,7 @@ class BinaryTree {
1203
1212
  }
1204
1213
  /**
1205
1214
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1206
- * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
1215
+ * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
1207
1216
  * the binary tree. It can be either a node object or `null`.
1208
1217
  * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
1209
1218
  * be added as a child.
@@ -1241,7 +1250,7 @@ class BinaryTree {
1241
1250
  /**
1242
1251
  * The function sets the root property of an object to a given value, and if the value is not null,
1243
1252
  * it also sets the parent property of the value to undefined.
1244
- * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
1253
+ * @param {N | null | undefined} v - The parameter `v` is of type `N | null | undefined`, which means it can either be of
1245
1254
  * type `N` or `null`.
1246
1255
  */
1247
1256
  _setRoot(v) {
@@ -1250,5 +1259,58 @@ class BinaryTree {
1250
1259
  }
1251
1260
  this._root = v;
1252
1261
  }
1262
+ print(beginRoot = this.root) {
1263
+ const display = (root) => {
1264
+ const [lines, , ,] = _displayAux(root);
1265
+ for (const line of lines) {
1266
+ console.log(line);
1267
+ }
1268
+ };
1269
+ const _displayAux = (node) => {
1270
+ if (node === undefined || node === null) {
1271
+ return [[], 0, 0, 0];
1272
+ }
1273
+ if (node && node.right === undefined && node.left === undefined) {
1274
+ const line = `${node.key}`;
1275
+ const width = line.length;
1276
+ const height = 1;
1277
+ const middle = Math.floor(width / 2);
1278
+ return [[line], width, height, middle];
1279
+ }
1280
+ if (node && node.right === undefined) {
1281
+ const [lines, n, p, x] = _displayAux(node.left);
1282
+ const s = `${node.key}`;
1283
+ const u = s.length;
1284
+ const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
1285
+ const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
1286
+ const shifted_lines = lines.map(line => line + ' '.repeat(u));
1287
+ return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1288
+ }
1289
+ if (node && node.left === undefined) {
1290
+ const [lines, n, p, u] = _displayAux(node.right);
1291
+ const s = `${node.key}`;
1292
+ const x = s.length;
1293
+ const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
1294
+ const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
1295
+ const shifted_lines = lines.map(line => ' '.repeat(u) + line);
1296
+ return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
1297
+ }
1298
+ const [left, n, p, x] = _displayAux(node.left);
1299
+ const [right, m, q, y] = _displayAux(node.right);
1300
+ const s = `${node.key}`;
1301
+ const u = s.length;
1302
+ const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
1303
+ const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
1304
+ if (p < q) {
1305
+ left.push(...new Array(q - p).fill(' '.repeat(n)));
1306
+ }
1307
+ else if (q < p) {
1308
+ right.push(...new Array(p - q).fill(' '.repeat(m)));
1309
+ }
1310
+ const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
1311
+ return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
1312
+ };
1313
+ display(beginRoot);
1314
+ }
1253
1315
  }
1254
1316
  exports.BinaryTree = BinaryTree;
@@ -10,7 +10,28 @@ import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
13
+ parent: N | undefined;
13
14
  constructor(key: BTNKey, value?: V);
15
+ protected _left: N | undefined;
16
+ /**
17
+ * Get the left child node.
18
+ */
19
+ get left(): N | undefined;
20
+ /**
21
+ * Set the left child node.
22
+ * @param {N | undefined} v - The left child node.
23
+ */
24
+ set left(v: N | undefined);
25
+ protected _right: N | undefined;
26
+ /**
27
+ * Get the right child node.
28
+ */
29
+ get right(): N | undefined;
30
+ /**
31
+ * Set the right child node.
32
+ * @param {N | undefined} v - The right child node.
33
+ */
34
+ set right(v: N | undefined);
14
35
  }
15
36
  export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
16
37
  /**
@@ -20,6 +41,11 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
20
41
  * binary search tree.
21
42
  */
22
43
  constructor(options?: BSTOptions);
44
+ protected _root: N | undefined;
45
+ /**
46
+ * Get the root node of the binary tree.
47
+ */
48
+ get root(): N | undefined;
23
49
  /**
24
50
  * The function creates a new binary search tree node with the given key and value.
25
51
  * @param {BTNKey} key - The key parameter is the key value that will be associated with
@@ -32,33 +58,33 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
32
58
  /**
33
59
  * The `add` function in a binary search tree class inserts a new node with a given key and value
34
60
  * into the tree.
35
- * @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
36
- * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
61
+ * @param {BTNKey | N | undefined} keyOrNode - The `keyOrNode` parameter can be either a
62
+ * `BTNKey` (which can be a number or a string), a `BSTNode` object, or `undefined`.
37
63
  * @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
38
64
  * binary search tree.
39
65
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
40
- * was not added or if the parameters were invalid, it returns null or undefined.
66
+ * was not added or if the parameters were invalid, it returns undefined or undefined.
41
67
  */
42
- add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
68
+ add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
43
69
  /**
44
70
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
45
71
  * maintaining balance.
46
72
  * @param {[BTNKey | N, V][]} keysOrNodes - The `arr` parameter in the `addMany` function
47
73
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
48
74
  * array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
49
- * `null
75
+ * `undefined
50
76
  * @param {V[]} data - The values of tree nodes
51
77
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
52
78
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
53
79
  * It can have two possible values:
54
- * @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
80
+ * @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
55
81
  */
56
- addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
82
+ addMany(keysOrNodes: (BTNKey | undefined)[] | (N | undefined)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
57
83
  /**
58
84
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
59
85
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
60
86
  * rightmost node otherwise.
61
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
87
+ * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting point for finding the last
62
88
  * key in a binary tree. It represents the root node of the subtree from which the search for the
63
89
  * last key should begin. If no specific `beginRoot` is provided, the search will start from the root
64
90
  * of the entire binary
@@ -69,7 +95,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
69
95
  * the key of the leftmost node if the comparison result is greater than, and the key of the
70
96
  * rightmost node otherwise. If no node is found, it returns 0.
71
97
  */
72
- lastKey(beginRoot?: N | null, iterationType?: IterationType): BTNKey;
98
+ lastKey(beginRoot?: N | undefined, iterationType?: IterationType): BTNKey;
73
99
  /**
74
100
  * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
75
101
  * using either recursive or iterative traversal.
@@ -84,14 +110,14 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
84
110
  * the first node that matches the nodeProperty. If set to true, the function will return an array
85
111
  * containing only that node. If set to false (default), the function will continue the traversal and
86
112
  * return an array containing all nodes that match the node
87
- * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
113
+ * @param {N | undefined} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
88
114
  * specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
89
- * is `null`, an empty array will be returned.
115
+ * is `undefined`, an empty array will be returned.
90
116
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
91
117
  * traverse the binary tree. It can have one of the following values:
92
118
  * @returns an array of nodes (N[]).
93
119
  */
94
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
120
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: N | undefined, iterationType?: IterationType): N[];
95
121
  /**
96
122
  * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
97
123
  * nodes that have a key value lesser or greater than a target key value.
@@ -101,15 +127,15 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
101
127
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
102
128
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
103
129
  * of the following values:
104
- * @param {BTNKey | N | null} targetNode - The `targetNode` parameter in the
130
+ * @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter in the
105
131
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
106
132
  * start. It can be either a reference to a specific node (`N`), the key of a node
107
- * (`BTNKey`), or `null` to
133
+ * (`BTNKey`), or `undefined` to
108
134
  * @param iterationType - The `iterationType` parameter determines whether the traversal should be
109
135
  * done recursively or iteratively. It can have two possible values:
110
136
  * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
111
137
  */
112
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | null, iterationType?: IterationType): ReturnType<C>[];
138
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
113
139
  /**
114
140
  * Balancing Adjustment:
115
141
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
@@ -136,6 +162,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
136
162
  */
137
163
  isAVLBalanced(iterationType?: IterationType): boolean;
138
164
  protected _comparator: BSTComparator;
165
+ protected _setRoot(v: N | undefined): void;
139
166
  /**
140
167
  * The function compares two values using a comparator function and returns whether the first value
141
168
  * is greater than, less than, or equal to the second value.