data-structure-typed 1.42.3 → 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.
- package/CHANGELOG.md +1 -1
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/cjs/src/data-structures/binary-tree/bst.js +77 -21
- package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/tree-multiset.d.ts +10 -10
- package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js +20 -17
- package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/mjs/src/data-structures/binary-tree/bst.js +79 -21
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +148 -184
- package/dist/mjs/src/data-structures/binary-tree/tree-multiset.d.ts +10 -10
- package/dist/mjs/src/data-structures/binary-tree/tree-multiset.js +19 -17
- package/dist/mjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +201 -131
- package/src/data-structures/binary-tree/bst.ts +100 -34
- package/src/data-structures/binary-tree/rb-tree.ts +227 -236
- package/src/data-structures/binary-tree/tree-multiset.ts +24 -23
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -31
- package/test/unit/data-structures/binary-tree/bst.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +205 -159
|
@@ -107,7 +107,7 @@ class BinaryTree {
|
|
|
107
107
|
this.iterationType = iterationType;
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
_root =
|
|
110
|
+
_root = undefined;
|
|
111
111
|
/**
|
|
112
112
|
* Get the root node of the binary tree.
|
|
113
113
|
*/
|
|
@@ -134,7 +134,7 @@ class BinaryTree {
|
|
|
134
134
|
* Clear the binary tree, removing all nodes.
|
|
135
135
|
*/
|
|
136
136
|
clear() {
|
|
137
|
-
this._setRoot(
|
|
137
|
+
this._setRoot(undefined);
|
|
138
138
|
this._size = 0;
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
@@ -306,10 +306,10 @@ class BinaryTree {
|
|
|
306
306
|
/**
|
|
307
307
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
308
308
|
* specified root node.
|
|
309
|
-
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
309
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
|
|
310
310
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
311
311
|
* of the node (`BTNKey`), or `null`.
|
|
312
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
312
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
313
313
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
314
314
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
315
315
|
* node of the binary tree.
|
|
@@ -333,7 +333,7 @@ class BinaryTree {
|
|
|
333
333
|
/**
|
|
334
334
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
335
335
|
* iterative approach.
|
|
336
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
336
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
337
337
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
338
338
|
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
339
339
|
* node is specified. If `
|
|
@@ -379,7 +379,7 @@ class BinaryTree {
|
|
|
379
379
|
/**
|
|
380
380
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
381
381
|
* recursive or iterative approach.
|
|
382
|
-
* @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
|
|
383
383
|
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
384
384
|
* not provided.
|
|
385
385
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -432,7 +432,7 @@ class BinaryTree {
|
|
|
432
432
|
/**
|
|
433
433
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
434
434
|
* height of the tree.
|
|
435
|
-
* @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
|
|
436
436
|
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
437
437
|
* @returns The method is returning a boolean value.
|
|
438
438
|
*/
|
|
@@ -453,7 +453,7 @@ class BinaryTree {
|
|
|
453
453
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
454
454
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
455
455
|
* function will continue searching for all
|
|
456
|
-
* @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
|
|
457
457
|
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
458
458
|
* tree.
|
|
459
459
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
@@ -584,7 +584,7 @@ class BinaryTree {
|
|
|
584
584
|
/**
|
|
585
585
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
586
586
|
* iterative traversal.
|
|
587
|
-
* @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
|
|
588
588
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
589
589
|
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
590
590
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -618,8 +618,8 @@ class BinaryTree {
|
|
|
618
618
|
/**
|
|
619
619
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
620
620
|
* iteratively.
|
|
621
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
622
|
-
* 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`
|
|
623
623
|
* or `null`. If it is `null`, it means there is no starting node
|
|
624
624
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
625
625
|
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
@@ -708,7 +708,7 @@ class BinaryTree {
|
|
|
708
708
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
709
709
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
710
710
|
* an array.
|
|
711
|
-
* @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
|
|
712
712
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
713
713
|
* start from the root of the tree.
|
|
714
714
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -727,12 +727,12 @@ class BinaryTree {
|
|
|
727
727
|
if (cur !== undefined) {
|
|
728
728
|
ans.push(callback(cur));
|
|
729
729
|
if (includeNull) {
|
|
730
|
-
cur
|
|
731
|
-
cur
|
|
730
|
+
cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
731
|
+
cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
732
732
|
}
|
|
733
733
|
else {
|
|
734
|
-
cur
|
|
735
|
-
cur
|
|
734
|
+
cur && cur.left && _traverse(cur.left);
|
|
735
|
+
cur && cur.right && _traverse(cur.right);
|
|
736
736
|
}
|
|
737
737
|
}
|
|
738
738
|
};
|
|
@@ -745,18 +745,27 @@ class BinaryTree {
|
|
|
745
745
|
if (cur !== undefined) {
|
|
746
746
|
ans.push(callback(cur));
|
|
747
747
|
if (includeNull) {
|
|
748
|
-
cur
|
|
749
|
-
cur
|
|
748
|
+
cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
749
|
+
cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
750
750
|
}
|
|
751
751
|
else {
|
|
752
|
-
cur
|
|
753
|
-
cur
|
|
752
|
+
cur && cur.right && stack.push(cur.right);
|
|
753
|
+
cur && cur.left && stack.push(cur.left);
|
|
754
754
|
}
|
|
755
755
|
}
|
|
756
756
|
}
|
|
757
757
|
}
|
|
758
758
|
return ans;
|
|
759
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
|
+
}
|
|
760
769
|
/**
|
|
761
770
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
762
771
|
* function on each node according to a specified order pattern.
|
|
@@ -765,7 +774,7 @@ class BinaryTree {
|
|
|
765
774
|
* is `this.defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
|
|
766
775
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
767
776
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
768
|
-
* @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
|
|
769
778
|
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
770
779
|
* is `null`, an empty array will be returned.
|
|
771
780
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -782,50 +791,50 @@ class BinaryTree {
|
|
|
782
791
|
switch (pattern) {
|
|
783
792
|
case 'in':
|
|
784
793
|
if (includeNull) {
|
|
785
|
-
if (node
|
|
794
|
+
if (node && this.isNodeOrNull(node.left))
|
|
786
795
|
_traverse(node.left);
|
|
787
|
-
ans.push(callback(node));
|
|
788
|
-
if (node
|
|
796
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
797
|
+
if (node && this.isNodeOrNull(node.right))
|
|
789
798
|
_traverse(node.right);
|
|
790
799
|
}
|
|
791
800
|
else {
|
|
792
|
-
if (node
|
|
801
|
+
if (node && node.left)
|
|
793
802
|
_traverse(node.left);
|
|
794
|
-
ans.push(callback(node));
|
|
795
|
-
if (node
|
|
803
|
+
this.isNode(node) && ans.push(callback(node));
|
|
804
|
+
if (node && node.right)
|
|
796
805
|
_traverse(node.right);
|
|
797
806
|
}
|
|
798
807
|
break;
|
|
799
808
|
case 'pre':
|
|
800
809
|
if (includeNull) {
|
|
801
|
-
ans.push(callback(node));
|
|
802
|
-
if (node
|
|
810
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
811
|
+
if (node && this.isNodeOrNull(node.left))
|
|
803
812
|
_traverse(node.left);
|
|
804
|
-
if (node
|
|
813
|
+
if (node && this.isNodeOrNull(node.right))
|
|
805
814
|
_traverse(node.right);
|
|
806
815
|
}
|
|
807
816
|
else {
|
|
808
|
-
ans.push(callback(node));
|
|
809
|
-
if (node
|
|
817
|
+
this.isNode(node) && ans.push(callback(node));
|
|
818
|
+
if (node && node.left)
|
|
810
819
|
_traverse(node.left);
|
|
811
|
-
if (node
|
|
820
|
+
if (node && node.right)
|
|
812
821
|
_traverse(node.right);
|
|
813
822
|
}
|
|
814
823
|
break;
|
|
815
824
|
case 'post':
|
|
816
825
|
if (includeNull) {
|
|
817
|
-
if (node
|
|
826
|
+
if (node && this.isNodeOrNull(node.left))
|
|
818
827
|
_traverse(node.left);
|
|
819
|
-
if (node
|
|
828
|
+
if (node && this.isNodeOrNull(node.right))
|
|
820
829
|
_traverse(node.right);
|
|
821
|
-
ans.push(callback(node));
|
|
830
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
822
831
|
}
|
|
823
832
|
else {
|
|
824
|
-
if (node
|
|
833
|
+
if (node && node.left)
|
|
825
834
|
_traverse(node.left);
|
|
826
|
-
if (node
|
|
835
|
+
if (node && node.right)
|
|
827
836
|
_traverse(node.right);
|
|
828
|
-
ans.push(callback(node));
|
|
837
|
+
this.isNode(node) && ans.push(callback(node));
|
|
829
838
|
}
|
|
830
839
|
break;
|
|
831
840
|
}
|
|
@@ -837,7 +846,7 @@ class BinaryTree {
|
|
|
837
846
|
const stack = [{ opt: 0, node: beginRoot }];
|
|
838
847
|
while (stack.length > 0) {
|
|
839
848
|
const cur = stack.pop();
|
|
840
|
-
if (cur === undefined)
|
|
849
|
+
if (cur === undefined || this.isNIL(cur.node))
|
|
841
850
|
continue;
|
|
842
851
|
if (includeNull) {
|
|
843
852
|
if (cur.node === undefined)
|
|
@@ -884,7 +893,7 @@ class BinaryTree {
|
|
|
884
893
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
885
894
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
886
895
|
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
|
|
887
|
-
* @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
|
|
888
897
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
889
898
|
* will not be performed and an empty array will be returned.
|
|
890
899
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
@@ -904,9 +913,9 @@ class BinaryTree {
|
|
|
904
913
|
const current = queue.shift();
|
|
905
914
|
ans.push(callback(current));
|
|
906
915
|
if (includeNull) {
|
|
907
|
-
if (current && current.left
|
|
916
|
+
if (current && this.isNodeOrNull(current.left))
|
|
908
917
|
queue.push(current.left);
|
|
909
|
-
if (current && current.right
|
|
918
|
+
if (current && this.isNodeOrNull(current.right))
|
|
910
919
|
queue.push(current.right);
|
|
911
920
|
}
|
|
912
921
|
else {
|
|
@@ -927,9 +936,9 @@ class BinaryTree {
|
|
|
927
936
|
const current = queue.shift();
|
|
928
937
|
ans.push(callback(current));
|
|
929
938
|
if (includeNull) {
|
|
930
|
-
if (current
|
|
939
|
+
if (current && this.isNodeOrNull(current.left))
|
|
931
940
|
queue.push(current.left);
|
|
932
|
-
if (current
|
|
941
|
+
if (current && this.isNodeOrNull(current.right))
|
|
933
942
|
queue.push(current.right);
|
|
934
943
|
}
|
|
935
944
|
else {
|
|
@@ -949,7 +958,7 @@ class BinaryTree {
|
|
|
949
958
|
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
|
950
959
|
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
|
951
960
|
* is determined by the generic type `C`.
|
|
952
|
-
* @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
|
|
953
962
|
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
|
954
963
|
* from the root node of the binary tree.
|
|
955
964
|
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
|
@@ -969,9 +978,9 @@ class BinaryTree {
|
|
|
969
978
|
levelsNodes[level] = [];
|
|
970
979
|
levelsNodes[level].push(callback(node));
|
|
971
980
|
if (includeNull) {
|
|
972
|
-
if (node && node.left
|
|
981
|
+
if (node && this.isNodeOrNull(node.left))
|
|
973
982
|
_recursive(node.left, level + 1);
|
|
974
|
-
if (node && node.right
|
|
983
|
+
if (node && this.isNodeOrNull(node.right))
|
|
975
984
|
_recursive(node.right, level + 1);
|
|
976
985
|
}
|
|
977
986
|
else {
|
|
@@ -992,9 +1001,9 @@ class BinaryTree {
|
|
|
992
1001
|
levelsNodes[level] = [];
|
|
993
1002
|
levelsNodes[level].push(callback(node));
|
|
994
1003
|
if (includeNull) {
|
|
995
|
-
if (node && node.right
|
|
1004
|
+
if (node && this.isNodeOrNull(node.right))
|
|
996
1005
|
stack.push([node.right, level + 1]);
|
|
997
|
-
if (node && node.left
|
|
1006
|
+
if (node && this.isNodeOrNull(node.left))
|
|
998
1007
|
stack.push([node.left, level + 1]);
|
|
999
1008
|
}
|
|
1000
1009
|
else {
|
|
@@ -1053,7 +1062,7 @@ class BinaryTree {
|
|
|
1053
1062
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1054
1063
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1055
1064
|
* following values:
|
|
1056
|
-
* @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
|
|
1057
1066
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1058
1067
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1059
1068
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
@@ -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 |
|
|
36
|
-
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `
|
|
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
|
|
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 |
|
|
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
|
-
* `
|
|
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`, `
|
|
80
|
+
* @returns The `addMany` function returns an array of `N`, `undefined`, or `undefined` values.
|
|
55
81
|
*/
|
|
56
|
-
addMany(keysOrNodes: (BTNKey |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 `
|
|
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> |
|
|
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 |
|
|
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 `
|
|
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 |
|
|
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.
|