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
|
}
|
|
@@ -88,7 +88,7 @@ class BinaryTree {
|
|
|
88
88
|
*/
|
|
89
89
|
constructor(options) {
|
|
90
90
|
this.iterationType = types_1.IterationType.ITERATIVE;
|
|
91
|
-
this._root =
|
|
91
|
+
this._root = undefined;
|
|
92
92
|
this._size = 0;
|
|
93
93
|
this.defaultOneParamCallback = (node) => node.key;
|
|
94
94
|
if (options !== undefined) {
|
|
@@ -121,7 +121,7 @@ class BinaryTree {
|
|
|
121
121
|
* Clear the binary tree, removing all nodes.
|
|
122
122
|
*/
|
|
123
123
|
clear() {
|
|
124
|
-
this._setRoot(
|
|
124
|
+
this._setRoot(undefined);
|
|
125
125
|
this._size = 0;
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
@@ -293,10 +293,10 @@ class BinaryTree {
|
|
|
293
293
|
/**
|
|
294
294
|
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
295
295
|
* specified root node.
|
|
296
|
-
* @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
|
|
296
|
+
* @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
|
|
297
297
|
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
298
298
|
* of the node (`BTNKey`), or `null`.
|
|
299
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
299
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
300
300
|
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
301
301
|
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
302
302
|
* node of the binary tree.
|
|
@@ -320,7 +320,7 @@ class BinaryTree {
|
|
|
320
320
|
/**
|
|
321
321
|
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
322
322
|
* iterative approach.
|
|
323
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
|
|
323
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
324
324
|
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
325
325
|
* object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
|
|
326
326
|
* node is specified. If `
|
|
@@ -366,7 +366,7 @@ class BinaryTree {
|
|
|
366
366
|
/**
|
|
367
367
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
368
368
|
* recursive or iterative approach.
|
|
369
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
369
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
370
370
|
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
371
371
|
* not provided.
|
|
372
372
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -420,7 +420,7 @@ class BinaryTree {
|
|
|
420
420
|
/**
|
|
421
421
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
422
422
|
* height of the tree.
|
|
423
|
-
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
423
|
+
* @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
|
|
424
424
|
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
425
425
|
* @returns The method is returning a boolean value.
|
|
426
426
|
*/
|
|
@@ -441,7 +441,7 @@ class BinaryTree {
|
|
|
441
441
|
* first node that matches the identifier. If set to true, the function will return an array with
|
|
442
442
|
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
443
443
|
* function will continue searching for all
|
|
444
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
444
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
445
445
|
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
446
446
|
* tree.
|
|
447
447
|
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
@@ -574,7 +574,7 @@ class BinaryTree {
|
|
|
574
574
|
/**
|
|
575
575
|
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
576
576
|
* iterative traversal.
|
|
577
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
577
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
578
578
|
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
579
579
|
* of a node (`BTNKey`), or `null` if the tree is empty.
|
|
580
580
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
@@ -608,8 +608,8 @@ class BinaryTree {
|
|
|
608
608
|
/**
|
|
609
609
|
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
610
610
|
* iteratively.
|
|
611
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
612
|
-
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
611
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
612
|
+
* find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
|
|
613
613
|
* or `null`. If it is `null`, it means there is no starting node
|
|
614
614
|
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
615
615
|
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
@@ -698,7 +698,7 @@ class BinaryTree {
|
|
|
698
698
|
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
699
699
|
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
700
700
|
* an array.
|
|
701
|
-
* @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
701
|
+
* @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
|
|
702
702
|
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
703
703
|
* start from the root of the tree.
|
|
704
704
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -717,12 +717,12 @@ class BinaryTree {
|
|
|
717
717
|
if (cur !== undefined) {
|
|
718
718
|
ans.push(callback(cur));
|
|
719
719
|
if (includeNull) {
|
|
720
|
-
cur
|
|
721
|
-
cur
|
|
720
|
+
cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
721
|
+
cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
722
722
|
}
|
|
723
723
|
else {
|
|
724
|
-
cur
|
|
725
|
-
cur
|
|
724
|
+
cur && cur.left && _traverse(cur.left);
|
|
725
|
+
cur && cur.right && _traverse(cur.right);
|
|
726
726
|
}
|
|
727
727
|
}
|
|
728
728
|
};
|
|
@@ -735,18 +735,27 @@ class BinaryTree {
|
|
|
735
735
|
if (cur !== undefined) {
|
|
736
736
|
ans.push(callback(cur));
|
|
737
737
|
if (includeNull) {
|
|
738
|
-
cur
|
|
739
|
-
cur
|
|
738
|
+
cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
739
|
+
cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
740
740
|
}
|
|
741
741
|
else {
|
|
742
|
-
cur
|
|
743
|
-
cur
|
|
742
|
+
cur && cur.right && stack.push(cur.right);
|
|
743
|
+
cur && cur.left && stack.push(cur.left);
|
|
744
744
|
}
|
|
745
745
|
}
|
|
746
746
|
}
|
|
747
747
|
}
|
|
748
748
|
return ans;
|
|
749
749
|
}
|
|
750
|
+
isNode(node) {
|
|
751
|
+
return node instanceof BinaryTreeNode && node.key.toString() !== 'NaN';
|
|
752
|
+
}
|
|
753
|
+
isNIL(node) {
|
|
754
|
+
return node instanceof BinaryTreeNode && node.key.toString() === 'NaN';
|
|
755
|
+
}
|
|
756
|
+
isNodeOrNull(node) {
|
|
757
|
+
return this.isNode(node) || node === null;
|
|
758
|
+
}
|
|
750
759
|
/**
|
|
751
760
|
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
752
761
|
* function on each node according to a specified order pattern.
|
|
@@ -755,7 +764,7 @@ class BinaryTree {
|
|
|
755
764
|
* is `this.defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
|
|
756
765
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
757
766
|
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
758
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
767
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
759
768
|
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
760
769
|
* is `null`, an empty array will be returned.
|
|
761
770
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -772,50 +781,50 @@ class BinaryTree {
|
|
|
772
781
|
switch (pattern) {
|
|
773
782
|
case 'in':
|
|
774
783
|
if (includeNull) {
|
|
775
|
-
if (node
|
|
784
|
+
if (node && this.isNodeOrNull(node.left))
|
|
776
785
|
_traverse(node.left);
|
|
777
|
-
ans.push(callback(node));
|
|
778
|
-
if (node
|
|
786
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
787
|
+
if (node && this.isNodeOrNull(node.right))
|
|
779
788
|
_traverse(node.right);
|
|
780
789
|
}
|
|
781
790
|
else {
|
|
782
|
-
if (node
|
|
791
|
+
if (node && node.left)
|
|
783
792
|
_traverse(node.left);
|
|
784
|
-
ans.push(callback(node));
|
|
785
|
-
if (node
|
|
793
|
+
this.isNode(node) && ans.push(callback(node));
|
|
794
|
+
if (node && node.right)
|
|
786
795
|
_traverse(node.right);
|
|
787
796
|
}
|
|
788
797
|
break;
|
|
789
798
|
case 'pre':
|
|
790
799
|
if (includeNull) {
|
|
791
|
-
ans.push(callback(node));
|
|
792
|
-
if (node
|
|
800
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
801
|
+
if (node && this.isNodeOrNull(node.left))
|
|
793
802
|
_traverse(node.left);
|
|
794
|
-
if (node
|
|
803
|
+
if (node && this.isNodeOrNull(node.right))
|
|
795
804
|
_traverse(node.right);
|
|
796
805
|
}
|
|
797
806
|
else {
|
|
798
|
-
ans.push(callback(node));
|
|
799
|
-
if (node
|
|
807
|
+
this.isNode(node) && ans.push(callback(node));
|
|
808
|
+
if (node && node.left)
|
|
800
809
|
_traverse(node.left);
|
|
801
|
-
if (node
|
|
810
|
+
if (node && node.right)
|
|
802
811
|
_traverse(node.right);
|
|
803
812
|
}
|
|
804
813
|
break;
|
|
805
814
|
case 'post':
|
|
806
815
|
if (includeNull) {
|
|
807
|
-
if (node
|
|
816
|
+
if (node && this.isNodeOrNull(node.left))
|
|
808
817
|
_traverse(node.left);
|
|
809
|
-
if (node
|
|
818
|
+
if (node && this.isNodeOrNull(node.right))
|
|
810
819
|
_traverse(node.right);
|
|
811
|
-
ans.push(callback(node));
|
|
820
|
+
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
812
821
|
}
|
|
813
822
|
else {
|
|
814
|
-
if (node
|
|
823
|
+
if (node && node.left)
|
|
815
824
|
_traverse(node.left);
|
|
816
|
-
if (node
|
|
825
|
+
if (node && node.right)
|
|
817
826
|
_traverse(node.right);
|
|
818
|
-
ans.push(callback(node));
|
|
827
|
+
this.isNode(node) && ans.push(callback(node));
|
|
819
828
|
}
|
|
820
829
|
break;
|
|
821
830
|
}
|
|
@@ -827,7 +836,7 @@ class BinaryTree {
|
|
|
827
836
|
const stack = [{ opt: 0, node: beginRoot }];
|
|
828
837
|
while (stack.length > 0) {
|
|
829
838
|
const cur = stack.pop();
|
|
830
|
-
if (cur === undefined)
|
|
839
|
+
if (cur === undefined || this.isNIL(cur.node))
|
|
831
840
|
continue;
|
|
832
841
|
if (includeNull) {
|
|
833
842
|
if (cur.node === undefined)
|
|
@@ -874,7 +883,7 @@ class BinaryTree {
|
|
|
874
883
|
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
875
884
|
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
876
885
|
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
|
|
877
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
886
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
878
887
|
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
879
888
|
* will not be performed and an empty array will be returned.
|
|
880
889
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
@@ -894,9 +903,9 @@ class BinaryTree {
|
|
|
894
903
|
const current = queue.shift();
|
|
895
904
|
ans.push(callback(current));
|
|
896
905
|
if (includeNull) {
|
|
897
|
-
if (current && current.left
|
|
906
|
+
if (current && this.isNodeOrNull(current.left))
|
|
898
907
|
queue.push(current.left);
|
|
899
|
-
if (current && current.right
|
|
908
|
+
if (current && this.isNodeOrNull(current.right))
|
|
900
909
|
queue.push(current.right);
|
|
901
910
|
}
|
|
902
911
|
else {
|
|
@@ -917,9 +926,9 @@ class BinaryTree {
|
|
|
917
926
|
const current = queue.shift();
|
|
918
927
|
ans.push(callback(current));
|
|
919
928
|
if (includeNull) {
|
|
920
|
-
if (current
|
|
929
|
+
if (current && this.isNodeOrNull(current.left))
|
|
921
930
|
queue.push(current.left);
|
|
922
|
-
if (current
|
|
931
|
+
if (current && this.isNodeOrNull(current.right))
|
|
923
932
|
queue.push(current.right);
|
|
924
933
|
}
|
|
925
934
|
else {
|
|
@@ -939,7 +948,7 @@ class BinaryTree {
|
|
|
939
948
|
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
|
940
949
|
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
|
941
950
|
* is determined by the generic type `C`.
|
|
942
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
951
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
|
943
952
|
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
|
944
953
|
* from the root node of the binary tree.
|
|
945
954
|
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
|
@@ -959,9 +968,9 @@ class BinaryTree {
|
|
|
959
968
|
levelsNodes[level] = [];
|
|
960
969
|
levelsNodes[level].push(callback(node));
|
|
961
970
|
if (includeNull) {
|
|
962
|
-
if (node && node.left
|
|
971
|
+
if (node && this.isNodeOrNull(node.left))
|
|
963
972
|
_recursive(node.left, level + 1);
|
|
964
|
-
if (node && node.right
|
|
973
|
+
if (node && this.isNodeOrNull(node.right))
|
|
965
974
|
_recursive(node.right, level + 1);
|
|
966
975
|
}
|
|
967
976
|
else {
|
|
@@ -982,9 +991,9 @@ class BinaryTree {
|
|
|
982
991
|
levelsNodes[level] = [];
|
|
983
992
|
levelsNodes[level].push(callback(node));
|
|
984
993
|
if (includeNull) {
|
|
985
|
-
if (node && node.right
|
|
994
|
+
if (node && this.isNodeOrNull(node.right))
|
|
986
995
|
stack.push([node.right, level + 1]);
|
|
987
|
-
if (node && node.left
|
|
996
|
+
if (node && this.isNodeOrNull(node.left))
|
|
988
997
|
stack.push([node.left, level + 1]);
|
|
989
998
|
}
|
|
990
999
|
else {
|
|
@@ -1043,7 +1052,7 @@ class BinaryTree {
|
|
|
1043
1052
|
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
1044
1053
|
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
1045
1054
|
* following values:
|
|
1046
|
-
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1055
|
+
* @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
1047
1056
|
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
1048
1057
|
* `beginRoot` is `null`, an empty array will be returned.
|
|
1049
1058
|
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
|
|
@@ -1192,7 +1201,7 @@ class BinaryTree {
|
|
|
1192
1201
|
}
|
|
1193
1202
|
/**
|
|
1194
1203
|
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1195
|
-
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1204
|
+
* @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1196
1205
|
* the binary tree. It can be either a node object or `null`.
|
|
1197
1206
|
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
1198
1207
|
* be added as a child.
|
|
@@ -1230,7 +1239,7 @@ class BinaryTree {
|
|
|
1230
1239
|
/**
|
|
1231
1240
|
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1232
1241
|
* it also sets the parent property of the value to undefined.
|
|
1233
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1242
|
+
* @param {N | null | undefined} v - The parameter `v` is of type `N | null | undefined`, which means it can either be of
|
|
1234
1243
|
* type `N` or `null`.
|
|
1235
1244
|
*/
|
|
1236
1245
|
_setRoot(v) {
|
|
@@ -1239,6 +1248,59 @@ class BinaryTree {
|
|
|
1239
1248
|
}
|
|
1240
1249
|
this._root = v;
|
|
1241
1250
|
}
|
|
1251
|
+
print(beginRoot = this.root) {
|
|
1252
|
+
const display = (root) => {
|
|
1253
|
+
const [lines, , ,] = _displayAux(root);
|
|
1254
|
+
for (const line of lines) {
|
|
1255
|
+
console.log(line);
|
|
1256
|
+
}
|
|
1257
|
+
};
|
|
1258
|
+
const _displayAux = (node) => {
|
|
1259
|
+
if (node === undefined || node === null) {
|
|
1260
|
+
return [[], 0, 0, 0];
|
|
1261
|
+
}
|
|
1262
|
+
if (node && node.right === undefined && node.left === undefined) {
|
|
1263
|
+
const line = `${node.key}`;
|
|
1264
|
+
const width = line.length;
|
|
1265
|
+
const height = 1;
|
|
1266
|
+
const middle = Math.floor(width / 2);
|
|
1267
|
+
return [[line], width, height, middle];
|
|
1268
|
+
}
|
|
1269
|
+
if (node && node.right === undefined) {
|
|
1270
|
+
const [lines, n, p, x] = _displayAux(node.left);
|
|
1271
|
+
const s = `${node.key}`;
|
|
1272
|
+
const u = s.length;
|
|
1273
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1274
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1275
|
+
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1276
|
+
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1277
|
+
}
|
|
1278
|
+
if (node && node.left === undefined) {
|
|
1279
|
+
const [lines, n, p, u] = _displayAux(node.right);
|
|
1280
|
+
const s = `${node.key}`;
|
|
1281
|
+
const x = s.length;
|
|
1282
|
+
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1283
|
+
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1284
|
+
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1285
|
+
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1286
|
+
}
|
|
1287
|
+
const [left, n, p, x] = _displayAux(node.left);
|
|
1288
|
+
const [right, m, q, y] = _displayAux(node.right);
|
|
1289
|
+
const s = `${node.key}`;
|
|
1290
|
+
const u = s.length;
|
|
1291
|
+
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1292
|
+
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1293
|
+
if (p < q) {
|
|
1294
|
+
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1295
|
+
}
|
|
1296
|
+
else if (q < p) {
|
|
1297
|
+
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1298
|
+
}
|
|
1299
|
+
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1300
|
+
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1301
|
+
};
|
|
1302
|
+
display(beginRoot);
|
|
1303
|
+
}
|
|
1242
1304
|
}
|
|
1243
1305
|
exports.BinaryTree = BinaryTree;
|
|
1244
1306
|
//# sourceMappingURL=binary-tree.js.map
|