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
@@ -87,9 +87,7 @@ export class BinaryTree {
87
87
  this._root = null;
88
88
  this._size = 0;
89
89
  this._loopType = LoopType.ITERATIVE;
90
- this.visitedKey = [];
91
- this.visitedVal = [];
92
- this.visitedNode = [];
90
+ this._defaultCallbackByKey = node => node.key;
93
91
  if (options !== undefined) {
94
92
  const { loopType = LoopType.ITERATIVE } = options;
95
93
  this._loopType = loopType;
@@ -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, 'key') : undefined;
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, 'key');
319
+ distNode = this.get(distNode);
323
320
  if (typeof beginRoot === 'number')
324
- beginRoot = this.get(beginRoot, 'key');
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) {
@@ -341,7 +338,7 @@ export class BinaryTree {
341
338
  */
342
339
  getHeight(beginRoot = this.root) {
343
340
  if (typeof beginRoot === 'number')
344
- beginRoot = this.get(beginRoot, 'key');
341
+ beginRoot = this.get(beginRoot);
345
342
  if (!beginRoot)
346
343
  return -1;
347
344
  if (this._loopType === LoopType.RECURSIVE) {
@@ -437,92 +434,99 @@ export class BinaryTree {
437
434
  }
438
435
  /**
439
436
  * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
437
+ * @param callback
440
438
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
441
439
  * 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
440
  * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
444
441
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
445
442
  * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
446
443
  * function will stop traversing the tree and return the first matching node. If `only
444
+ * @param beginRoot
447
445
  * @returns an array of nodes (type N).
448
446
  */
449
- getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
450
- if (!this.root)
447
+ getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
448
+ if (!beginRoot)
451
449
  return [];
452
- const result = [];
450
+ const ans = [];
453
451
  if (this.loopType === LoopType.RECURSIVE) {
454
452
  const _traverse = (cur) => {
455
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
456
- return;
453
+ if (callback(cur) === nodeProperty) {
454
+ ans.push(cur);
455
+ if (onlyOne)
456
+ return;
457
+ }
457
458
  if (!cur.left && !cur.right)
458
459
  return;
459
460
  cur.left && _traverse(cur.left);
460
461
  cur.right && _traverse(cur.right);
461
462
  };
462
- _traverse(this.root);
463
+ _traverse(beginRoot);
463
464
  }
464
465
  else {
465
- const queue = new Queue([this.root]);
466
+ const queue = new Queue([beginRoot]);
466
467
  while (queue.size > 0) {
467
468
  const cur = queue.shift();
468
469
  if (cur) {
469
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
470
- return result;
470
+ if (callback(cur) === nodeProperty) {
471
+ ans.push(cur);
472
+ if (onlyOne)
473
+ return ans;
474
+ }
471
475
  cur.left && queue.push(cur.left);
472
476
  cur.right && queue.push(cur.right);
473
477
  }
474
478
  }
475
479
  }
476
- return result;
480
+ return ans;
477
481
  }
478
482
  /**
479
483
  * The function checks if a binary tree node has a specific property.
484
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
480
485
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
481
486
  * 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
487
  * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
484
488
  * @returns a boolean value.
485
489
  */
486
- has(nodeProperty, propertyName = 'key') {
490
+ has(nodeProperty, callback = this._defaultCallbackByKey) {
487
491
  // TODO may support finding node by value equal
488
- return this.getNodes(nodeProperty, propertyName).length > 0;
492
+ return this.getNodes(nodeProperty, callback, true).length > 0;
489
493
  }
490
494
  /**
491
495
  * The function returns the first node that matches the given property name and value, or null if no matching node is
492
496
  * found.
497
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
493
498
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
494
499
  * 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
500
  * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
497
501
  * default value is set to `'key'`.
498
502
  * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
499
503
  * If no matching node is found, it returns null.
500
504
  */
501
- get(nodeProperty, propertyName = 'key') {
505
+ get(nodeProperty, callback = this._defaultCallbackByKey) {
502
506
  var _a;
503
507
  // TODO may support finding node by value equal
504
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
508
+ return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
505
509
  }
506
510
  /**
507
511
  * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
508
512
  * 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
513
  * type that represents a node in your specific implementation.
514
+ * @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
511
515
  * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
512
516
  * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
513
517
  * `isReverse` is set to `false` or not provided, the path will
514
518
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
515
519
  */
516
- getPathToRoot(node, isReverse = true) {
520
+ getPathToRoot(beginRoot, isReverse = true) {
517
521
  // TODO to support get path through passing key
518
522
  const result = [];
519
- while (node.parent) {
523
+ while (beginRoot.parent) {
520
524
  // Array.push + Array.reverse is more efficient than Array.unshift
521
525
  // TODO may consider using Deque, so far this is not the performance bottleneck
522
- result.push(node);
523
- node = node.parent;
526
+ result.push(beginRoot);
527
+ beginRoot = beginRoot.parent;
524
528
  }
525
- result.push(node);
529
+ result.push(beginRoot);
526
530
  return isReverse ? result.reverse() : result;
527
531
  }
528
532
  /**
@@ -538,7 +542,7 @@ export class BinaryTree {
538
542
  */
539
543
  getLeftMost(beginRoot = this.root) {
540
544
  if (typeof beginRoot === 'number')
541
- beginRoot = this.get(beginRoot, 'key');
545
+ beginRoot = this.get(beginRoot);
542
546
  if (!beginRoot)
543
547
  return beginRoot;
544
548
  if (this._loopType === LoopType.RECURSIVE) {
@@ -635,52 +639,22 @@ export class BinaryTree {
635
639
  return this.isSubtreeBST(this.root);
636
640
  }
637
641
  /**
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
- }
667
- }
668
- /**
669
- * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
642
+ * The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
670
643
  * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
671
644
  * 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
645
  * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
673
646
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
674
647
  * @returns a boolean value.
675
648
  */
676
- subTreeForeach(subTreeRoot, callback) {
649
+ subTreeTraverse(callback = this._defaultCallbackByKey, subTreeRoot = this.root) {
677
650
  if (typeof subTreeRoot === 'number')
678
- subTreeRoot = this.get(subTreeRoot, 'key');
651
+ subTreeRoot = this.get(subTreeRoot);
652
+ const ans = [];
679
653
  if (!subTreeRoot)
680
- return false;
654
+ return ans;
681
655
  if (this._loopType === LoopType.RECURSIVE) {
682
656
  const _traverse = (cur) => {
683
- callback(cur);
657
+ ans.push(callback(cur));
684
658
  cur.left && _traverse(cur.left);
685
659
  cur.right && _traverse(cur.right);
686
660
  };
@@ -690,57 +664,38 @@ export class BinaryTree {
690
664
  const stack = [subTreeRoot];
691
665
  while (stack.length > 0) {
692
666
  const cur = stack.pop();
693
- callback(cur);
667
+ ans.push(callback(cur));
694
668
  cur.right && stack.push(cur.right);
695
669
  cur.left && stack.push(cur.left);
696
670
  }
697
671
  }
698
- return true;
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);
672
+ return ans;
721
673
  }
722
674
  /**
723
675
  * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
724
676
  * each node based on the specified pattern and property name.
677
+ * @param callback
678
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
725
679
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
726
- * @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'`.
727
680
  * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
728
681
  * @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
682
  */
730
- dfs(pattern = 'in', nodeOrPropertyName = 'key', loopType = LoopType.ITERATIVE) {
731
- this._clearResults();
683
+ dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, loopType = LoopType.ITERATIVE) {
684
+ if (!beginRoot)
685
+ return [];
686
+ const ans = [];
732
687
  if (loopType === LoopType.RECURSIVE) {
733
688
  const _traverse = (node) => {
734
689
  switch (pattern) {
735
690
  case 'in':
736
691
  if (node.left)
737
692
  _traverse(node.left);
738
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
693
+ ans.push(callback(node));
739
694
  if (node.right)
740
695
  _traverse(node.right);
741
696
  break;
742
697
  case 'pre':
743
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
698
+ ans.push(callback(node));
744
699
  if (node.left)
745
700
  _traverse(node.left);
746
701
  if (node.right)
@@ -751,23 +706,21 @@ export class BinaryTree {
751
706
  _traverse(node.left);
752
707
  if (node.right)
753
708
  _traverse(node.right);
754
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
709
+ ans.push(callback(node));
755
710
  break;
756
711
  }
757
712
  };
758
- this.root && _traverse(this.root);
713
+ _traverse(beginRoot);
759
714
  }
760
715
  else {
761
- if (!this.root)
762
- return this._getResultByPropertyName(nodeOrPropertyName);
763
716
  // 0: visit, 1: print
764
- const stack = [{ opt: 0, node: this.root }];
717
+ const stack = [{ opt: 0, node: beginRoot }];
765
718
  while (stack.length > 0) {
766
719
  const cur = stack.pop();
767
720
  if (!cur || !cur.node)
768
721
  continue;
769
722
  if (cur.opt === 1) {
770
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
723
+ ans.push(callback(cur.node));
771
724
  }
772
725
  else {
773
726
  switch (pattern) {
@@ -795,39 +748,24 @@ export class BinaryTree {
795
748
  }
796
749
  }
797
750
  }
798
- return this._getResultByPropertyName(nodeOrPropertyName);
751
+ return ans;
799
752
  }
753
+ // --- start additional methods ---
800
754
  /**
801
755
  * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
802
756
  * @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.
803
- * @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'.
804
- * @returns A 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
757
+ * @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
758
+ * @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.
805
759
  */
806
- listLevels(node = this.root, nodeOrPropertyName = 'key') {
760
+ bfs(callback = this._defaultCallbackByKey, withLevel = false, node) {
761
+ if (!node)
762
+ node = this.root;
807
763
  if (!node)
808
764
  return [];
809
- const levelsNodes = [];
810
- const collectByProperty = (node, level) => {
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
- };
765
+ const ans = [];
826
766
  if (this.loopType === LoopType.RECURSIVE) {
827
767
  const _recursive = (node, level) => {
828
- if (!levelsNodes[level])
829
- levelsNodes[level] = [];
830
- collectByProperty(node, level);
768
+ callback && ans.push(callback(node, withLevel ? level : undefined));
831
769
  if (node.left)
832
770
  _recursive(node.left, level + 1);
833
771
  if (node.right)
@@ -840,16 +778,14 @@ export class BinaryTree {
840
778
  while (stack.length > 0) {
841
779
  const head = stack.pop();
842
780
  const [node, level] = head;
843
- if (!levelsNodes[level])
844
- levelsNodes[level] = [];
845
- collectByProperty(node, level);
781
+ callback && ans.push(callback(node, withLevel ? level : undefined));
846
782
  if (node.right)
847
783
  stack.push([node.right, level + 1]);
848
784
  if (node.left)
849
785
  stack.push([node.left, level + 1]);
850
786
  }
851
787
  }
852
- return levelsNodes;
788
+ return ans;
853
789
  }
854
790
  /**
855
791
  * The function returns the predecessor of a given node in a binary tree.
@@ -870,16 +806,20 @@ export class BinaryTree {
870
806
  return node;
871
807
  }
872
808
  }
809
+ /**
810
+ * Time complexity is O(n)
811
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
812
+ */
873
813
  /**
874
814
  * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
875
815
  * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
876
- * @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'.
816
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
877
817
  * @returns An array of BinaryTreeNodeProperties<N> objects.
878
818
  */
879
- morris(pattern = 'in', nodeOrPropertyName = 'key') {
819
+ morris(callback = this._defaultCallbackByKey, pattern = 'in') {
880
820
  if (this.root === null)
881
821
  return [];
882
- this._clearResults();
822
+ const ans = [];
883
823
  let cur = this.root;
884
824
  const _reverseEdge = (node) => {
885
825
  let pre = null;
@@ -896,7 +836,7 @@ export class BinaryTree {
896
836
  const tail = _reverseEdge(node);
897
837
  let cur = tail;
898
838
  while (cur) {
899
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
839
+ ans.push(callback(cur));
900
840
  cur = cur.right;
901
841
  }
902
842
  _reverseEdge(tail);
@@ -915,7 +855,7 @@ export class BinaryTree {
915
855
  predecessor.right = null;
916
856
  }
917
857
  }
918
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
858
+ ans.push(callback(cur));
919
859
  cur = cur.right;
920
860
  }
921
861
  break;
@@ -925,7 +865,7 @@ export class BinaryTree {
925
865
  const predecessor = this.getPredecessor(cur);
926
866
  if (!predecessor.right) {
927
867
  predecessor.right = cur;
928
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
868
+ ans.push(callback(cur));
929
869
  cur = cur.left;
930
870
  continue;
931
871
  }
@@ -934,7 +874,7 @@ export class BinaryTree {
934
874
  }
935
875
  }
936
876
  else {
937
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
877
+ ans.push(callback(cur));
938
878
  }
939
879
  cur = cur.right;
940
880
  }
@@ -958,7 +898,7 @@ export class BinaryTree {
958
898
  _printEdge(this.root);
959
899
  break;
960
900
  }
961
- return this._getResultByPropertyName(nodeOrPropertyName);
901
+ return ans;
962
902
  }
963
903
  /**
964
904
  * The function adds a new node to a binary tree if there is an available position.
@@ -1014,96 +954,4 @@ export class BinaryTree {
1014
954
  _setSize(v) {
1015
955
  this._size = v;
1016
956
  }
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
957
  }
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeNodeKey, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, BSTOptions } from '../../types';
8
+ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback, MapCallbackReturn } from '../../types';
9
9
  import { CP } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
@@ -52,11 +52,11 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
52
52
  * The function returns the first node in a binary tree that matches the given property name and value.
53
53
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
54
54
  * generic type `N`. It represents the property of the binary tree node that you want to search for.
55
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
55
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
56
56
  * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
57
57
  * @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
58
58
  */
59
- get(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName): N | null;
59
+ get(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>): N | null;
60
60
  /**
61
61
  * The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
62
62
  * leftmost node if the comparison is greater than, and the key of the rightmost node otherwise.
@@ -69,23 +69,24 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
69
69
  * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
70
70
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
71
71
  * `N` type. It represents the property of the binary tree node that you want to compare with.
72
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
72
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
73
73
  * specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
74
74
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
75
75
  * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
76
76
  * is set to `true`, the function will return an array with only one node (if
77
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to
77
78
  * @returns an array of nodes (type N).
78
79
  */
79
- getNodes(nodeProperty: BinaryTreeNodeKey | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
80
+ getNodes(nodeProperty: BinaryTreeNodeKey | N, callback?: MapCallback<N>, onlyOne?: boolean, beginRoot?: N | null): N[];
80
81
  /**
81
- * The `lesserOrGreaterForeach` function adds a delta value to the specified property of all nodes in a binary tree that
82
+ * The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
82
83
  * have a greater value than a given node.
84
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
83
85
  * @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
84
86
  * represents the node in the binary tree to which the delta value will be added.
85
87
  * @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
86
- * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean
87
88
  */
88
- lesserOrGreaterForeach(node: N | BinaryTreeNodeKey | null, lesserOrGreater: CP | undefined, callback: (node: N) => void): boolean;
89
+ lesserOrGreaterTraverse(callback: MapCallback<N> | undefined, lesserOrGreater: CP | undefined, node: N | BinaryTreeNodeKey | null): MapCallbackReturn<N>;
89
90
  /**
90
91
  * Balancing Adjustment:
91
92
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.