data-structure-typed 1.37.2 → 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 +36 -20
- package/dist/data-structures/binary-tree/binary-tree.js +66 -50
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +15 -13
- package/dist/data-structures/binary-tree/bst.js +35 -33
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- 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 +5 -7
- package/lib/data-structures/binary-tree/avl-tree.js +7 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +36 -20
- package/lib/data-structures/binary-tree/binary-tree.js +67 -51
- package/lib/data-structures/binary-tree/bst.d.ts +15 -13
- package/lib/data-structures/binary-tree/bst.js +36 -34
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/lib/data-structures/binary-tree/tree-multiset.js +3 -3
- 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 +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 +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,7 +6,7 @@
|
|
|
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
11
|
export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
|
|
12
12
|
/**
|
|
@@ -54,8 +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
|
|
57
|
+
get iterationType(): IterationType;
|
|
58
|
+
set iterationType(v: IterationType);
|
|
59
59
|
/**
|
|
60
60
|
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
61
61
|
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
@@ -128,9 +128,10 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
128
128
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
129
129
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
130
130
|
* node), or `null`.
|
|
131
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
|
|
131
132
|
* @returns the height of the binary tree.
|
|
132
133
|
*/
|
|
133
|
-
getHeight(beginRoot?: N | BinaryTreeNodeKey | null): number;
|
|
134
|
+
getHeight(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): number;
|
|
134
135
|
protected _defaultCallbackByKey: MapCallback<N>;
|
|
135
136
|
/**
|
|
136
137
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
@@ -138,9 +139,10 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
138
139
|
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
139
140
|
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
140
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
|
|
141
143
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
142
144
|
*/
|
|
143
|
-
getMinHeight(beginRoot?: N | null): number;
|
|
145
|
+
getMinHeight(beginRoot?: N | null, iterationType?: IterationType): number;
|
|
144
146
|
/**
|
|
145
147
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
|
|
146
148
|
* tree.
|
|
@@ -159,18 +161,21 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
159
161
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
160
162
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
161
163
|
* @param beginRoot
|
|
164
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
162
165
|
* @returns an array of nodes (type N).
|
|
163
166
|
*/
|
|
164
|
-
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null): N[];
|
|
167
|
+
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
165
168
|
/**
|
|
166
169
|
* The function checks if a binary tree node has a specific property.
|
|
167
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
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
|
|
171
176
|
* @returns a boolean value.
|
|
172
177
|
*/
|
|
173
|
-
has(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N
|
|
178
|
+
has(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
174
179
|
/**
|
|
175
180
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
176
181
|
* found.
|
|
@@ -179,10 +184,12 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
179
184
|
* It represents the property of the binary tree node that you want to search for.
|
|
180
185
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
181
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.
|
|
182
189
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
183
190
|
* If no matching node is found, it returns null.
|
|
184
191
|
*/
|
|
185
|
-
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N
|
|
192
|
+
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
186
193
|
/**
|
|
187
194
|
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
|
|
188
195
|
* an option to reverse the order of the nodes.
|
|
@@ -200,60 +207,65 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
200
207
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
201
208
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
202
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.
|
|
203
211
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
204
212
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
205
213
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
206
214
|
* node is found (
|
|
207
215
|
*/
|
|
208
|
-
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null): N | null;
|
|
216
|
+
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): N | null;
|
|
209
217
|
/**
|
|
210
218
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
211
219
|
* recursion optimization.
|
|
212
220
|
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
213
221
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
214
222
|
* using the root node of the data structure.
|
|
223
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
215
224
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
216
225
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
217
226
|
* rightmost node starting from the root of the binary tree.
|
|
218
227
|
*/
|
|
219
|
-
getRightMost(beginRoot?: N | null): N | null;
|
|
228
|
+
getRightMost(beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
220
229
|
/**
|
|
221
230
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
222
|
-
* @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
|
|
223
233
|
* @returns a boolean value.
|
|
224
234
|
*/
|
|
225
|
-
isSubtreeBST(
|
|
235
|
+
isSubtreeBST(beginRoot: N, iterationType?: IterationType): boolean;
|
|
226
236
|
/**
|
|
227
237
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
228
238
|
* @returns The `isBST()` function is returning a boolean value.
|
|
229
239
|
*/
|
|
230
|
-
isBST(): boolean;
|
|
240
|
+
isBST(iterationType?: IterationType): boolean;
|
|
231
241
|
/**
|
|
232
242
|
* The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
|
|
233
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
243
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
|
|
234
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.
|
|
235
245
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
236
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
|
|
237
248
|
* @returns a boolean value.
|
|
238
249
|
*/
|
|
239
|
-
subTreeTraverse(callback?: MapCallback<N>,
|
|
250
|
+
subTreeTraverse(callback?: MapCallback<N>, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>[];
|
|
240
251
|
/**
|
|
241
252
|
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
242
253
|
* each node based on the specified pattern and property name.
|
|
243
254
|
* @param callback
|
|
244
255
|
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
245
256
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
246
|
-
* @param
|
|
257
|
+
* @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
|
|
247
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.
|
|
248
259
|
*/
|
|
249
|
-
dfs(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null,
|
|
260
|
+
dfs(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): MapCallbackReturn<N>[];
|
|
250
261
|
/**
|
|
251
262
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
252
|
-
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
253
263
|
* @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
|
|
254
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
|
|
255
267
|
*/
|
|
256
|
-
bfs(callback?: BFSCallback<N>, withLevel?: boolean,
|
|
268
|
+
bfs(callback?: BFSCallback<N>, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): BFSCallbackReturn<N>[];
|
|
257
269
|
/**
|
|
258
270
|
* The function returns the predecessor of a given node in a binary tree.
|
|
259
271
|
* @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
|
|
@@ -266,11 +278,15 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
266
278
|
*/
|
|
267
279
|
/**
|
|
268
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.
|
|
269
284
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
270
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
|
|
271
287
|
* @returns An array of BinaryTreeNodeProperties<N> objects.
|
|
272
288
|
*/
|
|
273
|
-
morris(callback?: MapCallback<N>, pattern?: DFSOrderPattern): MapCallbackReturn<N>[];
|
|
289
|
+
morris(callback?: MapCallback<N>, pattern?: DFSOrderPattern, beginRoot?: N | null): MapCallbackReturn<N>[];
|
|
274
290
|
/**
|
|
275
291
|
* The function adds a new node to a binary tree if there is an available position.
|
|
276
292
|
* @param {N | null} newNode - The `newNode` parameter is of type `N | null`, which means it can either be a node of
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { FamilyPosition,
|
|
8
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
9
9
|
import { trampoline } from '../../utils';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
11
|
export class BinaryTreeNode {
|
|
@@ -86,11 +86,11 @@ export class BinaryTree {
|
|
|
86
86
|
// TODO placeholder node may need redesigned
|
|
87
87
|
this._root = null;
|
|
88
88
|
this._size = 0;
|
|
89
|
-
this._loopType =
|
|
89
|
+
this._loopType = IterationType.ITERATIVE;
|
|
90
90
|
this._defaultCallbackByKey = node => node.key;
|
|
91
91
|
if (options !== undefined) {
|
|
92
|
-
const {
|
|
93
|
-
this._loopType =
|
|
92
|
+
const { iterationType = IterationType.ITERATIVE } = options;
|
|
93
|
+
this._loopType = iterationType;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
@@ -110,10 +110,10 @@ export class BinaryTree {
|
|
|
110
110
|
get size() {
|
|
111
111
|
return this._size;
|
|
112
112
|
}
|
|
113
|
-
get
|
|
113
|
+
get iterationType() {
|
|
114
114
|
return this._loopType;
|
|
115
115
|
}
|
|
116
|
-
set
|
|
116
|
+
set iterationType(v) {
|
|
117
117
|
this._loopType = v;
|
|
118
118
|
}
|
|
119
119
|
/**
|
|
@@ -334,14 +334,15 @@ export class BinaryTree {
|
|
|
334
334
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
335
335
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
336
336
|
* node), or `null`.
|
|
337
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
|
|
337
338
|
* @returns the height of the binary tree.
|
|
338
339
|
*/
|
|
339
|
-
getHeight(beginRoot = this.root) {
|
|
340
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
340
341
|
if (typeof beginRoot === 'number')
|
|
341
342
|
beginRoot = this.get(beginRoot);
|
|
342
343
|
if (!beginRoot)
|
|
343
344
|
return -1;
|
|
344
|
-
if (
|
|
345
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
345
346
|
const _getMaxHeight = (cur) => {
|
|
346
347
|
if (!cur)
|
|
347
348
|
return -1;
|
|
@@ -376,13 +377,14 @@ export class BinaryTree {
|
|
|
376
377
|
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
377
378
|
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
378
379
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
380
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
379
381
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
380
382
|
*/
|
|
381
|
-
getMinHeight(beginRoot = this.root) {
|
|
383
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
382
384
|
var _a, _b, _c;
|
|
383
385
|
if (!beginRoot)
|
|
384
386
|
return -1;
|
|
385
|
-
if (
|
|
387
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
386
388
|
const _getMinHeight = (cur) => {
|
|
387
389
|
if (!cur)
|
|
388
390
|
return 0;
|
|
@@ -442,13 +444,14 @@ export class BinaryTree {
|
|
|
442
444
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
443
445
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
444
446
|
* @param beginRoot
|
|
447
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
445
448
|
* @returns an array of nodes (type N).
|
|
446
449
|
*/
|
|
447
|
-
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
|
|
450
|
+
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
448
451
|
if (!beginRoot)
|
|
449
452
|
return [];
|
|
450
453
|
const ans = [];
|
|
451
|
-
if (
|
|
454
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
452
455
|
const _traverse = (cur) => {
|
|
453
456
|
if (callback(cur) === nodeProperty) {
|
|
454
457
|
ans.push(cur);
|
|
@@ -485,11 +488,13 @@ export class BinaryTree {
|
|
|
485
488
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
486
489
|
* It represents the property of the binary tree node that you want to check.
|
|
487
490
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
491
|
+
* @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.
|
|
492
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
488
493
|
* @returns a boolean value.
|
|
489
494
|
*/
|
|
490
|
-
has(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
495
|
+
has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
491
496
|
// TODO may support finding node by value equal
|
|
492
|
-
return this.getNodes(nodeProperty, callback, true).length > 0;
|
|
497
|
+
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
|
|
493
498
|
}
|
|
494
499
|
/**
|
|
495
500
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
@@ -499,13 +504,15 @@ export class BinaryTree {
|
|
|
499
504
|
* It represents the property of the binary tree node that you want to search for.
|
|
500
505
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
501
506
|
* default value is set to `'key'`.
|
|
507
|
+
* @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.
|
|
508
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
502
509
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
503
510
|
* If no matching node is found, it returns null.
|
|
504
511
|
*/
|
|
505
|
-
get(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
512
|
+
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
506
513
|
var _a;
|
|
507
514
|
// TODO may support finding node by value equal
|
|
508
|
-
return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
515
|
+
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
509
516
|
}
|
|
510
517
|
/**
|
|
511
518
|
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
|
|
@@ -535,17 +542,18 @@ export class BinaryTree {
|
|
|
535
542
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
536
543
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
537
544
|
* node), or `null`.
|
|
545
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
538
546
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
539
547
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
540
548
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
541
549
|
* node is found (
|
|
542
550
|
*/
|
|
543
|
-
getLeftMost(beginRoot = this.root) {
|
|
551
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
544
552
|
if (typeof beginRoot === 'number')
|
|
545
553
|
beginRoot = this.get(beginRoot);
|
|
546
554
|
if (!beginRoot)
|
|
547
555
|
return beginRoot;
|
|
548
|
-
if (
|
|
556
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
549
557
|
const _traverse = (cur) => {
|
|
550
558
|
if (!cur.left)
|
|
551
559
|
return cur;
|
|
@@ -569,15 +577,16 @@ export class BinaryTree {
|
|
|
569
577
|
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
570
578
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
571
579
|
* using the root node of the data structure.
|
|
580
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
572
581
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
573
582
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
574
583
|
* rightmost node starting from the root of the binary tree.
|
|
575
584
|
*/
|
|
576
|
-
getRightMost(beginRoot = this.root) {
|
|
585
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
577
586
|
// TODO support get right most by passing key in
|
|
578
587
|
if (!beginRoot)
|
|
579
588
|
return beginRoot;
|
|
580
|
-
if (
|
|
589
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
581
590
|
const _traverse = (cur) => {
|
|
582
591
|
if (!cur.right)
|
|
583
592
|
return cur;
|
|
@@ -597,14 +606,15 @@ export class BinaryTree {
|
|
|
597
606
|
}
|
|
598
607
|
/**
|
|
599
608
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
600
|
-
* @param {N | null}
|
|
609
|
+
* @param {N | null} beginRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
610
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
601
611
|
* @returns a boolean value.
|
|
602
612
|
*/
|
|
603
|
-
isSubtreeBST(
|
|
613
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
604
614
|
// TODO there is a bug
|
|
605
|
-
if (!
|
|
615
|
+
if (!beginRoot)
|
|
606
616
|
return true;
|
|
607
|
-
if (
|
|
617
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
608
618
|
const dfs = (cur, min, max) => {
|
|
609
619
|
if (!cur)
|
|
610
620
|
return true;
|
|
@@ -612,11 +622,11 @@ export class BinaryTree {
|
|
|
612
622
|
return false;
|
|
613
623
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
614
624
|
};
|
|
615
|
-
return dfs(
|
|
625
|
+
return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
616
626
|
}
|
|
617
627
|
else {
|
|
618
628
|
const stack = [];
|
|
619
|
-
let prev = Number.MIN_SAFE_INTEGER, curr =
|
|
629
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
|
|
620
630
|
while (curr || stack.length > 0) {
|
|
621
631
|
while (curr) {
|
|
622
632
|
stack.push(curr);
|
|
@@ -635,33 +645,36 @@ export class BinaryTree {
|
|
|
635
645
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
636
646
|
* @returns The `isBST()` function is returning a boolean value.
|
|
637
647
|
*/
|
|
638
|
-
isBST() {
|
|
639
|
-
|
|
648
|
+
isBST(iterationType = this.iterationType) {
|
|
649
|
+
if (this.root === null)
|
|
650
|
+
return true;
|
|
651
|
+
return this.isSubtreeBST(this.root, iterationType);
|
|
640
652
|
}
|
|
641
653
|
/**
|
|
642
654
|
* The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
|
|
643
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
655
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
|
|
644
656
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
645
657
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
646
658
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
659
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
647
660
|
* @returns a boolean value.
|
|
648
661
|
*/
|
|
649
|
-
subTreeTraverse(callback = this._defaultCallbackByKey,
|
|
650
|
-
if (typeof
|
|
651
|
-
|
|
662
|
+
subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
663
|
+
if (typeof beginRoot === 'number')
|
|
664
|
+
beginRoot = this.get(beginRoot);
|
|
652
665
|
const ans = [];
|
|
653
|
-
if (!
|
|
666
|
+
if (!beginRoot)
|
|
654
667
|
return ans;
|
|
655
|
-
if (
|
|
668
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
656
669
|
const _traverse = (cur) => {
|
|
657
670
|
ans.push(callback(cur));
|
|
658
671
|
cur.left && _traverse(cur.left);
|
|
659
672
|
cur.right && _traverse(cur.right);
|
|
660
673
|
};
|
|
661
|
-
_traverse(
|
|
674
|
+
_traverse(beginRoot);
|
|
662
675
|
}
|
|
663
676
|
else {
|
|
664
|
-
const stack = [
|
|
677
|
+
const stack = [beginRoot];
|
|
665
678
|
while (stack.length > 0) {
|
|
666
679
|
const cur = stack.pop();
|
|
667
680
|
ans.push(callback(cur));
|
|
@@ -677,14 +690,14 @@ export class BinaryTree {
|
|
|
677
690
|
* @param callback
|
|
678
691
|
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
679
692
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
680
|
-
* @param
|
|
693
|
+
* @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
|
|
681
694
|
* @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.
|
|
682
695
|
*/
|
|
683
|
-
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root,
|
|
696
|
+
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE) {
|
|
684
697
|
if (!beginRoot)
|
|
685
698
|
return [];
|
|
686
699
|
const ans = [];
|
|
687
|
-
if (
|
|
700
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
688
701
|
const _traverse = (node) => {
|
|
689
702
|
switch (pattern) {
|
|
690
703
|
case 'in':
|
|
@@ -753,17 +766,16 @@ export class BinaryTree {
|
|
|
753
766
|
// --- start additional methods ---
|
|
754
767
|
/**
|
|
755
768
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
756
|
-
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
757
769
|
* @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
|
|
758
770
|
* @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.
|
|
771
|
+
* @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.
|
|
772
|
+
* @param iterationType
|
|
759
773
|
*/
|
|
760
|
-
bfs(callback = this._defaultCallbackByKey, withLevel = false,
|
|
761
|
-
if (!
|
|
762
|
-
node = this.root;
|
|
763
|
-
if (!node)
|
|
774
|
+
bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
775
|
+
if (!beginRoot)
|
|
764
776
|
return [];
|
|
765
777
|
const ans = [];
|
|
766
|
-
if (
|
|
778
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
767
779
|
const _recursive = (node, level) => {
|
|
768
780
|
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
769
781
|
if (node.left)
|
|
@@ -771,10 +783,10 @@ export class BinaryTree {
|
|
|
771
783
|
if (node.right)
|
|
772
784
|
_recursive(node.right, level + 1);
|
|
773
785
|
};
|
|
774
|
-
_recursive(
|
|
786
|
+
_recursive(beginRoot, 0);
|
|
775
787
|
}
|
|
776
788
|
else {
|
|
777
|
-
const stack = [[
|
|
789
|
+
const stack = [[beginRoot, 0]];
|
|
778
790
|
while (stack.length > 0) {
|
|
779
791
|
const head = stack.pop();
|
|
780
792
|
const [node, level] = head;
|
|
@@ -812,15 +824,19 @@ export class BinaryTree {
|
|
|
812
824
|
*/
|
|
813
825
|
/**
|
|
814
826
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
827
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
828
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
829
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
815
830
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
816
831
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
832
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
817
833
|
* @returns An array of BinaryTreeNodeProperties<N> objects.
|
|
818
834
|
*/
|
|
819
|
-
morris(callback = this._defaultCallbackByKey, pattern = 'in') {
|
|
820
|
-
if (
|
|
835
|
+
morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
|
|
836
|
+
if (beginRoot === null)
|
|
821
837
|
return [];
|
|
822
838
|
const ans = [];
|
|
823
|
-
let cur =
|
|
839
|
+
let cur = beginRoot;
|
|
824
840
|
const _reverseEdge = (node) => {
|
|
825
841
|
let pre = null;
|
|
826
842
|
let next = null;
|
|
@@ -895,7 +911,7 @@ export class BinaryTree {
|
|
|
895
911
|
}
|
|
896
912
|
cur = cur.right;
|
|
897
913
|
}
|
|
898
|
-
_printEdge(
|
|
914
|
+
_printEdge(beginRoot);
|
|
899
915
|
break;
|
|
900
916
|
}
|
|
901
917
|
return ans;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback, MapCallbackReturn } from '../../types';
|
|
9
|
-
import { CP } from '../../types';
|
|
9
|
+
import { CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
|
|
@@ -45,9 +45,10 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
45
45
|
* to the binary search tree.
|
|
46
46
|
* @param {N['val'][]} data - The values of tree nodes
|
|
47
47
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
48
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
|
|
48
49
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
49
50
|
*/
|
|
50
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean): (N | null | undefined)[];
|
|
51
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
|
|
51
52
|
/**
|
|
52
53
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
53
54
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
@@ -58,13 +59,12 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
58
59
|
*/
|
|
59
60
|
get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>): N | null;
|
|
60
61
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
62
|
+
* lastKey returns the last key in a binary tree. If the binary tree is empty, it returns 0.
|
|
63
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to begin
|
|
64
|
+
* the search for the last key.
|
|
65
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a recursive or iterative approach to search for the last key.
|
|
66
66
|
*/
|
|
67
|
-
lastKey(): BinaryTreeNodeKey;
|
|
67
|
+
lastKey(beginRoot?: N | null, iterationType?: IterationType): BinaryTreeNodeKey;
|
|
68
68
|
/**
|
|
69
69
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
70
70
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
|
|
@@ -75,18 +75,20 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
75
75
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
76
76
|
* is set to `true`, the function will return an array with only one node (if
|
|
77
77
|
* @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to
|
|
78
|
+
* @param iterationType
|
|
78
79
|
* @returns an array of nodes (type N).
|
|
79
80
|
*/
|
|
80
|
-
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null): N[];
|
|
81
|
+
getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
81
82
|
/**
|
|
82
83
|
* The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
83
84
|
* have a greater value than a given node.
|
|
84
85
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
85
|
-
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
|
|
86
86
|
* represents the node in the binary tree to which the delta value will be added.
|
|
87
87
|
* @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
|
|
88
|
+
* @param targetNode - The `targetNode` parameter is an optional parameter that specifies the node in the binary tree
|
|
89
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
|
|
88
90
|
*/
|
|
89
|
-
lesserOrGreaterTraverse(callback
|
|
91
|
+
lesserOrGreaterTraverse(callback?: MapCallback<N>, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): MapCallbackReturn<N>;
|
|
90
92
|
/**
|
|
91
93
|
* Balancing Adjustment:
|
|
92
94
|
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
@@ -101,12 +103,12 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
|
|
|
101
103
|
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
102
104
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
103
105
|
*/
|
|
104
|
-
perfectlyBalance(): boolean;
|
|
106
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
105
107
|
/**
|
|
106
108
|
* The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
|
|
107
109
|
* @returns a boolean value.
|
|
108
110
|
*/
|
|
109
|
-
isAVLBalanced(): boolean;
|
|
111
|
+
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
110
112
|
protected _comparator: BSTComparator;
|
|
111
113
|
/**
|
|
112
114
|
* The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
|