data-structure-typed 1.37.2 → 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 +3 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +44 -38
- package/dist/data-structures/binary-tree/avl-tree.js +46 -40
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/dist/data-structures/binary-tree/binary-tree.js +304 -201
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +111 -64
- package/dist/data-structures/binary-tree/bst.js +132 -85
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree.js +6 -6
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +44 -38
- package/lib/data-structures/binary-tree/avl-tree.js +46 -40
- package/lib/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/lib/data-structures/binary-tree/binary-tree.js +305 -202
- package/lib/data-structures/binary-tree/bst.d.ts +111 -64
- package/lib/data-structures/binary-tree/bst.js +133 -86
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/lib/data-structures/binary-tree/tree-multiset.js +50 -42
- package/lib/types/data-structures/binary-tree.d.ts +2 -2
- package/lib/types/data-structures/binary-tree.js +5 -5
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +46 -40
- package/src/data-structures/binary-tree/binary-tree.ts +328 -207
- package/src/data-structures/binary-tree/bst.ts +135 -88
- package/src/data-structures/binary-tree/tree-multiset.ts +50 -42
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +7 -8
- package/test/integration/bst.test.ts +17 -16
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/utils/big-o.ts +2 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -6,85 +6,116 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BFSCallback, BFSCallbackReturn, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback, MapCallbackReturn } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition,
|
|
9
|
+
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
/**
|
|
12
|
+
* Represents a node in a binary tree.
|
|
13
|
+
* @template V - The type of data stored in the node.
|
|
14
|
+
* @template FAMILY - The type of the family relationship in the binary tree.
|
|
15
|
+
*/
|
|
11
16
|
export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
|
|
12
17
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
15
|
-
*
|
|
16
|
-
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
|
17
|
-
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
18
|
+
* Creates a new instance of BinaryTreeNode.
|
|
19
|
+
* @param {BinaryTreeNodeKey} key - The key associated with the node.
|
|
20
|
+
* @param {V} val - The value stored in the node.
|
|
18
21
|
*/
|
|
19
22
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
23
|
+
/**
|
|
24
|
+
* The key associated with the node.
|
|
25
|
+
*/
|
|
20
26
|
key: BinaryTreeNodeKey;
|
|
27
|
+
/**
|
|
28
|
+
* The value stored in the node.
|
|
29
|
+
*/
|
|
21
30
|
val: V | undefined;
|
|
22
31
|
private _left;
|
|
32
|
+
/**
|
|
33
|
+
* Get the left child node.
|
|
34
|
+
*/
|
|
23
35
|
get left(): FAMILY | null | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Set the left child node.
|
|
38
|
+
* @param {FAMILY | null | undefined} v - The left child node.
|
|
39
|
+
*/
|
|
24
40
|
set left(v: FAMILY | null | undefined);
|
|
25
41
|
private _right;
|
|
42
|
+
/**
|
|
43
|
+
* Get the right child node.
|
|
44
|
+
*/
|
|
26
45
|
get right(): FAMILY | null | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Set the right child node.
|
|
48
|
+
* @param {FAMILY | null | undefined} v - The right child node.
|
|
49
|
+
*/
|
|
27
50
|
set right(v: FAMILY | null | undefined);
|
|
51
|
+
/**
|
|
52
|
+
* The parent node of the current node.
|
|
53
|
+
*/
|
|
28
54
|
parent: FAMILY | null | undefined;
|
|
29
55
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @returns
|
|
56
|
+
* Get the position of the node within its family.
|
|
57
|
+
* @returns {FamilyPosition} - The family position of the node.
|
|
32
58
|
*/
|
|
33
59
|
get familyPosition(): FamilyPosition;
|
|
34
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Represents a binary tree data structure.
|
|
63
|
+
* @template N - The type of the binary tree's nodes.
|
|
64
|
+
*/
|
|
35
65
|
export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
|
|
36
66
|
/**
|
|
37
|
-
*
|
|
38
|
-
* @param {BinaryTreeOptions} [options] - The
|
|
39
|
-
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
40
|
-
* different configuration options.
|
|
67
|
+
* Creates a new instance of BinaryTree.
|
|
68
|
+
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
41
69
|
*/
|
|
42
70
|
constructor(options?: BinaryTreeOptions);
|
|
43
71
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
46
|
-
*
|
|
47
|
-
* @
|
|
48
|
-
* stored in the node.
|
|
49
|
-
* @returns a new instance of a BinaryTreeNode with the specified key and value.
|
|
72
|
+
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
73
|
+
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
74
|
+
* @param {N['val']} val - The value for the new node.
|
|
75
|
+
* @returns {N} - The newly created BinaryTreeNode.
|
|
50
76
|
*/
|
|
51
77
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
52
78
|
private _root;
|
|
79
|
+
/**
|
|
80
|
+
* Get the root node of the binary tree.
|
|
81
|
+
*/
|
|
53
82
|
get root(): N | null;
|
|
54
83
|
private _size;
|
|
84
|
+
/**
|
|
85
|
+
* Get the number of nodes in the binary tree.
|
|
86
|
+
*/
|
|
55
87
|
get size(): number;
|
|
56
88
|
private _loopType;
|
|
57
|
-
get loopType(): LoopType;
|
|
58
|
-
set loopType(v: LoopType);
|
|
59
89
|
/**
|
|
60
|
-
*
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
*
|
|
90
|
+
* Get the iteration type used in the binary tree.
|
|
91
|
+
*/
|
|
92
|
+
get iterationType(): IterationType;
|
|
93
|
+
/**
|
|
94
|
+
* Set the iteration type for the binary tree.
|
|
95
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
96
|
+
*/
|
|
97
|
+
set iterationType(v: IterationType);
|
|
98
|
+
/**
|
|
99
|
+
* Swap the data of two nodes in the binary tree.
|
|
100
|
+
* @param {N} srcNode - The source node to swap.
|
|
101
|
+
* @param {N} destNode - The destination node to swap.
|
|
102
|
+
* @returns {N} - The destination node after the swap.
|
|
65
103
|
*/
|
|
66
104
|
protected _swap(srcNode: N, destNode: N): N;
|
|
67
105
|
/**
|
|
68
|
-
*
|
|
106
|
+
* Clear the binary tree, removing all nodes.
|
|
69
107
|
*/
|
|
70
108
|
clear(): void;
|
|
71
109
|
/**
|
|
72
|
-
*
|
|
73
|
-
* @returns
|
|
110
|
+
* Check if the binary tree is empty.
|
|
111
|
+
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
74
112
|
*/
|
|
75
113
|
isEmpty(): boolean;
|
|
76
114
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
82
|
-
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
|
83
|
-
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
84
|
-
* node itself. It can also be `null` if no node is specified.
|
|
85
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
86
|
-
* being added to the binary tree.
|
|
87
|
-
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
115
|
+
* Add a node with the given key and value to the binary tree.
|
|
116
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
117
|
+
* @param {N['val']} val - The value for the new node (optional).
|
|
118
|
+
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
88
119
|
*/
|
|
89
120
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
90
121
|
/**
|
|
@@ -92,12 +123,12 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
92
123
|
* values, and adds them to the binary tree.
|
|
93
124
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
94
125
|
* objects, or null values.
|
|
95
|
-
* @param {N['val'][]} [
|
|
96
|
-
* the nodes or node IDs being added. It is used to set the value of each node being added. If `
|
|
126
|
+
* @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
127
|
+
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
97
128
|
* the value of the nodes will be `undefined`.
|
|
98
129
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
99
130
|
*/
|
|
100
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
131
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[];
|
|
101
132
|
/**
|
|
102
133
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
103
134
|
* @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
@@ -109,188 +140,270 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
109
140
|
*/
|
|
110
141
|
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean;
|
|
111
142
|
/**
|
|
112
|
-
* The `delete` function
|
|
113
|
-
*
|
|
114
|
-
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node
|
|
115
|
-
*
|
|
116
|
-
*
|
|
143
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
144
|
+
* with the parent node that needs to be balanced.
|
|
145
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
|
|
146
|
+
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
147
|
+
* binary tree.
|
|
148
|
+
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
117
149
|
*/
|
|
118
150
|
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
119
151
|
/**
|
|
120
|
-
* The function calculates the depth of a node in a binary tree
|
|
121
|
-
*
|
|
122
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
123
|
-
*
|
|
152
|
+
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
153
|
+
* specified root node.
|
|
154
|
+
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
|
|
155
|
+
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
156
|
+
* of the node (`BinaryTreeNodeKey`), or `null`.
|
|
157
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
|
|
158
|
+
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
159
|
+
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
160
|
+
* node of the binary tree.
|
|
161
|
+
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
124
162
|
*/
|
|
125
163
|
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot?: N | BinaryTreeNodeKey | null): number;
|
|
126
164
|
/**
|
|
127
|
-
* The `getHeight` function calculates the maximum height of a binary tree
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
* node
|
|
165
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
166
|
+
* iterative approach.
|
|
167
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
|
|
168
|
+
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
169
|
+
* object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
|
|
170
|
+
* node is specified. If `
|
|
171
|
+
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
172
|
+
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
173
|
+
* possible values:
|
|
131
174
|
* @returns the height of the binary tree.
|
|
132
175
|
*/
|
|
133
|
-
getHeight(beginRoot?: N | BinaryTreeNodeKey | null): number;
|
|
176
|
+
getHeight(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): number;
|
|
134
177
|
protected _defaultCallbackByKey: MapCallback<N>;
|
|
135
178
|
/**
|
|
136
|
-
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
137
|
-
* approach.
|
|
138
|
-
* @param {N | null}
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
* @
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
*
|
|
148
|
-
*
|
|
179
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
180
|
+
* recursive or iterative approach.
|
|
181
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
182
|
+
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
183
|
+
* not provided.
|
|
184
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
185
|
+
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
186
|
+
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
187
|
+
*/
|
|
188
|
+
getMinHeight(beginRoot?: N | null, iterationType?: IterationType): number;
|
|
189
|
+
/**
|
|
190
|
+
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
191
|
+
* height of the tree.
|
|
192
|
+
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
193
|
+
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
149
194
|
* @returns The method is returning a boolean value.
|
|
150
195
|
*/
|
|
151
196
|
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
152
197
|
/**
|
|
153
|
-
* The function `getNodes` returns an array of nodes that match a given property
|
|
154
|
-
*
|
|
155
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter
|
|
156
|
-
* generic type `N`. It represents the property of the
|
|
157
|
-
*
|
|
158
|
-
* @param
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* @
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
* The
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
198
|
+
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
199
|
+
* recursive or iterative traversal.
|
|
200
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
|
|
201
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
202
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
203
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
204
|
+
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
205
|
+
* included in the result. The `callback` parameter has a default value of
|
|
206
|
+
* `this._defaultCallbackByKey`, which
|
|
207
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
208
|
+
* first node that matches the nodeProperty. If set to true, the function will return an array with
|
|
209
|
+
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
210
|
+
* function will continue searching for all
|
|
211
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
212
|
+
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
213
|
+
* tree.
|
|
214
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
215
|
+
* traverse the binary tree. It can have two possible values:
|
|
216
|
+
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
217
|
+
*/
|
|
218
|
+
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
219
|
+
/**
|
|
220
|
+
* The function checks if a binary tree has a node with a given property or key.
|
|
221
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
|
|
222
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
223
|
+
* generic type `N`.
|
|
224
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
225
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
226
|
+
* whether the node matches the criteria or not. The default callback function
|
|
227
|
+
* `this._defaultCallbackByKey` is used if no callback function is
|
|
228
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
229
|
+
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
230
|
+
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
231
|
+
* as
|
|
232
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
233
|
+
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
171
234
|
* @returns a boolean value.
|
|
172
235
|
*/
|
|
173
|
-
has(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N
|
|
174
|
-
/**
|
|
175
|
-
* The function returns the first node that matches the given property
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
236
|
+
has(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
237
|
+
/**
|
|
238
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
239
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
|
|
240
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
241
|
+
* type.
|
|
242
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
243
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
244
|
+
* whether the node matches the criteria or not. The default callback function
|
|
245
|
+
* (`this._defaultCallbackByKey`) is used if no callback function is
|
|
246
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
247
|
+
* the root node from which the search should begin.
|
|
248
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
249
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
250
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
251
|
+
*/
|
|
252
|
+
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
253
|
+
/**
|
|
254
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
255
|
+
* up to the root node, with the option to reverse the order of the nodes.
|
|
256
|
+
* @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
|
|
257
|
+
* to find the path to the root node.
|
|
258
|
+
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
259
|
+
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
260
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
261
|
+
* @returns The function `getPathToRoot` returns an array of type `N[]`.
|
|
195
262
|
*/
|
|
196
263
|
getPathToRoot(beginRoot: N, isReverse?: boolean): N[];
|
|
197
264
|
/**
|
|
198
|
-
* The function `getLeftMost` returns the leftmost node in a binary tree,
|
|
199
|
-
*
|
|
200
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
201
|
-
*
|
|
202
|
-
* node), or `null
|
|
203
|
-
* @
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* node
|
|
207
|
-
*/
|
|
208
|
-
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null): N | null;
|
|
209
|
-
/**
|
|
210
|
-
* The `getRightMost`
|
|
211
|
-
*
|
|
212
|
-
* @param {N | null}
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* @
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*
|
|
265
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
266
|
+
* iterative traversal.
|
|
267
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
268
|
+
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
269
|
+
* of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
|
|
270
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
271
|
+
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
272
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
273
|
+
* no leftmost node, it returns `null`.
|
|
274
|
+
*/
|
|
275
|
+
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): N | null;
|
|
276
|
+
/**
|
|
277
|
+
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
278
|
+
* iteratively.
|
|
279
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
280
|
+
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
281
|
+
* or `null`. If it is `null`, it means there is no starting node
|
|
282
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
283
|
+
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
284
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
|
|
285
|
+
* `beginRoot` parameter is `null`, it returns `null`.
|
|
286
|
+
*/
|
|
287
|
+
getRightMost(beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
288
|
+
/**
|
|
289
|
+
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
290
|
+
* @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
|
|
291
|
+
* to check if it is a binary search tree (BST) subtree.
|
|
292
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
293
|
+
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
294
|
+
* possible values:
|
|
295
|
+
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
296
|
+
*/
|
|
297
|
+
isSubtreeBST(beginRoot: N, iterationType?: IterationType): boolean;
|
|
298
|
+
/**
|
|
299
|
+
* The function checks if a binary tree is a binary search tree.
|
|
300
|
+
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
301
|
+
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
302
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
|
|
303
|
+
* provided in
|
|
223
304
|
* @returns a boolean value.
|
|
224
305
|
*/
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* The function
|
|
228
|
-
*
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
*
|
|
233
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* @param
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* @
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
*
|
|
252
|
-
* @param {
|
|
253
|
-
*
|
|
254
|
-
* @
|
|
255
|
-
*/
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* The function
|
|
259
|
-
*
|
|
260
|
-
* @
|
|
306
|
+
isBST(iterationType?: IterationType): boolean;
|
|
307
|
+
/**
|
|
308
|
+
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
309
|
+
* node, either recursively or iteratively.
|
|
310
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
311
|
+
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
312
|
+
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
313
|
+
* an array.
|
|
314
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
315
|
+
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
316
|
+
* start from the root of the tree.
|
|
317
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
318
|
+
* performed on the binary tree. It can have two possible values:
|
|
319
|
+
* @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
320
|
+
*/
|
|
321
|
+
subTreeTraverse(callback?: MapCallback<N>, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>[];
|
|
322
|
+
/**
|
|
323
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
324
|
+
* function on each node according to a specified order pattern.
|
|
325
|
+
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
326
|
+
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
327
|
+
* is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
|
|
328
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
329
|
+
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
330
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
331
|
+
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
332
|
+
* is `null`, an empty array will be returned.
|
|
333
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
334
|
+
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
335
|
+
* @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
|
|
336
|
+
*/
|
|
337
|
+
dfs(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): MapCallbackReturn<N>[];
|
|
338
|
+
/**
|
|
339
|
+
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
340
|
+
* function on each node.
|
|
341
|
+
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
342
|
+
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
343
|
+
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
344
|
+
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
|
345
|
+
* whether or not to include the level of each node in the callback function. If `withLevel` is set
|
|
346
|
+
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
|
347
|
+
* `withLevel` is
|
|
348
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
349
|
+
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
350
|
+
* will not be performed and an empty array will be returned.
|
|
351
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
352
|
+
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
353
|
+
* @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
|
|
354
|
+
*/
|
|
355
|
+
bfs(callback?: BFSCallback<N>, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): BFSCallbackReturn<N>[];
|
|
356
|
+
/**
|
|
357
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
358
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
359
|
+
* @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
|
|
261
360
|
*/
|
|
262
361
|
getPredecessor(node: N): N;
|
|
263
362
|
/**
|
|
264
363
|
* Time complexity is O(n)
|
|
265
364
|
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
*
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
*
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
*
|
|
276
|
-
* @param {
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* @
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
365
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
366
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
367
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
368
|
+
*/
|
|
369
|
+
/**
|
|
370
|
+
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
371
|
+
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
372
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
373
|
+
* tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
|
|
374
|
+
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
375
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
376
|
+
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
377
|
+
* following values:
|
|
378
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
379
|
+
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
380
|
+
* `beginRoot` is `null`, an empty array will be returned.
|
|
381
|
+
* @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
|
|
382
|
+
*/
|
|
383
|
+
morris(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null): MapCallbackReturn<N>[];
|
|
384
|
+
/**
|
|
385
|
+
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
386
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
387
|
+
* the binary tree. It can be either a node object or `null`.
|
|
388
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
389
|
+
* be added as a child.
|
|
390
|
+
* @returns either the left or right child node of the parent node, depending on which child is
|
|
391
|
+
* available for adding the new node. If a new node is added, the function also updates the size of
|
|
392
|
+
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
393
|
+
* If the parent node is null, the function also returns undefined.
|
|
283
394
|
*/
|
|
284
395
|
protected _addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
285
396
|
/**
|
|
286
|
-
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
287
|
-
* parent property of the value to undefined.
|
|
288
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
397
|
+
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
398
|
+
* it also sets the parent property of the value to undefined.
|
|
399
|
+
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
400
|
+
* type `N` or `null`.
|
|
289
401
|
*/
|
|
290
402
|
protected _setRoot(v: N | null): void;
|
|
291
403
|
/**
|
|
292
|
-
* The function sets the
|
|
293
|
-
* @param {number} v - number
|
|
404
|
+
* The function sets the value of the protected property "_size" to the given number.
|
|
405
|
+
* @param {number} v - The parameter "v" is a number that represents the size value that we want to
|
|
406
|
+
* set.
|
|
294
407
|
*/
|
|
295
408
|
protected _setSize(v: number): void;
|
|
296
409
|
}
|