data-structure-typed 1.42.3 → 1.42.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +17 -17
- package/benchmark/report.html +12 -12
- package/benchmark/report.json +106 -106
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/cjs/src/data-structures/binary-tree/bst.js +77 -21
- package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/cjs/src/data-structures/binary-tree/index.js +1 -1
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +148 -180
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/{tree-multiset.d.ts → tree-multimap.d.ts} +20 -20
- package/dist/cjs/src/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +35 -32
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -0
- package/dist/cjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/index.js +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/dist/cjs/src/types/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multimap.d.ts +4 -0
- package/dist/cjs/src/types/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +1 -1
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multimap.js.map +1 -0
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +5 -3
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +56 -52
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +115 -53
- package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +42 -15
- package/dist/mjs/src/data-structures/binary-tree/bst.js +79 -21
- package/dist/mjs/src/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/mjs/src/data-structures/binary-tree/index.js +1 -1
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +28 -51
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +148 -184
- package/dist/mjs/src/data-structures/binary-tree/{tree-multiset.d.ts → tree-multimap.d.ts} +20 -20
- package/dist/mjs/src/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +33 -31
- package/dist/mjs/src/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/index.js +1 -1
- package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/mjs/src/types/data-structures/binary-tree/rb-tree.js +0 -5
- package/dist/mjs/src/types/data-structures/binary-tree/tree-multimap.d.ts +4 -0
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -3
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-tree.ts +201 -131
- package/src/data-structures/binary-tree/bst.ts +100 -34
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/rb-tree.ts +227 -236
- package/src/data-structures/binary-tree/{tree-multiset.ts → tree-multimap.ts} +38 -37
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/index.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +5 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +6 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -31
- package/test/unit/data-structures/binary-tree/bst.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +205 -159
- package/test/unit/data-structures/binary-tree/{tree-multiset.test.ts → tree-multimap.test.ts} +182 -182
- package/dist/cjs/src/data-structures/binary-tree/tree-multiset.js.map +0 -1
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multiset.d.ts +0 -4
- package/dist/cjs/src/types/data-structures/binary-tree/tree-multiset.js.map +0 -1
- package/dist/mjs/src/types/data-structures/binary-tree/tree-multiset.d.ts +0 -4
- package/src/types/data-structures/binary-tree/tree-multiset.ts +0 -6
- /package/dist/mjs/src/types/data-structures/binary-tree/{tree-multiset.js → tree-multimap.js} +0 -0
- /package/test/performance/data-structures/binary-tree/{tree-multiset.test.ts → tree-multimap.test.ts} +0 -0
|
@@ -69,11 +69,11 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
69
69
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
70
70
|
*/
|
|
71
71
|
constructor(options?: BinaryTreeOptions);
|
|
72
|
-
protected _root: N | null;
|
|
72
|
+
protected _root: N | null | undefined;
|
|
73
73
|
/**
|
|
74
74
|
* Get the root node of the binary tree.
|
|
75
75
|
*/
|
|
76
|
-
get root(): N | null;
|
|
76
|
+
get root(): N | null | undefined;
|
|
77
77
|
protected _size: number;
|
|
78
78
|
/**
|
|
79
79
|
* Get the number of nodes in the binary tree.
|
|
@@ -101,7 +101,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
101
101
|
* @param {V} value - The value for the new node (optional).
|
|
102
102
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
103
103
|
*/
|
|
104
|
-
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined;
|
|
104
|
+
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | null | undefined;
|
|
105
105
|
/**
|
|
106
106
|
* The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
|
|
107
107
|
* values, and adds them to the binary tree.
|
|
@@ -112,7 +112,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
112
112
|
* the value of the nodes will be `undefined`.
|
|
113
113
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
114
114
|
*/
|
|
115
|
-
addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[];
|
|
115
|
+
addMany(keysOrNodes: (BTNKey | null | undefined)[] | (N | null | undefined)[], values?: V[]): (N | null | undefined)[];
|
|
116
116
|
/**
|
|
117
117
|
* The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
|
|
118
118
|
* @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
|
|
@@ -122,27 +122,27 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
122
122
|
* array. Each value in the `data` array will be assigned to the
|
|
123
123
|
* @returns The method is returning a boolean value.
|
|
124
124
|
*/
|
|
125
|
-
refill(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: Array<V>): boolean;
|
|
125
|
+
refill(keysOrNodes: (BTNKey | null | undefined)[] | (N | null | undefined)[], data?: Array<V>): boolean;
|
|
126
126
|
delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
127
|
-
delete<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
127
|
+
delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BinaryTreeDeletedResult<N>[];
|
|
128
128
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeletedResult<N>[];
|
|
129
129
|
/**
|
|
130
130
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
131
131
|
* specified root node.
|
|
132
|
-
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
132
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
|
|
133
133
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
134
134
|
* of the node (`BTNKey`), or `null`.
|
|
135
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
135
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
136
136
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
137
137
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
138
138
|
* node of the binary tree.
|
|
139
139
|
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
140
140
|
*/
|
|
141
|
-
getDepth(distNode: BTNKey | N | null, beginRoot?: BTNKey | N | null): number;
|
|
141
|
+
getDepth(distNode: BTNKey | N | null | undefined, beginRoot?: BTNKey | N | null | undefined): number;
|
|
142
142
|
/**
|
|
143
143
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
144
144
|
* iterative approach.
|
|
145
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
145
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
146
146
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
147
147
|
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
148
148
|
* node is specified. If `
|
|
@@ -151,38 +151,38 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
151
151
|
* possible values:
|
|
152
152
|
* @returns the height of the binary tree.
|
|
153
153
|
*/
|
|
154
|
-
getHeight(beginRoot?: BTNKey | N | null, iterationType?: IterationType): number;
|
|
154
|
+
getHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): number;
|
|
155
155
|
/**
|
|
156
156
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
157
157
|
* recursive or iterative approach.
|
|
158
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
158
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
159
159
|
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
160
160
|
* not provided.
|
|
161
161
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
162
162
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
163
163
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
164
164
|
*/
|
|
165
|
-
getMinHeight(beginRoot?: N | null, iterationType?: IterationType): number;
|
|
165
|
+
getMinHeight(beginRoot?: N | null | undefined, iterationType?: IterationType): number;
|
|
166
166
|
/**
|
|
167
167
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
168
168
|
* height of the tree.
|
|
169
|
-
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
169
|
+
* @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
|
|
170
170
|
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
171
171
|
* @returns The method is returning a boolean value.
|
|
172
172
|
*/
|
|
173
|
-
isPerfectlyBalanced(beginRoot?: N | null): boolean;
|
|
174
|
-
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
175
|
-
getNodes<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
176
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
|
|
177
|
-
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
178
|
-
has<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
179
|
-
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
|
|
180
|
-
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
181
|
-
getNode<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
182
|
-
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
183
|
-
get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null, iterationType?: IterationType): V | undefined;
|
|
184
|
-
get<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): V | undefined;
|
|
185
|
-
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | null, iterationType?: IterationType): V | undefined;
|
|
173
|
+
isPerfectlyBalanced(beginRoot?: N | null | undefined): boolean;
|
|
174
|
+
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, onlyOne?: boolean, beginRoot?: N | null | undefined, iterationType?: IterationType): N[];
|
|
175
|
+
getNodes<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, onlyOne?: boolean, beginRoot?: N | null | undefined, iterationType?: IterationType): N[];
|
|
176
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: N | null | undefined, iterationType?: IterationType): N[];
|
|
177
|
+
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType): boolean;
|
|
178
|
+
has<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType): boolean;
|
|
179
|
+
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback: C, beginRoot?: N | null | undefined, iterationType?: IterationType): boolean;
|
|
180
|
+
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
181
|
+
getNode<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
182
|
+
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
183
|
+
get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType): V | undefined;
|
|
184
|
+
get<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType): V | undefined;
|
|
185
|
+
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | null | undefined, iterationType?: IterationType): V | undefined;
|
|
186
186
|
/**
|
|
187
187
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
188
188
|
* up to the root node, with the option to reverse the order of the nodes.
|
|
@@ -197,7 +197,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
197
197
|
/**
|
|
198
198
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
199
199
|
* iterative traversal.
|
|
200
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
200
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
201
201
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
202
202
|
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
203
203
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -205,19 +205,19 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
205
205
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
206
206
|
* no leftmost node, it returns `null`.
|
|
207
207
|
*/
|
|
208
|
-
getLeftMost(beginRoot?: BTNKey | N | null, iterationType?: IterationType): N | null;
|
|
208
|
+
getLeftMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
209
209
|
/**
|
|
210
210
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
211
211
|
* iteratively.
|
|
212
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
213
|
-
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
212
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
213
|
+
* find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
|
|
214
214
|
* or `null`. If it is `null`, it means there is no starting node
|
|
215
215
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
216
216
|
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
217
217
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
|
|
218
218
|
* `beginRoot` parameter is `null`, it returns `null`.
|
|
219
219
|
*/
|
|
220
|
-
getRightMost(beginRoot?: N | null, iterationType?: IterationType): N | null;
|
|
220
|
+
getRightMost(beginRoot?: N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
221
221
|
/**
|
|
222
222
|
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
223
223
|
* @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
|
|
@@ -227,7 +227,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
227
227
|
* possible values:
|
|
228
228
|
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
229
229
|
*/
|
|
230
|
-
isSubtreeBST(beginRoot: N | null, iterationType?: IterationType): boolean;
|
|
230
|
+
isSubtreeBST(beginRoot: N | null | undefined, iterationType?: IterationType): boolean;
|
|
231
231
|
/**
|
|
232
232
|
* The function checks if a binary tree is a binary search tree.
|
|
233
233
|
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
@@ -237,18 +237,21 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
237
237
|
* @returns a boolean value.
|
|
238
238
|
*/
|
|
239
239
|
isBST(iterationType?: IterationType): boolean;
|
|
240
|
-
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
241
|
-
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
242
|
-
subTreeTraverse<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: BTNKey | N | null, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
240
|
+
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
241
|
+
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
242
|
+
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
243
|
+
isNode(node: any): node is N;
|
|
244
|
+
isNIL(node: any): boolean;
|
|
245
|
+
isNodeOrNull(node: any): node is (N | null);
|
|
246
|
+
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
247
|
+
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
248
|
+
dfs<C extends BTNCallback<N | null | undefined>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
249
|
+
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
250
|
+
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
251
|
+
bfs<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
252
|
+
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
|
|
253
|
+
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][];
|
|
254
|
+
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
252
255
|
/**
|
|
253
256
|
* The function returns the predecessor node of a given node in a binary tree.
|
|
254
257
|
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
@@ -272,12 +275,12 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
272
275
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
273
276
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
274
277
|
* following values:
|
|
275
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
278
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
276
279
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
277
280
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
278
281
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
279
282
|
*/
|
|
280
|
-
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
|
|
283
|
+
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null | undefined): ReturnType<C>[];
|
|
281
284
|
/**
|
|
282
285
|
* The above function is an iterator for a binary tree that can be used to traverse the tree in
|
|
283
286
|
* either an iterative or recursive manner.
|
|
@@ -287,7 +290,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
287
290
|
* @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
|
|
288
291
|
* binary tree nodes in a specific order.
|
|
289
292
|
*/
|
|
290
|
-
[Symbol.iterator](node?: N | null): Generator<BTNKey, void, undefined>;
|
|
293
|
+
[Symbol.iterator](node?: N | null | undefined): Generator<BTNKey, void, undefined>;
|
|
291
294
|
protected defaultOneParamCallback: (node: N) => number;
|
|
292
295
|
/**
|
|
293
296
|
* Swap the data of two nodes in the binary tree.
|
|
@@ -298,7 +301,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
298
301
|
protected _swap(srcNode: N, destNode: N): N;
|
|
299
302
|
/**
|
|
300
303
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
301
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
304
|
+
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
302
305
|
* the binary tree. It can be either a node object or `null`.
|
|
303
306
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
304
307
|
* be added as a child.
|
|
@@ -307,12 +310,13 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
307
310
|
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
308
311
|
* If the parent node is null, the function also returns undefined.
|
|
309
312
|
*/
|
|
310
|
-
protected _addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
313
|
+
protected _addTo(newNode: N | null | undefined, parent: N): N | null | undefined;
|
|
311
314
|
/**
|
|
312
315
|
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
313
316
|
* it also sets the parent property of the value to undefined.
|
|
314
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
317
|
+
* @param {N | null | undefined} v - The parameter `v` is of type `N | null | undefined`, which means it can either be of
|
|
315
318
|
* type `N` or `null`.
|
|
316
319
|
*/
|
|
317
|
-
protected _setRoot(v: N | null): void;
|
|
320
|
+
protected _setRoot(v: N | null | undefined): void;
|
|
321
|
+
print(beginRoot?: N | null | undefined): void;
|
|
318
322
|
}
|
|
@@ -107,7 +107,7 @@ class BinaryTree {
|
|
|
107
107
|
this.iterationType = iterationType;
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
_root =
|
|
110
|
+
_root = undefined;
|
|
111
111
|
/**
|
|
112
112
|
* Get the root node of the binary tree.
|
|
113
113
|
*/
|
|
@@ -134,7 +134,7 @@ class BinaryTree {
|
|
|
134
134
|
* Clear the binary tree, removing all nodes.
|
|
135
135
|
*/
|
|
136
136
|
clear() {
|
|
137
|
-
this._setRoot(
|
|
137
|
+
this._setRoot(undefined);
|
|
138
138
|
this._size = 0;
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
@@ -306,10 +306,10 @@ class BinaryTree {
|
|
|
306
306
|
/**
|
|
307
307
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
308
308
|
* specified root node.
|
|
309
|
-
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
309
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
|
|
310
310
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
311
311
|
* of the node (`BTNKey`), or `null`.
|
|
312
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
312
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
313
313
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
314
314
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
315
315
|
* node of the binary tree.
|
|
@@ -333,7 +333,7 @@ class BinaryTree {
|
|
|
333
333
|
/**
|
|
334
334
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
335
335
|
* iterative approach.
|
|
336
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
336
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
337
337
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
338
338
|
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
339
339
|
* node is specified. If `
|
|
@@ -379,7 +379,7 @@ class BinaryTree {
|
|
|
379
379
|
/**
|
|
380
380
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
381
381
|
* recursive or iterative approach.
|
|
382
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
382
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
383
383
|
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
384
384
|
* not provided.
|
|
385
385
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -432,7 +432,7 @@ class BinaryTree {
|
|
|
432
432
|
/**
|
|
433
433
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
434
434
|
* height of the tree.
|
|
435
|
-
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
435
|
+
* @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
|
|
436
436
|
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
437
437
|
* @returns The method is returning a boolean value.
|
|
438
438
|
*/
|
|
@@ -453,7 +453,7 @@ class BinaryTree {
|
|
|
453
453
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
454
454
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
455
455
|
* function will continue searching for all
|
|
456
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
456
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
457
457
|
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
458
458
|
* tree.
|
|
459
459
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
@@ -584,7 +584,7 @@ class BinaryTree {
|
|
|
584
584
|
/**
|
|
585
585
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
586
586
|
* iterative traversal.
|
|
587
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
587
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
588
588
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
589
589
|
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
590
590
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -618,8 +618,8 @@ class BinaryTree {
|
|
|
618
618
|
/**
|
|
619
619
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
620
620
|
* iteratively.
|
|
621
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
622
|
-
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
621
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
622
|
+
* find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
|
|
623
623
|
* or `null`. If it is `null`, it means there is no starting node
|
|
624
624
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
625
625
|
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
@@ -708,7 +708,7 @@ class BinaryTree {
|
|
|
708
708
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
709
709
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
710
710
|
* an array.
|
|
711
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
711
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
712
712
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
713
713
|
* start from the root of the tree.
|
|
714
714
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -727,12 +727,12 @@ class BinaryTree {
|
|
|
727
727
|
if (cur !== undefined) {
|
|
728
728
|
ans.push(callback(cur));
|
|
729
729
|
if (includeNull) {
|
|
730
|
-
cur
|
|
731
|
-
cur
|
|
730
|
+
cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
731
|
+
cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
732
732
|
}
|
|
733
733
|
else {
|
|
734
|
-
cur
|
|
735
|
-
cur
|
|
734
|
+
cur && cur.left && _traverse(cur.left);
|
|
735
|
+
cur && cur.right && _traverse(cur.right);
|
|
736
736
|
}
|
|
737
737
|
}
|
|
738
738
|
};
|
|
@@ -745,18 +745,27 @@ class BinaryTree {
|
|
|
745
745
|
if (cur !== undefined) {
|
|
746
746
|
ans.push(callback(cur));
|
|
747
747
|
if (includeNull) {
|
|
748
|
-
cur
|
|
749
|
-
cur
|
|
748
|
+
cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
749
|
+
cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
750
750
|
}
|
|
751
751
|
else {
|
|
752
|
-
cur
|
|
753
|
-
cur
|
|
752
|
+
cur && cur.right && stack.push(cur.right);
|
|
753
|
+
cur && cur.left && stack.push(cur.left);
|
|
754
754
|
}
|
|
755
755
|
}
|
|
756
756
|
}
|
|
757
757
|
}
|
|
758
758
|
return ans;
|
|
759
759
|
}
|
|
760
|
+
isNode(node) {
|
|
761
|
+
return node instanceof BinaryTreeNode && node.key.toString() !== 'NaN';
|
|
762
|
+
}
|
|
763
|
+
isNIL(node) {
|
|
764
|
+
return node instanceof BinaryTreeNode && node.key.toString() === 'NaN';
|
|
765
|
+
}
|
|
766
|
+
isNodeOrNull(node) {
|
|
767
|
+
return this.isNode(node) || node === null;
|
|
768
|
+
}
|
|
760
769
|
/**
|
|
761
770
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
762
771
|
* function on each node according to a specified order pattern.
|
|
@@ -765,7 +774,7 @@ class BinaryTree {
|
|
|
765
774
|
* is `this.defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
|
|
766
775
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
767
776
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
768
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
777
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
769
778
|
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
770
779
|
* is `null`, an empty array will be returned.
|
|
771
780
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -782,50 +791,50 @@ class BinaryTree {
|
|
|
782
791
|
switch (pattern) {
|
|
783
792
|
case 'in':
|
|
784
793
|
if (includeNull) {
|
|
785
|
-
if (node
|
|
794
|
+
if (node && this.isNodeOrNull(node.left))
|
|
786
795
|
_traverse(node.left);
|
|
787
|
-
ans.push(callback(node));
|
|
788
|
-
if (node
|
|
796
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
797
|
+
if (node && this.isNodeOrNull(node.right))
|
|
789
798
|
_traverse(node.right);
|
|
790
799
|
}
|
|
791
800
|
else {
|
|
792
|
-
if (node
|
|
801
|
+
if (node && node.left)
|
|
793
802
|
_traverse(node.left);
|
|
794
|
-
ans.push(callback(node));
|
|
795
|
-
if (node
|
|
803
|
+
this.isNode(node) && ans.push(callback(node));
|
|
804
|
+
if (node && node.right)
|
|
796
805
|
_traverse(node.right);
|
|
797
806
|
}
|
|
798
807
|
break;
|
|
799
808
|
case 'pre':
|
|
800
809
|
if (includeNull) {
|
|
801
|
-
ans.push(callback(node));
|
|
802
|
-
if (node
|
|
810
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
811
|
+
if (node && this.isNodeOrNull(node.left))
|
|
803
812
|
_traverse(node.left);
|
|
804
|
-
if (node
|
|
813
|
+
if (node && this.isNodeOrNull(node.right))
|
|
805
814
|
_traverse(node.right);
|
|
806
815
|
}
|
|
807
816
|
else {
|
|
808
|
-
ans.push(callback(node));
|
|
809
|
-
if (node
|
|
817
|
+
this.isNode(node) && ans.push(callback(node));
|
|
818
|
+
if (node && node.left)
|
|
810
819
|
_traverse(node.left);
|
|
811
|
-
if (node
|
|
820
|
+
if (node && node.right)
|
|
812
821
|
_traverse(node.right);
|
|
813
822
|
}
|
|
814
823
|
break;
|
|
815
824
|
case 'post':
|
|
816
825
|
if (includeNull) {
|
|
817
|
-
if (node
|
|
826
|
+
if (node && this.isNodeOrNull(node.left))
|
|
818
827
|
_traverse(node.left);
|
|
819
|
-
if (node
|
|
828
|
+
if (node && this.isNodeOrNull(node.right))
|
|
820
829
|
_traverse(node.right);
|
|
821
|
-
ans.push(callback(node));
|
|
830
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
822
831
|
}
|
|
823
832
|
else {
|
|
824
|
-
if (node
|
|
833
|
+
if (node && node.left)
|
|
825
834
|
_traverse(node.left);
|
|
826
|
-
if (node
|
|
835
|
+
if (node && node.right)
|
|
827
836
|
_traverse(node.right);
|
|
828
|
-
ans.push(callback(node));
|
|
837
|
+
this.isNode(node) && ans.push(callback(node));
|
|
829
838
|
}
|
|
830
839
|
break;
|
|
831
840
|
}
|
|
@@ -837,7 +846,7 @@ class BinaryTree {
|
|
|
837
846
|
const stack = [{ opt: 0, node: beginRoot }];
|
|
838
847
|
while (stack.length > 0) {
|
|
839
848
|
const cur = stack.pop();
|
|
840
|
-
if (cur === undefined)
|
|
849
|
+
if (cur === undefined || this.isNIL(cur.node))
|
|
841
850
|
continue;
|
|
842
851
|
if (includeNull) {
|
|
843
852
|
if (cur.node === undefined)
|
|
@@ -884,7 +893,7 @@ class BinaryTree {
|
|
|
884
893
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
885
894
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
886
895
|
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
|
|
887
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
896
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
888
897
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
889
898
|
* will not be performed and an empty array will be returned.
|
|
890
899
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
@@ -904,9 +913,9 @@ class BinaryTree {
|
|
|
904
913
|
const current = queue.shift();
|
|
905
914
|
ans.push(callback(current));
|
|
906
915
|
if (includeNull) {
|
|
907
|
-
if (current && current.left
|
|
916
|
+
if (current && this.isNodeOrNull(current.left))
|
|
908
917
|
queue.push(current.left);
|
|
909
|
-
if (current && current.right
|
|
918
|
+
if (current && this.isNodeOrNull(current.right))
|
|
910
919
|
queue.push(current.right);
|
|
911
920
|
}
|
|
912
921
|
else {
|
|
@@ -927,9 +936,9 @@ class BinaryTree {
|
|
|
927
936
|
const current = queue.shift();
|
|
928
937
|
ans.push(callback(current));
|
|
929
938
|
if (includeNull) {
|
|
930
|
-
if (current
|
|
939
|
+
if (current && this.isNodeOrNull(current.left))
|
|
931
940
|
queue.push(current.left);
|
|
932
|
-
if (current
|
|
941
|
+
if (current && this.isNodeOrNull(current.right))
|
|
933
942
|
queue.push(current.right);
|
|
934
943
|
}
|
|
935
944
|
else {
|
|
@@ -949,7 +958,7 @@ class BinaryTree {
|
|
|
949
958
|
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
|
950
959
|
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
|
951
960
|
* is determined by the generic type `C`.
|
|
952
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
961
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
953
962
|
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
|
954
963
|
* from the root node of the binary tree.
|
|
955
964
|
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
|
@@ -969,9 +978,9 @@ class BinaryTree {
|
|
|
969
978
|
levelsNodes[level] = [];
|
|
970
979
|
levelsNodes[level].push(callback(node));
|
|
971
980
|
if (includeNull) {
|
|
972
|
-
if (node && node.left
|
|
981
|
+
if (node && this.isNodeOrNull(node.left))
|
|
973
982
|
_recursive(node.left, level + 1);
|
|
974
|
-
if (node && node.right
|
|
983
|
+
if (node && this.isNodeOrNull(node.right))
|
|
975
984
|
_recursive(node.right, level + 1);
|
|
976
985
|
}
|
|
977
986
|
else {
|
|
@@ -992,9 +1001,9 @@ class BinaryTree {
|
|
|
992
1001
|
levelsNodes[level] = [];
|
|
993
1002
|
levelsNodes[level].push(callback(node));
|
|
994
1003
|
if (includeNull) {
|
|
995
|
-
if (node && node.right
|
|
1004
|
+
if (node && this.isNodeOrNull(node.right))
|
|
996
1005
|
stack.push([node.right, level + 1]);
|
|
997
|
-
if (node && node.left
|
|
1006
|
+
if (node && this.isNodeOrNull(node.left))
|
|
998
1007
|
stack.push([node.left, level + 1]);
|
|
999
1008
|
}
|
|
1000
1009
|
else {
|
|
@@ -1053,7 +1062,7 @@ class BinaryTree {
|
|
|
1053
1062
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1054
1063
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1055
1064
|
* following values:
|
|
1056
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1065
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1057
1066
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1058
1067
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1059
1068
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
@@ -1203,7 +1212,7 @@ class BinaryTree {
|
|
|
1203
1212
|
}
|
|
1204
1213
|
/**
|
|
1205
1214
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1206
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1215
|
+
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1207
1216
|
* the binary tree. It can be either a node object or `null`.
|
|
1208
1217
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
1209
1218
|
* be added as a child.
|
|
@@ -1241,7 +1250,7 @@ class BinaryTree {
|
|
|
1241
1250
|
/**
|
|
1242
1251
|
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1243
1252
|
* it also sets the parent property of the value to undefined.
|
|
1244
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1253
|
+
* @param {N | null | undefined} v - The parameter `v` is of type `N | null | undefined`, which means it can either be of
|
|
1245
1254
|
* type `N` or `null`.
|
|
1246
1255
|
*/
|
|
1247
1256
|
_setRoot(v) {
|
|
@@ -1250,5 +1259,58 @@ class BinaryTree {
|
|
|
1250
1259
|
}
|
|
1251
1260
|
this._root = v;
|
|
1252
1261
|
}
|
|
1262
|
+
print(beginRoot = this.root) {
|
|
1263
|
+
const display = (root) => {
|
|
1264
|
+
const [lines, , ,] = _displayAux(root);
|
|
1265
|
+
for (const line of lines) {
|
|
1266
|
+
console.log(line);
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1269
|
+
const _displayAux = (node) => {
|
|
1270
|
+
if (node === undefined || node === null) {
|
|
1271
|
+
return [[], 0, 0, 0];
|
|
1272
|
+
}
|
|
1273
|
+
if (node && node.right === undefined && node.left === undefined) {
|
|
1274
|
+
const line = `${node.key}`;
|
|
1275
|
+
const width = line.length;
|
|
1276
|
+
const height = 1;
|
|
1277
|
+
const middle = Math.floor(width / 2);
|
|
1278
|
+
return [[line], width, height, middle];
|
|
1279
|
+
}
|
|
1280
|
+
if (node && node.right === undefined) {
|
|
1281
|
+
const [lines, n, p, x] = _displayAux(node.left);
|
|
1282
|
+
const s = `${node.key}`;
|
|
1283
|
+
const u = s.length;
|
|
1284
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1285
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1286
|
+
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1287
|
+
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1288
|
+
}
|
|
1289
|
+
if (node && node.left === undefined) {
|
|
1290
|
+
const [lines, n, p, u] = _displayAux(node.right);
|
|
1291
|
+
const s = `${node.key}`;
|
|
1292
|
+
const x = s.length;
|
|
1293
|
+
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1294
|
+
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1295
|
+
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1296
|
+
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1297
|
+
}
|
|
1298
|
+
const [left, n, p, x] = _displayAux(node.left);
|
|
1299
|
+
const [right, m, q, y] = _displayAux(node.right);
|
|
1300
|
+
const s = `${node.key}`;
|
|
1301
|
+
const u = s.length;
|
|
1302
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1303
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1304
|
+
if (p < q) {
|
|
1305
|
+
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1306
|
+
}
|
|
1307
|
+
else if (q < p) {
|
|
1308
|
+
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1309
|
+
}
|
|
1310
|
+
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1311
|
+
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1312
|
+
};
|
|
1313
|
+
display(beginRoot);
|
|
1314
|
+
}
|
|
1253
1315
|
}
|
|
1254
1316
|
exports.BinaryTree = BinaryTree;
|