data-structure-typed 1.47.5 → 1.47.6
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/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +307 -229
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
- package/test/integration/conversion.test.ts +0 -0
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
|
-
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
|
|
9
|
+
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterableEntriesOrKeys, IterationType, NodeDisplayLayout } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
12
12
|
* Represents a node in a binary tree.
|
|
@@ -63,12 +63,12 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
|
|
|
63
63
|
* @template N - The type of the binary tree's nodes.
|
|
64
64
|
*/
|
|
65
65
|
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> implements IBinaryTree<V, N, TREE> {
|
|
66
|
-
|
|
66
|
+
iterationType: IterationType;
|
|
67
67
|
/**
|
|
68
68
|
* Creates a new instance of BinaryTree.
|
|
69
69
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
70
70
|
*/
|
|
71
|
-
constructor(options?: BinaryTreeOptions);
|
|
71
|
+
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BinaryTreeOptions>);
|
|
72
72
|
protected _root?: N | null;
|
|
73
73
|
/**
|
|
74
74
|
* Get the root node of the binary tree.
|
|
@@ -86,12 +86,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
86
86
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
87
87
|
*/
|
|
88
88
|
createNode(key: BTNKey, value?: V): N;
|
|
89
|
-
createTree(options?: BinaryTreeOptions): TREE;
|
|
90
|
-
/**
|
|
91
|
-
* Time Complexity: O(n)
|
|
92
|
-
* Space Complexity: O(1)
|
|
93
|
-
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
94
|
-
*/
|
|
89
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
95
90
|
/**
|
|
96
91
|
* Time Complexity: O(n)
|
|
97
92
|
* Space Complexity: O(1)
|
|
@@ -107,8 +102,9 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
107
102
|
*/
|
|
108
103
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | null | undefined;
|
|
109
104
|
/**
|
|
110
|
-
* Time Complexity: O(
|
|
105
|
+
* Time Complexity: O(n)
|
|
111
106
|
* Space Complexity: O(1)
|
|
107
|
+
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
112
108
|
*/
|
|
113
109
|
/**
|
|
114
110
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -142,6 +138,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
142
138
|
* @returns The method is returning a boolean value.
|
|
143
139
|
*/
|
|
144
140
|
refill(keysOrNodes: (BTNKey | N | null | undefined)[], values?: (V | undefined)[]): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
|
+
*/
|
|
145
145
|
delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BiTreeDeleteResult<N>[];
|
|
146
146
|
delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
|
|
147
147
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BiTreeDeleteResult<N>[];
|
|
@@ -165,8 +165,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
165
165
|
getDepth(distNode: BTNKey | N | null | undefined, beginRoot?: BTNKey | N | null | undefined): number;
|
|
166
166
|
/**
|
|
167
167
|
* Time Complexity: O(n)
|
|
168
|
-
* Space Complexity: O(
|
|
169
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
168
|
+
* Space Complexity: O(1)
|
|
170
169
|
*/
|
|
171
170
|
/**
|
|
172
171
|
* Time Complexity: O(n)
|
|
@@ -182,7 +181,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
182
181
|
* values:
|
|
183
182
|
* @returns the height of the binary tree.
|
|
184
183
|
*/
|
|
185
|
-
getHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType
|
|
184
|
+
getHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): number;
|
|
186
185
|
/**
|
|
187
186
|
* Time Complexity: O(n)
|
|
188
187
|
* Space Complexity: O(log n)
|
|
@@ -201,10 +200,11 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
201
200
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
202
201
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
203
202
|
*/
|
|
204
|
-
getMinHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType
|
|
203
|
+
getMinHeight(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): number;
|
|
205
204
|
/**
|
|
206
205
|
* Time Complexity: O(n)
|
|
207
206
|
* Space Complexity: O(log n)
|
|
207
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
208
208
|
*/
|
|
209
209
|
/**
|
|
210
210
|
* Time Complexity: O(n)
|
|
@@ -218,12 +218,24 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
218
218
|
* @returns a boolean value.
|
|
219
219
|
*/
|
|
220
220
|
isPerfectlyBalanced(beginRoot?: BTNKey | N | null | undefined): boolean;
|
|
221
|
+
/**
|
|
222
|
+
* Time Complexity: O(n)
|
|
223
|
+
* Space Complexity: O(log n)
|
|
224
|
+
*/
|
|
221
225
|
getNodes<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N[];
|
|
222
226
|
getNodes<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N[];
|
|
223
227
|
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, onlyOne?: boolean, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N[];
|
|
228
|
+
/**
|
|
229
|
+
* Time Complexity: O(n)
|
|
230
|
+
* Space Complexity: O(log n).
|
|
231
|
+
*/
|
|
224
232
|
has<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): boolean;
|
|
225
233
|
has<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): boolean;
|
|
226
234
|
has<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Time Complexity: O(n)
|
|
237
|
+
* Space Complexity: O(log n).
|
|
238
|
+
*/
|
|
227
239
|
getNode<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
228
240
|
getNode<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
229
241
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
@@ -246,6 +258,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
246
258
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
247
259
|
*/
|
|
248
260
|
getNodeByKey(key: BTNKey, iterationType?: IterationType): N | undefined;
|
|
261
|
+
/**
|
|
262
|
+
* Time Complexity: O(n)
|
|
263
|
+
* Space Complexity: O(log n)
|
|
264
|
+
*/
|
|
249
265
|
/**
|
|
250
266
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
|
|
251
267
|
* key, otherwise it returns the key itself.
|
|
@@ -261,6 +277,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
261
277
|
get<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): V | undefined;
|
|
262
278
|
get<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): V | undefined;
|
|
263
279
|
get<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): V | undefined;
|
|
280
|
+
/**
|
|
281
|
+
* Time Complexity: O(n)
|
|
282
|
+
* Space Complexity: O(log n)
|
|
283
|
+
*/
|
|
264
284
|
/**
|
|
265
285
|
* Clear the binary tree, removing all nodes.
|
|
266
286
|
*/
|
|
@@ -270,10 +290,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
270
290
|
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
271
291
|
*/
|
|
272
292
|
isEmpty(): boolean;
|
|
273
|
-
/**
|
|
274
|
-
* Time Complexity: O(log n)
|
|
275
|
-
* Space Complexity: O(log n)
|
|
276
|
-
*/
|
|
277
293
|
/**
|
|
278
294
|
* Time Complexity: O(log n)
|
|
279
295
|
* Space Complexity: O(log n)
|
|
@@ -291,7 +307,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
291
307
|
getPathToRoot(beginRoot: BTNKey | N | null | undefined, isReverse?: boolean): N[];
|
|
292
308
|
/**
|
|
293
309
|
* Time Complexity: O(log n)
|
|
294
|
-
* Space Complexity: O(
|
|
310
|
+
* Space Complexity: O(log n)
|
|
295
311
|
*/
|
|
296
312
|
/**
|
|
297
313
|
* Time Complexity: O(log n)
|
|
@@ -307,7 +323,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
307
323
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
308
324
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
309
325
|
*/
|
|
310
|
-
getLeftMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType
|
|
326
|
+
getLeftMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
311
327
|
/**
|
|
312
328
|
* Time Complexity: O(log n)
|
|
313
329
|
* Space Complexity: O(1)
|
|
@@ -327,9 +343,9 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
327
343
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
328
344
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
329
345
|
*/
|
|
330
|
-
getRightMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType
|
|
346
|
+
getRightMost(beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType): N | null | undefined;
|
|
331
347
|
/**
|
|
332
|
-
* Time Complexity: O(n)
|
|
348
|
+
* Time Complexity: O(log n)
|
|
333
349
|
* Space Complexity: O(1)
|
|
334
350
|
*/
|
|
335
351
|
/**
|
|
@@ -344,7 +360,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
344
360
|
* possible values:
|
|
345
361
|
* @returns a boolean value.
|
|
346
362
|
*/
|
|
347
|
-
isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType?: IterationType
|
|
363
|
+
isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType?: IterationType): boolean;
|
|
348
364
|
/**
|
|
349
365
|
* Time Complexity: O(n)
|
|
350
366
|
* Space Complexity: O(1)
|
|
@@ -360,10 +376,18 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
360
376
|
* expected to be
|
|
361
377
|
* @returns a boolean value.
|
|
362
378
|
*/
|
|
363
|
-
isBST(iterationType?: IterationType
|
|
379
|
+
isBST(iterationType?: IterationType): boolean;
|
|
380
|
+
/**
|
|
381
|
+
* Time Complexity: O(n)
|
|
382
|
+
* Space Complexity: O(1)
|
|
383
|
+
*/
|
|
364
384
|
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
365
385
|
subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
366
386
|
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
387
|
+
/**
|
|
388
|
+
* Time complexity: O(n)
|
|
389
|
+
* Space complexity: O(log n)
|
|
390
|
+
*/
|
|
367
391
|
/**
|
|
368
392
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
369
393
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -393,12 +417,24 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
393
417
|
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
394
418
|
dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
395
419
|
dfs<C extends BTNCallback<N | null | undefined>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
420
|
+
/**
|
|
421
|
+
* Time complexity: O(n)
|
|
422
|
+
* Space complexity: O(n)
|
|
423
|
+
*/
|
|
396
424
|
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
|
|
397
425
|
bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
|
|
398
426
|
bfs<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
|
|
427
|
+
/**
|
|
428
|
+
* Time complexity: O(n)
|
|
429
|
+
* Space complexity: O(n)
|
|
430
|
+
*/
|
|
399
431
|
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
|
|
400
432
|
listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][];
|
|
401
433
|
listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: BTNKey | N | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
|
|
434
|
+
/**
|
|
435
|
+
* Time complexity: O(n)
|
|
436
|
+
* Space complexity: O(n)
|
|
437
|
+
*/
|
|
402
438
|
getPredecessor(node: N): N;
|
|
403
439
|
/**
|
|
404
440
|
* The function `getSuccessor` returns the next node in a binary tree given a current node.
|
|
@@ -407,10 +443,6 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
407
443
|
* after the given node in the inorder traversal of the binary tree.
|
|
408
444
|
*/
|
|
409
445
|
getSuccessor(x?: BTNKey | N | null): N | null | undefined;
|
|
410
|
-
/**
|
|
411
|
-
* Time complexity: O(n)
|
|
412
|
-
* Space complexity: O(1)
|
|
413
|
-
*/
|
|
414
446
|
/**
|
|
415
447
|
* Time complexity: O(n)
|
|
416
448
|
* Space complexity: O(1)
|
|
@@ -430,6 +462,10 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
430
462
|
* by the return type of the `callback` function.
|
|
431
463
|
*/
|
|
432
464
|
morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNKey | N | null | undefined): ReturnType<C>[];
|
|
465
|
+
/**
|
|
466
|
+
* Time complexity: O(n)
|
|
467
|
+
* Space complexity: O(1)
|
|
468
|
+
*/
|
|
433
469
|
/**
|
|
434
470
|
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
435
471
|
* entry and the tree as arguments.
|
|
@@ -484,6 +520,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
|
|
|
484
520
|
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
|
485
521
|
*/
|
|
486
522
|
print(beginRoot?: BTNKey | N | null | undefined, options?: BinaryTreePrintOptions): void;
|
|
523
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
487
524
|
protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
488
525
|
protected _defaultOneParamCallback: (node: N) => number;
|
|
489
526
|
/**
|
|
@@ -92,19 +92,21 @@ export class BinaryTreeNode {
|
|
|
92
92
|
* @template N - The type of the binary tree's nodes.
|
|
93
93
|
*/
|
|
94
94
|
export class BinaryTree {
|
|
95
|
-
|
|
95
|
+
iterationType = IterationType.ITERATIVE;
|
|
96
96
|
/**
|
|
97
97
|
* Creates a new instance of BinaryTree.
|
|
98
98
|
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
99
99
|
*/
|
|
100
|
-
constructor(options) {
|
|
100
|
+
constructor(elements, options) {
|
|
101
101
|
if (options) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
102
|
+
const { iterationType } = options;
|
|
103
|
+
if (iterationType) {
|
|
104
|
+
this.iterationType = iterationType;
|
|
105
|
+
}
|
|
106
106
|
}
|
|
107
107
|
this._size = 0;
|
|
108
|
+
if (elements)
|
|
109
|
+
this.init(elements);
|
|
108
110
|
}
|
|
109
111
|
_root;
|
|
110
112
|
/**
|
|
@@ -130,13 +132,8 @@ export class BinaryTree {
|
|
|
130
132
|
return new BinaryTreeNode(key, value);
|
|
131
133
|
}
|
|
132
134
|
createTree(options) {
|
|
133
|
-
return new BinaryTree({
|
|
135
|
+
return new BinaryTree([], { iterationType: this.iterationType, ...options });
|
|
134
136
|
}
|
|
135
|
-
/**
|
|
136
|
-
* Time Complexity: O(n)
|
|
137
|
-
* Space Complexity: O(1)
|
|
138
|
-
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
139
|
-
*/
|
|
140
137
|
/**
|
|
141
138
|
* Time Complexity: O(n)
|
|
142
139
|
* Space Complexity: O(1)
|
|
@@ -197,8 +194,9 @@ export class BinaryTree {
|
|
|
197
194
|
return inserted;
|
|
198
195
|
}
|
|
199
196
|
/**
|
|
200
|
-
* Time Complexity: O(
|
|
197
|
+
* Time Complexity: O(n)
|
|
201
198
|
* Space Complexity: O(1)
|
|
199
|
+
* Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
|
|
202
200
|
*/
|
|
203
201
|
/**
|
|
204
202
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
@@ -247,10 +245,6 @@ export class BinaryTree {
|
|
|
247
245
|
this.clear();
|
|
248
246
|
return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
|
|
249
247
|
}
|
|
250
|
-
/**
|
|
251
|
-
* Time Complexity: O(n)
|
|
252
|
-
* Space Complexity: O(1)
|
|
253
|
-
*/
|
|
254
248
|
/**
|
|
255
249
|
* Time Complexity: O(n)
|
|
256
250
|
* Space Complexity: O(1)
|
|
@@ -346,8 +340,7 @@ export class BinaryTree {
|
|
|
346
340
|
}
|
|
347
341
|
/**
|
|
348
342
|
* Time Complexity: O(n)
|
|
349
|
-
* Space Complexity: O(
|
|
350
|
-
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
343
|
+
* Space Complexity: O(1)
|
|
351
344
|
*/
|
|
352
345
|
/**
|
|
353
346
|
* Time Complexity: O(n)
|
|
@@ -363,7 +356,7 @@ export class BinaryTree {
|
|
|
363
356
|
* values:
|
|
364
357
|
* @returns the height of the binary tree.
|
|
365
358
|
*/
|
|
366
|
-
getHeight(beginRoot = this.root, iterationType = this.
|
|
359
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
367
360
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
368
361
|
if (!beginRoot)
|
|
369
362
|
return -1;
|
|
@@ -409,7 +402,7 @@ export class BinaryTree {
|
|
|
409
402
|
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
410
403
|
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
411
404
|
*/
|
|
412
|
-
getMinHeight(beginRoot = this.root, iterationType = this.
|
|
405
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
413
406
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
414
407
|
if (!beginRoot)
|
|
415
408
|
return -1;
|
|
@@ -456,6 +449,7 @@ export class BinaryTree {
|
|
|
456
449
|
/**
|
|
457
450
|
* Time Complexity: O(n)
|
|
458
451
|
* Space Complexity: O(log n)
|
|
452
|
+
* Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
|
|
459
453
|
*/
|
|
460
454
|
/**
|
|
461
455
|
* Time Complexity: O(n)
|
|
@@ -471,10 +465,6 @@ export class BinaryTree {
|
|
|
471
465
|
isPerfectlyBalanced(beginRoot = this.root) {
|
|
472
466
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
473
467
|
}
|
|
474
|
-
/**
|
|
475
|
-
* Time Complexity: O(n)
|
|
476
|
-
* Space Complexity: O(log n).
|
|
477
|
-
*/
|
|
478
468
|
/**
|
|
479
469
|
* Time Complexity: O(n)
|
|
480
470
|
* Space Complexity: O(log n).
|
|
@@ -500,7 +490,7 @@ export class BinaryTree {
|
|
|
500
490
|
* traverse the binary tree. It can have two possible values:
|
|
501
491
|
* @returns an array of nodes of type `N`.
|
|
502
492
|
*/
|
|
503
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.
|
|
493
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
494
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
505
495
|
callback = (node => node);
|
|
506
496
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -538,10 +528,6 @@ export class BinaryTree {
|
|
|
538
528
|
}
|
|
539
529
|
return ans;
|
|
540
530
|
}
|
|
541
|
-
/**
|
|
542
|
-
* Time Complexity: O(n)
|
|
543
|
-
* Space Complexity: O(log n).
|
|
544
|
-
*/
|
|
545
531
|
/**
|
|
546
532
|
* Time Complexity: O(n)
|
|
547
533
|
*
|
|
@@ -562,15 +548,11 @@ export class BinaryTree {
|
|
|
562
548
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
563
549
|
* @returns a boolean value.
|
|
564
550
|
*/
|
|
565
|
-
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
551
|
+
has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
566
552
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
567
553
|
callback = (node => node);
|
|
568
554
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
569
555
|
}
|
|
570
|
-
/**
|
|
571
|
-
* Time Complexity: O(n)
|
|
572
|
-
* Space Complexity: O(log n)
|
|
573
|
-
*/
|
|
574
556
|
/**
|
|
575
557
|
* Time Complexity: O(n)
|
|
576
558
|
* Space Complexity: O(log n)
|
|
@@ -592,7 +574,7 @@ export class BinaryTree {
|
|
|
592
574
|
* nodes are visited during the search.
|
|
593
575
|
* @returns a value of type `N | null | undefined`.
|
|
594
576
|
*/
|
|
595
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
577
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
596
578
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
597
579
|
callback = (node => node);
|
|
598
580
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -644,6 +626,10 @@ export class BinaryTree {
|
|
|
644
626
|
}
|
|
645
627
|
}
|
|
646
628
|
}
|
|
629
|
+
/**
|
|
630
|
+
* Time Complexity: O(n)
|
|
631
|
+
* Space Complexity: O(log n)
|
|
632
|
+
*/
|
|
647
633
|
/**
|
|
648
634
|
* The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
|
|
649
635
|
* key, otherwise it returns the key itself.
|
|
@@ -658,10 +644,6 @@ export class BinaryTree {
|
|
|
658
644
|
ensureNotKey(key, iterationType = IterationType.ITERATIVE) {
|
|
659
645
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
660
646
|
}
|
|
661
|
-
/**
|
|
662
|
-
* Time Complexity: O(n)
|
|
663
|
-
* Space Complexity: O(log n)
|
|
664
|
-
*/
|
|
665
647
|
/**
|
|
666
648
|
* Time Complexity: O(n)
|
|
667
649
|
* Space Complexity: O(log n)
|
|
@@ -684,11 +666,15 @@ export class BinaryTree {
|
|
|
684
666
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
685
667
|
* found, `undefined` is returned.
|
|
686
668
|
*/
|
|
687
|
-
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
669
|
+
get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
688
670
|
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
689
671
|
callback = (node => node);
|
|
690
672
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
691
673
|
}
|
|
674
|
+
/**
|
|
675
|
+
* Time Complexity: O(n)
|
|
676
|
+
* Space Complexity: O(log n)
|
|
677
|
+
*/
|
|
692
678
|
/**
|
|
693
679
|
* Clear the binary tree, removing all nodes.
|
|
694
680
|
*/
|
|
@@ -703,10 +689,6 @@ export class BinaryTree {
|
|
|
703
689
|
isEmpty() {
|
|
704
690
|
return this.size === 0;
|
|
705
691
|
}
|
|
706
|
-
/**
|
|
707
|
-
* Time Complexity: O(log n)
|
|
708
|
-
* Space Complexity: O(log n)
|
|
709
|
-
*/
|
|
710
692
|
/**
|
|
711
693
|
* Time Complexity: O(log n)
|
|
712
694
|
* Space Complexity: O(log n)
|
|
@@ -738,7 +720,7 @@ export class BinaryTree {
|
|
|
738
720
|
}
|
|
739
721
|
/**
|
|
740
722
|
* Time Complexity: O(log n)
|
|
741
|
-
* Space Complexity: O(
|
|
723
|
+
* Space Complexity: O(log n)
|
|
742
724
|
*/
|
|
743
725
|
/**
|
|
744
726
|
* Time Complexity: O(log n)
|
|
@@ -754,7 +736,7 @@ export class BinaryTree {
|
|
|
754
736
|
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
|
|
755
737
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
756
738
|
*/
|
|
757
|
-
getLeftMost(beginRoot = this.root, iterationType = this.
|
|
739
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
758
740
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
759
741
|
if (!beginRoot)
|
|
760
742
|
return beginRoot;
|
|
@@ -795,7 +777,7 @@ export class BinaryTree {
|
|
|
795
777
|
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
|
|
796
778
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
797
779
|
*/
|
|
798
|
-
getRightMost(beginRoot = this.root, iterationType = this.
|
|
780
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
799
781
|
// TODO support get right most by passing key in
|
|
800
782
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
801
783
|
if (!beginRoot)
|
|
@@ -819,7 +801,7 @@ export class BinaryTree {
|
|
|
819
801
|
}
|
|
820
802
|
}
|
|
821
803
|
/**
|
|
822
|
-
* Time Complexity: O(n)
|
|
804
|
+
* Time Complexity: O(log n)
|
|
823
805
|
* Space Complexity: O(1)
|
|
824
806
|
*/
|
|
825
807
|
/**
|
|
@@ -834,7 +816,7 @@ export class BinaryTree {
|
|
|
834
816
|
* possible values:
|
|
835
817
|
* @returns a boolean value.
|
|
836
818
|
*/
|
|
837
|
-
isSubtreeBST(beginRoot, iterationType = this.
|
|
819
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
838
820
|
// TODO there is a bug
|
|
839
821
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
840
822
|
if (!beginRoot)
|
|
@@ -881,15 +863,11 @@ export class BinaryTree {
|
|
|
881
863
|
* expected to be
|
|
882
864
|
* @returns a boolean value.
|
|
883
865
|
*/
|
|
884
|
-
isBST(iterationType = this.
|
|
866
|
+
isBST(iterationType = this.iterationType) {
|
|
885
867
|
if (this.root === null)
|
|
886
868
|
return true;
|
|
887
869
|
return this.isSubtreeBST(this.root, iterationType);
|
|
888
870
|
}
|
|
889
|
-
/**
|
|
890
|
-
* Time complexity: O(n)
|
|
891
|
-
* Space complexity: O(log n)
|
|
892
|
-
*/
|
|
893
871
|
/**
|
|
894
872
|
* Time complexity: O(n)
|
|
895
873
|
* Space complexity: O(log n)
|
|
@@ -912,7 +890,7 @@ export class BinaryTree {
|
|
|
912
890
|
* the `callback` function on each node in the subtree. The type of the array elements is determined
|
|
913
891
|
* by the return type of the `callback` function.
|
|
914
892
|
*/
|
|
915
|
-
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
893
|
+
subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
916
894
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
917
895
|
const ans = [];
|
|
918
896
|
if (!beginRoot)
|
|
@@ -952,6 +930,10 @@ export class BinaryTree {
|
|
|
952
930
|
}
|
|
953
931
|
return ans;
|
|
954
932
|
}
|
|
933
|
+
/**
|
|
934
|
+
* Time complexity: O(n)
|
|
935
|
+
* Space complexity: O(log n)
|
|
936
|
+
*/
|
|
955
937
|
/**
|
|
956
938
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
957
939
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -986,10 +968,6 @@ export class BinaryTree {
|
|
|
986
968
|
isNodeKey(potentialKey) {
|
|
987
969
|
return typeof potentialKey === 'number';
|
|
988
970
|
}
|
|
989
|
-
/**
|
|
990
|
-
* Time complexity: O(n)
|
|
991
|
-
* Space complexity: O(n)
|
|
992
|
-
*/
|
|
993
971
|
/**
|
|
994
972
|
* Time complexity: O(n)
|
|
995
973
|
* Space complexity: O(n)
|
|
@@ -1119,10 +1097,6 @@ export class BinaryTree {
|
|
|
1119
1097
|
}
|
|
1120
1098
|
return ans;
|
|
1121
1099
|
}
|
|
1122
|
-
/**
|
|
1123
|
-
* Time complexity: O(n)
|
|
1124
|
-
* Space complexity: O(n)
|
|
1125
|
-
*/
|
|
1126
1100
|
/**
|
|
1127
1101
|
* Time complexity: O(n)
|
|
1128
1102
|
* Space complexity: O(n)
|
|
@@ -1144,7 +1118,7 @@ export class BinaryTree {
|
|
|
1144
1118
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1145
1119
|
* the breadth-first traversal of a binary tree.
|
|
1146
1120
|
*/
|
|
1147
|
-
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
1121
|
+
bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1148
1122
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
1149
1123
|
if (!beginRoot)
|
|
1150
1124
|
return [];
|
|
@@ -1196,10 +1170,6 @@ export class BinaryTree {
|
|
|
1196
1170
|
}
|
|
1197
1171
|
return ans;
|
|
1198
1172
|
}
|
|
1199
|
-
/**
|
|
1200
|
-
* Time complexity: O(n)
|
|
1201
|
-
* Space complexity: O(n)
|
|
1202
|
-
*/
|
|
1203
1173
|
/**
|
|
1204
1174
|
* Time complexity: O(n)
|
|
1205
1175
|
* Space complexity: O(n)
|
|
@@ -1221,7 +1191,7 @@ export class BinaryTree {
|
|
|
1221
1191
|
* be excluded
|
|
1222
1192
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1223
1193
|
*/
|
|
1224
|
-
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
1194
|
+
listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1225
1195
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
1226
1196
|
const levelsNodes = [];
|
|
1227
1197
|
if (!beginRoot)
|
|
@@ -1313,10 +1283,6 @@ export class BinaryTree {
|
|
|
1313
1283
|
}
|
|
1314
1284
|
return y;
|
|
1315
1285
|
}
|
|
1316
|
-
/**
|
|
1317
|
-
* Time complexity: O(n)
|
|
1318
|
-
* Space complexity: O(1)
|
|
1319
|
-
*/
|
|
1320
1286
|
/**
|
|
1321
1287
|
* Time complexity: O(n)
|
|
1322
1288
|
* Space complexity: O(1)
|
|
@@ -1420,6 +1386,10 @@ export class BinaryTree {
|
|
|
1420
1386
|
}
|
|
1421
1387
|
return ans;
|
|
1422
1388
|
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Time complexity: O(n)
|
|
1391
|
+
* Space complexity: O(1)
|
|
1392
|
+
*/
|
|
1423
1393
|
/**
|
|
1424
1394
|
* The `forEach` function iterates over each entry in a tree and calls a callback function with the
|
|
1425
1395
|
* entry and the tree as arguments.
|
|
@@ -1448,14 +1418,6 @@ export class BinaryTree {
|
|
|
1448
1418
|
}
|
|
1449
1419
|
return newTree;
|
|
1450
1420
|
}
|
|
1451
|
-
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1452
|
-
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1453
|
-
// const newTree = this.createTree();
|
|
1454
|
-
// for (const [key, value] of this) {
|
|
1455
|
-
// newTree.add(key, callback([key, value], this));
|
|
1456
|
-
// }
|
|
1457
|
-
// return newTree;
|
|
1458
|
-
// }
|
|
1459
1421
|
/**
|
|
1460
1422
|
* The `map` function creates a new tree by applying a callback function to each entry in the current
|
|
1461
1423
|
* tree.
|
|
@@ -1469,6 +1431,14 @@ export class BinaryTree {
|
|
|
1469
1431
|
}
|
|
1470
1432
|
return newTree;
|
|
1471
1433
|
}
|
|
1434
|
+
// TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1435
|
+
// map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
|
|
1436
|
+
// const newTree = this.createTree();
|
|
1437
|
+
// for (const [key, value] of this) {
|
|
1438
|
+
// newTree.add(key, callback([key, value], this));
|
|
1439
|
+
// }
|
|
1440
|
+
// return newTree;
|
|
1441
|
+
// }
|
|
1472
1442
|
/**
|
|
1473
1443
|
* The `reduce` function iterates over the entries of a tree and applies a callback function to each
|
|
1474
1444
|
* entry, accumulating a single value.
|
|
@@ -1500,7 +1470,7 @@ export class BinaryTree {
|
|
|
1500
1470
|
*[Symbol.iterator](node = this.root) {
|
|
1501
1471
|
if (!node)
|
|
1502
1472
|
return;
|
|
1503
|
-
if (this.
|
|
1473
|
+
if (this.iterationType === IterationType.ITERATIVE) {
|
|
1504
1474
|
const stack = [];
|
|
1505
1475
|
let current = node;
|
|
1506
1476
|
while (current || stack.length > 0) {
|
|
@@ -1554,6 +1524,19 @@ export class BinaryTree {
|
|
|
1554
1524
|
};
|
|
1555
1525
|
display(beginRoot);
|
|
1556
1526
|
}
|
|
1527
|
+
init(elements) {
|
|
1528
|
+
if (elements) {
|
|
1529
|
+
for (const entryOrKey of elements) {
|
|
1530
|
+
if (Array.isArray(entryOrKey)) {
|
|
1531
|
+
const [key, value] = entryOrKey;
|
|
1532
|
+
this.add(key, value);
|
|
1533
|
+
}
|
|
1534
|
+
else {
|
|
1535
|
+
this.add(entryOrKey);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1557
1540
|
_displayAux(node, options) {
|
|
1558
1541
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1559
1542
|
const emptyDisplayLayout = [['─'], 1, 0, 0];
|