data-structure-typed 1.36.9 → 1.37.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/CHANGELOG.md +2 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -11
- package/dist/data-structures/binary-tree/binary-tree.js +8 -57
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +6 -20
- package/dist/data-structures/binary-tree/bst.js +22 -122
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -55
- package/dist/data-structures/binary-tree/tree-multiset.js +3 -240
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.js +4 -3
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/lib/data-structures/binary-tree/binary-tree.d.ts +1 -11
- package/lib/data-structures/binary-tree/binary-tree.js +8 -57
- package/lib/data-structures/binary-tree/bst.d.ts +6 -20
- package/lib/data-structures/binary-tree/bst.js +22 -122
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -55
- package/lib/data-structures/binary-tree/tree-multiset.js +3 -240
- package/lib/data-structures/graph/abstract-graph.js +4 -3
- package/package.json +9 -7
- package/src/data-structures/binary-tree/binary-tree.ts +100 -504
- package/src/data-structures/binary-tree/bst.ts +58 -128
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -312
- package/src/data-structures/graph/abstract-graph.ts +4 -3
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/binary-tree.ts +4 -0
- package/src/types/data-structures/index.ts +3 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -9
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +85 -18
- package/test/unit/data-structures/binary-tree/overall.test.ts +3 -2
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +43 -18
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +17 -0
- package/test/unit/data-structures/queue/queue.test.ts +42 -0
- package/test/utils/big-o.ts +5 -4
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -7,22 +7,23 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
+
BFSCallback,
|
|
11
|
+
BFSCallbackReturn,
|
|
10
12
|
BinaryTreeNodeKey,
|
|
11
13
|
BinaryTreeNodeNested,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
BinaryTreeOptions,
|
|
15
|
+
MapCallback,
|
|
16
|
+
MapCallbackReturn
|
|
15
17
|
} from '../../types';
|
|
16
18
|
import {
|
|
17
19
|
BinaryTreeDeletedResult,
|
|
18
|
-
BinaryTreeNodePropertyName,
|
|
19
20
|
DFSOrderPattern,
|
|
20
21
|
FamilyPosition,
|
|
21
|
-
LoopType
|
|
22
|
-
NodeOrPropertyName
|
|
22
|
+
LoopType
|
|
23
23
|
} from '../../types';
|
|
24
24
|
import {IBinaryTree} from '../../interfaces';
|
|
25
25
|
import {trampoline} from '../../utils';
|
|
26
|
+
import {Queue} from '../queue';
|
|
26
27
|
|
|
27
28
|
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
|
|
28
29
|
/**
|
|
@@ -126,6 +127,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
126
127
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
127
128
|
return new BinaryTreeNode<N['val'], N>(key, val) as N;
|
|
128
129
|
}
|
|
130
|
+
|
|
129
131
|
// TODO placeholder node may need redesigned
|
|
130
132
|
private _root: N | null = null;
|
|
131
133
|
|
|
@@ -149,12 +151,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
149
151
|
this._loopType = v;
|
|
150
152
|
}
|
|
151
153
|
|
|
152
|
-
visitedKey: BinaryTreeNodeKey[] = [];
|
|
153
|
-
|
|
154
|
-
visitedVal: N['val'][] = [];
|
|
155
|
-
|
|
156
|
-
visitedNode: N[] = [];
|
|
157
|
-
|
|
158
154
|
/**
|
|
159
155
|
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
160
156
|
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
@@ -183,7 +179,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
183
179
|
clear() {
|
|
184
180
|
this._root = null;
|
|
185
181
|
this._size = 0;
|
|
186
|
-
this._clearResults();
|
|
187
182
|
}
|
|
188
183
|
|
|
189
184
|
/**
|
|
@@ -210,8 +205,8 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
210
205
|
*/
|
|
211
206
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
212
207
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
213
|
-
const queue
|
|
214
|
-
while (queue.
|
|
208
|
+
const queue = new Queue<N | null>([root]);
|
|
209
|
+
while (queue.size > 0) {
|
|
215
210
|
const cur = queue.shift();
|
|
216
211
|
if (cur) {
|
|
217
212
|
if (newNode && cur.key === newNode.key) return;
|
|
@@ -236,7 +231,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
236
231
|
return;
|
|
237
232
|
}
|
|
238
233
|
|
|
239
|
-
const existNode = keyOrNode ? this.get(keyOrNode,
|
|
234
|
+
const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined;
|
|
240
235
|
|
|
241
236
|
if (this.root) {
|
|
242
237
|
if (existNode) {
|
|
@@ -359,8 +354,8 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
359
354
|
* @returns the depth of the given node or binary tree.
|
|
360
355
|
*/
|
|
361
356
|
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
362
|
-
if (typeof distNode === 'number') distNode = this.get(distNode
|
|
363
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot
|
|
357
|
+
if (typeof distNode === 'number') distNode = this.get(distNode);
|
|
358
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
364
359
|
let depth = 0;
|
|
365
360
|
while (distNode?.parent) {
|
|
366
361
|
if (distNode === beginRoot) {
|
|
@@ -380,7 +375,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
380
375
|
* @returns the height of the binary tree.
|
|
381
376
|
*/
|
|
382
377
|
getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
383
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot
|
|
378
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
384
379
|
if (!beginRoot) return -1;
|
|
385
380
|
|
|
386
381
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
@@ -418,6 +413,8 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
418
413
|
}
|
|
419
414
|
}
|
|
420
415
|
|
|
416
|
+
protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
|
|
417
|
+
|
|
421
418
|
/**
|
|
422
419
|
* The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
|
|
423
420
|
* approach.
|
|
@@ -481,125 +478,108 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
481
478
|
|
|
482
479
|
/**
|
|
483
480
|
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
481
|
+
* @param callback
|
|
484
482
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
485
483
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
486
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
487
484
|
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
|
|
488
485
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
489
486
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
490
487
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
488
|
+
* @param beginRoot
|
|
491
489
|
* @returns an array of nodes (type N).
|
|
492
490
|
*/
|
|
493
491
|
getNodes(
|
|
494
492
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
495
|
-
|
|
496
|
-
onlyOne = false
|
|
493
|
+
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
494
|
+
onlyOne = false,
|
|
495
|
+
beginRoot: N | null = this.root
|
|
497
496
|
): N[] {
|
|
498
|
-
if (!
|
|
497
|
+
if (!beginRoot) return [];
|
|
499
498
|
|
|
500
|
-
const
|
|
499
|
+
const ans: N[] = [];
|
|
501
500
|
|
|
502
501
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
503
502
|
const _traverse = (cur: N) => {
|
|
504
|
-
if (
|
|
503
|
+
if (callback(cur) === nodeProperty) {
|
|
504
|
+
ans.push(cur);
|
|
505
|
+
if (onlyOne) return;
|
|
506
|
+
}
|
|
505
507
|
if (!cur.left && !cur.right) return;
|
|
506
508
|
cur.left && _traverse(cur.left);
|
|
507
509
|
cur.right && _traverse(cur.right);
|
|
508
510
|
};
|
|
509
511
|
|
|
510
|
-
_traverse(
|
|
512
|
+
_traverse(beginRoot);
|
|
511
513
|
} else {
|
|
512
|
-
const queue
|
|
513
|
-
while (queue.
|
|
514
|
+
const queue = new Queue<N>([beginRoot]);
|
|
515
|
+
while (queue.size > 0) {
|
|
514
516
|
const cur = queue.shift();
|
|
515
517
|
if (cur) {
|
|
516
|
-
if (
|
|
518
|
+
if (callback(cur) === nodeProperty) {
|
|
519
|
+
ans.push(cur);
|
|
520
|
+
if (onlyOne) return ans;
|
|
521
|
+
}
|
|
517
522
|
cur.left && queue.push(cur.left);
|
|
518
523
|
cur.right && queue.push(cur.right);
|
|
519
524
|
}
|
|
520
525
|
}
|
|
521
526
|
}
|
|
522
527
|
|
|
523
|
-
return
|
|
528
|
+
return ans;
|
|
524
529
|
}
|
|
525
530
|
|
|
526
531
|
/**
|
|
527
532
|
* The function checks if a binary tree node has a specific property.
|
|
533
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
528
534
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
529
535
|
* It represents the property of the binary tree node that you want to check.
|
|
530
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
531
536
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
532
537
|
* @returns a boolean value.
|
|
533
538
|
*/
|
|
534
|
-
has(nodeProperty: BinaryTreeNodeKey | N,
|
|
539
|
+
has(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback<N> = this._defaultCallbackByKey): boolean {
|
|
535
540
|
// TODO may support finding node by value equal
|
|
536
|
-
return this.getNodes(nodeProperty,
|
|
541
|
+
return this.getNodes(nodeProperty, callback, true).length > 0;
|
|
537
542
|
}
|
|
538
543
|
|
|
539
544
|
/**
|
|
540
545
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
541
546
|
* found.
|
|
547
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
542
548
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
543
549
|
* It represents the property of the binary tree node that you want to search for.
|
|
544
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
545
550
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
546
551
|
* default value is set to `'key'`.
|
|
547
552
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
548
553
|
* If no matching node is found, it returns null.
|
|
549
554
|
*/
|
|
550
|
-
get(nodeProperty: BinaryTreeNodeKey | N,
|
|
555
|
+
get(nodeProperty: BinaryTreeNodeKey | N, callback: MapCallback<N> = this._defaultCallbackByKey): N | null {
|
|
551
556
|
// TODO may support finding node by value equal
|
|
552
|
-
return this.getNodes(nodeProperty,
|
|
557
|
+
return this.getNodes(nodeProperty, callback, true)[0] ?? null;
|
|
553
558
|
}
|
|
554
559
|
|
|
555
560
|
/**
|
|
556
561
|
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
|
|
557
562
|
* an option to reverse the order of the nodes.
|
|
558
|
-
* @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
|
|
559
563
|
* type that represents a node in your specific implementation.
|
|
564
|
+
* @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
|
|
560
565
|
* @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
|
|
561
566
|
* path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
|
|
562
567
|
* `isReverse` is set to `false` or not provided, the path will
|
|
563
568
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
564
569
|
*/
|
|
565
|
-
getPathToRoot(
|
|
570
|
+
getPathToRoot(beginRoot: N, isReverse = true): N[] {
|
|
566
571
|
// TODO to support get path through passing key
|
|
567
572
|
const result: N[] = [];
|
|
568
|
-
while (
|
|
573
|
+
while (beginRoot.parent) {
|
|
569
574
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
570
575
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
571
|
-
result.push(
|
|
572
|
-
|
|
576
|
+
result.push(beginRoot);
|
|
577
|
+
beginRoot = beginRoot.parent;
|
|
573
578
|
}
|
|
574
|
-
result.push(
|
|
579
|
+
result.push(beginRoot);
|
|
575
580
|
return isReverse ? result.reverse() : result;
|
|
576
581
|
}
|
|
577
582
|
|
|
578
|
-
/**
|
|
579
|
-
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
580
|
-
* no node is specified.
|
|
581
|
-
* generic type representing a node in a binary tree, `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
582
|
-
* node), or `null`.
|
|
583
|
-
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
584
|
-
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
585
|
-
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
586
|
-
* node is found (
|
|
587
|
-
*/
|
|
588
|
-
getLeftMost(): N | null;
|
|
589
|
-
|
|
590
|
-
/**
|
|
591
|
-
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
592
|
-
* no node is specified.
|
|
593
|
-
* @param {N | BinaryTreeNodeKey | null} [node] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
594
|
-
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
595
|
-
* node).
|
|
596
|
-
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
597
|
-
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
598
|
-
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
599
|
-
* node is found (
|
|
600
|
-
*/
|
|
601
|
-
getLeftMost(node: N): N;
|
|
602
|
-
|
|
603
583
|
/**
|
|
604
584
|
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
|
|
605
585
|
* no node is specified.
|
|
@@ -612,7 +592,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
612
592
|
* node is found (
|
|
613
593
|
*/
|
|
614
594
|
getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root): N | null {
|
|
615
|
-
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot
|
|
595
|
+
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
616
596
|
|
|
617
597
|
if (!beginRoot) return beginRoot;
|
|
618
598
|
|
|
@@ -634,25 +614,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
634
614
|
}
|
|
635
615
|
}
|
|
636
616
|
|
|
637
|
-
/**
|
|
638
|
-
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
639
|
-
* recursion optimization.
|
|
640
|
-
* @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the
|
|
641
|
-
* rightmost node starting from the root of the binary tree.
|
|
642
|
-
*/
|
|
643
|
-
getRightMost(): N | null;
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
647
|
-
* recursion optimization.
|
|
648
|
-
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
649
|
-
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
650
|
-
* using the root node of the data structure.
|
|
651
|
-
* @returns The `getRightMost` function returns the rightmost node in a binary tree. It returns the rightmost node
|
|
652
|
-
* starting from that node.
|
|
653
|
-
*/
|
|
654
|
-
getRightMost(beginRoot: N): N;
|
|
655
|
-
|
|
656
617
|
/**
|
|
657
618
|
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using tail
|
|
658
619
|
* recursion optimization.
|
|
@@ -729,109 +690,25 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
729
690
|
}
|
|
730
691
|
|
|
731
692
|
/**
|
|
732
|
-
* The function
|
|
733
|
-
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
734
|
-
* binary tree.
|
|
735
|
-
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
736
|
-
*/
|
|
737
|
-
getSubTreeSize(subTreeRoot: N | null | undefined) {
|
|
738
|
-
// TODO support key passed in
|
|
739
|
-
let size = 0;
|
|
740
|
-
if (!subTreeRoot) return size;
|
|
741
|
-
|
|
742
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
743
|
-
const _traverse = (cur: N) => {
|
|
744
|
-
size++;
|
|
745
|
-
cur.left && _traverse(cur.left);
|
|
746
|
-
cur.right && _traverse(cur.right);
|
|
747
|
-
};
|
|
748
|
-
|
|
749
|
-
_traverse(subTreeRoot);
|
|
750
|
-
return size;
|
|
751
|
-
} else {
|
|
752
|
-
const stack: N[] = [subTreeRoot];
|
|
753
|
-
|
|
754
|
-
while (stack.length > 0) {
|
|
755
|
-
const cur = stack.pop()!;
|
|
756
|
-
size++;
|
|
757
|
-
cur.right && stack.push(cur.right);
|
|
758
|
-
cur.left && stack.push(cur.left);
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
return size;
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
/**
|
|
766
|
-
* The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
|
|
767
|
-
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
768
|
-
* tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
|
|
769
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
|
|
770
|
-
* property of the binary tree node to use for calculating the sum. It can be either 'key' or 'val'. If propertyName is
|
|
771
|
-
* not provided, it defaults to 'key'.
|
|
772
|
-
* @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
|
|
773
|
-
*/
|
|
774
|
-
subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName: BinaryTreeNodePropertyName = 'key'): number {
|
|
775
|
-
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'key');
|
|
776
|
-
|
|
777
|
-
if (!subTreeRoot) return 0;
|
|
778
|
-
|
|
779
|
-
let sum = 0;
|
|
780
|
-
|
|
781
|
-
const _sumByProperty = (cur: N) => {
|
|
782
|
-
let needSum: number;
|
|
783
|
-
switch (propertyName) {
|
|
784
|
-
case 'key':
|
|
785
|
-
needSum = cur.key;
|
|
786
|
-
break;
|
|
787
|
-
case 'val':
|
|
788
|
-
needSum = typeof cur.val === 'number' ? cur.val : 0;
|
|
789
|
-
break;
|
|
790
|
-
default:
|
|
791
|
-
needSum = cur.key;
|
|
792
|
-
break;
|
|
793
|
-
}
|
|
794
|
-
return needSum;
|
|
795
|
-
};
|
|
796
|
-
|
|
797
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
798
|
-
const _traverse = (cur: N): void => {
|
|
799
|
-
sum += _sumByProperty(cur);
|
|
800
|
-
cur.left && _traverse(cur.left);
|
|
801
|
-
cur.right && _traverse(cur.right);
|
|
802
|
-
};
|
|
803
|
-
|
|
804
|
-
_traverse(subTreeRoot);
|
|
805
|
-
} else {
|
|
806
|
-
const stack: N[] = [subTreeRoot];
|
|
807
|
-
|
|
808
|
-
while (stack.length > 0) {
|
|
809
|
-
const cur = stack.pop()!;
|
|
810
|
-
sum += _sumByProperty(cur);
|
|
811
|
-
cur.right && stack.push(cur.right);
|
|
812
|
-
cur.left && stack.push(cur.left);
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
return sum;
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
/**
|
|
820
|
-
* The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
|
|
693
|
+
* The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
|
|
821
694
|
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
822
695
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
823
|
-
* @param
|
|
696
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
824
697
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
825
698
|
* @returns a boolean value.
|
|
826
699
|
*/
|
|
827
|
-
|
|
828
|
-
|
|
700
|
+
subTreeTraverse(
|
|
701
|
+
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
702
|
+
subTreeRoot: N | BinaryTreeNodeKey | null = this.root
|
|
703
|
+
): MapCallbackReturn<N>[] {
|
|
704
|
+
if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot);
|
|
829
705
|
|
|
830
|
-
|
|
706
|
+
const ans: MapCallbackReturn<N>[] = [];
|
|
707
|
+
if (!subTreeRoot) return ans;
|
|
831
708
|
|
|
832
709
|
if (this._loopType === LoopType.RECURSIVE) {
|
|
833
710
|
const _traverse = (cur: N) => {
|
|
834
|
-
callback(cur);
|
|
711
|
+
ans.push(callback(cur));
|
|
835
712
|
cur.left && _traverse(cur.left);
|
|
836
713
|
cur.right && _traverse(cur.right);
|
|
837
714
|
};
|
|
@@ -843,151 +720,64 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
843
720
|
while (stack.length > 0) {
|
|
844
721
|
const cur = stack.pop()!;
|
|
845
722
|
|
|
846
|
-
callback(cur);
|
|
723
|
+
ans.push(callback(cur));
|
|
847
724
|
cur.right && stack.push(cur.right);
|
|
848
725
|
cur.left && stack.push(cur.left);
|
|
849
726
|
}
|
|
850
727
|
}
|
|
851
|
-
return
|
|
728
|
+
return ans;
|
|
852
729
|
}
|
|
853
730
|
|
|
854
|
-
/**
|
|
855
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
|
|
856
|
-
* @returns An array of binary tree node IDs.
|
|
857
|
-
*/
|
|
858
|
-
bfs(): BinaryTreeNodeKey[];
|
|
859
|
-
|
|
860
|
-
/**
|
|
861
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on the specified property name.
|
|
862
|
-
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
863
|
-
* @returns An array of values corresponding to the specified property.
|
|
864
|
-
*/
|
|
865
|
-
bfs(nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
866
|
-
|
|
867
|
-
/**
|
|
868
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating the 'val' property of each node.
|
|
869
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
870
|
-
* @returns An array of 'val' properties from each node.
|
|
871
|
-
*/
|
|
872
|
-
bfs(nodeOrPropertyName: 'val'): N['val'][];
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* Performs a breadth-first search (bfs) on a binary tree, accumulating nodes themselves.
|
|
876
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
877
|
-
* @returns An array of binary tree nodes.
|
|
878
|
-
*/
|
|
879
|
-
bfs(nodeOrPropertyName: 'node'): N[];
|
|
880
|
-
|
|
881
|
-
/**
|
|
882
|
-
* The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
|
|
883
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
|
|
884
|
-
* If a node is provided, the bfs algorithm will be performed starting from that node.
|
|
885
|
-
* If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
|
|
886
|
-
* @returns An instance of the `BinaryTreeNodeProperties` class with generic type `N`.
|
|
887
|
-
*/
|
|
888
|
-
bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties<N> {
|
|
889
|
-
this._clearResults();
|
|
890
|
-
const queue: Array<N | null | undefined> = [this.root];
|
|
891
|
-
|
|
892
|
-
while (queue.length !== 0) {
|
|
893
|
-
// TODO Array.shift is not efficient, consider using Deque
|
|
894
|
-
const cur = queue.shift();
|
|
895
|
-
if (cur) {
|
|
896
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
897
|
-
if (cur?.left !== null) queue.push(cur.left);
|
|
898
|
-
if (cur?.right !== null) queue.push(cur.right);
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
/**
|
|
906
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
907
|
-
* @returns An array of binary tree node IDs.
|
|
908
|
-
*/
|
|
909
|
-
dfs(): BinaryTreeNodeKey[];
|
|
910
|
-
|
|
911
|
-
/**
|
|
912
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
913
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
914
|
-
* @returns An array of values corresponding to the specified property.
|
|
915
|
-
*/
|
|
916
|
-
dfs(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
917
|
-
|
|
918
|
-
/**
|
|
919
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
920
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
921
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
922
|
-
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
923
|
-
* @returns An array of values corresponding to the specified property.
|
|
924
|
-
*/
|
|
925
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[];
|
|
926
|
-
|
|
927
|
-
/**
|
|
928
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
929
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
930
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
931
|
-
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
932
|
-
* @returns An array of 'val' properties from each node.
|
|
933
|
-
*/
|
|
934
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[];
|
|
935
|
-
|
|
936
|
-
/**
|
|
937
|
-
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
938
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
939
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
940
|
-
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
941
|
-
* @returns An array of binary tree nodes.
|
|
942
|
-
*/
|
|
943
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[];
|
|
944
|
-
|
|
945
731
|
/**
|
|
946
732
|
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
947
733
|
* each node based on the specified pattern and property name.
|
|
734
|
+
* @param callback
|
|
735
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
948
736
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
949
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
|
|
950
737
|
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
951
738
|
* @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
|
|
952
739
|
*/
|
|
953
740
|
dfs(
|
|
741
|
+
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
954
742
|
pattern: DFSOrderPattern = 'in',
|
|
955
|
-
|
|
743
|
+
beginRoot: N | null = this.root,
|
|
956
744
|
loopType: LoopType = LoopType.ITERATIVE
|
|
957
|
-
):
|
|
958
|
-
|
|
745
|
+
): MapCallbackReturn<N>[] {
|
|
746
|
+
if (!beginRoot) return [];
|
|
747
|
+
const ans: MapCallbackReturn<N>[] = [];
|
|
959
748
|
if (loopType === LoopType.RECURSIVE) {
|
|
960
749
|
const _traverse = (node: N) => {
|
|
961
750
|
switch (pattern) {
|
|
962
751
|
case 'in':
|
|
963
752
|
if (node.left) _traverse(node.left);
|
|
964
|
-
|
|
753
|
+
ans.push(callback(node));
|
|
965
754
|
if (node.right) _traverse(node.right);
|
|
966
755
|
break;
|
|
967
756
|
case 'pre':
|
|
968
|
-
|
|
757
|
+
ans.push(callback(node));
|
|
758
|
+
|
|
969
759
|
if (node.left) _traverse(node.left);
|
|
970
760
|
if (node.right) _traverse(node.right);
|
|
971
761
|
break;
|
|
972
762
|
case 'post':
|
|
973
763
|
if (node.left) _traverse(node.left);
|
|
974
764
|
if (node.right) _traverse(node.right);
|
|
975
|
-
|
|
765
|
+
ans.push(callback(node));
|
|
766
|
+
|
|
976
767
|
break;
|
|
977
768
|
}
|
|
978
769
|
};
|
|
979
770
|
|
|
980
|
-
|
|
771
|
+
_traverse(beginRoot);
|
|
981
772
|
} else {
|
|
982
|
-
if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
|
|
983
773
|
// 0: visit, 1: print
|
|
984
|
-
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node:
|
|
774
|
+
const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
|
|
985
775
|
|
|
986
776
|
while (stack.length > 0) {
|
|
987
777
|
const cur = stack.pop();
|
|
988
778
|
if (!cur || !cur.node) continue;
|
|
989
779
|
if (cur.opt === 1) {
|
|
990
|
-
|
|
780
|
+
ans.push(callback(cur.node));
|
|
991
781
|
} else {
|
|
992
782
|
switch (pattern) {
|
|
993
783
|
case 'in':
|
|
@@ -1015,83 +805,30 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1015
805
|
}
|
|
1016
806
|
}
|
|
1017
807
|
|
|
1018
|
-
return
|
|
808
|
+
return ans;
|
|
1019
809
|
}
|
|
1020
810
|
|
|
1021
811
|
// --- start additional methods ---
|
|
1022
812
|
|
|
1023
|
-
/**
|
|
1024
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1025
|
-
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
1026
|
-
*/
|
|
1027
|
-
listLevels(): BinaryTreeNodeKey[][];
|
|
1028
|
-
|
|
1029
|
-
/**
|
|
1030
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1031
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
1032
|
-
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
1033
|
-
*/
|
|
1034
|
-
listLevels(node: N | null): BinaryTreeNodeKey[][];
|
|
1035
|
-
|
|
1036
|
-
/**
|
|
1037
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1038
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
1039
|
-
* @param {'key} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1040
|
-
* @returns A 2D array of values corresponding to the specified property.
|
|
1041
|
-
*/
|
|
1042
|
-
listLevels(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[][];
|
|
1043
|
-
|
|
1044
|
-
/**
|
|
1045
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1046
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
1047
|
-
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1048
|
-
* @returns A 2D array of 'val' properties from each node.
|
|
1049
|
-
*/
|
|
1050
|
-
listLevels(node: N | null, nodeOrPropertyName: 'val'): N['val'][][];
|
|
1051
|
-
|
|
1052
|
-
/**
|
|
1053
|
-
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1054
|
-
* @param {N | null} node - The root node of the binary tree or null. If null, the function will use the root node of the current binary tree instance.
|
|
1055
|
-
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1056
|
-
* @returns A 2D array of binary tree nodes.
|
|
1057
|
-
*/
|
|
1058
|
-
listLevels(node: N | null, nodeOrPropertyName: 'node'): N[][];
|
|
1059
|
-
|
|
1060
813
|
/**
|
|
1061
814
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
1062
815
|
* @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
|
|
1063
|
-
* @param
|
|
1064
|
-
* @
|
|
1065
|
-
*/
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
816
|
+
* @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
|
|
817
|
+
* @param withLevel - The `withLevel` parameter is a boolean flag that determines whether to include the level of each node in the result. If `withLevel` is set to `true`, the function will include the level of each node in the result. If `withLevel` is set to `false` or not provided, the function will not include the level of each node in the result.
|
|
818
|
+
*/
|
|
819
|
+
bfs(
|
|
820
|
+
callback: BFSCallback<N> = this._defaultCallbackByKey,
|
|
821
|
+
withLevel: boolean = false,
|
|
822
|
+
node?: N | null
|
|
823
|
+
): BFSCallbackReturn<N>[] {
|
|
824
|
+
if (!node) node = this.root;
|
|
1070
825
|
if (!node) return [];
|
|
1071
826
|
|
|
1072
|
-
const
|
|
1073
|
-
|
|
1074
|
-
const collectByProperty = (node: N, level: number) => {
|
|
1075
|
-
switch (nodeOrPropertyName) {
|
|
1076
|
-
case 'key':
|
|
1077
|
-
levelsNodes[level].push(node.key);
|
|
1078
|
-
break;
|
|
1079
|
-
case 'val':
|
|
1080
|
-
levelsNodes[level].push(node.val);
|
|
1081
|
-
break;
|
|
1082
|
-
case 'node':
|
|
1083
|
-
levelsNodes[level].push(node);
|
|
1084
|
-
break;
|
|
1085
|
-
default:
|
|
1086
|
-
levelsNodes[level].push(node.key);
|
|
1087
|
-
break;
|
|
1088
|
-
}
|
|
1089
|
-
};
|
|
827
|
+
const ans: BFSCallbackReturn<N>[] = [];
|
|
1090
828
|
|
|
1091
829
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
1092
830
|
const _recursive = (node: N, level: number) => {
|
|
1093
|
-
|
|
1094
|
-
collectByProperty(node, level);
|
|
831
|
+
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
1095
832
|
if (node.left) _recursive(node.left, level + 1);
|
|
1096
833
|
if (node.right) _recursive(node.right, level + 1);
|
|
1097
834
|
};
|
|
@@ -1104,14 +841,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1104
841
|
const head = stack.pop()!;
|
|
1105
842
|
const [node, level] = head;
|
|
1106
843
|
|
|
1107
|
-
|
|
1108
|
-
collectByProperty(node, level);
|
|
844
|
+
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
1109
845
|
if (node.right) stack.push([node.right, level + 1]);
|
|
1110
846
|
if (node.left) stack.push([node.left, level + 1]);
|
|
1111
847
|
}
|
|
1112
848
|
}
|
|
1113
|
-
|
|
1114
|
-
return levelsNodes;
|
|
849
|
+
return ans;
|
|
1115
850
|
}
|
|
1116
851
|
|
|
1117
852
|
/**
|
|
@@ -1138,53 +873,18 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1138
873
|
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
1139
874
|
*/
|
|
1140
875
|
|
|
1141
|
-
/**
|
|
1142
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
1143
|
-
* @returns An array of binary tree node IDs.
|
|
1144
|
-
*/
|
|
1145
|
-
morris(): BinaryTreeNodeKey[];
|
|
1146
|
-
|
|
1147
|
-
/**
|
|
1148
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
|
|
1149
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1150
|
-
* @param {'key'} nodeOrPropertyName - The name of the property to accumulate.
|
|
1151
|
-
* @returns An array of values corresponding to the specified property.
|
|
1152
|
-
*/
|
|
1153
|
-
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
1154
|
-
|
|
1155
|
-
/**
|
|
1156
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates properties of each node based on the specified property name.
|
|
1157
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1158
|
-
* @returns An array of values corresponding to the specified property.
|
|
1159
|
-
*/
|
|
1160
|
-
morris(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
1161
|
-
|
|
1162
|
-
/**
|
|
1163
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates the 'val' property of each node.
|
|
1164
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1165
|
-
* @param {'val'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1166
|
-
* @returns An array of 'val' properties from each node.
|
|
1167
|
-
*/
|
|
1168
|
-
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
1169
|
-
|
|
1170
|
-
/**
|
|
1171
|
-
* Performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm and accumulates nodes themselves.
|
|
1172
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1173
|
-
* @param {'node'} nodeOrPropertyName - The property of the BinaryTreeNode object to collect at each level.
|
|
1174
|
-
* @returns An array of binary tree nodes.
|
|
1175
|
-
*/
|
|
1176
|
-
morris(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
1177
|
-
|
|
1178
876
|
/**
|
|
1179
877
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
1180
878
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
1181
|
-
* @param
|
|
879
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
1182
880
|
* @returns An array of BinaryTreeNodeProperties<N> objects.
|
|
1183
881
|
*/
|
|
1184
|
-
morris(
|
|
882
|
+
morris(
|
|
883
|
+
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
884
|
+
pattern: DFSOrderPattern = 'in'
|
|
885
|
+
): MapCallbackReturn<N>[] {
|
|
1185
886
|
if (this.root === null) return [];
|
|
1186
|
-
|
|
1187
|
-
this._clearResults();
|
|
887
|
+
const ans: MapCallbackReturn<N>[] = [];
|
|
1188
888
|
|
|
1189
889
|
let cur: N | null | undefined = this.root;
|
|
1190
890
|
const _reverseEdge = (node: N | null | undefined) => {
|
|
@@ -1202,7 +902,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1202
902
|
const tail: N | null | undefined = _reverseEdge(node);
|
|
1203
903
|
let cur: N | null | undefined = tail;
|
|
1204
904
|
while (cur) {
|
|
1205
|
-
|
|
905
|
+
ans.push(callback(cur));
|
|
1206
906
|
cur = cur.right;
|
|
1207
907
|
}
|
|
1208
908
|
_reverseEdge(tail);
|
|
@@ -1220,7 +920,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1220
920
|
predecessor.right = null;
|
|
1221
921
|
}
|
|
1222
922
|
}
|
|
1223
|
-
|
|
923
|
+
ans.push(callback(cur));
|
|
1224
924
|
cur = cur.right;
|
|
1225
925
|
}
|
|
1226
926
|
break;
|
|
@@ -1230,14 +930,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1230
930
|
const predecessor = this.getPredecessor(cur);
|
|
1231
931
|
if (!predecessor.right) {
|
|
1232
932
|
predecessor.right = cur;
|
|
1233
|
-
|
|
933
|
+
ans.push(callback(cur));
|
|
1234
934
|
cur = cur.left;
|
|
1235
935
|
continue;
|
|
1236
936
|
} else {
|
|
1237
937
|
predecessor.right = null;
|
|
1238
938
|
}
|
|
1239
939
|
} else {
|
|
1240
|
-
|
|
940
|
+
ans.push(callback(cur));
|
|
1241
941
|
}
|
|
1242
942
|
cur = cur.right;
|
|
1243
943
|
}
|
|
@@ -1260,8 +960,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1260
960
|
_printEdge(this.root);
|
|
1261
961
|
break;
|
|
1262
962
|
}
|
|
1263
|
-
|
|
1264
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
963
|
+
return ans;
|
|
1265
964
|
}
|
|
1266
965
|
|
|
1267
966
|
/**
|
|
@@ -1318,108 +1017,5 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1318
1017
|
this._size = v;
|
|
1319
1018
|
}
|
|
1320
1019
|
|
|
1321
|
-
/**
|
|
1322
|
-
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
|
|
1323
|
-
* properties.
|
|
1324
|
-
*/
|
|
1325
|
-
protected _clearResults() {
|
|
1326
|
-
this.visitedKey = [];
|
|
1327
|
-
this.visitedVal = [];
|
|
1328
|
-
this.visitedNode = [];
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
/**
|
|
1332
|
-
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1333
|
-
* a result array.
|
|
1334
|
-
* @param {N} cur - The current node being processed.
|
|
1335
|
-
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
1336
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
|
|
1337
|
-
* type. It represents the property value that we are comparing against in the switch statement.
|
|
1338
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1339
|
-
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
|
|
1340
|
-
* or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
|
|
1341
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1342
|
-
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1343
|
-
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1344
|
-
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
1345
|
-
*/
|
|
1346
|
-
protected _pushByPropertyNameStopOrNot(
|
|
1347
|
-
cur: N,
|
|
1348
|
-
result: (N | null | undefined)[],
|
|
1349
|
-
nodeProperty: BinaryTreeNodeKey | N,
|
|
1350
|
-
propertyName: BinaryTreeNodePropertyName = 'key',
|
|
1351
|
-
onlyOne = false
|
|
1352
|
-
) {
|
|
1353
|
-
switch (propertyName) {
|
|
1354
|
-
case 'key':
|
|
1355
|
-
if (cur.key === nodeProperty) {
|
|
1356
|
-
result.push(cur);
|
|
1357
|
-
return onlyOne;
|
|
1358
|
-
}
|
|
1359
|
-
break;
|
|
1360
|
-
case 'val':
|
|
1361
|
-
if (cur.val === nodeProperty) {
|
|
1362
|
-
result.push(cur);
|
|
1363
|
-
return onlyOne;
|
|
1364
|
-
}
|
|
1365
|
-
break;
|
|
1366
|
-
default:
|
|
1367
|
-
if (cur.key === nodeProperty) {
|
|
1368
|
-
result.push(cur);
|
|
1369
|
-
return onlyOne;
|
|
1370
|
-
}
|
|
1371
|
-
break;
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
/**
|
|
1376
|
-
* The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
|
|
1377
|
-
* @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
|
|
1378
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1379
|
-
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1380
|
-
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1381
|
-
*/
|
|
1382
|
-
protected _accumulatedByPropertyName(node: N, nodeOrPropertyName: NodeOrPropertyName = 'key') {
|
|
1383
|
-
switch (nodeOrPropertyName) {
|
|
1384
|
-
case 'key':
|
|
1385
|
-
this.visitedKey.push(node.key);
|
|
1386
|
-
break;
|
|
1387
|
-
case 'val':
|
|
1388
|
-
this.visitedVal.push(node.val);
|
|
1389
|
-
break;
|
|
1390
|
-
case 'node':
|
|
1391
|
-
this.visitedNode.push(node);
|
|
1392
|
-
break;
|
|
1393
|
-
default:
|
|
1394
|
-
this.visitedKey.push(node.key);
|
|
1395
|
-
break;
|
|
1396
|
-
}
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
/**
|
|
1400
|
-
* The time complexity of Morris traversal is O(n), it may slower than others
|
|
1401
|
-
* The space complexity Morris traversal is O(1) because no using stack
|
|
1402
|
-
*/
|
|
1403
|
-
|
|
1404
|
-
/**
|
|
1405
|
-
* The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
|
|
1406
|
-
* name.
|
|
1407
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1408
|
-
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1409
|
-
* @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties<N>`.
|
|
1410
|
-
*/
|
|
1411
|
-
protected _getResultByPropertyName(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties<N> {
|
|
1412
|
-
switch (nodeOrPropertyName) {
|
|
1413
|
-
case 'key':
|
|
1414
|
-
return this.visitedKey;
|
|
1415
|
-
case 'val':
|
|
1416
|
-
return this.visitedVal;
|
|
1417
|
-
case 'node':
|
|
1418
|
-
return this.visitedNode;
|
|
1419
|
-
default:
|
|
1420
|
-
return this.visitedKey;
|
|
1421
|
-
}
|
|
1422
|
-
}
|
|
1423
|
-
|
|
1424
1020
|
// --- end additional methods ---
|
|
1425
1021
|
}
|