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