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
package/CHANGELOG.md
CHANGED
|
@@ -8,10 +8,12 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v1.37.
|
|
11
|
+
## [v1.37.3](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
|
|
12
12
|
|
|
13
13
|
### Changes
|
|
14
14
|
|
|
15
|
+
- Optimization [`#23`](https://github.com/zrwusa/data-structure-typed/pull/23)
|
|
16
|
+
- Optimization [`#20`](https://github.com/zrwusa/data-structure-typed/pull/20)
|
|
15
17
|
- [binary-tree, graph] Replace all code that uses Arrays as makeshift Q… [`#18`](https://github.com/zrwusa/data-structure-typed/pull/18)
|
|
16
18
|
- 1. No need for dfsIterative; integrate it directly into the dfs metho… [`#17`](https://github.com/zrwusa/data-structure-typed/pull/17)
|
|
17
19
|
- [heap] fibonacci heap implemented. [test] big O estimate. [project] n… [`#15`](https://github.com/zrwusa/data-structure-typed/pull/15)
|
|
@@ -39,20 +39,18 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
39
39
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
40
40
|
/**
|
|
41
41
|
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
42
|
-
* @param
|
|
42
|
+
* @param keyOrNode - The `keyOrNode` parameter is either a key or a node that needs to be added to the binary tree.
|
|
43
43
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
44
44
|
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
45
45
|
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
46
46
|
*/
|
|
47
|
-
add(
|
|
47
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
48
48
|
/**
|
|
49
|
-
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
|
|
50
|
-
* deletion.
|
|
51
|
-
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
52
|
-
* removed.
|
|
49
|
+
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after deletion.
|
|
53
50
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
51
|
+
* @param nodeOrKey - The `nodeOrKey` parameter is either a node or a key that needs to be deleted from the binary tree.
|
|
54
52
|
*/
|
|
55
|
-
delete(
|
|
53
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
56
54
|
/**
|
|
57
55
|
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
58
56
|
* height of its right subtree.
|
|
@@ -60,27 +60,25 @@ class AVLTree extends bst_1.BST {
|
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
63
|
-
* @param
|
|
63
|
+
* @param keyOrNode - The `keyOrNode` parameter is either a key or a node that needs to be added to the binary tree.
|
|
64
64
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
65
65
|
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
66
66
|
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
67
67
|
*/
|
|
68
|
-
add(
|
|
68
|
+
add(keyOrNode, val) {
|
|
69
69
|
// TODO support node as a param
|
|
70
|
-
const inserted = super.add(
|
|
70
|
+
const inserted = super.add(keyOrNode, val);
|
|
71
71
|
if (inserted)
|
|
72
72
|
this._balancePath(inserted);
|
|
73
73
|
return inserted;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
|
-
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
|
|
77
|
-
* deletion.
|
|
78
|
-
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
79
|
-
* removed.
|
|
76
|
+
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after deletion.
|
|
80
77
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
78
|
+
* @param nodeOrKey - The `nodeOrKey` parameter is either a node or a key that needs to be deleted from the binary tree.
|
|
81
79
|
*/
|
|
82
|
-
delete(
|
|
83
|
-
const deletedResults = super.delete(
|
|
80
|
+
delete(nodeOrKey) {
|
|
81
|
+
const deletedResults = super.delete(nodeOrKey);
|
|
84
82
|
for (const { needBalanced } of deletedResults) {
|
|
85
83
|
if (needBalanced) {
|
|
86
84
|
this._balancePath(needBalanced);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"avl-tree.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/avl-tree.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,+BAAmC;AAInC,MAAa,WAAmF,SAAQ,aAGvG;IAGC,YAAY,GAAsB,EAAE,GAAO;QACzC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AAVD,kCAUC;AAED,MAAa,OAA0D,SAAQ,SAAM;IACnF;;;;;OAKG;IACH,YAAY,OAAwB;QAClC,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACgB,KAAK,CAAC,OAAU,EAAE,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AAS,CAAC,IAAI,GAAG,CAAC,CAAC;iBACpB;qBAAM;oBACL,SAAS,CAAC,KAAK,GAAG,CAAC,CAAC;iBACrB;aACF;SACF;QAED,IAAI,CAAC;YAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC;YAAE,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC;QAC7B,IAAI,CAAC;YAAE,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC;YAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;QAEnB,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QACtB,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QAC3B,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IAC7B,CAAC;CACF;AAtTD,0BAsTC"}
|
|
@@ -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
|
|
@@ -90,11 +90,11 @@ class BinaryTree {
|
|
|
90
90
|
// TODO placeholder node may need redesigned
|
|
91
91
|
this._root = null;
|
|
92
92
|
this._size = 0;
|
|
93
|
-
this._loopType = types_1.
|
|
93
|
+
this._loopType = types_1.IterationType.ITERATIVE;
|
|
94
94
|
this._defaultCallbackByKey = node => node.key;
|
|
95
95
|
if (options !== undefined) {
|
|
96
|
-
const {
|
|
97
|
-
this._loopType =
|
|
96
|
+
const { iterationType = types_1.IterationType.ITERATIVE } = options;
|
|
97
|
+
this._loopType = iterationType;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
@@ -114,10 +114,10 @@ class BinaryTree {
|
|
|
114
114
|
get size() {
|
|
115
115
|
return this._size;
|
|
116
116
|
}
|
|
117
|
-
get
|
|
117
|
+
get iterationType() {
|
|
118
118
|
return this._loopType;
|
|
119
119
|
}
|
|
120
|
-
set
|
|
120
|
+
set iterationType(v) {
|
|
121
121
|
this._loopType = v;
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
@@ -338,14 +338,15 @@ class BinaryTree {
|
|
|
338
338
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
339
339
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
340
340
|
* node), or `null`.
|
|
341
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
|
|
341
342
|
* @returns the height of the binary tree.
|
|
342
343
|
*/
|
|
343
|
-
getHeight(beginRoot = this.root) {
|
|
344
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
344
345
|
if (typeof beginRoot === 'number')
|
|
345
346
|
beginRoot = this.get(beginRoot);
|
|
346
347
|
if (!beginRoot)
|
|
347
348
|
return -1;
|
|
348
|
-
if (
|
|
349
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
349
350
|
const _getMaxHeight = (cur) => {
|
|
350
351
|
if (!cur)
|
|
351
352
|
return -1;
|
|
@@ -380,13 +381,14 @@ class BinaryTree {
|
|
|
380
381
|
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
381
382
|
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
382
383
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
384
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
383
385
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
384
386
|
*/
|
|
385
|
-
getMinHeight(beginRoot = this.root) {
|
|
387
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
386
388
|
var _a, _b, _c;
|
|
387
389
|
if (!beginRoot)
|
|
388
390
|
return -1;
|
|
389
|
-
if (
|
|
391
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
390
392
|
const _getMinHeight = (cur) => {
|
|
391
393
|
if (!cur)
|
|
392
394
|
return 0;
|
|
@@ -446,13 +448,14 @@ class BinaryTree {
|
|
|
446
448
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
447
449
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
448
450
|
* @param beginRoot
|
|
451
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
449
452
|
* @returns an array of nodes (type N).
|
|
450
453
|
*/
|
|
451
|
-
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
|
|
454
|
+
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
452
455
|
if (!beginRoot)
|
|
453
456
|
return [];
|
|
454
457
|
const ans = [];
|
|
455
|
-
if (
|
|
458
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
456
459
|
const _traverse = (cur) => {
|
|
457
460
|
if (callback(cur) === nodeProperty) {
|
|
458
461
|
ans.push(cur);
|
|
@@ -489,11 +492,13 @@ class BinaryTree {
|
|
|
489
492
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
490
493
|
* It represents the property of the binary tree node that you want to check.
|
|
491
494
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
495
|
+
* @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.
|
|
496
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
492
497
|
* @returns a boolean value.
|
|
493
498
|
*/
|
|
494
|
-
has(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
499
|
+
has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
495
500
|
// TODO may support finding node by value equal
|
|
496
|
-
return this.getNodes(nodeProperty, callback, true).length > 0;
|
|
501
|
+
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
|
|
497
502
|
}
|
|
498
503
|
/**
|
|
499
504
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
@@ -503,13 +508,15 @@ class BinaryTree {
|
|
|
503
508
|
* It represents the property of the binary tree node that you want to search for.
|
|
504
509
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
505
510
|
* default value is set to `'key'`.
|
|
511
|
+
* @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.
|
|
512
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
506
513
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
507
514
|
* If no matching node is found, it returns null.
|
|
508
515
|
*/
|
|
509
|
-
get(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
516
|
+
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
510
517
|
var _a;
|
|
511
518
|
// TODO may support finding node by value equal
|
|
512
|
-
return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
519
|
+
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
513
520
|
}
|
|
514
521
|
/**
|
|
515
522
|
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
|
|
@@ -539,17 +546,18 @@ class BinaryTree {
|
|
|
539
546
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
540
547
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
541
548
|
* node), or `null`.
|
|
549
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
542
550
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
543
551
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
544
552
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
545
553
|
* node is found (
|
|
546
554
|
*/
|
|
547
|
-
getLeftMost(beginRoot = this.root) {
|
|
555
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
548
556
|
if (typeof beginRoot === 'number')
|
|
549
557
|
beginRoot = this.get(beginRoot);
|
|
550
558
|
if (!beginRoot)
|
|
551
559
|
return beginRoot;
|
|
552
|
-
if (
|
|
560
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
553
561
|
const _traverse = (cur) => {
|
|
554
562
|
if (!cur.left)
|
|
555
563
|
return cur;
|
|
@@ -573,15 +581,16 @@ class BinaryTree {
|
|
|
573
581
|
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
574
582
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
575
583
|
* using the root node of the data structure.
|
|
584
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
576
585
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
577
586
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
578
587
|
* rightmost node starting from the root of the binary tree.
|
|
579
588
|
*/
|
|
580
|
-
getRightMost(beginRoot = this.root) {
|
|
589
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
581
590
|
// TODO support get right most by passing key in
|
|
582
591
|
if (!beginRoot)
|
|
583
592
|
return beginRoot;
|
|
584
|
-
if (
|
|
593
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
585
594
|
const _traverse = (cur) => {
|
|
586
595
|
if (!cur.right)
|
|
587
596
|
return cur;
|
|
@@ -601,14 +610,15 @@ class BinaryTree {
|
|
|
601
610
|
}
|
|
602
611
|
/**
|
|
603
612
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
604
|
-
* @param {N | null}
|
|
613
|
+
* @param {N | null} beginRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
614
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
605
615
|
* @returns a boolean value.
|
|
606
616
|
*/
|
|
607
|
-
isSubtreeBST(
|
|
617
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
608
618
|
// TODO there is a bug
|
|
609
|
-
if (!
|
|
619
|
+
if (!beginRoot)
|
|
610
620
|
return true;
|
|
611
|
-
if (
|
|
621
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
612
622
|
const dfs = (cur, min, max) => {
|
|
613
623
|
if (!cur)
|
|
614
624
|
return true;
|
|
@@ -616,11 +626,11 @@ class BinaryTree {
|
|
|
616
626
|
return false;
|
|
617
627
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
618
628
|
};
|
|
619
|
-
return dfs(
|
|
629
|
+
return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
620
630
|
}
|
|
621
631
|
else {
|
|
622
632
|
const stack = [];
|
|
623
|
-
let prev = Number.MIN_SAFE_INTEGER, curr =
|
|
633
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
|
|
624
634
|
while (curr || stack.length > 0) {
|
|
625
635
|
while (curr) {
|
|
626
636
|
stack.push(curr);
|
|
@@ -639,33 +649,36 @@ class BinaryTree {
|
|
|
639
649
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
640
650
|
* @returns The `isBST()` function is returning a boolean value.
|
|
641
651
|
*/
|
|
642
|
-
isBST() {
|
|
643
|
-
|
|
652
|
+
isBST(iterationType = this.iterationType) {
|
|
653
|
+
if (this.root === null)
|
|
654
|
+
return true;
|
|
655
|
+
return this.isSubtreeBST(this.root, iterationType);
|
|
644
656
|
}
|
|
645
657
|
/**
|
|
646
658
|
* The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
|
|
647
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
659
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
|
|
648
660
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
649
661
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
650
662
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
663
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
651
664
|
* @returns a boolean value.
|
|
652
665
|
*/
|
|
653
|
-
subTreeTraverse(callback = this._defaultCallbackByKey,
|
|
654
|
-
if (typeof
|
|
655
|
-
|
|
666
|
+
subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
667
|
+
if (typeof beginRoot === 'number')
|
|
668
|
+
beginRoot = this.get(beginRoot);
|
|
656
669
|
const ans = [];
|
|
657
|
-
if (!
|
|
670
|
+
if (!beginRoot)
|
|
658
671
|
return ans;
|
|
659
|
-
if (
|
|
672
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
660
673
|
const _traverse = (cur) => {
|
|
661
674
|
ans.push(callback(cur));
|
|
662
675
|
cur.left && _traverse(cur.left);
|
|
663
676
|
cur.right && _traverse(cur.right);
|
|
664
677
|
};
|
|
665
|
-
_traverse(
|
|
678
|
+
_traverse(beginRoot);
|
|
666
679
|
}
|
|
667
680
|
else {
|
|
668
|
-
const stack = [
|
|
681
|
+
const stack = [beginRoot];
|
|
669
682
|
while (stack.length > 0) {
|
|
670
683
|
const cur = stack.pop();
|
|
671
684
|
ans.push(callback(cur));
|
|
@@ -681,14 +694,14 @@ class BinaryTree {
|
|
|
681
694
|
* @param callback
|
|
682
695
|
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
683
696
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
684
|
-
* @param
|
|
697
|
+
* @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
|
|
685
698
|
* @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.
|
|
686
699
|
*/
|
|
687
|
-
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root,
|
|
700
|
+
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
|
|
688
701
|
if (!beginRoot)
|
|
689
702
|
return [];
|
|
690
703
|
const ans = [];
|
|
691
|
-
if (
|
|
704
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
692
705
|
const _traverse = (node) => {
|
|
693
706
|
switch (pattern) {
|
|
694
707
|
case 'in':
|
|
@@ -757,17 +770,16 @@ class BinaryTree {
|
|
|
757
770
|
// --- start additional methods ---
|
|
758
771
|
/**
|
|
759
772
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
760
|
-
* @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.
|
|
761
773
|
* @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
|
|
762
774
|
* @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.
|
|
775
|
+
* @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.
|
|
776
|
+
* @param iterationType
|
|
763
777
|
*/
|
|
764
|
-
bfs(callback = this._defaultCallbackByKey, withLevel = false,
|
|
765
|
-
if (!
|
|
766
|
-
node = this.root;
|
|
767
|
-
if (!node)
|
|
778
|
+
bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
779
|
+
if (!beginRoot)
|
|
768
780
|
return [];
|
|
769
781
|
const ans = [];
|
|
770
|
-
if (
|
|
782
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
771
783
|
const _recursive = (node, level) => {
|
|
772
784
|
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
773
785
|
if (node.left)
|
|
@@ -775,10 +787,10 @@ class BinaryTree {
|
|
|
775
787
|
if (node.right)
|
|
776
788
|
_recursive(node.right, level + 1);
|
|
777
789
|
};
|
|
778
|
-
_recursive(
|
|
790
|
+
_recursive(beginRoot, 0);
|
|
779
791
|
}
|
|
780
792
|
else {
|
|
781
|
-
const stack = [[
|
|
793
|
+
const stack = [[beginRoot, 0]];
|
|
782
794
|
while (stack.length > 0) {
|
|
783
795
|
const head = stack.pop();
|
|
784
796
|
const [node, level] = head;
|
|
@@ -816,15 +828,19 @@ class BinaryTree {
|
|
|
816
828
|
*/
|
|
817
829
|
/**
|
|
818
830
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
831
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
832
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
833
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
819
834
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
820
835
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
836
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
821
837
|
* @returns An array of BinaryTreeNodeProperties<N> objects.
|
|
822
838
|
*/
|
|
823
|
-
morris(callback = this._defaultCallbackByKey, pattern = 'in') {
|
|
824
|
-
if (
|
|
839
|
+
morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
|
|
840
|
+
if (beginRoot === null)
|
|
825
841
|
return [];
|
|
826
842
|
const ans = [];
|
|
827
|
-
let cur =
|
|
843
|
+
let cur = beginRoot;
|
|
828
844
|
const _reverseEdge = (node) => {
|
|
829
845
|
let pre = null;
|
|
830
846
|
let next = null;
|
|
@@ -899,7 +915,7 @@ class BinaryTree {
|
|
|
899
915
|
}
|
|
900
916
|
cur = cur.right;
|
|
901
917
|
}
|
|
902
|
-
_printEdge(
|
|
918
|
+
_printEdge(beginRoot);
|
|
903
919
|
break;
|
|
904
920
|
}
|
|
905
921
|
return ans;
|