data-structure-typed 1.37.1 → 1.37.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -200
  2. package/dist/data-structures/binary-tree/binary-tree.js +79 -231
  3. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  4. package/dist/data-structures/binary-tree/bst.d.ts +9 -8
  5. package/dist/data-structures/binary-tree/bst.js +37 -25
  6. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  7. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -35
  8. package/dist/data-structures/binary-tree/tree-multiset.js +1 -79
  9. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  10. package/dist/data-structures/heap/heap.d.ts +1 -1
  11. package/dist/data-structures/heap/heap.js +1 -1
  12. package/dist/types/data-structures/binary-tree.d.ts +2 -0
  13. package/dist/types/data-structures/index.d.ts +2 -0
  14. package/lib/data-structures/binary-tree/binary-tree.d.ts +36 -200
  15. package/lib/data-structures/binary-tree/binary-tree.js +79 -231
  16. package/lib/data-structures/binary-tree/bst.d.ts +9 -8
  17. package/lib/data-structures/binary-tree/bst.js +37 -25
  18. package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -35
  19. package/lib/data-structures/binary-tree/tree-multiset.js +1 -79
  20. package/lib/data-structures/heap/heap.d.ts +1 -1
  21. package/lib/data-structures/heap/heap.js +1 -1
  22. package/lib/types/data-structures/binary-tree.d.ts +2 -0
  23. package/lib/types/data-structures/index.d.ts +2 -0
  24. package/package.json +5 -5
  25. package/test/integration/avl-tree.test.ts +19 -16
  26. package/test/integration/bst.test.ts +37 -33
  27. package/umd/bundle.min.js +1 -1
  28. package/umd/bundle.min.js.map +1 -1
