data-structure-typed 1.37.1 → 1.37.3
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 +3 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -7
- package/dist/data-structures/binary-tree/avl-tree.js +7 -9
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +61 -209
- package/dist/data-structures/binary-tree/binary-tree.js +133 -269
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +22 -19
- package/dist/data-structures/binary-tree/bst.js +68 -54
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -36
- package/dist/data-structures/binary-tree/tree-multiset.js +3 -81
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +4 -2
- package/dist/types/data-structures/binary-tree.js +6 -6
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/dist/types/data-structures/index.d.ts +2 -0
- package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -7
- package/lib/data-structures/binary-tree/avl-tree.js +7 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +61 -209
- package/lib/data-structures/binary-tree/binary-tree.js +134 -270
- package/lib/data-structures/binary-tree/bst.d.ts +22 -19
- package/lib/data-structures/binary-tree/bst.js +69 -55
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +2 -36
- package/lib/data-structures/binary-tree/tree-multiset.js +4 -82
- package/lib/data-structures/heap/heap.d.ts +1 -1
- package/lib/data-structures/heap/heap.js +1 -1
- package/lib/types/data-structures/binary-tree.d.ts +4 -2
- package/lib/types/data-structures/binary-tree.js +5 -5
- package/lib/types/data-structures/index.d.ts +2 -0
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -9
- package/src/data-structures/binary-tree/binary-tree.ts +79 -54
- package/src/data-structures/binary-tree/bst.ts +37 -32
- package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +23 -21
- package/test/integration/bst.test.ts +49 -44
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/utils/big-o.ts +2 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { FamilyPosition,
|
|
8
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
9
9
|
import { trampoline } from '../../utils';
|
|
10
10
|
import { Queue } from '../queue';
|
|
11
11
|
export class BinaryTreeNode {
|
|
@@ -86,13 +86,11 @@ export class BinaryTree {
|
|
|
86
86
|
// TODO placeholder node may need redesigned
|
|
87
87
|
this._root = null;
|
|
88
88
|
this._size = 0;
|
|
89
|
-
this._loopType =
|
|
90
|
-
this.
|
|
91
|
-
this.visitedVal = [];
|
|
92
|
-
this.visitedNode = [];
|
|
89
|
+
this._loopType = IterationType.ITERATIVE;
|
|
90
|
+
this._defaultCallbackByKey = node => node.key;
|
|
93
91
|
if (options !== undefined) {
|
|
94
|
-
const {
|
|
95
|
-
this._loopType =
|
|
92
|
+
const { iterationType = IterationType.ITERATIVE } = options;
|
|
93
|
+
this._loopType = iterationType;
|
|
96
94
|
}
|
|
97
95
|
}
|
|
98
96
|
/**
|
|
@@ -112,10 +110,10 @@ export class BinaryTree {
|
|
|
112
110
|
get size() {
|
|
113
111
|
return this._size;
|
|
114
112
|
}
|
|
115
|
-
get
|
|
113
|
+
get iterationType() {
|
|
116
114
|
return this._loopType;
|
|
117
115
|
}
|
|
118
|
-
set
|
|
116
|
+
set iterationType(v) {
|
|
119
117
|
this._loopType = v;
|
|
120
118
|
}
|
|
121
119
|
/**
|
|
@@ -142,7 +140,6 @@ export class BinaryTree {
|
|
|
142
140
|
clear() {
|
|
143
141
|
this._root = null;
|
|
144
142
|
this._size = 0;
|
|
145
|
-
this._clearResults();
|
|
146
143
|
}
|
|
147
144
|
/**
|
|
148
145
|
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
@@ -198,7 +195,7 @@ export class BinaryTree {
|
|
|
198
195
|
else {
|
|
199
196
|
return;
|
|
200
197
|
}
|
|
201
|
-
const existNode = keyOrNode ? this.get(keyOrNode,
|
|
198
|
+
const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined;
|
|
202
199
|
if (this.root) {
|
|
203
200
|
if (existNode) {
|
|
204
201
|
existNode.val = val;
|
|
@@ -319,9 +316,9 @@ export class BinaryTree {
|
|
|
319
316
|
*/
|
|
320
317
|
getDepth(distNode, beginRoot = this.root) {
|
|
321
318
|
if (typeof distNode === 'number')
|
|
322
|
-
distNode = this.get(distNode
|
|
319
|
+
distNode = this.get(distNode);
|
|
323
320
|
if (typeof beginRoot === 'number')
|
|
324
|
-
beginRoot = this.get(beginRoot
|
|
321
|
+
beginRoot = this.get(beginRoot);
|
|
325
322
|
let depth = 0;
|
|
326
323
|
while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
|
|
327
324
|
if (distNode === beginRoot) {
|
|
@@ -337,14 +334,15 @@ export class BinaryTree {
|
|
|
337
334
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
338
335
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
339
336
|
* node), or `null`.
|
|
337
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
|
|
340
338
|
* @returns the height of the binary tree.
|
|
341
339
|
*/
|
|
342
|
-
getHeight(beginRoot = this.root) {
|
|
340
|
+
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
343
341
|
if (typeof beginRoot === 'number')
|
|
344
|
-
beginRoot = this.get(beginRoot
|
|
342
|
+
beginRoot = this.get(beginRoot);
|
|
345
343
|
if (!beginRoot)
|
|
346
344
|
return -1;
|
|
347
|
-
if (
|
|
345
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
348
346
|
const _getMaxHeight = (cur) => {
|
|
349
347
|
if (!cur)
|
|
350
348
|
return -1;
|
|
@@ -379,13 +377,14 @@ export class BinaryTree {
|
|
|
379
377
|
* @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
|
|
380
378
|
* represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
|
|
381
379
|
* for `beginRoot`, the `this.root` property is used as the default value.
|
|
380
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
382
381
|
* @returns The function `getMinHeight` returns the minimum height of the binary tree.
|
|
383
382
|
*/
|
|
384
|
-
getMinHeight(beginRoot = this.root) {
|
|
383
|
+
getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
385
384
|
var _a, _b, _c;
|
|
386
385
|
if (!beginRoot)
|
|
387
386
|
return -1;
|
|
388
|
-
if (
|
|
387
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
389
388
|
const _getMinHeight = (cur) => {
|
|
390
389
|
if (!cur)
|
|
391
390
|
return 0;
|
|
@@ -437,92 +436,104 @@ export class BinaryTree {
|
|
|
437
436
|
}
|
|
438
437
|
/**
|
|
439
438
|
* The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
|
|
439
|
+
* @param callback
|
|
440
440
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
441
441
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
442
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
443
442
|
* specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
|
|
444
443
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
445
444
|
* return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
|
|
446
445
|
* function will stop traversing the tree and return the first matching node. If `only
|
|
446
|
+
* @param beginRoot
|
|
447
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
447
448
|
* @returns an array of nodes (type N).
|
|
448
449
|
*/
|
|
449
|
-
getNodes(nodeProperty,
|
|
450
|
-
if (!
|
|
450
|
+
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
451
|
+
if (!beginRoot)
|
|
451
452
|
return [];
|
|
452
|
-
const
|
|
453
|
-
if (
|
|
453
|
+
const ans = [];
|
|
454
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
454
455
|
const _traverse = (cur) => {
|
|
455
|
-
if (
|
|
456
|
-
|
|
456
|
+
if (callback(cur) === nodeProperty) {
|
|
457
|
+
ans.push(cur);
|
|
458
|
+
if (onlyOne)
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
457
461
|
if (!cur.left && !cur.right)
|
|
458
462
|
return;
|
|
459
463
|
cur.left && _traverse(cur.left);
|
|
460
464
|
cur.right && _traverse(cur.right);
|
|
461
465
|
};
|
|
462
|
-
_traverse(
|
|
466
|
+
_traverse(beginRoot);
|
|
463
467
|
}
|
|
464
468
|
else {
|
|
465
|
-
const queue = new Queue([
|
|
469
|
+
const queue = new Queue([beginRoot]);
|
|
466
470
|
while (queue.size > 0) {
|
|
467
471
|
const cur = queue.shift();
|
|
468
472
|
if (cur) {
|
|
469
|
-
if (
|
|
470
|
-
|
|
473
|
+
if (callback(cur) === nodeProperty) {
|
|
474
|
+
ans.push(cur);
|
|
475
|
+
if (onlyOne)
|
|
476
|
+
return ans;
|
|
477
|
+
}
|
|
471
478
|
cur.left && queue.push(cur.left);
|
|
472
479
|
cur.right && queue.push(cur.right);
|
|
473
480
|
}
|
|
474
481
|
}
|
|
475
482
|
}
|
|
476
|
-
return
|
|
483
|
+
return ans;
|
|
477
484
|
}
|
|
478
485
|
/**
|
|
479
486
|
* The function checks if a binary tree node has a specific property.
|
|
487
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
480
488
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
481
489
|
* It represents the property of the binary tree node that you want to check.
|
|
482
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
483
490
|
* specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
|
|
491
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
|
|
492
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
484
493
|
* @returns a boolean value.
|
|
485
494
|
*/
|
|
486
|
-
has(nodeProperty,
|
|
495
|
+
has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
487
496
|
// TODO may support finding node by value equal
|
|
488
|
-
return this.getNodes(nodeProperty,
|
|
497
|
+
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
|
|
489
498
|
}
|
|
490
499
|
/**
|
|
491
500
|
* The function returns the first node that matches the given property name and value, or null if no matching node is
|
|
492
501
|
* found.
|
|
502
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
493
503
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
|
|
494
504
|
* It represents the property of the binary tree node that you want to search for.
|
|
495
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
496
505
|
* specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
|
|
497
506
|
* default value is set to `'key'`.
|
|
507
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
|
|
508
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
498
509
|
* @returns either the value of the specified property of the node, or the node itself if no property name is provided.
|
|
499
510
|
* If no matching node is found, it returns null.
|
|
500
511
|
*/
|
|
501
|
-
get(nodeProperty,
|
|
512
|
+
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
502
513
|
var _a;
|
|
503
514
|
// TODO may support finding node by value equal
|
|
504
|
-
return (_a = this.getNodes(nodeProperty,
|
|
515
|
+
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
505
516
|
}
|
|
506
517
|
/**
|
|
507
518
|
* The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
|
|
508
519
|
* an option to reverse the order of the nodes.
|
|
509
|
-
* @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
|
|
510
520
|
* type that represents a node in your specific implementation.
|
|
521
|
+
* @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
|
|
511
522
|
* @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
|
|
512
523
|
* path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
|
|
513
524
|
* `isReverse` is set to `false` or not provided, the path will
|
|
514
525
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
515
526
|
*/
|
|
516
|
-
getPathToRoot(
|
|
527
|
+
getPathToRoot(beginRoot, isReverse = true) {
|
|
517
528
|
// TODO to support get path through passing key
|
|
518
529
|
const result = [];
|
|
519
|
-
while (
|
|
530
|
+
while (beginRoot.parent) {
|
|
520
531
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
521
532
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
522
|
-
result.push(
|
|
523
|
-
|
|
533
|
+
result.push(beginRoot);
|
|
534
|
+
beginRoot = beginRoot.parent;
|
|
524
535
|
}
|
|
525
|
-
result.push(
|
|
536
|
+
result.push(beginRoot);
|
|
526
537
|
return isReverse ? result.reverse() : result;
|
|
527
538
|
}
|
|
528
539
|
/**
|
|
@@ -531,17 +542,18 @@ export class BinaryTree {
|
|
|
531
542
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
532
543
|
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
|
|
533
544
|
* node), or `null`.
|
|
545
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
|
|
534
546
|
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
|
|
535
547
|
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
|
|
536
548
|
* from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
|
|
537
549
|
* node is found (
|
|
538
550
|
*/
|
|
539
|
-
getLeftMost(beginRoot = this.root) {
|
|
551
|
+
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
540
552
|
if (typeof beginRoot === 'number')
|
|
541
|
-
beginRoot = this.get(beginRoot
|
|
553
|
+
beginRoot = this.get(beginRoot);
|
|
542
554
|
if (!beginRoot)
|
|
543
555
|
return beginRoot;
|
|
544
|
-
if (
|
|
556
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
545
557
|
const _traverse = (cur) => {
|
|
546
558
|
if (!cur.left)
|
|
547
559
|
return cur;
|
|
@@ -565,15 +577,16 @@ export class BinaryTree {
|
|
|
565
577
|
* @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
566
578
|
* starting node from which we want to find the rightmost node. If no node is provided, the function will default to
|
|
567
579
|
* using the root node of the data structure.
|
|
580
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
568
581
|
* @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
|
|
569
582
|
* it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
|
|
570
583
|
* rightmost node starting from the root of the binary tree.
|
|
571
584
|
*/
|
|
572
|
-
getRightMost(beginRoot = this.root) {
|
|
585
|
+
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
573
586
|
// TODO support get right most by passing key in
|
|
574
587
|
if (!beginRoot)
|
|
575
588
|
return beginRoot;
|
|
576
|
-
if (
|
|
589
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
577
590
|
const _traverse = (cur) => {
|
|
578
591
|
if (!cur.right)
|
|
579
592
|
return cur;
|
|
@@ -593,14 +606,15 @@ export class BinaryTree {
|
|
|
593
606
|
}
|
|
594
607
|
/**
|
|
595
608
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
596
|
-
* @param {N | null}
|
|
609
|
+
* @param {N | null} beginRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
610
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
597
611
|
* @returns a boolean value.
|
|
598
612
|
*/
|
|
599
|
-
isSubtreeBST(
|
|
613
|
+
isSubtreeBST(beginRoot, iterationType = this.iterationType) {
|
|
600
614
|
// TODO there is a bug
|
|
601
|
-
if (!
|
|
615
|
+
if (!beginRoot)
|
|
602
616
|
return true;
|
|
603
|
-
if (
|
|
617
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
604
618
|
const dfs = (cur, min, max) => {
|
|
605
619
|
if (!cur)
|
|
606
620
|
return true;
|
|
@@ -608,11 +622,11 @@ export class BinaryTree {
|
|
|
608
622
|
return false;
|
|
609
623
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
610
624
|
};
|
|
611
|
-
return dfs(
|
|
625
|
+
return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
612
626
|
}
|
|
613
627
|
else {
|
|
614
628
|
const stack = [];
|
|
615
|
-
let prev = Number.MIN_SAFE_INTEGER, curr =
|
|
629
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
|
|
616
630
|
while (curr || stack.length > 0) {
|
|
617
631
|
while (curr) {
|
|
618
632
|
stack.push(curr);
|
|
@@ -631,116 +645,70 @@ export class BinaryTree {
|
|
|
631
645
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
632
646
|
* @returns The `isBST()` function is returning a boolean value.
|
|
633
647
|
*/
|
|
634
|
-
isBST() {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
* The function calculates the size of a subtree by traversing it either recursively or iteratively.
|
|
639
|
-
* @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
|
|
640
|
-
* binary tree.
|
|
641
|
-
* @returns the size of the subtree rooted at `subTreeRoot`.
|
|
642
|
-
*/
|
|
643
|
-
getSubTreeSize(subTreeRoot) {
|
|
644
|
-
// TODO support key passed in
|
|
645
|
-
let size = 0;
|
|
646
|
-
if (!subTreeRoot)
|
|
647
|
-
return size;
|
|
648
|
-
if (this._loopType === LoopType.RECURSIVE) {
|
|
649
|
-
const _traverse = (cur) => {
|
|
650
|
-
size++;
|
|
651
|
-
cur.left && _traverse(cur.left);
|
|
652
|
-
cur.right && _traverse(cur.right);
|
|
653
|
-
};
|
|
654
|
-
_traverse(subTreeRoot);
|
|
655
|
-
return size;
|
|
656
|
-
}
|
|
657
|
-
else {
|
|
658
|
-
const stack = [subTreeRoot];
|
|
659
|
-
while (stack.length > 0) {
|
|
660
|
-
const cur = stack.pop();
|
|
661
|
-
size++;
|
|
662
|
-
cur.right && stack.push(cur.right);
|
|
663
|
-
cur.left && stack.push(cur.left);
|
|
664
|
-
}
|
|
665
|
-
return size;
|
|
666
|
-
}
|
|
648
|
+
isBST(iterationType = this.iterationType) {
|
|
649
|
+
if (this.root === null)
|
|
650
|
+
return true;
|
|
651
|
+
return this.isSubtreeBST(this.root, iterationType);
|
|
667
652
|
}
|
|
668
653
|
/**
|
|
669
|
-
* The function `
|
|
670
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
654
|
+
* The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
|
|
655
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
|
|
671
656
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
672
657
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
673
658
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
659
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
|
|
674
660
|
* @returns a boolean value.
|
|
675
661
|
*/
|
|
676
|
-
|
|
677
|
-
if (typeof
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
662
|
+
subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
|
|
663
|
+
if (typeof beginRoot === 'number')
|
|
664
|
+
beginRoot = this.get(beginRoot);
|
|
665
|
+
const ans = [];
|
|
666
|
+
if (!beginRoot)
|
|
667
|
+
return ans;
|
|
668
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
682
669
|
const _traverse = (cur) => {
|
|
683
|
-
callback(cur);
|
|
670
|
+
ans.push(callback(cur));
|
|
684
671
|
cur.left && _traverse(cur.left);
|
|
685
672
|
cur.right && _traverse(cur.right);
|
|
686
673
|
};
|
|
687
|
-
_traverse(
|
|
674
|
+
_traverse(beginRoot);
|
|
688
675
|
}
|
|
689
676
|
else {
|
|
690
|
-
const stack = [
|
|
677
|
+
const stack = [beginRoot];
|
|
691
678
|
while (stack.length > 0) {
|
|
692
679
|
const cur = stack.pop();
|
|
693
|
-
callback(cur);
|
|
680
|
+
ans.push(callback(cur));
|
|
694
681
|
cur.right && stack.push(cur.right);
|
|
695
682
|
cur.left && stack.push(cur.left);
|
|
696
683
|
}
|
|
697
684
|
}
|
|
698
|
-
return
|
|
699
|
-
}
|
|
700
|
-
/**
|
|
701
|
-
* The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
|
|
702
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
|
|
703
|
-
* If a node is provided, the bfs algorithm will be performed starting from that node.
|
|
704
|
-
* If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
|
|
705
|
-
* @returns An instance of the `BinaryTreeNodeProperties` class with generic type `N`.
|
|
706
|
-
*/
|
|
707
|
-
bfs(nodeOrPropertyName = 'key') {
|
|
708
|
-
this._clearResults();
|
|
709
|
-
const queue = new Queue([this.root]);
|
|
710
|
-
while (queue.size !== 0) {
|
|
711
|
-
const cur = queue.shift();
|
|
712
|
-
if (cur) {
|
|
713
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
714
|
-
if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
|
|
715
|
-
queue.push(cur.left);
|
|
716
|
-
if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
|
|
717
|
-
queue.push(cur.right);
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
685
|
+
return ans;
|
|
721
686
|
}
|
|
722
687
|
/**
|
|
723
688
|
* The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
|
|
724
689
|
* each node based on the specified pattern and property name.
|
|
690
|
+
* @param callback
|
|
691
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
725
692
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
726
|
-
* @param
|
|
727
|
-
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
693
|
+
* @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
|
|
728
694
|
* @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.
|
|
729
695
|
*/
|
|
730
|
-
dfs(pattern = 'in',
|
|
731
|
-
|
|
732
|
-
|
|
696
|
+
dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE) {
|
|
697
|
+
if (!beginRoot)
|
|
698
|
+
return [];
|
|
699
|
+
const ans = [];
|
|
700
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
733
701
|
const _traverse = (node) => {
|
|
734
702
|
switch (pattern) {
|
|
735
703
|
case 'in':
|
|
736
704
|
if (node.left)
|
|
737
705
|
_traverse(node.left);
|
|
738
|
-
|
|
706
|
+
ans.push(callback(node));
|
|
739
707
|
if (node.right)
|
|
740
708
|
_traverse(node.right);
|
|
741
709
|
break;
|
|
742
710
|
case 'pre':
|
|
743
|
-
|
|
711
|
+
ans.push(callback(node));
|
|
744
712
|
if (node.left)
|
|
745
713
|
_traverse(node.left);
|
|
746
714
|
if (node.right)
|
|
@@ -751,23 +719,21 @@ export class BinaryTree {
|
|
|
751
719
|
_traverse(node.left);
|
|
752
720
|
if (node.right)
|
|
753
721
|
_traverse(node.right);
|
|
754
|
-
|
|
722
|
+
ans.push(callback(node));
|
|
755
723
|
break;
|
|
756
724
|
}
|
|
757
725
|
};
|
|
758
|
-
|
|
726
|
+
_traverse(beginRoot);
|
|
759
727
|
}
|
|
760
728
|
else {
|
|
761
|
-
if (!this.root)
|
|
762
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
763
729
|
// 0: visit, 1: print
|
|
764
|
-
const stack = [{ opt: 0, node:
|
|
730
|
+
const stack = [{ opt: 0, node: beginRoot }];
|
|
765
731
|
while (stack.length > 0) {
|
|
766
732
|
const cur = stack.pop();
|
|
767
733
|
if (!cur || !cur.node)
|
|
768
734
|
continue;
|
|
769
735
|
if (cur.opt === 1) {
|
|
770
|
-
|
|
736
|
+
ans.push(callback(cur.node));
|
|
771
737
|
}
|
|
772
738
|
else {
|
|
773
739
|
switch (pattern) {
|
|
@@ -795,61 +761,43 @@ export class BinaryTree {
|
|
|
795
761
|
}
|
|
796
762
|
}
|
|
797
763
|
}
|
|
798
|
-
return
|
|
764
|
+
return ans;
|
|
799
765
|
}
|
|
766
|
+
// --- start additional methods ---
|
|
800
767
|
/**
|
|
801
768
|
* The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
802
|
-
* @param
|
|
803
|
-
* @param
|
|
804
|
-
* @
|
|
769
|
+
* @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
|
|
770
|
+
* @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.
|
|
771
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the root node of a tree or null if the tree is empty.
|
|
772
|
+
* @param iterationType
|
|
805
773
|
*/
|
|
806
|
-
|
|
807
|
-
if (!
|
|
774
|
+
bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
775
|
+
if (!beginRoot)
|
|
808
776
|
return [];
|
|
809
|
-
const
|
|
810
|
-
|
|
811
|
-
switch (nodeOrPropertyName) {
|
|
812
|
-
case 'key':
|
|
813
|
-
levelsNodes[level].push(node.key);
|
|
814
|
-
break;
|
|
815
|
-
case 'val':
|
|
816
|
-
levelsNodes[level].push(node.val);
|
|
817
|
-
break;
|
|
818
|
-
case 'node':
|
|
819
|
-
levelsNodes[level].push(node);
|
|
820
|
-
break;
|
|
821
|
-
default:
|
|
822
|
-
levelsNodes[level].push(node.key);
|
|
823
|
-
break;
|
|
824
|
-
}
|
|
825
|
-
};
|
|
826
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
777
|
+
const ans = [];
|
|
778
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
827
779
|
const _recursive = (node, level) => {
|
|
828
|
-
|
|
829
|
-
levelsNodes[level] = [];
|
|
830
|
-
collectByProperty(node, level);
|
|
780
|
+
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
831
781
|
if (node.left)
|
|
832
782
|
_recursive(node.left, level + 1);
|
|
833
783
|
if (node.right)
|
|
834
784
|
_recursive(node.right, level + 1);
|
|
835
785
|
};
|
|
836
|
-
_recursive(
|
|
786
|
+
_recursive(beginRoot, 0);
|
|
837
787
|
}
|
|
838
788
|
else {
|
|
839
|
-
const stack = [[
|
|
789
|
+
const stack = [[beginRoot, 0]];
|
|
840
790
|
while (stack.length > 0) {
|
|
841
791
|
const head = stack.pop();
|
|
842
792
|
const [node, level] = head;
|
|
843
|
-
|
|
844
|
-
levelsNodes[level] = [];
|
|
845
|
-
collectByProperty(node, level);
|
|
793
|
+
callback && ans.push(callback(node, withLevel ? level : undefined));
|
|
846
794
|
if (node.right)
|
|
847
795
|
stack.push([node.right, level + 1]);
|
|
848
796
|
if (node.left)
|
|
849
797
|
stack.push([node.left, level + 1]);
|
|
850
798
|
}
|
|
851
799
|
}
|
|
852
|
-
return
|
|
800
|
+
return ans;
|
|
853
801
|
}
|
|
854
802
|
/**
|
|
855
803
|
* The function returns the predecessor of a given node in a binary tree.
|
|
@@ -870,17 +818,25 @@ export class BinaryTree {
|
|
|
870
818
|
return node;
|
|
871
819
|
}
|
|
872
820
|
}
|
|
821
|
+
/**
|
|
822
|
+
* Time complexity is O(n)
|
|
823
|
+
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
824
|
+
*/
|
|
873
825
|
/**
|
|
874
826
|
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
827
|
+
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
828
|
+
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
829
|
+
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
875
830
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
876
|
-
* @param
|
|
831
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
832
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
|
|
877
833
|
* @returns An array of BinaryTreeNodeProperties<N> objects.
|
|
878
834
|
*/
|
|
879
|
-
morris(pattern = 'in',
|
|
880
|
-
if (
|
|
835
|
+
morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
|
|
836
|
+
if (beginRoot === null)
|
|
881
837
|
return [];
|
|
882
|
-
|
|
883
|
-
let cur =
|
|
838
|
+
const ans = [];
|
|
839
|
+
let cur = beginRoot;
|
|
884
840
|
const _reverseEdge = (node) => {
|
|
885
841
|
let pre = null;
|
|
886
842
|
let next = null;
|
|
@@ -896,7 +852,7 @@ export class BinaryTree {
|
|
|
896
852
|
const tail = _reverseEdge(node);
|
|
897
853
|
let cur = tail;
|
|
898
854
|
while (cur) {
|
|
899
|
-
|
|
855
|
+
ans.push(callback(cur));
|
|
900
856
|
cur = cur.right;
|
|
901
857
|
}
|
|
902
858
|
_reverseEdge(tail);
|
|
@@ -915,7 +871,7 @@ export class BinaryTree {
|
|
|
915
871
|
predecessor.right = null;
|
|
916
872
|
}
|
|
917
873
|
}
|
|
918
|
-
|
|
874
|
+
ans.push(callback(cur));
|
|
919
875
|
cur = cur.right;
|
|
920
876
|
}
|
|
921
877
|
break;
|
|
@@ -925,7 +881,7 @@ export class BinaryTree {
|
|
|
925
881
|
const predecessor = this.getPredecessor(cur);
|
|
926
882
|
if (!predecessor.right) {
|
|
927
883
|
predecessor.right = cur;
|
|
928
|
-
|
|
884
|
+
ans.push(callback(cur));
|
|
929
885
|
cur = cur.left;
|
|
930
886
|
continue;
|
|
931
887
|
}
|
|
@@ -934,7 +890,7 @@ export class BinaryTree {
|
|
|
934
890
|
}
|
|
935
891
|
}
|
|
936
892
|
else {
|
|
937
|
-
|
|
893
|
+
ans.push(callback(cur));
|
|
938
894
|
}
|
|
939
895
|
cur = cur.right;
|
|
940
896
|
}
|
|
@@ -955,10 +911,10 @@ export class BinaryTree {
|
|
|
955
911
|
}
|
|
956
912
|
cur = cur.right;
|
|
957
913
|
}
|
|
958
|
-
_printEdge(
|
|
914
|
+
_printEdge(beginRoot);
|
|
959
915
|
break;
|
|
960
916
|
}
|
|
961
|
-
return
|
|
917
|
+
return ans;
|
|
962
918
|
}
|
|
963
919
|
/**
|
|
964
920
|
* The function adds a new node to a binary tree if there is an available position.
|
|
@@ -1014,96 +970,4 @@ export class BinaryTree {
|
|
|
1014
970
|
_setSize(v) {
|
|
1015
971
|
this._size = v;
|
|
1016
972
|
}
|
|
1017
|
-
/**
|
|
1018
|
-
* The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
|
|
1019
|
-
* properties.
|
|
1020
|
-
*/
|
|
1021
|
-
_clearResults() {
|
|
1022
|
-
this.visitedKey = [];
|
|
1023
|
-
this.visitedVal = [];
|
|
1024
|
-
this.visitedNode = [];
|
|
1025
|
-
}
|
|
1026
|
-
/**
|
|
1027
|
-
* The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
|
|
1028
|
-
* a result array.
|
|
1029
|
-
* @param {N} cur - The current node being processed.
|
|
1030
|
-
* @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
|
|
1031
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
|
|
1032
|
-
* type. It represents the property value that we are comparing against in the switch statement.
|
|
1033
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
1034
|
-
* specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
|
|
1035
|
-
* or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
|
|
1036
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
1037
|
-
* stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
|
|
1038
|
-
* `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
|
|
1039
|
-
* @returns a boolean value indicating whether only one matching node should be pushed into the result array.
|
|
1040
|
-
*/
|
|
1041
|
-
_pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
|
|
1042
|
-
switch (propertyName) {
|
|
1043
|
-
case 'key':
|
|
1044
|
-
if (cur.key === nodeProperty) {
|
|
1045
|
-
result.push(cur);
|
|
1046
|
-
return onlyOne;
|
|
1047
|
-
}
|
|
1048
|
-
break;
|
|
1049
|
-
case 'val':
|
|
1050
|
-
if (cur.val === nodeProperty) {
|
|
1051
|
-
result.push(cur);
|
|
1052
|
-
return onlyOne;
|
|
1053
|
-
}
|
|
1054
|
-
break;
|
|
1055
|
-
default:
|
|
1056
|
-
if (cur.key === nodeProperty) {
|
|
1057
|
-
result.push(cur);
|
|
1058
|
-
return onlyOne;
|
|
1059
|
-
}
|
|
1060
|
-
break;
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
/**
|
|
1064
|
-
* The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
|
|
1065
|
-
* @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
|
|
1066
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
|
|
1067
|
-
* can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
|
|
1068
|
-
* specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
|
|
1069
|
-
*/
|
|
1070
|
-
_accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
|
|
1071
|
-
switch (nodeOrPropertyName) {
|
|
1072
|
-
case 'key':
|
|
1073
|
-
this.visitedKey.push(node.key);
|
|
1074
|
-
break;
|
|
1075
|
-
case 'val':
|
|
1076
|
-
this.visitedVal.push(node.val);
|
|
1077
|
-
break;
|
|
1078
|
-
case 'node':
|
|
1079
|
-
this.visitedNode.push(node);
|
|
1080
|
-
break;
|
|
1081
|
-
default:
|
|
1082
|
-
this.visitedKey.push(node.key);
|
|
1083
|
-
break;
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
/**
|
|
1087
|
-
* The time complexity of Morris traversal is O(n), it may slower than others
|
|
1088
|
-
* The space complexity Morris traversal is O(1) because no using stack
|
|
1089
|
-
*/
|
|
1090
|
-
/**
|
|
1091
|
-
* The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
|
|
1092
|
-
* name.
|
|
1093
|
-
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
|
|
1094
|
-
* can accept either a `NodeOrPropertyName` type or be undefined.
|
|
1095
|
-
* @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties<N>`.
|
|
1096
|
-
*/
|
|
1097
|
-
_getResultByPropertyName(nodeOrPropertyName = 'key') {
|
|
1098
|
-
switch (nodeOrPropertyName) {
|
|
1099
|
-
case 'key':
|
|
1100
|
-
return this.visitedKey;
|
|
1101
|
-
case 'val':
|
|
1102
|
-
return this.visitedVal;
|
|
1103
|
-
case 'node':
|
|
1104
|
-
return this.visitedNode;
|
|
1105
|
-
default:
|
|
1106
|
-
return this.visitedKey;
|
|
1107
|
-
}
|
|
1108
|
-
}
|
|
1109
973
|
}
|