min-heap-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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/data-structures/binary-tree/binary-tree.d.ts +57 -53
- package/dist/data-structures/binary-tree/binary-tree.js +116 -54
- package/dist/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/data-structures/binary-tree/bst.js +77 -21
- package/dist/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +10 -10
- package/dist/data-structures/binary-tree/tree-multiset.js +20 -17
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +227 -158
- 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/data-structures/graph/abstract-graph.ts +18 -14
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
|
@@ -88,7 +88,7 @@ class BinaryTree {
|
|
|
88
88
|
*/
|
|
89
89
|
constructor(options) {
|
|
90
90
|
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
91
|
-
this._root =
|
|
91
|
+
this._root = undefined;
|
|
92
92
|
this._size = 0;
|
|
93
93
|
this.defaultOneParamCallback = (node) => node.key;
|
|
94
94
|
if (options !== undefined) {
|
|
@@ -121,7 +121,7 @@ class BinaryTree {
|
|
|
121
121
|
* Clear the binary tree, removing all nodes.
|
|
122
122
|
*/
|
|
123
123
|
clear() {
|
|
124
|
-
this._setRoot(
|
|
124
|
+
this._setRoot(undefined);
|
|
125
125
|
this._size = 0;
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
@@ -293,10 +293,10 @@ class BinaryTree {
|
|
|
293
293
|
/**
|
|
294
294
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
295
295
|
* specified root node.
|
|
296
|
-
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
296
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
|
|
297
297
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
298
298
|
* of the node (`BTNKey`), or `null`.
|
|
299
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
299
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
300
300
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
301
301
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
302
302
|
* node of the binary tree.
|
|
@@ -320,7 +320,7 @@ class BinaryTree {
|
|
|
320
320
|
/**
|
|
321
321
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
322
322
|
* iterative approach.
|
|
323
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
323
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
324
324
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
325
325
|
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
326
326
|
* node is specified. If `
|
|
@@ -366,7 +366,7 @@ class BinaryTree {
|
|
|
366
366
|
/**
|
|
367
367
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
368
368
|
* recursive or iterative approach.
|
|
369
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
369
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
370
370
|
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
371
371
|
* not provided.
|
|
372
372
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -420,7 +420,7 @@ class BinaryTree {
|
|
|
420
420
|
/**
|
|
421
421
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
422
422
|
* height of the tree.
|
|
423
|
-
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
423
|
+
* @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
|
|
424
424
|
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
425
425
|
* @returns The method is returning a boolean value.
|
|
426
426
|
*/
|
|
@@ -441,7 +441,7 @@ class BinaryTree {
|
|
|
441
441
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
442
442
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
443
443
|
* function will continue searching for all
|
|
444
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
444
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
445
445
|
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
446
446
|
* tree.
|
|
447
447
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
@@ -574,7 +574,7 @@ class BinaryTree {
|
|
|
574
574
|
/**
|
|
575
575
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
576
576
|
* iterative traversal.
|
|
577
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
577
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
578
578
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
579
579
|
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
580
580
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -608,8 +608,8 @@ class BinaryTree {
|
|
|
608
608
|
/**
|
|
609
609
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
610
610
|
* iteratively.
|
|
611
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
612
|
-
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
611
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
612
|
+
* find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
|
|
613
613
|
* or `null`. If it is `null`, it means there is no starting node
|
|
614
614
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
615
615
|
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
@@ -698,7 +698,7 @@ class BinaryTree {
|
|
|
698
698
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
699
699
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
700
700
|
* an array.
|
|
701
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
701
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
702
702
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
703
703
|
* start from the root of the tree.
|
|
704
704
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -717,12 +717,12 @@ class BinaryTree {
|
|
|
717
717
|
if (cur !== undefined) {
|
|
718
718
|
ans.push(callback(cur));
|
|
719
719
|
if (includeNull) {
|
|
720
|
-
cur
|
|
721
|
-
cur
|
|
720
|
+
cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
721
|
+
cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
722
722
|
}
|
|
723
723
|
else {
|
|
724
|
-
cur
|
|
725
|
-
cur
|
|
724
|
+
cur && cur.left && _traverse(cur.left);
|
|
725
|
+
cur && cur.right && _traverse(cur.right);
|
|
726
726
|
}
|
|
727
727
|
}
|
|
728
728
|
};
|
|
@@ -735,18 +735,27 @@ class BinaryTree {
|
|
|
735
735
|
if (cur !== undefined) {
|
|
736
736
|
ans.push(callback(cur));
|
|
737
737
|
if (includeNull) {
|
|
738
|
-
cur
|
|
739
|
-
cur
|
|
738
|
+
cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
739
|
+
cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
740
740
|
}
|
|
741
741
|
else {
|
|
742
|
-
cur
|
|
743
|
-
cur
|
|
742
|
+
cur && cur.right && stack.push(cur.right);
|
|
743
|
+
cur && cur.left && stack.push(cur.left);
|
|
744
744
|
}
|
|
745
745
|
}
|
|
746
746
|
}
|
|
747
747
|
}
|
|
748
748
|
return ans;
|
|
749
749
|
}
|
|
750
|
+
isNode(node) {
|
|
751
|
+
return node instanceof BinaryTreeNode && node.key.toString() !== 'NaN';
|
|
752
|
+
}
|
|
753
|
+
isNIL(node) {
|
|
754
|
+
return node instanceof BinaryTreeNode && node.key.toString() === 'NaN';
|
|
755
|
+
}
|
|
756
|
+
isNodeOrNull(node) {
|
|
757
|
+
return this.isNode(node) || node === null;
|
|
758
|
+
}
|
|
750
759
|
/**
|
|
751
760
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
752
761
|
* function on each node according to a specified order pattern.
|
|
@@ -755,7 +764,7 @@ class BinaryTree {
|
|
|
755
764
|
* is `this.defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
|
|
756
765
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
757
766
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
758
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
767
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
759
768
|
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
760
769
|
* is `null`, an empty array will be returned.
|
|
761
770
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -772,50 +781,50 @@ class BinaryTree {
|
|
|
772
781
|
switch (pattern) {
|
|
773
782
|
case 'in':
|
|
774
783
|
if (includeNull) {
|
|
775
|
-
if (node
|
|
784
|
+
if (node && this.isNodeOrNull(node.left))
|
|
776
785
|
_traverse(node.left);
|
|
777
|
-
ans.push(callback(node));
|
|
778
|
-
if (node
|
|
786
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
787
|
+
if (node && this.isNodeOrNull(node.right))
|
|
779
788
|
_traverse(node.right);
|
|
780
789
|
}
|
|
781
790
|
else {
|
|
782
|
-
if (node
|
|
791
|
+
if (node && node.left)
|
|
783
792
|
_traverse(node.left);
|
|
784
|
-
ans.push(callback(node));
|
|
785
|
-
if (node
|
|
793
|
+
this.isNode(node) && ans.push(callback(node));
|
|
794
|
+
if (node && node.right)
|
|
786
795
|
_traverse(node.right);
|
|
787
796
|
}
|
|
788
797
|
break;
|
|
789
798
|
case 'pre':
|
|
790
799
|
if (includeNull) {
|
|
791
|
-
ans.push(callback(node));
|
|
792
|
-
if (node
|
|
800
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
801
|
+
if (node && this.isNodeOrNull(node.left))
|
|
793
802
|
_traverse(node.left);
|
|
794
|
-
if (node
|
|
803
|
+
if (node && this.isNodeOrNull(node.right))
|
|
795
804
|
_traverse(node.right);
|
|
796
805
|
}
|
|
797
806
|
else {
|
|
798
|
-
ans.push(callback(node));
|
|
799
|
-
if (node
|
|
807
|
+
this.isNode(node) && ans.push(callback(node));
|
|
808
|
+
if (node && node.left)
|
|
800
809
|
_traverse(node.left);
|
|
801
|
-
if (node
|
|
810
|
+
if (node && node.right)
|
|
802
811
|
_traverse(node.right);
|
|
803
812
|
}
|
|
804
813
|
break;
|
|
805
814
|
case 'post':
|
|
806
815
|
if (includeNull) {
|
|
807
|
-
if (node
|
|
816
|
+
if (node && this.isNodeOrNull(node.left))
|
|
808
817
|
_traverse(node.left);
|
|
809
|
-
if (node
|
|
818
|
+
if (node && this.isNodeOrNull(node.right))
|
|
810
819
|
_traverse(node.right);
|
|
811
|
-
ans.push(callback(node));
|
|
820
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
812
821
|
}
|
|
813
822
|
else {
|
|
814
|
-
if (node
|
|
823
|
+
if (node && node.left)
|
|
815
824
|
_traverse(node.left);
|
|
816
|
-
if (node
|
|
825
|
+
if (node && node.right)
|
|
817
826
|
_traverse(node.right);
|
|
818
|
-
ans.push(callback(node));
|
|
827
|
+
this.isNode(node) && ans.push(callback(node));
|
|
819
828
|
}
|
|
820
829
|
break;
|
|
821
830
|
}
|
|
@@ -827,7 +836,7 @@ class BinaryTree {
|
|
|
827
836
|
const stack = [{ opt: 0, node: beginRoot }];
|
|
828
837
|
while (stack.length > 0) {
|
|
829
838
|
const cur = stack.pop();
|
|
830
|
-
if (cur === undefined)
|
|
839
|
+
if (cur === undefined || this.isNIL(cur.node))
|
|
831
840
|
continue;
|
|
832
841
|
if (includeNull) {
|
|
833
842
|
if (cur.node === undefined)
|
|
@@ -874,7 +883,7 @@ class BinaryTree {
|
|
|
874
883
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
875
884
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
876
885
|
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
|
|
877
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
886
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
878
887
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
879
888
|
* will not be performed and an empty array will be returned.
|
|
880
889
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
@@ -894,9 +903,9 @@ class BinaryTree {
|
|
|
894
903
|
const current = queue.shift();
|
|
895
904
|
ans.push(callback(current));
|
|
896
905
|
if (includeNull) {
|
|
897
|
-
if (current && current.left
|
|
906
|
+
if (current && this.isNodeOrNull(current.left))
|
|
898
907
|
queue.push(current.left);
|
|
899
|
-
if (current && current.right
|
|
908
|
+
if (current && this.isNodeOrNull(current.right))
|
|
900
909
|
queue.push(current.right);
|
|
901
910
|
}
|
|
902
911
|
else {
|
|
@@ -917,9 +926,9 @@ class BinaryTree {
|
|
|
917
926
|
const current = queue.shift();
|
|
918
927
|
ans.push(callback(current));
|
|
919
928
|
if (includeNull) {
|
|
920
|
-
if (current
|
|
929
|
+
if (current && this.isNodeOrNull(current.left))
|
|
921
930
|
queue.push(current.left);
|
|
922
|
-
if (current
|
|
931
|
+
if (current && this.isNodeOrNull(current.right))
|
|
923
932
|
queue.push(current.right);
|
|
924
933
|
}
|
|
925
934
|
else {
|
|
@@ -939,7 +948,7 @@ class BinaryTree {
|
|
|
939
948
|
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
|
940
949
|
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
|
941
950
|
* is determined by the generic type `C`.
|
|
942
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
951
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
943
952
|
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
|
944
953
|
* from the root node of the binary tree.
|
|
945
954
|
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
|
@@ -959,9 +968,9 @@ class BinaryTree {
|
|
|
959
968
|
levelsNodes[level] = [];
|
|
960
969
|
levelsNodes[level].push(callback(node));
|
|
961
970
|
if (includeNull) {
|
|
962
|
-
if (node && node.left
|
|
971
|
+
if (node && this.isNodeOrNull(node.left))
|
|
963
972
|
_recursive(node.left, level + 1);
|
|
964
|
-
if (node && node.right
|
|
973
|
+
if (node && this.isNodeOrNull(node.right))
|
|
965
974
|
_recursive(node.right, level + 1);
|
|
966
975
|
}
|
|
967
976
|
else {
|
|
@@ -982,9 +991,9 @@ class BinaryTree {
|
|
|
982
991
|
levelsNodes[level] = [];
|
|
983
992
|
levelsNodes[level].push(callback(node));
|
|
984
993
|
if (includeNull) {
|
|
985
|
-
if (node && node.right
|
|
994
|
+
if (node && this.isNodeOrNull(node.right))
|
|
986
995
|
stack.push([node.right, level + 1]);
|
|
987
|
-
if (node && node.left
|
|
996
|
+
if (node && this.isNodeOrNull(node.left))
|
|
988
997
|
stack.push([node.left, level + 1]);
|
|
989
998
|
}
|
|
990
999
|
else {
|
|
@@ -1034,7 +1043,6 @@ class BinaryTree {
|
|
|
1034
1043
|
}
|
|
1035
1044
|
return y;
|
|
1036
1045
|
}
|
|
1037
|
-
// --- start additional methods ---
|
|
1038
1046
|
/**
|
|
1039
1047
|
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
1040
1048
|
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
@@ -1044,7 +1052,7 @@ class BinaryTree {
|
|
|
1044
1052
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1045
1053
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1046
1054
|
* following values:
|
|
1047
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1055
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1048
1056
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1049
1057
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1050
1058
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
@@ -1133,6 +1141,7 @@ class BinaryTree {
|
|
|
1133
1141
|
}
|
|
1134
1142
|
return ans;
|
|
1135
1143
|
}
|
|
1144
|
+
// --- start additional methods ---
|
|
1136
1145
|
/**
|
|
1137
1146
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
1138
1147
|
* either an iterative or recursive manner.
|
|
@@ -1192,7 +1201,7 @@ class BinaryTree {
|
|
|
1192
1201
|
}
|
|
1193
1202
|
/**
|
|
1194
1203
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1195
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1204
|
+
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1196
1205
|
* the binary tree. It can be either a node object or `null`.
|
|
1197
1206
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
1198
1207
|
* be added as a child.
|
|
@@ -1230,7 +1239,7 @@ class BinaryTree {
|
|
|
1230
1239
|
/**
|
|
1231
1240
|
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1232
1241
|
* it also sets the parent property of the value to undefined.
|
|
1233
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1242
|
+
* @param {N | null | undefined} v - The parameter `v` is of type `N | null | undefined`, which means it can either be of
|
|
1234
1243
|
* type `N` or `null`.
|
|
1235
1244
|
*/
|
|
1236
1245
|
_setRoot(v) {
|
|
@@ -1239,5 +1248,58 @@ class BinaryTree {
|
|
|
1239
1248
|
}
|
|
1240
1249
|
this._root = v;
|
|
1241
1250
|
}
|
|
1251
|
+
print(beginRoot = this.root) {
|
|
1252
|
+
const display = (root) => {
|
|
1253
|
+
const [lines, , ,] = _displayAux(root);
|
|
1254
|
+
for (const line of lines) {
|
|
1255
|
+
console.log(line);
|
|
1256
|
+
}
|
|
1257
|
+
};
|
|
1258
|
+
const _displayAux = (node) => {
|
|
1259
|
+
if (node === undefined || node === null) {
|
|
1260
|
+
return [[], 0, 0, 0];
|
|
1261
|
+
}
|
|
1262
|
+
if (node && node.right === undefined && node.left === undefined) {
|
|
1263
|
+
const line = `${node.key}`;
|
|
1264
|
+
const width = line.length;
|
|
1265
|
+
const height = 1;
|
|
1266
|
+
const middle = Math.floor(width / 2);
|
|
1267
|
+
return [[line], width, height, middle];
|
|
1268
|
+
}
|
|
1269
|
+
if (node && node.right === undefined) {
|
|
1270
|
+
const [lines, n, p, x] = _displayAux(node.left);
|
|
1271
|
+
const s = `${node.key}`;
|
|
1272
|
+
const u = s.length;
|
|
1273
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1274
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1275
|
+
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1276
|
+
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1277
|
+
}
|
|
1278
|
+
if (node && node.left === undefined) {
|
|
1279
|
+
const [lines, n, p, u] = _displayAux(node.right);
|
|
1280
|
+
const s = `${node.key}`;
|
|
1281
|
+
const x = s.length;
|
|
1282
|
+
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1283
|
+
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1284
|
+
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1285
|
+
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1286
|
+
}
|
|
1287
|
+
const [left, n, p, x] = _displayAux(node.left);
|
|
1288
|
+
const [right, m, q, y] = _displayAux(node.right);
|
|
1289
|
+
const s = `${node.key}`;
|
|
1290
|
+
const u = s.length;
|
|
1291
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1292
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1293
|
+
if (p < q) {
|
|
1294
|
+
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1295
|
+
}
|
|
1296
|
+
else if (q < p) {
|
|
1297
|
+
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1298
|
+
}
|
|
1299
|
+
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1300
|
+
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1301
|
+
};
|
|
1302
|
+
display(beginRoot);
|
|
1303
|
+
}
|
|
1242
1304
|
}
|
|
1243
1305
|
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.
|