data-structure-typed 1.37.3 → 1.37.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/data-structures/binary-tree/avl-tree.d.ts +42 -34
- package/dist/data-structures/binary-tree/avl-tree.js +42 -34
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +265 -168
- package/dist/data-structures/binary-tree/binary-tree.js +257 -170
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +104 -59
- package/dist/data-structures/binary-tree/bst.js +105 -60
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +47 -39
- package/dist/data-structures/binary-tree/tree-multiset.js +47 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +42 -34
- package/lib/data-structures/binary-tree/avl-tree.js +42 -34
- package/lib/data-structures/binary-tree/binary-tree.d.ts +265 -168
- package/lib/data-structures/binary-tree/binary-tree.js +257 -170
- package/lib/data-structures/binary-tree/bst.d.ts +104 -59
- package/lib/data-structures/binary-tree/bst.js +105 -60
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +47 -39
- package/lib/data-structures/binary-tree/tree-multiset.js +47 -39
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +42 -34
- package/src/data-structures/binary-tree/binary-tree.ts +270 -174
- package/src/data-structures/binary-tree/bst.ts +108 -66
- package/src/data-structures/binary-tree/tree-multiset.ts +47 -39
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -14,79 +14,119 @@ export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNod
|
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
|
|
16
16
|
/**
|
|
17
|
-
* The constructor function initializes a binary search tree object with an optional comparator
|
|
18
|
-
*
|
|
17
|
+
* The constructor function initializes a binary search tree object with an optional comparator
|
|
18
|
+
* function.
|
|
19
|
+
* @param {BSTOptions} [options] - An optional object that contains configuration options for the
|
|
20
|
+
* binary search tree.
|
|
19
21
|
*/
|
|
20
22
|
constructor(options?: BSTOptions);
|
|
21
23
|
/**
|
|
22
24
|
* The function creates a new binary search tree node with the given key and value.
|
|
23
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
24
|
-
*
|
|
25
|
-
* @param [val] - The `val`
|
|
26
|
-
*
|
|
25
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
26
|
+
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
27
|
+
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
28
|
+
* represents the value associated with the node in a binary search tree.
|
|
27
29
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
28
30
|
*/
|
|
29
31
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
30
32
|
/**
|
|
31
|
-
* The `add` function
|
|
32
|
-
*
|
|
33
|
-
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
34
|
-
* (which
|
|
35
|
-
* @param [val] - The `val` parameter is
|
|
36
|
-
*
|
|
37
|
-
* @returns
|
|
33
|
+
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
34
|
+
* into the tree.
|
|
35
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
36
|
+
* `BinaryTreeNodeKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
37
|
+
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
38
|
+
* binary search tree.
|
|
39
|
+
* @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.
|
|
38
41
|
*/
|
|
39
42
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
40
43
|
/**
|
|
41
|
-
* The `addMany` function
|
|
42
|
-
*
|
|
43
|
-
* @param {[BinaryTreeNodeKey | N
|
|
44
|
-
*
|
|
45
|
-
*
|
|
44
|
+
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
45
|
+
* maintaining balance.
|
|
46
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
|
|
47
|
+
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
48
|
+
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
49
|
+
* `null
|
|
46
50
|
* @param {N['val'][]} data - The values of tree nodes
|
|
47
51
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
48
|
-
* @param iterationType - The `iterationType` parameter
|
|
49
|
-
*
|
|
52
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
53
|
+
* It can have two possible values:
|
|
54
|
+
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
50
55
|
*/
|
|
51
56
|
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
|
|
52
57
|
/**
|
|
53
|
-
* The function returns the first node in
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
58
|
+
* The function returns the first node in the binary tree that matches the given node property and
|
|
59
|
+
* callback.
|
|
60
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
|
|
61
|
+
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
62
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
63
|
+
* whether a node matches the desired property.
|
|
64
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
65
|
+
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
66
|
+
* whether the node matches the property or not. If no callback function is provided, the default
|
|
67
|
+
* callback function `_defaultCallbackByKey` is used
|
|
68
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
69
|
+
* the root node from which the search should begin.
|
|
70
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
71
|
+
* be performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
72
|
+
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
73
|
+
* matching node is found.
|
|
59
74
|
*/
|
|
60
|
-
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N
|
|
75
|
+
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
61
76
|
/**
|
|
62
|
-
* lastKey returns the
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* @param
|
|
77
|
+
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
78
|
+
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
79
|
+
* rightmost node otherwise.
|
|
80
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
|
|
81
|
+
* key in a binary tree. It represents the root node of the subtree from which the search for the
|
|
82
|
+
* last key should begin. If no specific `beginRoot` is provided, the search will start from the root
|
|
83
|
+
* of the entire binary
|
|
84
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
85
|
+
* be performed when finding the last key. It determines whether the iteration should be performed in
|
|
86
|
+
* pre-order, in-order, or post-order.
|
|
87
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
88
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
89
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
66
90
|
*/
|
|
67
91
|
lastKey(beginRoot?: N | null, iterationType?: IterationType): BinaryTreeNodeKey;
|
|
68
92
|
/**
|
|
69
|
-
* The function `getNodes`
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @param
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* @param
|
|
79
|
-
*
|
|
93
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
94
|
+
* using either recursive or iterative traversal.
|
|
95
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
|
|
96
|
+
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
97
|
+
* generic type `N`.
|
|
98
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
99
|
+
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
100
|
+
* included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
|
|
101
|
+
* a
|
|
102
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
103
|
+
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
104
|
+
* containing only that node. If set to false (default), the function will continue the traversal and
|
|
105
|
+
* return an array containing all nodes that match the node
|
|
106
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
|
|
107
|
+
* specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
|
|
108
|
+
* is `null`, an empty array will be returned.
|
|
109
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
110
|
+
* traverse the binary tree. It can have one of the following values:
|
|
111
|
+
* @returns an array of nodes (N[]).
|
|
80
112
|
*/
|
|
81
113
|
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
82
114
|
/**
|
|
83
|
-
* The `lesserOrGreaterTraverse` function
|
|
84
|
-
* have a
|
|
85
|
-
* @param callback - The `callback` parameter is a function that
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* @param
|
|
89
|
-
*
|
|
115
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
116
|
+
* nodes that have a key value lesser or greater than a target key value.
|
|
117
|
+
* @param callback - The `callback` parameter is a function that will be called for each node that
|
|
118
|
+
* meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
|
|
119
|
+
* and returns a value.
|
|
120
|
+
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
121
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
122
|
+
* of the following values:
|
|
123
|
+
* @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
|
|
124
|
+
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
125
|
+
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
126
|
+
* (`BinaryTreeNodeKey`), or `null` to
|
|
127
|
+
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
128
|
+
* done recursively or iteratively. It can have two possible values:
|
|
129
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
90
130
|
*/
|
|
91
131
|
lesserOrGreaterTraverse(callback?: MapCallback<N>, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>;
|
|
92
132
|
/**
|
|
@@ -99,24 +139,29 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
99
139
|
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
100
140
|
*/
|
|
101
141
|
/**
|
|
102
|
-
* The `perfectlyBalance` function
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
142
|
+
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
143
|
+
* ensures the tree is perfectly balanced.
|
|
144
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
145
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
146
|
+
* values:
|
|
147
|
+
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
105
148
|
*/
|
|
106
149
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
107
150
|
/**
|
|
108
|
-
* The function
|
|
151
|
+
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
152
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
153
|
+
* to check if the AVL tree is balanced. It can have two possible values:
|
|
109
154
|
* @returns a boolean value.
|
|
110
155
|
*/
|
|
111
156
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
112
157
|
protected _comparator: BSTComparator;
|
|
113
158
|
/**
|
|
114
|
-
* The function compares two
|
|
115
|
-
* greater than, less than, or equal to the second
|
|
116
|
-
* @param {BinaryTreeNodeKey} a - "a" is
|
|
117
|
-
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code
|
|
118
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
119
|
-
* than), or CP.eq (equal).
|
|
159
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
160
|
+
* is greater than, less than, or equal to the second value.
|
|
161
|
+
* @param {BinaryTreeNodeKey} a - The parameter "a" is of type BinaryTreeNodeKey.
|
|
162
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code represents a BinaryTreeNodeKey.
|
|
163
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
164
|
+
* than), CP.lt (less than), or CP.eq (equal).
|
|
120
165
|
*/
|
|
121
166
|
protected _compare(a: BinaryTreeNodeKey, b: BinaryTreeNodeKey): CP;
|
|
122
167
|
}
|
|
@@ -8,8 +8,10 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
8
8
|
}
|
|
9
9
|
export class BST extends BinaryTree {
|
|
10
10
|
/**
|
|
11
|
-
* The constructor function initializes a binary search tree object with an optional comparator
|
|
12
|
-
*
|
|
11
|
+
* The constructor function initializes a binary search tree object with an optional comparator
|
|
12
|
+
* function.
|
|
13
|
+
* @param {BSTOptions} [options] - An optional object that contains configuration options for the
|
|
14
|
+
* binary search tree.
|
|
13
15
|
*/
|
|
14
16
|
constructor(options) {
|
|
15
17
|
super(options);
|
|
@@ -23,23 +25,24 @@ export class BST extends BinaryTree {
|
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* The function creates a new binary search tree node with the given key and value.
|
|
26
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
27
|
-
*
|
|
28
|
-
* @param [val] - The `val`
|
|
29
|
-
*
|
|
28
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
29
|
+
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
30
|
+
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It
|
|
31
|
+
* represents the value associated with the node in a binary search tree.
|
|
30
32
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
31
33
|
*/
|
|
32
34
|
createNode(key, val) {
|
|
33
35
|
return new BSTNode(key, val);
|
|
34
36
|
}
|
|
35
37
|
/**
|
|
36
|
-
* The `add` function
|
|
37
|
-
*
|
|
38
|
-
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
39
|
-
* (which
|
|
40
|
-
* @param [val] - The `val` parameter is
|
|
41
|
-
*
|
|
42
|
-
* @returns
|
|
38
|
+
* The `add` function in a binary search tree class inserts a new node with a given key and value
|
|
39
|
+
* into the tree.
|
|
40
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
41
|
+
* `BinaryTreeNodeKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
42
|
+
* @param [val] - The `val` parameter is the value to be assigned to the new node being added to the
|
|
43
|
+
* binary search tree.
|
|
44
|
+
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
45
|
+
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
43
46
|
*/
|
|
44
47
|
add(keyOrNode, val) {
|
|
45
48
|
// TODO support node as a parameter
|
|
@@ -117,15 +120,17 @@ export class BST extends BinaryTree {
|
|
|
117
120
|
return inserted;
|
|
118
121
|
}
|
|
119
122
|
/**
|
|
120
|
-
* The `addMany` function
|
|
121
|
-
*
|
|
122
|
-
* @param {[BinaryTreeNodeKey | N
|
|
123
|
-
*
|
|
124
|
-
*
|
|
123
|
+
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
124
|
+
* maintaining balance.
|
|
125
|
+
* @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
|
|
126
|
+
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
127
|
+
* array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
|
|
128
|
+
* `null
|
|
125
129
|
* @param {N['val'][]} data - The values of tree nodes
|
|
126
130
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
127
|
-
* @param iterationType - The `iterationType` parameter
|
|
128
|
-
*
|
|
131
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
|
|
132
|
+
* It can have two possible values:
|
|
133
|
+
* @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
|
|
129
134
|
*/
|
|
130
135
|
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
131
136
|
// TODO this addMany function is inefficient, it should be optimized
|
|
@@ -197,22 +202,41 @@ export class BST extends BinaryTree {
|
|
|
197
202
|
return inserted;
|
|
198
203
|
}
|
|
199
204
|
/**
|
|
200
|
-
* The function returns the first node in
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
205
|
+
* The function returns the first node in the binary tree that matches the given node property and
|
|
206
|
+
* callback.
|
|
207
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
|
|
208
|
+
* property of the binary tree node that you want to search for. It can be either a specific key
|
|
209
|
+
* value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
|
|
210
|
+
* whether a node matches the desired property.
|
|
211
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
212
|
+
* matches the desired property. It takes a node as input and returns a boolean value indicating
|
|
213
|
+
* whether the node matches the property or not. If no callback function is provided, the default
|
|
214
|
+
* callback function `_defaultCallbackByKey` is used
|
|
215
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
216
|
+
* the root node from which the search should begin.
|
|
217
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
218
|
+
* be performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
219
|
+
* @returns either the first node that matches the given nodeProperty and callback, or null if no
|
|
220
|
+
* matching node is found.
|
|
206
221
|
*/
|
|
207
|
-
get(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
222
|
+
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
208
223
|
var _a;
|
|
209
|
-
return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
224
|
+
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
210
225
|
}
|
|
211
226
|
/**
|
|
212
|
-
* lastKey returns the
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @param
|
|
227
|
+
* The function `lastKey` returns the key of the rightmost node if the comparison result is less
|
|
228
|
+
* than, the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
229
|
+
* rightmost node otherwise.
|
|
230
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting point for finding the last
|
|
231
|
+
* key in a binary tree. It represents the root node of the subtree from which the search for the
|
|
232
|
+
* last key should begin. If no specific `beginRoot` is provided, the search will start from the root
|
|
233
|
+
* of the entire binary
|
|
234
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
235
|
+
* be performed when finding the last key. It determines whether the iteration should be performed in
|
|
236
|
+
* pre-order, in-order, or post-order.
|
|
237
|
+
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
238
|
+
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
239
|
+
* rightmost node otherwise. If no node is found, it returns 0.
|
|
216
240
|
*/
|
|
217
241
|
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
218
242
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -224,17 +248,25 @@ export class BST extends BinaryTree {
|
|
|
224
248
|
return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
225
249
|
}
|
|
226
250
|
/**
|
|
227
|
-
* The function `getNodes`
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* @param
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* @param
|
|
237
|
-
*
|
|
251
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
|
|
252
|
+
* using either recursive or iterative traversal.
|
|
253
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
|
|
254
|
+
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
|
|
255
|
+
* generic type `N`.
|
|
256
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
257
|
+
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
258
|
+
* included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is
|
|
259
|
+
* a
|
|
260
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding
|
|
261
|
+
* the first node that matches the nodeProperty. If set to true, the function will return an array
|
|
262
|
+
* containing only that node. If set to false (default), the function will continue the traversal and
|
|
263
|
+
* return an array containing all nodes that match the node
|
|
264
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the traversal. It
|
|
265
|
+
* specifies the root node of the binary tree from which the traversal should begin. If `beginRoot`
|
|
266
|
+
* is `null`, an empty array will be returned.
|
|
267
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
268
|
+
* traverse the binary tree. It can have one of the following values:
|
|
269
|
+
* @returns an array of nodes (N[]).
|
|
238
270
|
*/
|
|
239
271
|
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
240
272
|
if (!beginRoot)
|
|
@@ -293,13 +325,21 @@ export class BST extends BinaryTree {
|
|
|
293
325
|
}
|
|
294
326
|
// --- start additional functions
|
|
295
327
|
/**
|
|
296
|
-
* The `lesserOrGreaterTraverse` function
|
|
297
|
-
* have a
|
|
298
|
-
* @param callback - The `callback` parameter is a function that
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* @param
|
|
302
|
-
*
|
|
328
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
|
|
329
|
+
* nodes that have a key value lesser or greater than a target key value.
|
|
330
|
+
* @param callback - The `callback` parameter is a function that will be called for each node that
|
|
331
|
+
* meets the condition specified by the `lesserOrGreater` parameter. It takes a node as an argument
|
|
332
|
+
* and returns a value.
|
|
333
|
+
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
334
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
|
|
335
|
+
* of the following values:
|
|
336
|
+
* @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
|
|
337
|
+
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
|
|
338
|
+
* start. It can be either a reference to a specific node (`N`), the key of a node
|
|
339
|
+
* (`BinaryTreeNodeKey`), or `null` to
|
|
340
|
+
* @param iterationType - The `iterationType` parameter determines whether the traversal should be
|
|
341
|
+
* done recursively or iteratively. It can have two possible values:
|
|
342
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
303
343
|
*/
|
|
304
344
|
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
305
345
|
if (typeof targetNode === 'number')
|
|
@@ -352,9 +392,12 @@ export class BST extends BinaryTree {
|
|
|
352
392
|
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
353
393
|
*/
|
|
354
394
|
/**
|
|
355
|
-
* The `perfectlyBalance` function
|
|
356
|
-
*
|
|
357
|
-
* @
|
|
395
|
+
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
396
|
+
* ensures the tree is perfectly balanced.
|
|
397
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
398
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
399
|
+
* values:
|
|
400
|
+
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
358
401
|
*/
|
|
359
402
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
360
403
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
@@ -393,7 +436,9 @@ export class BST extends BinaryTree {
|
|
|
393
436
|
}
|
|
394
437
|
}
|
|
395
438
|
/**
|
|
396
|
-
* The function
|
|
439
|
+
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
440
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
441
|
+
* to check if the AVL tree is balanced. It can have two possible values:
|
|
397
442
|
* @returns a boolean value.
|
|
398
443
|
*/
|
|
399
444
|
isAVLBalanced(iterationType = this.iterationType) {
|
|
@@ -443,12 +488,12 @@ export class BST extends BinaryTree {
|
|
|
443
488
|
return balanced;
|
|
444
489
|
}
|
|
445
490
|
/**
|
|
446
|
-
* The function compares two
|
|
447
|
-
* greater than, less than, or equal to the second
|
|
448
|
-
* @param {BinaryTreeNodeKey} a - "a" is
|
|
449
|
-
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code
|
|
450
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
451
|
-
* than), or CP.eq (equal).
|
|
491
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
492
|
+
* is greater than, less than, or equal to the second value.
|
|
493
|
+
* @param {BinaryTreeNodeKey} a - The parameter "a" is of type BinaryTreeNodeKey.
|
|
494
|
+
* @param {BinaryTreeNodeKey} b - The parameter "b" in the above code represents a BinaryTreeNodeKey.
|
|
495
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
|
|
496
|
+
* than), CP.lt (less than), or CP.eq (equal).
|
|
452
497
|
*/
|
|
453
498
|
_compare(a, b) {
|
|
454
499
|
const compared = this._comparator(a, b);
|
|
@@ -47,67 +47,75 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
47
47
|
*/
|
|
48
48
|
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
|
49
49
|
/**
|
|
50
|
-
* The function swaps the
|
|
51
|
-
* @param {N} srcNode - The source node that
|
|
52
|
-
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
53
|
-
* be swapped
|
|
54
|
-
* @returns the `destNode` after swapping its
|
|
50
|
+
* The function swaps the values of two nodes in a binary tree.
|
|
51
|
+
* @param {N} srcNode - The source node that needs to be swapped with the destination node.
|
|
52
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
53
|
+
* from `srcNode` will be swapped into.
|
|
54
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
55
55
|
*/
|
|
56
56
|
protected _swap(srcNode: N, destNode: N): N;
|
|
57
57
|
/**
|
|
58
|
-
* The `add` function adds a new node to a binary search tree,
|
|
59
|
-
* necessary.
|
|
60
|
-
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a
|
|
61
|
-
* represents a `
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
64
|
-
*
|
|
65
|
-
* @
|
|
58
|
+
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
59
|
+
* exists, and balancing the tree if necessary.
|
|
60
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
61
|
+
* `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
62
|
+
* node to be added), or `null` (which represents a null node).
|
|
63
|
+
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
64
|
+
* added to the binary tree.
|
|
65
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
66
|
+
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
67
|
+
* count is specified, the default count will be 1.
|
|
68
|
+
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
66
69
|
*/
|
|
67
70
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
|
|
68
71
|
/**
|
|
69
|
-
* The function adds a new node to a binary tree if there is an available slot
|
|
70
|
-
* node
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
72
|
+
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
73
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
74
|
+
* the tree. It can be either a node object (`N`) or `null`.
|
|
75
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
76
|
+
* be added as a child.
|
|
77
|
+
* @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
76
78
|
*/
|
|
77
79
|
_addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
78
80
|
/**
|
|
79
|
-
* The `addMany` function
|
|
80
|
-
*
|
|
81
|
-
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of
|
|
82
|
-
*
|
|
83
|
-
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values
|
|
84
|
-
* the nodes being added. It is used
|
|
85
|
-
*
|
|
81
|
+
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
82
|
+
* inserted nodes.
|
|
83
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
84
|
+
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
85
|
+
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
|
|
86
|
+
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
87
|
+
* each key or node.
|
|
86
88
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
87
89
|
*/
|
|
88
90
|
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
89
91
|
/**
|
|
90
|
-
* The `perfectlyBalance` function
|
|
91
|
-
*
|
|
92
|
-
* @
|
|
92
|
+
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
93
|
+
* binary search tree using either a recursive or iterative approach.
|
|
94
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
95
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
96
|
+
* values:
|
|
97
|
+
* @returns a boolean value.
|
|
93
98
|
*/
|
|
94
99
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
95
100
|
/**
|
|
96
|
-
* The `delete` function
|
|
97
|
-
* node that needs to be balanced.
|
|
98
|
-
* @param {N | BinaryTreeNodeKey
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
101
|
+
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
102
|
+
* node along with the parent node that needs to be balanced.
|
|
103
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
|
|
104
|
+
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
|
|
105
|
+
* from the binary tree.
|
|
106
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
107
|
+
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
108
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
109
|
+
* decremented by 1 and
|
|
110
|
+
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
111
|
*/
|
|
104
112
|
delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
113
|
/**
|
|
106
|
-
* The clear() function clears the data and sets the count to
|
|
114
|
+
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
107
115
|
*/
|
|
108
116
|
clear(): void;
|
|
109
117
|
/**
|
|
110
|
-
* The function
|
|
118
|
+
* The function sets the value of the "_count" property.
|
|
111
119
|
* @param {number} v - number
|
|
112
120
|
*/
|
|
113
121
|
protected _setCount(v: number): void;
|