data-structure-typed 1.37.1 → 1.37.3
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 +5 -7
- package/dist/data-structures/binary-tree/avl-tree.js +7 -9
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +61 -209
- package/dist/data-structures/binary-tree/binary-tree.js +133 -269
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +22 -19
- package/dist/data-structures/binary-tree/bst.js +68 -54
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -36
- package/dist/data-structures/binary-tree/tree-multiset.js +3 -81
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +4 -2
- package/dist/types/data-structures/binary-tree.js +6 -6
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/dist/types/data-structures/index.d.ts +2 -0
- package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -7
- package/lib/data-structures/binary-tree/avl-tree.js +7 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +61 -209
- package/lib/data-structures/binary-tree/binary-tree.js +134 -270
- package/lib/data-structures/binary-tree/bst.d.ts +22 -19
- package/lib/data-structures/binary-tree/bst.js +69 -55
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +2 -36
- package/lib/data-structures/binary-tree/tree-multiset.js +4 -82
- package/lib/data-structures/heap/heap.d.ts +1 -1
- package/lib/data-structures/heap/heap.js +1 -1
- package/lib/types/data-structures/binary-tree.d.ts +4 -2
- package/lib/types/data-structures/binary-tree.js +5 -5
- package/lib/types/data-structures/index.d.ts +2 -0
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -9
- package/src/data-structures/binary-tree/binary-tree.ts +79 -54
- package/src/data-structures/binary-tree/bst.ts +37 -32
- package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +23 -21
- package/test/integration/bst.test.ts +49 -44
- 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
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeNodeKey, BinaryTreeNodeNested,
|
|
9
|
-
import { BinaryTreeDeletedResult,
|
|
8
|
+
import type { BFSCallback, BFSCallbackReturn, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback, MapCallbackReturn } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
|
|
12
12
|
/**
|
|
@@ -54,11 +54,8 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
54
54
|
private _size;
|
|
55
55
|
get size(): number;
|
|
56
56
|
private _loopType;
|
|
57
|
-
get
|
|
58
|
-
set
|
|
59
|
-
visitedKey: BinaryTreeNodeKey[];
|
|
60
|
-
visitedVal: N['val'][];
|
|
61
|
-
visitedNode: N[];
|
|
57
|
+
get iterationType(): IterationType;
|
|
58
|
+
set iterationType(v: IterationType);
|
|
62
59
|
/**
|
|
63
60
|
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
64
61
|
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
@@ -131,18 +128,21 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
131
128
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
132
129
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
133
130
|
* node), or `null`.
|
|
131
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
|
|
134
132
|
* @returns the height of the binary tree.
|
|
135
133
|
*/
|
|
136
|
-
getHeight(beginRoot?: N | BinaryTreeNodeKey | null): number;
|
|
134
|
+
getHeight(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): number;
|
|
135
|
+
protected _defaultCallbackByKey: MapCallback<N>;
|
|
137
136
|
/**
|
|
138
137
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
139
138
|
* approach.
|
|
140
139
|
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
141
140
|
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
142
141
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
142
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
143
143
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
144
144
|
*/
|
|
145
|
-
getMinHeight(beginRoot?: N | null): number;
|
|
145
|
+
getMinHeight(beginRoot?: N | null, iterationType?: IterationType): number;
|
|
146
146
|
/**
|
|
147
147
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
|
|
148
148
|
* tree.
|
|
@@ -153,205 +153,119 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
153
153
|
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
154
154
|
/**
|
|
155
155
|
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
156
|
+
* @param callback
|
|
156
157
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
157
158
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
158
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
159
159
|
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
|
|
160
160
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
161
161
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
162
162
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
163
|
+
* @param beginRoot
|
|
164
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
163
165
|
* @returns an array of nodes (type N).
|
|
164
166
|
*/
|
|
165
|
-
getNodes(nodeProperty: BinaryTreeNodeKey | N,
|
|
167
|
+
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
166
168
|
/**
|
|
167
169
|
* The function checks if a binary tree node has a specific property.
|
|
170
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
168
171
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
169
172
|
* It represents the property of the binary tree node that you want to check.
|
|
170
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
171
173
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
174
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
|
|
175
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
172
176
|
* @returns a boolean value.
|
|
173
177
|
*/
|
|
174
|
-
has(nodeProperty: BinaryTreeNodeKey | N,
|
|
178
|
+
has(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
175
179
|
/**
|
|
176
180
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
177
181
|
* found.
|
|
182
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
178
183
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
179
184
|
* It represents the property of the binary tree node that you want to search for.
|
|
180
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
181
185
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
182
186
|
* default value is set to `'key'`.
|
|
187
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
|
|
188
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
183
189
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
184
190
|
* If no matching node is found, it returns null.
|
|
185
191
|
*/
|
|
186
|
-
get(nodeProperty: BinaryTreeNodeKey | N,
|
|
192
|
+
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
187
193
|
/**
|
|
188
194
|
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
|
|
189
195
|
* an option to reverse the order of the nodes.
|
|
190
|
-
* @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
|
|
191
196
|
* type that represents a node in your specific implementation.
|
|
197
|
+
* @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
|
|
192
198
|
* @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
|
|
193
199
|
* path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
|
|
194
200
|
* `isReverse` is set to `false` or not provided, the path will
|
|
195
201
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
196
202
|
*/
|
|
197
|
-
getPathToRoot(
|
|
198
|
-
/**
|
|
199
|
-
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
200
|
-
* no node is specified.
|
|
201
|
-
* generic type representing a node in a binary tree, `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
202
|
-
* node), or `null`.
|
|
203
|
-
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
204
|
-
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
205
|
-
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
206
|
-
* node is found (
|
|
207
|
-
*/
|
|
208
|
-
getLeftMost(): N | null;
|
|
203
|
+
getPathToRoot(beginRoot: N, isReverse?: boolean): N[];
|
|
209
204
|
/**
|
|
210
205
|
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
211
206
|
* no node is specified.
|
|
212
|
-
* @param {N | BinaryTreeNodeKey | null} [
|
|
207
|
+
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
213
208
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
214
|
-
* node)
|
|
209
|
+
* node), or `null`.
|
|
210
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
215
211
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
216
212
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
217
213
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
218
214
|
* node is found (
|
|
219
215
|
*/
|
|
220
|
-
getLeftMost(
|
|
221
|
-
/**
|
|
222
|
-
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
223
|
-
* recursion optimization.
|
|
224
|
-
* @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the
|
|
225
|
-
* rightmost node starting from the root of the binary tree.
|
|
226
|
-
*/
|
|
227
|
-
getRightMost(): N | null;
|
|
216
|
+
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): N | null;
|
|
228
217
|
/**
|
|
229
218
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
230
219
|
* recursion optimization.
|
|
231
220
|
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
232
221
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
233
222
|
* using the root node of the data structure.
|
|
234
|
-
* @
|
|
235
|
-
*
|
|
223
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
224
|
+
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
225
|
+
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
226
|
+
* rightmost node starting from the root of the binary tree.
|
|
236
227
|
*/
|
|
237
|
-
getRightMost(beginRoot
|
|
228
|
+
getRightMost(beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
238
229
|
/**
|
|
239
230
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
240
|
-
* @param {N | null}
|
|
231
|
+
* @param {N | null} beginRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
232
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
241
233
|
* @returns a boolean value.
|
|
242
234
|
*/
|
|
243
|
-
isSubtreeBST(
|
|
235
|
+
isSubtreeBST(beginRoot: N, iterationType?: IterationType): boolean;
|
|
244
236
|
/**
|
|
245
237
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
246
238
|
* @returns The `isBST()` function is returning a boolean value.
|
|
247
239
|
*/
|
|
248
|
-
isBST(): boolean;
|
|
249
|
-
/**
|
|
250
|
-
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
|
|
251
|
-
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
252
|
-
* binary tree.
|
|
253
|
-
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
254
|
-
*/
|
|
255
|
-
getSubTreeSize(subTreeRoot: N | null | undefined): number;
|
|
240
|
+
isBST(iterationType?: IterationType): boolean;
|
|
256
241
|
/**
|
|
257
|
-
* The function `
|
|
258
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
242
|
+
* The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
|
|
243
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
|
|
259
244
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
260
245
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
261
246
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
247
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
262
248
|
* @returns a boolean value.
|
|
263
249
|
*/
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
|
|
267
|
-
* @returns An array of binary tree node IDs.
|
|
268
|
-
*/
|
|
269
|
-
bfs(): BinaryTreeNodeKey[];
|
|
270
|
-
/**
|
|
271
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on the specified property name.
|
|
272
|
-
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
273
|
-
* @returns An array of values corresponding to the specified property.
|
|
274
|
-
*/
|
|
275
|
-
bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
276
|
-
/**
|
|
277
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating the 'val' property of each node.
|
|
278
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
279
|
-
* @returns An array of 'val' properties from each node.
|
|
280
|
-
*/
|
|
281
|
-
bfs(nodeOrPropertyName: 'val'): N['val'][];
|
|
282
|
-
/**
|
|
283
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating nodes themselves.
|
|
284
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
285
|
-
* @returns An array of binary tree nodes.
|
|
286
|
-
*/
|
|
287
|
-
bfs(nodeOrPropertyName: 'node'): N[];
|
|
288
|
-
/**
|
|
289
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
290
|
-
* @returns An array of binary tree node IDs.
|
|
291
|
-
*/
|
|
292
|
-
dfs(): BinaryTreeNodeKey[];
|
|
293
|
-
/**
|
|
294
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
295
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
296
|
-
* @returns An array of values corresponding to the specified property.
|
|
297
|
-
*/
|
|
298
|
-
dfs(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
299
|
-
/**
|
|
300
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
301
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
302
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
303
|
-
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
304
|
-
* @returns An array of values corresponding to the specified property.
|
|
305
|
-
*/
|
|
306
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[];
|
|
307
|
-
/**
|
|
308
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
309
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
310
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
311
|
-
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
312
|
-
* @returns An array of 'val' properties from each node.
|
|
313
|
-
*/
|
|
314
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[];
|
|
250
|
+
subTreeTraverse(callback?: MapCallback<N>, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>[];
|
|
315
251
|
/**
|
|
316
|
-
*
|
|
252
|
+
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
253
|
+
* each node based on the specified pattern and property name.
|
|
254
|
+
* @param callback
|
|
255
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
317
256
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
318
|
-
* @param
|
|
319
|
-
* @
|
|
320
|
-
* @returns An array of binary tree nodes.
|
|
321
|
-
*/
|
|
322
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[];
|
|
323
|
-
/**
|
|
324
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
325
|
-
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
326
|
-
*/
|
|
327
|
-
listLevels(): BinaryTreeNodeKey[][];
|
|
328
|
-
/**
|
|
329
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
330
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
331
|
-
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
257
|
+
* @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
|
|
258
|
+
* @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
|
|
332
259
|
*/
|
|
333
|
-
|
|
260
|
+
dfs(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): MapCallbackReturn<N>[];
|
|
334
261
|
/**
|
|
335
|
-
*
|
|
336
|
-
* @param
|
|
337
|
-
* @param
|
|
338
|
-
* @
|
|
262
|
+
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
263
|
+
* @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
|
|
264
|
+
* @param withLevel - The `withLevel` parameter is a boolean flag that determines whether to include the level of each node in the result. If `withLevel` is set to `true`, the function will include the level of each node in the result. If `withLevel` is set to `false` or not provided, the function will not include the level of each node in the result.
|
|
265
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
|
|
266
|
+
* @param iterationType
|
|
339
267
|
*/
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
343
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
344
|
-
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
345
|
-
* @returns A 2D array of 'val' properties from each node.
|
|
346
|
-
*/
|
|
347
|
-
listLevels(node: N | null, nodeOrPropertyName: 'val'): N['val'][][];
|
|
348
|
-
/**
|
|
349
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
350
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
351
|
-
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
352
|
-
* @returns A 2D array of binary tree nodes.
|
|
353
|
-
*/
|
|
354
|
-
listLevels(node: N | null, nodeOrPropertyName: 'node'): N[][];
|
|
268
|
+
bfs(callback?: BFSCallback<N>, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): BFSCallbackReturn<N>[];
|
|
355
269
|
/**
|
|
356
270
|
* The function returns the predecessor of a given node in a binary tree.
|
|
357
271
|
* @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
|
|
@@ -363,37 +277,16 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
363
277
|
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
364
278
|
*/
|
|
365
279
|
/**
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
|
|
372
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
373
|
-
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
374
|
-
* @returns An array of values corresponding to the specified property.
|
|
375
|
-
*/
|
|
376
|
-
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
377
|
-
/**
|
|
378
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
|
|
280
|
+
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
281
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
282
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
283
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
379
284
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
380
|
-
* @
|
|
285
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
286
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
287
|
+
* @returns An array of BinaryTreeNodeProperties<N> objects.
|
|
381
288
|
*/
|
|
382
|
-
morris(pattern
|
|
383
|
-
/**
|
|
384
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates the 'val' property of each node.
|
|
385
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
386
|
-
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
387
|
-
* @returns An array of 'val' properties from each node.
|
|
388
|
-
*/
|
|
389
|
-
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
390
|
-
/**
|
|
391
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves.
|
|
392
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
393
|
-
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
394
|
-
* @returns An array of binary tree nodes.
|
|
395
|
-
*/
|
|
396
|
-
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
289
|
+
morris(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null): MapCallbackReturn<N>[];
|
|
397
290
|
/**
|
|
398
291
|
* The function adds a new node to a binary tree if there is an available position.
|
|
399
292
|
* @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
|
|
@@ -416,45 +309,4 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
416
309
|
* @param {number} v - number
|
|
417
310
|
*/
|
|
418
311
|
protected _setSize(v: number): void;
|
|
419
|
-
/**
|
|
420
|
-
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
|
|
421
|
-
* properties.
|
|
422
|
-
*/
|
|
423
|
-
protected _clearResults(): void;
|
|
424
|
-
/**
|
|
425
|
-
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
426
|
-
* a result array.
|
|
427
|
-
* @param {N} cur - The current node being processed.
|
|
428
|
-
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
429
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
|
|
430
|
-
* type. It represents the property value that we are comparing against in the switch statement.
|
|
431
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
432
|
-
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
|
|
433
|
-
* or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
|
|
434
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
435
|
-
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
436
|
-
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
437
|
-
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
438
|
-
*/
|
|
439
|
-
protected _pushByPropertyNameStopOrNot(cur: N, result: (N | null | undefined)[], nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): boolean | undefined;
|
|
440
|
-
/**
|
|
441
|
-
* The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
|
|
442
|
-
* @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
|
|
443
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
444
|
-
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
445
|
-
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
446
|
-
*/
|
|
447
|
-
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName?: NodeOrPropertyName): void;
|
|
448
|
-
/**
|
|
449
|
-
* The time complexity of Morris traversal is O(n), it may slower than others
|
|
450
|
-
* The space complexity Morris traversal is O(1) because no using stack
|
|
451
|
-
*/
|
|
452
|
-
/**
|
|
453
|
-
* The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
|
|
454
|
-
* name.
|
|
455
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
456
|
-
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
457
|
-
* @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties<N>`.
|
|
458
|
-
*/
|
|
459
|
-
protected _getResultByPropertyName(nodeOrPropertyName?: NodeOrPropertyName): BinaryTreeNodeProperties<N>;
|
|
460
312
|
}
|