@@ -91,9 +91,7 @@ class BinaryTree {
91
91
  this._root = null;
92
92
  this._size = 0;
93
93
  this._loopType = types_1.LoopType.ITERATIVE;
94
- this.visitedKey = [];
95
- this.visitedVal = [];
96
- this.visitedNode = [];
94
+ this._defaultCallbackByKey = node => node.key;
97
95
  if (options !== undefined) {
98
96
  const { loopType = types_1.LoopType.ITERATIVE } = options;
99
97
  this._loopType = loopType;
@@ -146,7 +144,6 @@ class BinaryTree {
146
144
  clear() {
147
145
  this._root = null;
148
146
  this._size = 0;
149
- this._clearResults();
150
147
  }
151
148
  /**
152
149
  * The function checks if the size of an object is equal to zero and returns a boolean value.
@@ -202,7 +199,7 @@ class BinaryTree {
202
199
  else {
203
200
  return;
204
201
  }
205
- const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
202
+ const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined;
206
203
  if (this.root) {
207
204
  if (existNode) {
208
205
  existNode.val = val;
@@ -323,9 +320,9 @@ class BinaryTree {
323
320
  */
324
321
  getDepth(distNode, beginRoot = this.root) {
325
322
  if (typeof distNode === 'number')
326
- distNode = this.get(distNode, 'key');
323
+ distNode = this.get(distNode);
327
324
  if (typeof beginRoot === 'number')
328
- beginRoot = this.get(beginRoot, 'key');
325
+ beginRoot = this.get(beginRoot);
329
326
  let depth = 0;
330
327
  while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
331
328
  if (distNode === beginRoot) {
@@ -345,7 +342,7 @@ class BinaryTree {
345
342
  */
346
343
  getHeight(beginRoot = this.root) {
347
344
  if (typeof beginRoot === 'number')
348
- beginRoot = this.get(beginRoot, 'key');
345
+ beginRoot = this.get(beginRoot);
349
346
  if (!beginRoot)
350
347
  return -1;
351
348
  if (this._loopType === types_1.LoopType.RECURSIVE) {
@@ -441,92 +438,99 @@ class BinaryTree {
441
438
  }
442
439
  /**
443
440
  * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
441
+ * @param callback
444
442
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
445
443
  * generic type `N`. It represents the property of the binary tree node that you want to search for.
446
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
447
444
  * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
448
445
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
449
446
  * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
450
447
  * function will stop traversing the tree and return the first matching node. If `only
448
+ * @param beginRoot
451
449
  * @returns an array of nodes (type N).
452
450
  */
453
- getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
454
- if (!this.root)
451
+ getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
452
+ if (!beginRoot)
455
453
  return [];
456
- const result = [];
454
+ const ans = [];
457
455
  if (this.loopType === types_1.LoopType.RECURSIVE) {
458
456
  const _traverse = (cur) => {
459
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
460
- return;
457
+ if (callback(cur) === nodeProperty) {
458
+ ans.push(cur);
459
+ if (onlyOne)
460
+ return;
461
+ }
461
462
  if (!cur.left && !cur.right)
462
463
  return;
463
464
  cur.left && _traverse(cur.left);
464
465
  cur.right && _traverse(cur.right);
465
466
  };
466
- _traverse(this.root);
467
+ _traverse(beginRoot);
467
468
  }
468
469
  else {
469
- const queue = new queue_1.Queue([this.root]);
470
+ const queue = new queue_1.Queue([beginRoot]);
470
471
  while (queue.size > 0) {
471
472
  const cur = queue.shift();
472
473
  if (cur) {
473
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
474
- return result;
474
+ if (callback(cur) === nodeProperty) {
475
+ ans.push(cur);
476
+ if (onlyOne)
477
+ return ans;
478
+ }
475
479
  cur.left && queue.push(cur.left);
476
480
  cur.right && queue.push(cur.right);
477
481
  }
478
482
  }
479
483
  }
480
- return result;
484
+ return ans;
481
485
  }
482
486
  /**
483
487
  * The function checks if a binary tree node has a specific property.
488
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
484
489
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
485
490
  * It represents the property of the binary tree node that you want to check.
486
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
487
491
  * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
488
492
  * @returns a boolean value.
489
493
  */
490
- has(nodeProperty, propertyName = 'key') {
494
+ has(nodeProperty, callback = this._defaultCallbackByKey) {
491
495
  // TODO may support finding node by value equal
492
- return this.getNodes(nodeProperty, propertyName).length > 0;
496
+ return this.getNodes(nodeProperty, callback, true).length > 0;
493
497
  }
494
498
  /**
495
499
  * The function returns the first node that matches the given property name and value, or null if no matching node is
496
500
  * found.
501
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
497
502
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
498
503
  * It represents the property of the binary tree node that you want to search for.
499
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
500
504
  * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
501
505
  * default value is set to `'key'`.
502
506
  * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
503
507
  * If no matching node is found, it returns null.
504
508
  */
505
- get(nodeProperty, propertyName = 'key') {
509
+ get(nodeProperty, callback = this._defaultCallbackByKey) {
506
510
  var _a;
507
511
  // TODO may support finding node by value equal
508
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
512
+ return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
509
513
  }
510
514
  /**
511
515
  * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
512
516
  * an option to reverse the order of the nodes.
513
- * @param {N} node - The `node` parameter represents a node in a tree structure. It is of type `N`, which could be any
514
517
  * type that represents a node in your specific implementation.
518
+ * @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
515
519
  * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
516
520
  * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
517
521
  * `isReverse` is set to `false` or not provided, the path will
518
522
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
519
523
  */
520
- getPathToRoot(node, isReverse = true) {
524
+ getPathToRoot(beginRoot, isReverse = true) {
521
525
  // TODO to support get path through passing key
522
526
  const result = [];
523
- while (node.parent) {
527
+ while (beginRoot.parent) {
524
528
  // Array.push + Array.reverse is more efficient than Array.unshift
525
529
  // TODO may consider using Deque, so far this is not the performance bottleneck
526
- result.push(node);
527
- node = node.parent;
530
+ result.push(beginRoot);
531
+ beginRoot = beginRoot.parent;
528
532
  }
529
- result.push(node);
533
+ result.push(beginRoot);
530
534
  return isReverse ? result.reverse() : result;
531
535
  }
532
536
  /**
@@ -542,7 +546,7 @@ class BinaryTree {
542
546
  */
543
547
  getLeftMost(beginRoot = this.root) {
544
548
  if (typeof beginRoot === 'number')
545
- beginRoot = this.get(beginRoot, 'key');
549
+ beginRoot = this.get(beginRoot);
546
550
  if (!beginRoot)
547
551
  return beginRoot;
548
552
  if (this._loopType === types_1.LoopType.RECURSIVE) {
@@ -639,52 +643,22 @@ class BinaryTree {
639
643
  return this.isSubtreeBST(this.root);
640
644
  }
641
645
  /**
642
- * The function calculates the size of a subtree by traversing it either recursively or iteratively.
643
- * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
644
- * binary tree.
645
- * @returns the size of the subtree rooted at `subTreeRoot`.
646
- */
647
- getSubTreeSize(subTreeRoot) {
648
- // TODO support key passed in
649
- let size = 0;
650
- if (!subTreeRoot)
651
- return size;
652
- if (this._loopType === types_1.LoopType.RECURSIVE) {
653
- const _traverse = (cur) => {
654
- size++;
655
- cur.left && _traverse(cur.left);
656
- cur.right && _traverse(cur.right);
657
- };
658
- _traverse(subTreeRoot);
659
- return size;
660
- }
661
- else {
662
- const stack = [subTreeRoot];
663
- while (stack.length > 0) {
664
- const cur = stack.pop();
665
- size++;
666
- cur.right && stack.push(cur.right);
667
- cur.left && stack.push(cur.left);
668
- }
669
- return size;
670
- }
671
- }
672
- /**
673
- * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
646
+ * The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
674
647
  * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
675
648
  * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
676
649
  * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
677
650
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
678
651
  * @returns a boolean value.
679
652
  */
680
- subTreeForeach(subTreeRoot, callback) {
653
+ subTreeTraverse(callback = this._defaultCallbackByKey, subTreeRoot = this.root) {
681
654
  if (typeof subTreeRoot === 'number')
682
- subTreeRoot = this.get(subTreeRoot, 'key');
655
+ subTreeRoot = this.get(subTreeRoot);
656
+ const ans = [];
683
657
  if (!subTreeRoot)
684
- return false;
658
+ return ans;
685
659
  if (this._loopType === types_1.LoopType.RECURSIVE) {
686
660
  const _traverse = (cur) => {
687
- callback(cur);
661
+ ans.push(callback(cur));
688
662
  cur.left && _traverse(cur.left);
689
663
  cur.right && _traverse(cur.right);
690
664
  };
@@ -694,57 +668,38 @@ class BinaryTree {
694
668
  const stack = [subTreeRoot];
695
669
  while (stack.length > 0) {
696
670
  const cur = stack.pop();
697
- callback(cur);
671
+ ans.push(callback(cur));
698
672
  cur.right && stack.push(cur.right);
699
673
  cur.left && stack.push(cur.left);
700
674
  }
701
675
  }
702
- return true;
703
- }
704
- /**
705
- * The bfs function performs a breadth-first search on a binary tree, accumulating properties of each node based on a specified property name.
706
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
707
- * If a node is provided, the bfs algorithm will be performed starting from that node.
708
- * If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
709
- * @returns An instance of the `BinaryTreeNodeProperties` class with generic type `N`.
710
- */
711
- bfs(nodeOrPropertyName = 'key') {
712
- this._clearResults();
713
- const queue = new queue_1.Queue([this.root]);
714
- while (queue.size !== 0) {
715
- const cur = queue.shift();
716
- if (cur) {
717
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
718
- if ((cur === null || cur === void 0 ? void 0 : cur.left) !== null)
719
- queue.push(cur.left);
720
- if ((cur === null || cur === void 0 ? void 0 : cur.right) !== null)
721
- queue.push(cur.right);
722
- }
723
- }
724
- return this._getResultByPropertyName(nodeOrPropertyName);
676
+ return ans;
725
677
  }
726
678
  /**
727
679
  * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
728
680
  * each node based on the specified pattern and property name.
681
+ * @param callback
682
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
729
683
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
730
- * @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'`.
731
684
  * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
732
685
  * @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.
733
686
  */
734
- dfs(pattern = 'in', nodeOrPropertyName = 'key', loopType = types_1.LoopType.ITERATIVE) {
735
- this._clearResults();
687
+ dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, loopType = types_1.LoopType.ITERATIVE) {
688
+ if (!beginRoot)
689
+ return [];
690
+ const ans = [];
736
691
  if (loopType === types_1.LoopType.RECURSIVE) {
737
692
  const _traverse = (node) => {
738
693
  switch (pattern) {
739
694
  case 'in':
740
695
  if (node.left)
741
696
  _traverse(node.left);
742
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
697
+ ans.push(callback(node));
743
698
  if (node.right)
744
699
  _traverse(node.right);
745
700
  break;
746
701
  case 'pre':
747
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
702
+ ans.push(callback(node));
748
703
  if (node.left)
749
704
  _traverse(node.left);
750
705
  if (node.right)
@@ -755,23 +710,21 @@ class BinaryTree {
755
710
  _traverse(node.left);
756
711
  if (node.right)
757
712
  _traverse(node.right);
758
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
713
+ ans.push(callback(node));
759
714
  break;
760
715
  }
761
716
  };
762
- this.root && _traverse(this.root);
717
+ _traverse(beginRoot);
763
718
  }
764
719
  else {
765
- if (!this.root)
766
- return this._getResultByPropertyName(nodeOrPropertyName);
767
720
  // 0: visit, 1: print
768
- const stack = [{ opt: 0, node: this.root }];
721
+ const stack = [{ opt: 0, node: beginRoot }];
769
722
  while (stack.length > 0) {
770
723
  const cur = stack.pop();
771
724
  if (!cur || !cur.node)
772
725
  continue;
773
726
  if (cur.opt === 1) {
774
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
727
+ ans.push(callback(cur.node));
775
728
  }
776
729
  else {
777
730
  switch (pattern) {
@@ -799,39 +752,24 @@ class BinaryTree {
799
752
  }
800
753
  }
801
754
  }
802
- return this._getResultByPropertyName(nodeOrPropertyName);
755
+ return ans;
803
756
  }
757
+ // --- start additional methods ---
804
758
  /**
805
759
  * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
806
760
  * @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.
807
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following values: 'key', 'val', or 'node'. If not provided, it defaults to 'key'.
808
- * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
761
+ * @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
762
+ * @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.
809
763
  */
810
- listLevels(node = this.root, nodeOrPropertyName = 'key') {
764
+ bfs(callback = this._defaultCallbackByKey, withLevel = false, node) {
765
+ if (!node)
766
+ node = this.root;
811
767
  if (!node)
812
768
  return [];
813
- const levelsNodes = [];
814
- const collectByProperty = (node, level) => {
815
- switch (nodeOrPropertyName) {
816
- case 'key':
817
- levelsNodes[level].push(node.key);
818
- break;
819
- case 'val':
820
- levelsNodes[level].push(node.val);
821
- break;
822
- case 'node':
823
- levelsNodes[level].push(node);
824
- break;
825
- default:
826
- levelsNodes[level].push(node.key);
827
- break;
828
- }
829
- };
769
+ const ans = [];
830
770
  if (this.loopType === types_1.LoopType.RECURSIVE) {
831
771
  const _recursive = (node, level) => {
832
- if (!levelsNodes[level])
833
- levelsNodes[level] = [];
834
- collectByProperty(node, level);
772
+ callback && ans.push(callback(node, withLevel ? level : undefined));
835
773
  if (node.left)
836
774
  _recursive(node.left, level + 1);
837
775
  if (node.right)
@@ -844,16 +782,14 @@ class BinaryTree {
844
782
  while (stack.length > 0) {
845
783
  const head = stack.pop();
846
784
  const [node, level] = head;
847
- if (!levelsNodes[level])
848
- levelsNodes[level] = [];
849
- collectByProperty(node, level);
785
+ callback && ans.push(callback(node, withLevel ? level : undefined));
850
786
  if (node.right)
851
787
  stack.push([node.right, level + 1]);
852
788
  if (node.left)
853
789
  stack.push([node.left, level + 1]);
854
790
  }
855
791
  }
856
- return levelsNodes;
792
+ return ans;
857
793
  }
858
794
  /**
859
795
  * The function returns the predecessor of a given node in a binary tree.
@@ -874,16 +810,20 @@ class BinaryTree {
874
810
  return node;
875
811
  }
876
812
  }
813
+ /**
814
+ * Time complexity is O(n)
815
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
816
+ */
877
817
  /**
878
818
  * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
879
819
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
880
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
820
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
881
821
  * @returns An array of BinaryTreeNodeProperties<N> objects.
882
822
  */
883
- morris(pattern = 'in', nodeOrPropertyName = 'key') {
823
+ morris(callback = this._defaultCallbackByKey, pattern = 'in') {
884
824
  if (this.root === null)
885
825
  return [];
886
- this._clearResults();
826
+ const ans = [];
887
827
  let cur = this.root;
888
828
  const _reverseEdge = (node) => {
889
829
  let pre = null;
@@ -900,7 +840,7 @@ class BinaryTree {
900
840
  const tail = _reverseEdge(node);
901
841
  let cur = tail;
902
842
  while (cur) {
903
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
843
+ ans.push(callback(cur));
904
844
  cur = cur.right;
905
845
  }
906
846
  _reverseEdge(tail);
@@ -919,7 +859,7 @@ class BinaryTree {
919
859
  predecessor.right = null;
920
860
  }
921
861
  }
922
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
862
+ ans.push(callback(cur));
923
863
  cur = cur.right;
924
864
  }
925
865
  break;
@@ -929,7 +869,7 @@ class BinaryTree {
929
869
  const predecessor = this.getPredecessor(cur);
930
870
  if (!predecessor.right) {
931
871
  predecessor.right = cur;
932
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
872
+ ans.push(callback(cur));
933
873
  cur = cur.left;
934
874
  continue;
935
875
  }
@@ -938,7 +878,7 @@ class BinaryTree {
938
878
  }
939
879
  }
940
880
  else {
941
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
881
+ ans.push(callback(cur));
942
882
  }
943
883
  cur = cur.right;
944
884
  }
@@ -962,7 +902,7 @@ class BinaryTree {
962
902
  _printEdge(this.root);
963
903
  break;
964
904
  }
965
- return this._getResultByPropertyName(nodeOrPropertyName);
905
+ return ans;
966
906
  }
967
907
  /**
968
908
  * The function adds a new node to a binary tree if there is an available position.
@@ -1018,98 +958,6 @@ class BinaryTree {
1018
958
  _setSize(v) {
1019
959
  this._size = v;
1020
960
  }
1021
- /**
1022
- * The function `_clearResults` resets the values of several arrays used for tracking visited nodes and their
1023
- * properties.
1024
- */
1025
- _clearResults() {
1026
- this.visitedKey = [];
1027
- this.visitedVal = [];
1028
- this.visitedNode = [];
1029
- }
1030
- /**
1031
- * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1032
- * a result array.
1033
- * @param {N} cur - The current node being processed.
1034
- * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1035
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeKey` or a `N`
1036
- * type. It represents the property value that we are comparing against in the switch statement.
1037
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1038
- * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'key'`
1039
- * or `'val'`. If it is not provided or is not equal to `'key'` or `'val'`, the
1040
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1041
- * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1042
- * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1043
- * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1044
- */
1045
- _pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName = 'key', onlyOne = false) {
1046
- switch (propertyName) {
1047
- case 'key':
1048
- if (cur.key === nodeProperty) {
1049
- result.push(cur);
1050
- return onlyOne;
1051
- }
1052
- break;
1053
- case 'val':
1054
- if (cur.val === nodeProperty) {
1055
- result.push(cur);
1056
- return onlyOne;
1057
- }
1058
- break;
1059
- default:
1060
- if (cur.key === nodeProperty) {
1061
- result.push(cur);
1062
- return onlyOne;
1063
- }
1064
- break;
1065
- }
1066
- }
1067
- /**
1068
- * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
1069
- * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
1070
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1071
- * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1072
- * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1073
- */
1074
- _accumulatedByPropertyName(node, nodeOrPropertyName = 'key') {
1075
- switch (nodeOrPropertyName) {
1076
- case 'key':
1077
- this.visitedKey.push(node.key);
1078
- break;
1079
- case 'val':
1080
- this.visitedVal.push(node.val);
1081
- break;
1082
- case 'node':
1083
- this.visitedNode.push(node);
1084
- break;
1085
- default:
1086
- this.visitedKey.push(node.key);
1087
- break;
1088
- }
1089
- }
1090
- /**
1091
- * The time complexity of Morris traversal is O(n), it may slower than others
1092
- * The space complexity Morris traversal is O(1) because no using stack
1093
- */
1094
- /**
1095
- * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
1096
- * name.
1097
- * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1098
- * can accept either a `NodeOrPropertyName` type or be undefined.
1099
- * @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties<N>`.
1100
- */
1101
- _getResultByPropertyName(nodeOrPropertyName = 'key') {
1102
- switch (nodeOrPropertyName) {
1103
- case 'key':
1104
- return this.visitedKey;
1105
- case 'val':
1106
- return this.visitedVal;
1107
- case 'node':
1108
- return this.visitedNode;
1109
- default:
1110
- return this.visitedKey;
1111
- }
1112
- }
1113
961
  }
1114
962
  exports.BinaryTree = BinaryTree;
1115
963
  //# sourceMappingURL=binary-tree.js.map