avl-tree-typed 1.52.0 → 1.52.2
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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +97 -97
- package/dist/data-structures/binary-tree/binary-tree.js +52 -52
- package/dist/data-structures/binary-tree/bst.d.ts +35 -35
- package/dist/data-structures/binary-tree/bst.js +17 -17
- package/dist/data-structures/binary-tree/rb-tree.d.ts +8 -8
- package/dist/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
- package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/data-structures/graph/directed-graph.js +2 -1
- package/dist/data-structures/queue/deque.d.ts +7 -0
- package/dist/data-structures/queue/deque.js +16 -1
- package/dist/data-structures/queue/queue.d.ts +18 -1
- package/dist/data-structures/queue/queue.js +32 -7
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -1
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +1 -22
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -24
- package/src/data-structures/binary-tree/avl-tree.ts +20 -19
- package/src/data-structures/binary-tree/binary-tree.ts +162 -157
- package/src/data-structures/binary-tree/bst.ts +54 -50
- package/src/data-structures/binary-tree/rb-tree.ts +18 -17
- package/src/data-structures/binary-tree/tree-multi-map.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +9 -7
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +18 -4
- package/src/data-structures/queue/queue.ts +38 -8
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/index.ts +4 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/binary-tree/binary-tree.ts +21 -1
- package/src/types/data-structures/binary-tree/bst.ts +7 -0
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/queue/deque.ts +4 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
|
@@ -14,12 +14,13 @@ import type {
|
|
|
14
14
|
BinaryTreePrintOptions,
|
|
15
15
|
BTNCallback,
|
|
16
16
|
BTNEntry,
|
|
17
|
+
BTNKeyOrNodeOrEntry,
|
|
17
18
|
DFSOrderPattern,
|
|
18
19
|
EntryCallback,
|
|
19
20
|
FamilyPosition,
|
|
20
21
|
IterationType,
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
NodeDisplayLayout,
|
|
23
|
+
OptBTNOrNull
|
|
23
24
|
} from '../../types';
|
|
24
25
|
import { IBinaryTree } from '../../interfaces';
|
|
25
26
|
import { trampoline } from '../../utils';
|
|
@@ -62,16 +63,16 @@ export class BinaryTreeNode<
|
|
|
62
63
|
* @returns The left node of the current node is being returned. It can be either a NODE object,
|
|
63
64
|
* null, or undefined.
|
|
64
65
|
*/
|
|
65
|
-
get left(): NODE
|
|
66
|
+
get left(): OptBTNOrNull<NODE> {
|
|
66
67
|
return this._left;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
/**
|
|
70
71
|
* The function sets the left child of a node and updates its parent reference.
|
|
71
|
-
* @param {NODE
|
|
72
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
72
73
|
* `undefined`.
|
|
73
74
|
*/
|
|
74
|
-
set left(v: NODE
|
|
75
|
+
set left(v: OptBTNOrNull<NODE>) {
|
|
75
76
|
if (v) {
|
|
76
77
|
v.parent = this as unknown as NODE;
|
|
77
78
|
}
|
|
@@ -85,16 +86,16 @@ export class BinaryTreeNode<
|
|
|
85
86
|
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
|
|
86
87
|
* `null`, or `undefined`.
|
|
87
88
|
*/
|
|
88
|
-
get right(): NODE
|
|
89
|
+
get right(): OptBTNOrNull<NODE> {
|
|
89
90
|
return this._right;
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
/**
|
|
93
94
|
* The function sets the right child of a node and updates its parent.
|
|
94
|
-
* @param {NODE
|
|
95
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
|
|
95
96
|
* `undefined`.
|
|
96
97
|
*/
|
|
97
|
-
set right(v: NODE
|
|
98
|
+
set right(v: OptBTNOrNull<NODE>) {
|
|
98
99
|
if (v) {
|
|
99
100
|
v.parent = this as unknown as NODE;
|
|
100
101
|
}
|
|
@@ -130,19 +131,20 @@ export class BinaryTreeNode<
|
|
|
130
131
|
*/
|
|
131
132
|
|
|
132
133
|
export class BinaryTree<
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
>
|
|
134
|
+
K = any,
|
|
135
|
+
V = any,
|
|
136
|
+
R = BTNEntry<K, V>,
|
|
137
|
+
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
138
|
+
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
|
|
139
|
+
>
|
|
139
140
|
extends IterableEntryBase<K, V | undefined>
|
|
140
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
141
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
142
|
+
{
|
|
141
143
|
iterationType: IterationType = 'ITERATIVE';
|
|
142
144
|
|
|
143
145
|
/**
|
|
144
146
|
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
|
|
145
|
-
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of
|
|
147
|
+
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
|
|
146
148
|
* nodes to be added to the binary tree.
|
|
147
149
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
148
150
|
* configuration options for the binary tree. In this case, it is of type
|
|
@@ -150,7 +152,7 @@ export class BinaryTree<
|
|
|
150
152
|
* required.
|
|
151
153
|
*/
|
|
152
154
|
constructor(
|
|
153
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
155
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
154
156
|
options?: BinaryTreeOptions<K, V, R>
|
|
155
157
|
) {
|
|
156
158
|
super();
|
|
@@ -164,14 +166,14 @@ export class BinaryTree<
|
|
|
164
166
|
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
165
167
|
}
|
|
166
168
|
|
|
167
|
-
protected _root?: NODE
|
|
169
|
+
protected _root?: OptBTNOrNull<NODE>;
|
|
168
170
|
|
|
169
171
|
/**
|
|
170
172
|
* The function returns the root node, which can be of type NODE, null, or undefined.
|
|
171
173
|
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
|
|
172
174
|
* `null`, or `undefined`.
|
|
173
175
|
*/
|
|
174
|
-
get root(): NODE
|
|
176
|
+
get root(): OptBTNOrNull<NODE> {
|
|
175
177
|
return this._root;
|
|
176
178
|
}
|
|
177
179
|
|
|
@@ -229,8 +231,8 @@ export class BinaryTree<
|
|
|
229
231
|
/**
|
|
230
232
|
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
|
|
231
233
|
* into a node object.
|
|
232
|
-
* @param {R |
|
|
233
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
234
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
235
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
234
236
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
235
237
|
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
|
|
236
238
|
* key-value pair. If provided, it will be used to create a node with the specified key and value.
|
|
@@ -238,9 +240,9 @@ export class BinaryTree<
|
|
|
238
240
|
* or `undefined`.
|
|
239
241
|
*/
|
|
240
242
|
keyValueOrEntryOrRawElementToNode(
|
|
241
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
243
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
242
244
|
value?: V
|
|
243
|
-
): NODE
|
|
245
|
+
): OptBTNOrNull<NODE> {
|
|
244
246
|
if (keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
245
247
|
if (keyOrNodeOrEntryOrRawElement === null) return null;
|
|
246
248
|
|
|
@@ -275,8 +277,8 @@ export class BinaryTree<
|
|
|
275
277
|
*
|
|
276
278
|
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
|
|
277
279
|
* node if it is a key or entry.
|
|
278
|
-
* @param {R |
|
|
279
|
-
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `
|
|
280
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
281
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
280
282
|
* a raw element.
|
|
281
283
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
282
284
|
* parameter that specifies the type of iteration to be used when searching for a node. It has a
|
|
@@ -284,9 +286,9 @@ export class BinaryTree<
|
|
|
284
286
|
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
|
|
285
287
|
*/
|
|
286
288
|
ensureNode(
|
|
287
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
289
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
288
290
|
iterationType: IterationType = 'ITERATIVE'
|
|
289
|
-
): NODE
|
|
291
|
+
): OptBTNOrNull<NODE> {
|
|
290
292
|
if (keyOrNodeOrEntryOrRawElement === null) return null;
|
|
291
293
|
if (keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
292
294
|
if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
|
|
@@ -310,55 +312,55 @@ export class BinaryTree<
|
|
|
310
312
|
|
|
311
313
|
/**
|
|
312
314
|
* The function checks if the input is an instance of the BinaryTreeNode class.
|
|
313
|
-
* @param {R |
|
|
314
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
315
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
316
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
315
317
|
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
316
318
|
* an instance of the `BinaryTreeNode` class.
|
|
317
319
|
*/
|
|
318
|
-
isNode(keyOrNodeOrEntryOrRawElement: R |
|
|
320
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
|
|
319
321
|
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
|
|
320
322
|
}
|
|
321
323
|
|
|
322
324
|
/**
|
|
323
325
|
* The function checks if a given node is a valid node in a binary search tree.
|
|
324
|
-
* @param {R |
|
|
325
|
-
* `
|
|
326
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
327
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
326
328
|
* @returns a boolean value.
|
|
327
329
|
*/
|
|
328
|
-
isRealNode(node: R |
|
|
330
|
+
isRealNode(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE {
|
|
329
331
|
if (node === this.NIL || node === null || node === undefined) return false;
|
|
330
332
|
return this.isNode(node);
|
|
331
333
|
}
|
|
332
334
|
|
|
333
335
|
/**
|
|
334
336
|
* The function checks if a given node is a real node or null.
|
|
335
|
-
* @param {R |
|
|
336
|
-
* `
|
|
337
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
338
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
337
339
|
* @returns a boolean value.
|
|
338
340
|
*/
|
|
339
|
-
isNodeOrNull(node: R |
|
|
341
|
+
isNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
|
|
340
342
|
return this.isRealNode(node) || node === null;
|
|
341
343
|
}
|
|
342
344
|
|
|
343
345
|
/**
|
|
344
346
|
* The function checks if a given node is equal to the NIL value.
|
|
345
|
-
* @param {R |
|
|
346
|
-
* `
|
|
347
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
|
|
348
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
347
349
|
* @returns a boolean value.
|
|
348
350
|
*/
|
|
349
|
-
isNIL(node: R |
|
|
351
|
+
isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>) {
|
|
350
352
|
return node === this.NIL;
|
|
351
353
|
}
|
|
352
354
|
|
|
353
355
|
/**
|
|
354
356
|
* The function checks if the input is an array with two elements, indicating it is a binary tree
|
|
355
357
|
* node entry.
|
|
356
|
-
* @param {R |
|
|
357
|
-
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `
|
|
358
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
359
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
358
360
|
* @returns a boolean value.
|
|
359
361
|
*/
|
|
360
362
|
isEntry(
|
|
361
|
-
keyOrNodeOrEntryOrRawElement: R |
|
|
363
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
362
364
|
): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
|
|
363
365
|
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
|
|
364
366
|
}
|
|
@@ -402,17 +404,17 @@ export class BinaryTree<
|
|
|
402
404
|
*
|
|
403
405
|
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
|
|
404
406
|
* and finding the appropriate insertion position.
|
|
405
|
-
* @param {R |
|
|
407
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
406
408
|
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
|
|
407
409
|
* node, entry, or raw element to be added to the tree. It can also accept a value of type
|
|
408
|
-
* `
|
|
410
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
409
411
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
410
412
|
* key being added to the tree. It represents the value that will be stored in the tree for the given
|
|
411
413
|
* key.
|
|
412
414
|
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
413
415
|
* insertion position cannot be found or if there are duplicate keys.
|
|
414
416
|
*/
|
|
415
|
-
add(keyOrNodeOrEntryOrRawElement: R |
|
|
417
|
+
add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
416
418
|
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
417
419
|
if (newNode === undefined) return false;
|
|
418
420
|
|
|
@@ -486,7 +488,7 @@ export class BinaryTree<
|
|
|
486
488
|
* successfully added to the data structure.
|
|
487
489
|
*/
|
|
488
490
|
addMany(
|
|
489
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
491
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
490
492
|
values?: Iterable<V | undefined>
|
|
491
493
|
): boolean[] {
|
|
492
494
|
// TODO not sure addMany not be run multi times
|
|
@@ -525,13 +527,13 @@ export class BinaryTree<
|
|
|
525
527
|
*
|
|
526
528
|
* The `refill` function clears the current data and adds new data to the collection.
|
|
527
529
|
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
|
|
528
|
-
* elements. These can be of any type (R) or a specific type (
|
|
530
|
+
* elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
|
|
529
531
|
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
|
|
530
532
|
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
|
|
531
533
|
* keys or nodes. If not provided, the values will be set to `undefined`.
|
|
532
534
|
*/
|
|
533
535
|
refill(
|
|
534
|
-
keysOrNodesOrEntriesOrRawElements: Iterable<R |
|
|
536
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
535
537
|
values?: Iterable<V | undefined>
|
|
536
538
|
): void {
|
|
537
539
|
this.clear();
|
|
@@ -541,7 +543,7 @@ export class BinaryTree<
|
|
|
541
543
|
delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
542
544
|
|
|
543
545
|
delete<C extends BTNCallback<NODE, NODE>>(
|
|
544
|
-
identifier: NODE
|
|
546
|
+
identifier: OptBTNOrNull<NODE>,
|
|
545
547
|
callback?: C
|
|
546
548
|
): BinaryTreeDeleteResult<NODE>[];
|
|
547
549
|
|
|
@@ -619,15 +621,15 @@ export class BinaryTree<
|
|
|
619
621
|
identifier: K,
|
|
620
622
|
callback?: C,
|
|
621
623
|
onlyOne?: boolean,
|
|
622
|
-
beginRoot?: R |
|
|
624
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
623
625
|
iterationType?: IterationType
|
|
624
626
|
): NODE[];
|
|
625
627
|
|
|
626
628
|
getNodes<C extends BTNCallback<NODE, NODE>>(
|
|
627
|
-
identifier: NODE
|
|
629
|
+
identifier: OptBTNOrNull<NODE>,
|
|
628
630
|
callback?: C,
|
|
629
631
|
onlyOne?: boolean,
|
|
630
|
-
beginRoot?: R |
|
|
632
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
631
633
|
iterationType?: IterationType
|
|
632
634
|
): NODE[];
|
|
633
635
|
|
|
@@ -635,7 +637,7 @@ export class BinaryTree<
|
|
|
635
637
|
identifier: ReturnType<C>,
|
|
636
638
|
callback: C,
|
|
637
639
|
onlyOne?: boolean,
|
|
638
|
-
beginRoot?: R |
|
|
640
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
639
641
|
iterationType?: IterationType
|
|
640
642
|
): NODE[];
|
|
641
643
|
|
|
@@ -661,7 +663,7 @@ export class BinaryTree<
|
|
|
661
663
|
* the identifier or all nodes that match the identifier. If set to true, only the first matching
|
|
662
664
|
* node will be returned. If set to false, all matching nodes will be returned. The default value is
|
|
663
665
|
* false.
|
|
664
|
-
* @param {R |
|
|
666
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
665
667
|
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
|
|
666
668
|
* provided, the `root` of the data structure is used as the starting point.
|
|
667
669
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
@@ -672,7 +674,7 @@ export class BinaryTree<
|
|
|
672
674
|
identifier: ReturnType<C> | null | undefined,
|
|
673
675
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
674
676
|
onlyOne = false,
|
|
675
|
-
beginRoot: R |
|
|
677
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
676
678
|
iterationType: IterationType = this.iterationType
|
|
677
679
|
): NODE[] {
|
|
678
680
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -714,23 +716,23 @@ export class BinaryTree<
|
|
|
714
716
|
getNode<C extends BTNCallback<NODE, K>>(
|
|
715
717
|
identifier: K,
|
|
716
718
|
callback?: C,
|
|
717
|
-
beginRoot?: R |
|
|
719
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
718
720
|
iterationType?: IterationType
|
|
719
|
-
): NODE
|
|
721
|
+
): OptBTNOrNull<NODE>;
|
|
720
722
|
|
|
721
723
|
getNode<C extends BTNCallback<NODE, NODE>>(
|
|
722
|
-
identifier: NODE
|
|
724
|
+
identifier: OptBTNOrNull<NODE>,
|
|
723
725
|
callback?: C,
|
|
724
|
-
beginRoot?: R |
|
|
726
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
725
727
|
iterationType?: IterationType
|
|
726
|
-
): NODE
|
|
728
|
+
): OptBTNOrNull<NODE>;
|
|
727
729
|
|
|
728
730
|
getNode<C extends BTNCallback<NODE>>(
|
|
729
731
|
identifier: ReturnType<C>,
|
|
730
732
|
callback: C,
|
|
731
|
-
beginRoot?: R |
|
|
733
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
732
734
|
iterationType?: IterationType
|
|
733
|
-
): NODE
|
|
735
|
+
): OptBTNOrNull<NODE>;
|
|
734
736
|
|
|
735
737
|
/**
|
|
736
738
|
* Time Complexity: O(n)
|
|
@@ -748,7 +750,7 @@ export class BinaryTree<
|
|
|
748
750
|
* the `C` callback function, or it can be `null` or `undefined`.
|
|
749
751
|
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
750
752
|
* node matches the desired criteria. It should return a value that can be used to identify the node.
|
|
751
|
-
* @param {R |
|
|
753
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
752
754
|
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
|
|
753
755
|
* a node entry. If not provided, the search will start from the root of the tree.
|
|
754
756
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -758,9 +760,9 @@ export class BinaryTree<
|
|
|
758
760
|
getNode<C extends BTNCallback<NODE>>(
|
|
759
761
|
identifier: ReturnType<C> | null | undefined,
|
|
760
762
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
761
|
-
beginRoot: R |
|
|
763
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
762
764
|
iterationType: IterationType = this.iterationType
|
|
763
|
-
): NODE
|
|
765
|
+
): OptBTNOrNull<NODE> {
|
|
764
766
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
765
767
|
}
|
|
766
768
|
|
|
@@ -781,28 +783,28 @@ export class BinaryTree<
|
|
|
781
783
|
* It has a default value of `'ITERATIVE'`.
|
|
782
784
|
* @returns a value of type NODE, null, or undefined.
|
|
783
785
|
*/
|
|
784
|
-
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE
|
|
786
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBTNOrNull<NODE> {
|
|
785
787
|
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
786
788
|
}
|
|
787
789
|
|
|
788
790
|
override get<C extends BTNCallback<NODE, K>>(
|
|
789
791
|
identifier: K,
|
|
790
792
|
callback?: C,
|
|
791
|
-
beginRoot?: R |
|
|
793
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
792
794
|
iterationType?: IterationType
|
|
793
795
|
): V | undefined;
|
|
794
796
|
|
|
795
797
|
override get<C extends BTNCallback<NODE, NODE>>(
|
|
796
|
-
identifier: NODE
|
|
798
|
+
identifier: OptBTNOrNull<NODE>,
|
|
797
799
|
callback?: C,
|
|
798
|
-
beginRoot?: R |
|
|
800
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
799
801
|
iterationType?: IterationType
|
|
800
802
|
): V | undefined;
|
|
801
803
|
|
|
802
804
|
override get<C extends BTNCallback<NODE>>(
|
|
803
805
|
identifier: ReturnType<C>,
|
|
804
806
|
callback: C,
|
|
805
|
-
beginRoot?: R |
|
|
807
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
806
808
|
iterationType?: IterationType
|
|
807
809
|
): V | undefined;
|
|
808
810
|
|
|
@@ -822,7 +824,7 @@ export class BinaryTree<
|
|
|
822
824
|
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
|
|
823
825
|
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
824
826
|
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
|
|
825
|
-
* @param {R |
|
|
827
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
826
828
|
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
|
|
827
829
|
* or entry object that exists in the tree. If no specific starting point is provided, the search
|
|
828
830
|
* will begin from the root of the
|
|
@@ -835,7 +837,7 @@ export class BinaryTree<
|
|
|
835
837
|
override get<C extends BTNCallback<NODE>>(
|
|
836
838
|
identifier: ReturnType<C> | null | undefined,
|
|
837
839
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
838
|
-
beginRoot: R |
|
|
840
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
839
841
|
iterationType: IterationType = this.iterationType
|
|
840
842
|
): V | undefined {
|
|
841
843
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
@@ -844,21 +846,21 @@ export class BinaryTree<
|
|
|
844
846
|
override has<C extends BTNCallback<NODE, K>>(
|
|
845
847
|
identifier: K,
|
|
846
848
|
callback?: C,
|
|
847
|
-
beginRoot?: R |
|
|
849
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
848
850
|
iterationType?: IterationType
|
|
849
851
|
): boolean;
|
|
850
852
|
|
|
851
853
|
override has<C extends BTNCallback<NODE, NODE>>(
|
|
852
|
-
identifier: NODE
|
|
854
|
+
identifier: OptBTNOrNull<NODE>,
|
|
853
855
|
callback?: C,
|
|
854
|
-
beginRoot?: R |
|
|
856
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
855
857
|
iterationType?: IterationType
|
|
856
858
|
): boolean;
|
|
857
859
|
|
|
858
860
|
override has<C extends BTNCallback<NODE>>(
|
|
859
861
|
identifier: ReturnType<C> | null | undefined,
|
|
860
862
|
callback: C,
|
|
861
|
-
beginRoot?: R |
|
|
863
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
862
864
|
iterationType?: IterationType
|
|
863
865
|
): boolean;
|
|
864
866
|
|
|
@@ -880,7 +882,7 @@ export class BinaryTree<
|
|
|
880
882
|
* @param {C} callback - The `callback` parameter is a function that will be used to determine
|
|
881
883
|
* whether a node should be included in the result or not. It is of type `C`, which extends the
|
|
882
884
|
* `BTNCallback<NODE>` type.
|
|
883
|
-
* @param {R |
|
|
885
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
884
886
|
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
|
|
885
887
|
* a node entry. If not specified, it defaults to the root of the data structure.
|
|
886
888
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -890,7 +892,7 @@ export class BinaryTree<
|
|
|
890
892
|
override has<C extends BTNCallback<NODE>>(
|
|
891
893
|
identifier: ReturnType<C> | null | undefined,
|
|
892
894
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
893
|
-
beginRoot: R |
|
|
895
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
894
896
|
iterationType: IterationType = this.iterationType
|
|
895
897
|
): boolean {
|
|
896
898
|
callback = this._ensureCallback(identifier, callback);
|
|
@@ -941,13 +943,13 @@ export class BinaryTree<
|
|
|
941
943
|
*
|
|
942
944
|
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
943
945
|
* height of the tree.
|
|
944
|
-
* @param {R |
|
|
946
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
|
|
945
947
|
* has a default value of `this.root`. It represents the starting point for checking if the tree is
|
|
946
948
|
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
|
|
947
|
-
* (`
|
|
949
|
+
* (`BTNKeyOrNodeOrEntry<K, V, NODE
|
|
948
950
|
* @returns a boolean value.
|
|
949
951
|
*/
|
|
950
|
-
isPerfectlyBalanced(beginRoot: R |
|
|
952
|
+
isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
|
|
951
953
|
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
|
952
954
|
}
|
|
953
955
|
|
|
@@ -961,7 +963,7 @@ export class BinaryTree<
|
|
|
961
963
|
* Space Complexity: O(1)
|
|
962
964
|
*
|
|
963
965
|
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
|
|
964
|
-
* @param {R |
|
|
966
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
965
967
|
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
|
|
966
968
|
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
|
|
967
969
|
* of a node in the BST
|
|
@@ -971,7 +973,7 @@ export class BinaryTree<
|
|
|
971
973
|
* @returns a boolean value.
|
|
972
974
|
*/
|
|
973
975
|
isBST(
|
|
974
|
-
beginRoot: R |
|
|
976
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
975
977
|
iterationType: IterationType = this.iterationType
|
|
976
978
|
): boolean {
|
|
977
979
|
// TODO there is a bug
|
|
@@ -979,7 +981,7 @@ export class BinaryTree<
|
|
|
979
981
|
if (!beginRoot) return true;
|
|
980
982
|
|
|
981
983
|
if (iterationType === 'RECURSIVE') {
|
|
982
|
-
const dfs = (cur: NODE
|
|
984
|
+
const dfs = (cur: OptBTNOrNull<NODE>, min: number, max: number): boolean => {
|
|
983
985
|
if (!this.isRealNode(cur)) return true;
|
|
984
986
|
const numKey = Number(cur.key);
|
|
985
987
|
if (numKey <= min || numKey >= max) return false;
|
|
@@ -994,7 +996,7 @@ export class BinaryTree<
|
|
|
994
996
|
const stack = [];
|
|
995
997
|
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
996
998
|
// @ts-ignore
|
|
997
|
-
let curr: NODE
|
|
999
|
+
let curr: OptBTNOrNull<NODE> = beginRoot;
|
|
998
1000
|
while (this.isRealNode(curr) || stack.length > 0) {
|
|
999
1001
|
while (this.isRealNode(curr)) {
|
|
1000
1002
|
stack.push(curr);
|
|
@@ -1024,16 +1026,19 @@ export class BinaryTree<
|
|
|
1024
1026
|
* Space Complexity: O(1)
|
|
1025
1027
|
*
|
|
1026
1028
|
* The function calculates the depth of a given node or key in a tree-like data structure.
|
|
1027
|
-
* @param {R |
|
|
1028
|
-
* (representing a root node), or a `
|
|
1029
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
|
|
1030
|
+
* (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
|
|
1029
1031
|
* entry).
|
|
1030
|
-
* @param {R |
|
|
1032
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
|
|
1031
1033
|
* represents the starting point from which to calculate the depth. It can be either a reference to a
|
|
1032
1034
|
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
|
|
1033
1035
|
* `this.root`, which refers to the root node
|
|
1034
1036
|
* @returns the depth of a node in a tree structure.
|
|
1035
1037
|
*/
|
|
1036
|
-
getDepth(
|
|
1038
|
+
getDepth(
|
|
1039
|
+
dist: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1040
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1041
|
+
): number {
|
|
1037
1042
|
let distEnsured = this.ensureNode(dist);
|
|
1038
1043
|
const beginRootEnsured = this.ensureNode(beginRoot);
|
|
1039
1044
|
let depth = 0;
|
|
@@ -1058,22 +1063,22 @@ export class BinaryTree<
|
|
|
1058
1063
|
*
|
|
1059
1064
|
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
|
|
1060
1065
|
* or iterative approach.
|
|
1061
|
-
* @param {R |
|
|
1066
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1062
1067
|
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
|
|
1063
|
-
* node or entry (`
|
|
1068
|
+
* node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
|
|
1064
1069
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1065
1070
|
* iteration used to calculate the height of the tree. It can have two possible values:
|
|
1066
1071
|
* @returns the maximum height of the binary tree.
|
|
1067
1072
|
*/
|
|
1068
1073
|
getHeight(
|
|
1069
|
-
beginRoot: R |
|
|
1074
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1070
1075
|
iterationType: IterationType = this.iterationType
|
|
1071
1076
|
): number {
|
|
1072
1077
|
beginRoot = this.ensureNode(beginRoot);
|
|
1073
1078
|
if (!this.isRealNode(beginRoot)) return -1;
|
|
1074
1079
|
|
|
1075
1080
|
if (iterationType === 'RECURSIVE') {
|
|
1076
|
-
const _getMaxHeight = (cur: NODE
|
|
1081
|
+
const _getMaxHeight = (cur: OptBTNOrNull<NODE>): number => {
|
|
1077
1082
|
if (!this.isRealNode(cur)) return -1;
|
|
1078
1083
|
const leftHeight = _getMaxHeight(cur.left);
|
|
1079
1084
|
const rightHeight = _getMaxHeight(cur.right);
|
|
@@ -1109,9 +1114,9 @@ export class BinaryTree<
|
|
|
1109
1114
|
*
|
|
1110
1115
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
1111
1116
|
* recursive or iterative approach.
|
|
1112
|
-
* @param {R |
|
|
1117
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1113
1118
|
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
|
|
1114
|
-
* key or node or entry (`
|
|
1119
|
+
* key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
|
|
1115
1120
|
* tree.
|
|
1116
1121
|
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
1117
1122
|
* iteration to be used when calculating the minimum height of the tree. It can have two possible
|
|
@@ -1120,14 +1125,14 @@ export class BinaryTree<
|
|
|
1120
1125
|
* binary tree.
|
|
1121
1126
|
*/
|
|
1122
1127
|
getMinHeight(
|
|
1123
|
-
beginRoot: R |
|
|
1128
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1124
1129
|
iterationType: IterationType = this.iterationType
|
|
1125
1130
|
): number {
|
|
1126
1131
|
beginRoot = this.ensureNode(beginRoot);
|
|
1127
1132
|
if (!beginRoot) return -1;
|
|
1128
1133
|
|
|
1129
1134
|
if (iterationType === 'RECURSIVE') {
|
|
1130
|
-
const _getMinHeight = (cur: NODE
|
|
1135
|
+
const _getMinHeight = (cur: OptBTNOrNull<NODE>): number => {
|
|
1131
1136
|
if (!this.isRealNode(cur)) return 0;
|
|
1132
1137
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
|
|
1133
1138
|
const leftMinHeight = _getMinHeight(cur.left);
|
|
@@ -1138,8 +1143,8 @@ export class BinaryTree<
|
|
|
1138
1143
|
return _getMinHeight(beginRoot);
|
|
1139
1144
|
} else {
|
|
1140
1145
|
const stack: NODE[] = [];
|
|
1141
|
-
let node: NODE
|
|
1142
|
-
last: NODE
|
|
1146
|
+
let node: OptBTNOrNull<NODE> = beginRoot,
|
|
1147
|
+
last: OptBTNOrNull<NODE> = null;
|
|
1143
1148
|
const depths: Map<NODE, number> = new Map();
|
|
1144
1149
|
|
|
1145
1150
|
while (stack.length > 0 || node) {
|
|
@@ -1151,8 +1156,8 @@ export class BinaryTree<
|
|
|
1151
1156
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
1152
1157
|
node = stack.pop();
|
|
1153
1158
|
if (this.isRealNode(node)) {
|
|
1154
|
-
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) ?? -1 : -1;
|
|
1155
|
-
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) ?? -1 : -1;
|
|
1159
|
+
const leftMinHeight = this.isRealNode(node.left) ? (depths.get(node.left) ?? -1) : -1;
|
|
1160
|
+
const rightMinHeight = this.isRealNode(node.right) ? (depths.get(node.right) ?? -1) : -1;
|
|
1156
1161
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
1157
1162
|
last = node;
|
|
1158
1163
|
node = null;
|
|
@@ -1176,14 +1181,14 @@ export class BinaryTree<
|
|
|
1176
1181
|
*
|
|
1177
1182
|
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
1178
1183
|
* up to the root node, with an option to reverse the order of the nodes.
|
|
1179
|
-
* @param {R |
|
|
1180
|
-
* type `R` or `
|
|
1184
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
|
|
1185
|
+
* type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
1181
1186
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
1182
1187
|
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
1183
1188
|
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
1184
1189
|
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
|
|
1185
1190
|
*/
|
|
1186
|
-
getPathToRoot(beginNode: R |
|
|
1191
|
+
getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
|
|
1187
1192
|
const result: NODE[] = [];
|
|
1188
1193
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
1189
1194
|
|
|
@@ -1209,17 +1214,17 @@ export class BinaryTree<
|
|
|
1209
1214
|
*
|
|
1210
1215
|
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
|
|
1211
1216
|
* iterative traversal.
|
|
1212
|
-
* @param {R |
|
|
1217
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1213
1218
|
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
|
|
1214
|
-
* a key or node or entry (`
|
|
1219
|
+
* a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1215
1220
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1216
1221
|
* of iteration to be performed. It can have two possible values:
|
|
1217
1222
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
|
|
1218
1223
|
*/
|
|
1219
1224
|
getLeftMost(
|
|
1220
|
-
beginRoot: R |
|
|
1225
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1221
1226
|
iterationType: IterationType = this.iterationType
|
|
1222
|
-
): NODE
|
|
1227
|
+
): OptBTNOrNull<NODE> {
|
|
1223
1228
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1224
1229
|
beginRoot = this.ensureNode(beginRoot);
|
|
1225
1230
|
|
|
@@ -1254,18 +1259,18 @@ export class BinaryTree<
|
|
|
1254
1259
|
*
|
|
1255
1260
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
|
|
1256
1261
|
* iteratively.
|
|
1257
|
-
* @param {R |
|
|
1262
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1258
1263
|
* starting point for finding the rightmost node in a binary tree. It can be either a root node
|
|
1259
|
-
* (`R`), a key or node or entry (`
|
|
1264
|
+
* (`R`), a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
|
|
1260
1265
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
1261
1266
|
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
|
|
1262
1267
|
* possible values:
|
|
1263
1268
|
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
|
|
1264
1269
|
*/
|
|
1265
1270
|
getRightMost(
|
|
1266
|
-
beginRoot: R |
|
|
1271
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1267
1272
|
iterationType: IterationType = this.iterationType
|
|
1268
|
-
): NODE
|
|
1273
|
+
): OptBTNOrNull<NODE> {
|
|
1269
1274
|
if (this.isNIL(beginRoot)) return beginRoot as NODE;
|
|
1270
1275
|
// TODO support get right most by passing key in
|
|
1271
1276
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -1305,7 +1310,7 @@ export class BinaryTree<
|
|
|
1305
1310
|
*/
|
|
1306
1311
|
getPredecessor(node: NODE): NODE {
|
|
1307
1312
|
if (this.isRealNode(node.left)) {
|
|
1308
|
-
let predecessor: NODE
|
|
1313
|
+
let predecessor: OptBTNOrNull<NODE> = node.left;
|
|
1309
1314
|
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
|
|
1310
1315
|
if (this.isRealNode(predecessor)) {
|
|
1311
1316
|
predecessor = predecessor.right;
|
|
@@ -1331,7 +1336,7 @@ export class BinaryTree<
|
|
|
1331
1336
|
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
|
|
1332
1337
|
* there is no successor, and `undefined` if the input `x` is not a valid node.
|
|
1333
1338
|
*/
|
|
1334
|
-
getSuccessor(x?: K | NODE | null): NODE
|
|
1339
|
+
getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
|
|
1335
1340
|
x = this.ensureNode(x);
|
|
1336
1341
|
if (!this.isRealNode(x)) return undefined;
|
|
1337
1342
|
|
|
@@ -1339,7 +1344,7 @@ export class BinaryTree<
|
|
|
1339
1344
|
return this.getLeftMost(x.right);
|
|
1340
1345
|
}
|
|
1341
1346
|
|
|
1342
|
-
let y: NODE
|
|
1347
|
+
let y: OptBTNOrNull<NODE> = x.parent;
|
|
1343
1348
|
while (this.isRealNode(y) && x === y.right) {
|
|
1344
1349
|
x = y;
|
|
1345
1350
|
y = y.parent;
|
|
@@ -1350,7 +1355,7 @@ export class BinaryTree<
|
|
|
1350
1355
|
dfs<C extends BTNCallback<NODE>>(
|
|
1351
1356
|
callback?: C,
|
|
1352
1357
|
pattern?: DFSOrderPattern,
|
|
1353
|
-
beginRoot?: R |
|
|
1358
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1354
1359
|
iterationType?: IterationType,
|
|
1355
1360
|
includeNull?: false
|
|
1356
1361
|
): ReturnType<C>[];
|
|
@@ -1358,7 +1363,7 @@ export class BinaryTree<
|
|
|
1358
1363
|
dfs<C extends BTNCallback<NODE | null>>(
|
|
1359
1364
|
callback?: C,
|
|
1360
1365
|
pattern?: DFSOrderPattern,
|
|
1361
|
-
beginRoot?: R |
|
|
1366
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1362
1367
|
iterationType?: IterationType,
|
|
1363
1368
|
includeNull?: true
|
|
1364
1369
|
): ReturnType<C>[];
|
|
@@ -1379,7 +1384,7 @@ export class BinaryTree<
|
|
|
1379
1384
|
* return type of the callback function is determined by the generic type `C`.
|
|
1380
1385
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
|
|
1381
1386
|
* nodes are visited during the depth-first search. It can have one of the following values:
|
|
1382
|
-
* @param {R |
|
|
1387
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1383
1388
|
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
|
|
1384
1389
|
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
|
|
1385
1390
|
* search from there.
|
|
@@ -1391,10 +1396,10 @@ export class BinaryTree<
|
|
|
1391
1396
|
* values will
|
|
1392
1397
|
* @returns an array of the return types of the callback function.
|
|
1393
1398
|
*/
|
|
1394
|
-
dfs<C extends BTNCallback<NODE
|
|
1399
|
+
dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
|
|
1395
1400
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1396
1401
|
pattern: DFSOrderPattern = 'IN',
|
|
1397
|
-
beginRoot: R |
|
|
1402
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1398
1403
|
iterationType: IterationType = 'ITERATIVE',
|
|
1399
1404
|
includeNull = false
|
|
1400
1405
|
): ReturnType<C>[] {
|
|
@@ -1402,7 +1407,7 @@ export class BinaryTree<
|
|
|
1402
1407
|
if (!beginRoot) return [];
|
|
1403
1408
|
const ans: ReturnType<C>[] = [];
|
|
1404
1409
|
if (iterationType === 'RECURSIVE') {
|
|
1405
|
-
const dfs = (node: NODE
|
|
1410
|
+
const dfs = (node: OptBTNOrNull<NODE>) => {
|
|
1406
1411
|
switch (pattern) {
|
|
1407
1412
|
case 'IN':
|
|
1408
1413
|
if (includeNull) {
|
|
@@ -1444,7 +1449,7 @@ export class BinaryTree<
|
|
|
1444
1449
|
dfs(beginRoot);
|
|
1445
1450
|
} else {
|
|
1446
1451
|
// 0: visit, 1: print
|
|
1447
|
-
const stack: { opt: 0 | 1; node: NODE
|
|
1452
|
+
const stack: { opt: 0 | 1; node: OptBTNOrNull<NODE> }[] = [{ opt: 0, node: beginRoot }];
|
|
1448
1453
|
|
|
1449
1454
|
while (stack.length > 0) {
|
|
1450
1455
|
const cur = stack.pop();
|
|
@@ -1488,14 +1493,14 @@ export class BinaryTree<
|
|
|
1488
1493
|
|
|
1489
1494
|
bfs<C extends BTNCallback<NODE>>(
|
|
1490
1495
|
callback?: C,
|
|
1491
|
-
beginRoot?: R |
|
|
1496
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1492
1497
|
iterationType?: IterationType,
|
|
1493
1498
|
includeNull?: false
|
|
1494
1499
|
): ReturnType<C>[];
|
|
1495
1500
|
|
|
1496
1501
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1497
1502
|
callback?: C,
|
|
1498
|
-
beginRoot?: R |
|
|
1503
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1499
1504
|
iterationType?: IterationType,
|
|
1500
1505
|
includeNull?: true
|
|
1501
1506
|
): ReturnType<C>[];
|
|
@@ -1514,7 +1519,7 @@ export class BinaryTree<
|
|
|
1514
1519
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1515
1520
|
* the breadth-first search traversal. It takes a single argument, which is the current node being
|
|
1516
1521
|
* visited, and returns a value of any type.
|
|
1517
|
-
* @param {R |
|
|
1522
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1518
1523
|
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
|
|
1519
1524
|
* or entry object. If no value is provided, the `root` property of the class is used as the default
|
|
1520
1525
|
* starting point.
|
|
@@ -1529,7 +1534,7 @@ export class BinaryTree<
|
|
|
1529
1534
|
*/
|
|
1530
1535
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1531
1536
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1532
|
-
beginRoot: R |
|
|
1537
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1533
1538
|
iterationType: IterationType = this.iterationType,
|
|
1534
1539
|
includeNull = false
|
|
1535
1540
|
): ReturnType<C>[] {
|
|
@@ -1539,7 +1544,7 @@ export class BinaryTree<
|
|
|
1539
1544
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1540
1545
|
|
|
1541
1546
|
if (iterationType === 'RECURSIVE') {
|
|
1542
|
-
const queue: Queue<NODE
|
|
1547
|
+
const queue: Queue<OptBTNOrNull<NODE>> = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
|
|
1543
1548
|
|
|
1544
1549
|
const dfs = (level: number) => {
|
|
1545
1550
|
if (queue.size === 0) return;
|
|
@@ -1560,7 +1565,7 @@ export class BinaryTree<
|
|
|
1560
1565
|
|
|
1561
1566
|
dfs(0);
|
|
1562
1567
|
} else {
|
|
1563
|
-
const queue = new Queue<NODE
|
|
1568
|
+
const queue = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
|
|
1564
1569
|
while (queue.size > 0) {
|
|
1565
1570
|
const levelSize = queue.size;
|
|
1566
1571
|
|
|
@@ -1583,14 +1588,14 @@ export class BinaryTree<
|
|
|
1583
1588
|
|
|
1584
1589
|
listLevels<C extends BTNCallback<NODE>>(
|
|
1585
1590
|
callback?: C,
|
|
1586
|
-
beginRoot?: R |
|
|
1591
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1587
1592
|
iterationType?: IterationType,
|
|
1588
1593
|
includeNull?: false
|
|
1589
1594
|
): ReturnType<C>[][];
|
|
1590
1595
|
|
|
1591
1596
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1592
1597
|
callback?: C,
|
|
1593
|
-
beginRoot?: R |
|
|
1598
|
+
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
1594
1599
|
iterationType?: IterationType,
|
|
1595
1600
|
includeNull?: true
|
|
1596
1601
|
): ReturnType<C>[][];
|
|
@@ -1609,7 +1614,7 @@ export class BinaryTree<
|
|
|
1609
1614
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1610
1615
|
* the tree. It takes a node as an argument and returns a value. The return type of the callback
|
|
1611
1616
|
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
|
|
1612
|
-
* @param {R |
|
|
1617
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
|
|
1613
1618
|
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
|
|
1614
1619
|
* entry. If no value is provided, the `root` property of the class is used as the default starting
|
|
1615
1620
|
* point.
|
|
@@ -1623,7 +1628,7 @@ export class BinaryTree<
|
|
|
1623
1628
|
*/
|
|
1624
1629
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1625
1630
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1626
|
-
beginRoot: R |
|
|
1631
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1627
1632
|
iterationType: IterationType = this.iterationType,
|
|
1628
1633
|
includeNull = false
|
|
1629
1634
|
): ReturnType<C>[][] {
|
|
@@ -1686,7 +1691,7 @@ export class BinaryTree<
|
|
|
1686
1691
|
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
|
|
1687
1692
|
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
|
|
1688
1693
|
* following values:
|
|
1689
|
-
* @param {R |
|
|
1694
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1690
1695
|
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
|
|
1691
1696
|
* provided, the `root` of the tree is used as the starting point.
|
|
1692
1697
|
* @returns The function `morris` returns an array of values that are the return values of the
|
|
@@ -1695,16 +1700,16 @@ export class BinaryTree<
|
|
|
1695
1700
|
morris<C extends BTNCallback<NODE>>(
|
|
1696
1701
|
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1697
1702
|
pattern: DFSOrderPattern = 'IN',
|
|
1698
|
-
beginRoot: R |
|
|
1703
|
+
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1699
1704
|
): ReturnType<C>[] {
|
|
1700
1705
|
beginRoot = this.ensureNode(beginRoot);
|
|
1701
1706
|
if (beginRoot === null) return [];
|
|
1702
1707
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
1703
1708
|
|
|
1704
|
-
let cur: NODE
|
|
1705
|
-
const _reverseEdge = (node: NODE
|
|
1706
|
-
let pre: NODE
|
|
1707
|
-
let next: NODE
|
|
1709
|
+
let cur: OptBTNOrNull<NODE> = beginRoot;
|
|
1710
|
+
const _reverseEdge = (node: OptBTNOrNull<NODE>) => {
|
|
1711
|
+
let pre: OptBTNOrNull<NODE> = null;
|
|
1712
|
+
let next: OptBTNOrNull<NODE> = null;
|
|
1708
1713
|
while (node) {
|
|
1709
1714
|
next = node.right;
|
|
1710
1715
|
node.right = pre;
|
|
@@ -1713,9 +1718,9 @@ export class BinaryTree<
|
|
|
1713
1718
|
}
|
|
1714
1719
|
return pre;
|
|
1715
1720
|
};
|
|
1716
|
-
const _printEdge = (node: NODE
|
|
1717
|
-
const tail: NODE
|
|
1718
|
-
let cur: NODE
|
|
1721
|
+
const _printEdge = (node: OptBTNOrNull<NODE>) => {
|
|
1722
|
+
const tail: OptBTNOrNull<NODE> = _reverseEdge(node);
|
|
1723
|
+
let cur: OptBTNOrNull<NODE> = tail;
|
|
1719
1724
|
while (cur) {
|
|
1720
1725
|
ans.push(callback(cur));
|
|
1721
1726
|
cur = cur.right;
|
|
@@ -1884,7 +1889,7 @@ export class BinaryTree<
|
|
|
1884
1889
|
* Space Complexity: O(n)
|
|
1885
1890
|
*
|
|
1886
1891
|
* The `print` function in TypeScript prints the binary tree structure with customizable options.
|
|
1887
|
-
* @param {R |
|
|
1892
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
|
|
1888
1893
|
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
|
|
1889
1894
|
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
|
|
1890
1895
|
* as the starting point.
|
|
@@ -1893,7 +1898,7 @@ export class BinaryTree<
|
|
|
1893
1898
|
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
|
|
1894
1899
|
* not return any value.
|
|
1895
1900
|
*/
|
|
1896
|
-
override print(beginRoot: R |
|
|
1901
|
+
override print(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
|
|
1897
1902
|
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
|
1898
1903
|
beginRoot = this.ensureNode(beginRoot);
|
|
1899
1904
|
if (!beginRoot) return;
|
|
@@ -1908,7 +1913,7 @@ export class BinaryTree<
|
|
|
1908
1913
|
console.log(`S for Sentinel Node(NIL)
|
|
1909
1914
|
`);
|
|
1910
1915
|
|
|
1911
|
-
const display = (root: NODE
|
|
1916
|
+
const display = (root: OptBTNOrNull<NODE>): void => {
|
|
1912
1917
|
const [lines, , ,] = this._displayAux(root, opts);
|
|
1913
1918
|
for (const line of lines) {
|
|
1914
1919
|
console.log(line);
|
|
@@ -1933,12 +1938,12 @@ export class BinaryTree<
|
|
|
1933
1938
|
* initially set to the root node of the tree.
|
|
1934
1939
|
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1935
1940
|
*/
|
|
1936
|
-
protected*
|
|
1941
|
+
protected *_getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1937
1942
|
if (!node) return;
|
|
1938
1943
|
|
|
1939
1944
|
if (this.iterationType === 'ITERATIVE') {
|
|
1940
|
-
const stack:
|
|
1941
|
-
let current: NODE
|
|
1945
|
+
const stack: OptBTNOrNull<NODE>[] = [];
|
|
1946
|
+
let current: OptBTNOrNull<NODE> = node;
|
|
1942
1947
|
|
|
1943
1948
|
while (current || stack.length > 0) {
|
|
1944
1949
|
while (this.isRealNode(current)) {
|
|
@@ -1975,7 +1980,7 @@ export class BinaryTree<
|
|
|
1975
1980
|
*
|
|
1976
1981
|
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
|
|
1977
1982
|
* taking into account various options such as whether to show null, undefined, or NaN nodes.
|
|
1978
|
-
* @param {NODE
|
|
1983
|
+
* @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
|
|
1979
1984
|
* It can be of type `NODE`, `null`, or `undefined`.
|
|
1980
1985
|
* @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
|
|
1981
1986
|
* following properties:
|
|
@@ -1986,7 +1991,7 @@ export class BinaryTree<
|
|
|
1986
1991
|
* 3. `totalHeight`: The total height of the node display.
|
|
1987
1992
|
* 4. `middleIndex`: The index of the middle character
|
|
1988
1993
|
*/
|
|
1989
|
-
protected _displayAux(node: NODE
|
|
1994
|
+
protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
|
|
1990
1995
|
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
|
|
1991
1996
|
const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
|
|
1992
1997
|
|
|
@@ -2054,7 +2059,7 @@ export class BinaryTree<
|
|
|
2054
2059
|
}
|
|
2055
2060
|
}
|
|
2056
2061
|
|
|
2057
|
-
protected _DEFAULT_CALLBACK = (node: NODE
|
|
2062
|
+
protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
|
|
2058
2063
|
|
|
2059
2064
|
/**
|
|
2060
2065
|
* Time Complexity: O(1)
|
|
@@ -2066,17 +2071,17 @@ export class BinaryTree<
|
|
|
2066
2071
|
* Space Complexity: O(1)
|
|
2067
2072
|
*
|
|
2068
2073
|
* The function `_swapProperties` swaps the key-value properties between two nodes.
|
|
2069
|
-
* @param {R |
|
|
2074
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
|
|
2070
2075
|
* destination node. It can be either an instance of the class `R`, or an object of type
|
|
2071
|
-
* `
|
|
2072
|
-
* @param {R |
|
|
2076
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
2077
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
|
|
2073
2078
|
* the properties will be swapped with the `srcNode`.
|
|
2074
2079
|
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
|
|
2075
2080
|
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
|
|
2076
2081
|
*/
|
|
2077
2082
|
protected _swapProperties(
|
|
2078
|
-
srcNode: R |
|
|
2079
|
-
destNode: R |
|
|
2083
|
+
srcNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
2084
|
+
destNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
2080
2085
|
): NODE | undefined {
|
|
2081
2086
|
srcNode = this.ensureNode(srcNode);
|
|
2082
2087
|
destNode = this.ensureNode(destNode);
|
|
@@ -2144,10 +2149,10 @@ export class BinaryTree<
|
|
|
2144
2149
|
*
|
|
2145
2150
|
* The function sets the root property of an object to the provided value, and also updates the
|
|
2146
2151
|
* parent property of the new root.
|
|
2147
|
-
* @param {NODE
|
|
2152
|
+
* @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
|
|
2148
2153
|
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
|
|
2149
2154
|
*/
|
|
2150
|
-
protected _setRoot(v: NODE
|
|
2155
|
+
protected _setRoot(v: OptBTNOrNull<NODE>) {
|
|
2151
2156
|
if (v) {
|
|
2152
2157
|
v.parent = undefined;
|
|
2153
2158
|
}
|