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.
Files changed (48) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -7
  3. package/dist/data-structures/binary-tree/avl-tree.js +7 -9
  4. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +61 -209
  6. package/dist/data-structures/binary-tree/binary-tree.js +133 -269
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +22 -19
  9. package/dist/data-structures/binary-tree/bst.js +68 -54
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -36
  12. package/dist/data-structures/binary-tree/tree-multiset.js +3 -81
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/dist/data-structures/heap/heap.d.ts +1 -1
  15. package/dist/data-structures/heap/heap.js +1 -1
  16. package/dist/types/data-structures/binary-tree.d.ts +4 -2
  17. package/dist/types/data-structures/binary-tree.js +6 -6
  18. package/dist/types/data-structures/binary-tree.js.map +1 -1
  19. package/dist/types/data-structures/index.d.ts +2 -0
  20. package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -7
  21. package/lib/data-structures/binary-tree/avl-tree.js +7 -9
  22. package/lib/data-structures/binary-tree/binary-tree.d.ts +61 -209
  23. package/lib/data-structures/binary-tree/binary-tree.js +134 -270
  24. package/lib/data-structures/binary-tree/bst.d.ts +22 -19
  25. package/lib/data-structures/binary-tree/bst.js +69 -55
  26. package/lib/data-structures/binary-tree/tree-multiset.d.ts +2 -36
  27. package/lib/data-structures/binary-tree/tree-multiset.js +4 -82
  28. package/lib/data-structures/heap/heap.d.ts +1 -1
  29. package/lib/data-structures/heap/heap.js +1 -1
  30. package/lib/types/data-structures/binary-tree.d.ts +4 -2
  31. package/lib/types/data-structures/binary-tree.js +5 -5
  32. package/lib/types/data-structures/index.d.ts +2 -0
  33. package/package.json +6 -6
  34. package/src/data-structures/binary-tree/avl-tree.ts +7 -9
  35. package/src/data-structures/binary-tree/binary-tree.ts +79 -54
  36. package/src/data-structures/binary-tree/bst.ts +37 -32
  37. package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
  38. package/src/types/data-structures/binary-tree.ts +2 -2
  39. package/test/config.ts +1 -0
  40. package/test/integration/avl-tree.test.ts +23 -21
  41. package/test/integration/bst.test.ts +49 -44
  42. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
  43. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
  44. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
  45. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  46. package/test/utils/big-o.ts +2 -1
  47. package/umd/bundle.min.js +1 -1
  48. package/umd/bundle.min.js.map +1 -1
@@ -90,13 +90,11 @@ class BinaryTree {
90
90
  // TODO placeholder node may need redesigned
91
91
  this._root = null;
92
92
  this._size = 0;
93
- this._loopType = types_1.LoopType.ITERATIVE;
94
- this.visitedKey = [];
95
- this.visitedVal = [];
96
- this.visitedNode = [];
93
+ this._loopType = types_1.IterationType.ITERATIVE;
94
+ this._defaultCallbackByKey = node => node.key;
97
95
  if (options !== undefined) {
98
- const { loopType = types_1.LoopType.ITERATIVE } = options;
99
- this._loopType = loopType;
96
+ const { iterationType = types_1.IterationType.ITERATIVE } = options;
97
+ this._loopType = iterationType;
100
98
  }
101
99
  }
102
100
  /**
@@ -116,10 +114,10 @@ class BinaryTree {
116
114
  get size() {
117
115
  return this._size;
118
116
  }
119
- get loopType() {
117
+ get iterationType() {
120
118
  return this._loopType;
121
119
  }
122
- set loopType(v) {
120
+ set iterationType(v) {
123
121
  this._loopType = v;
124
122
  }
125
123
  /**
@@ -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) {
@@ -341,14 +338,15 @@ class BinaryTree {
341
338
  * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
342
339
  * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
343
340
  * node), or `null`.
341
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of
344
342
  * @returns the height of the binary tree.
345
343
  */
346
- getHeight(beginRoot = this.root) {
344
+ getHeight(beginRoot = this.root, iterationType = this.iterationType) {
347
345
  if (typeof beginRoot === 'number')
348
- beginRoot = this.get(beginRoot, 'key');
346
+ beginRoot = this.get(beginRoot);
349
347
  if (!beginRoot)
350
348
  return -1;
351
- if (this._loopType === types_1.LoopType.RECURSIVE) {
349
+ if (iterationType === types_1.IterationType.RECURSIVE) {
352
350
  const _getMaxHeight = (cur) => {
353
351
  if (!cur)
354
352
  return -1;
@@ -383,13 +381,14 @@ class BinaryTree {
383
381
  * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
384
382
  * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
385
383
  * for `beginRoot`, the `this.root` property is used as the default value.
384
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
386
385
  * @returns The function `getMinHeight` returns the minimum height of the binary tree.
387
386
  */
388
- getMinHeight(beginRoot = this.root) {
387
+ getMinHeight(beginRoot = this.root, iterationType = this.iterationType) {
389
388
  var _a, _b, _c;
390
389
  if (!beginRoot)
391
390
  return -1;
392
- if (this._loopType === types_1.LoopType.RECURSIVE) {
391
+ if (iterationType === types_1.IterationType.RECURSIVE) {
393
392
  const _getMinHeight = (cur) => {
394
393
  if (!cur)
395
394
  return 0;
@@ -441,92 +440,104 @@ class BinaryTree {
441
440
  }
442
441
  /**
443
442
  * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
443
+ * @param callback
444
444
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
445
445
  * 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
446
  * specifies the property name to use when searching for nodes. If not provided, it defaults to 'key'.
448
447
  * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
449
448
  * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
450
449
  * function will stop traversing the tree and return the first matching node. If `only
450
+ * @param beginRoot
451
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
451
452
  * @returns an array of nodes (type N).
452
453
  */
453
- getNodes(nodeProperty, propertyName = 'key', onlyOne = false) {
454
- if (!this.root)
454
+ getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
455
+ if (!beginRoot)
455
456
  return [];
456
- const result = [];
457
- if (this.loopType === types_1.LoopType.RECURSIVE) {
457
+ const ans = [];
458
+ if (iterationType === types_1.IterationType.RECURSIVE) {
458
459
  const _traverse = (cur) => {
459
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
460
- return;
460
+ if (callback(cur) === nodeProperty) {
461
+ ans.push(cur);
462
+ if (onlyOne)
463
+ return;
464
+ }
461
465
  if (!cur.left && !cur.right)
462
466
  return;
463
467
  cur.left && _traverse(cur.left);
464
468
  cur.right && _traverse(cur.right);
465
469
  };
466
- _traverse(this.root);
470
+ _traverse(beginRoot);
467
471
  }
468
472
  else {
469
- const queue = new queue_1.Queue([this.root]);
473
+ const queue = new queue_1.Queue([beginRoot]);
470
474
  while (queue.size > 0) {
471
475
  const cur = queue.shift();
472
476
  if (cur) {
473
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
474
- return result;
477
+ if (callback(cur) === nodeProperty) {
478
+ ans.push(cur);
479
+ if (onlyOne)
480
+ return ans;
481
+ }
475
482
  cur.left && queue.push(cur.left);
476
483
  cur.right && queue.push(cur.right);
477
484
  }
478
485
  }
479
486
  }
480
- return result;
487
+ return ans;
481
488
  }
482
489
  /**
483
490
  * The function checks if a binary tree node has a specific property.
491
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
484
492
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
485
493
  * 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
494
  * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'key'.
495
+ * @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.
496
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
488
497
  * @returns a boolean value.
489
498
  */
490
- has(nodeProperty, propertyName = 'key') {
499
+ has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
491
500
  // TODO may support finding node by value equal
492
- return this.getNodes(nodeProperty, propertyName).length > 0;
501
+ return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0;
493
502
  }
494
503
  /**
495
504
  * The function returns the first node that matches the given property name and value, or null if no matching node is
496
505
  * found.
506
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
497
507
  * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or `N`.
498
508
  * 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
509
  * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
501
510
  * default value is set to `'key'`.
511
+ * @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.
512
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
502
513
  * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
503
514
  * If no matching node is found, it returns null.
504
515
  */
505
- get(nodeProperty, propertyName = 'key') {
516
+ get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
506
517
  var _a;
507
518
  // TODO may support finding node by value equal
508
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
519
+ return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
509
520
  }
510
521
  /**
511
522
  * The function `getPathToRoot` returns an array of nodes representing the path from a given node to the root node, with
512
523
  * 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
524
  * type that represents a node in your specific implementation.
525
+ * @param beginRoot - The `beginRoot` parameter is of type `N` and represents the starting node from which you want to
515
526
  * @param {boolean} [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the resulting
516
527
  * path should be reversed or not. If `isReverse` is set to `true`, the path will be reversed before returning it. If
517
528
  * `isReverse` is set to `false` or not provided, the path will
518
529
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
519
530
  */
520
- getPathToRoot(node, isReverse = true) {
531
+ getPathToRoot(beginRoot, isReverse = true) {
521
532
  // TODO to support get path through passing key
522
533
  const result = [];
523
- while (node.parent) {
534
+ while (beginRoot.parent) {
524
535
  // Array.push + Array.reverse is more efficient than Array.unshift
525
536
  // TODO may consider using Deque, so far this is not the performance bottleneck
526
- result.push(node);
527
- node = node.parent;
537
+ result.push(beginRoot);
538
+ beginRoot = beginRoot.parent;
528
539
  }
529
- result.push(node);
540
+ result.push(beginRoot);
530
541
  return isReverse ? result.reverse() : result;
531
542
  }
532
543
  /**
@@ -535,17 +546,18 @@ class BinaryTree {
535
546
  * @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
536
547
  * generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
537
548
  * node), or `null`.
549
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop used to traverse the binary tree.
538
550
  * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
539
551
  * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal
540
552
  * from the root of the binary tree. The function returns the leftmost node found during the traversal. If no leftmost
541
553
  * node is found (
542
554
  */
543
- getLeftMost(beginRoot = this.root) {
555
+ getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
544
556
  if (typeof beginRoot === 'number')
545
- beginRoot = this.get(beginRoot, 'key');
557
+ beginRoot = this.get(beginRoot);
546
558
  if (!beginRoot)
547
559
  return beginRoot;
548
- if (this._loopType === types_1.LoopType.RECURSIVE) {
560
+ if (iterationType === types_1.IterationType.RECURSIVE) {
549
561
  const _traverse = (cur) => {
550
562
  if (!cur.left)
551
563
  return cur;
@@ -569,15 +581,16 @@ class BinaryTree {
569
581
  * @param {N | null} [beginRoot] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
570
582
  * starting node from which we want to find the rightmost node. If no node is provided, the function will default to
571
583
  * using the root node of the data structure.
584
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
572
585
  * @returns The `getRightMost` function returns the rightmost node in a binary tree. If the `node` parameter is provided,
573
586
  * it returns the rightmost node starting from that node. If the `node` parameter is not provided, it returns the
574
587
  * rightmost node starting from the root of the binary tree.
575
588
  */
576
- getRightMost(beginRoot = this.root) {
589
+ getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
577
590
  // TODO support get right most by passing key in
578
591
  if (!beginRoot)
579
592
  return beginRoot;
580
- if (this._loopType === types_1.LoopType.RECURSIVE) {
593
+ if (iterationType === types_1.IterationType.RECURSIVE) {
581
594
  const _traverse = (cur) => {
582
595
  if (!cur.right)
583
596
  return cur;
@@ -597,14 +610,15 @@ class BinaryTree {
597
610
  }
598
611
  /**
599
612
  * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
600
- * @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
613
+ * @param {N | null} beginRoot - The `node` parameter represents the root node of a binary search tree (BST).
614
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
601
615
  * @returns a boolean value.
602
616
  */
603
- isSubtreeBST(subTreeRoot) {
617
+ isSubtreeBST(beginRoot, iterationType = this.iterationType) {
604
618
  // TODO there is a bug
605
- if (!subTreeRoot)
619
+ if (!beginRoot)
606
620
  return true;
607
- if (this._loopType === types_1.LoopType.RECURSIVE) {
621
+ if (iterationType === types_1.IterationType.RECURSIVE) {
608
622
  const dfs = (cur, min, max) => {
609
623
  if (!cur)
610
624
  return true;
@@ -612,11 +626,11 @@ class BinaryTree {
612
626
  return false;
613
627
  return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
614
628
  };
615
- return dfs(subTreeRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
629
+ return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
616
630
  }
617
631
  else {
618
632
  const stack = [];
619
- let prev = Number.MIN_SAFE_INTEGER, curr = subTreeRoot;
633
+ let prev = Number.MIN_SAFE_INTEGER, curr = beginRoot;
620
634
  while (curr || stack.length > 0) {
621
635
  while (curr) {
622
636
  stack.push(curr);
@@ -635,116 +649,70 @@ class BinaryTree {
635
649
  * The function isBST checks if the binary tree is valid binary search tree.
636
650
  * @returns The `isBST()` function is returning a boolean value.
637
651
  */
638
- isBST() {
639
- return this.isSubtreeBST(this.root);
640
- }
641
- /**
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
- }
652
+ isBST(iterationType = this.iterationType) {
653
+ if (this.root === null)
654
+ return true;
655
+ return this.isSubtreeBST(this.root, iterationType);
671
656
  }
672
657
  /**
673
- * The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
674
- * @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
658
+ * The function `subTreeTraverse` adds a delta value to a specified property of each node in a subtree.
659
+ * @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the root node of a binary
675
660
  * 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
661
  * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
677
662
  * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
663
+ * @param iterationType - The `iterationType` parameter is an optional parameter of type `IterationType`. It represents the type of loop
678
664
  * @returns a boolean value.
679
665
  */
680
- subTreeForeach(subTreeRoot, callback) {
681
- if (typeof subTreeRoot === 'number')
682
- subTreeRoot = this.get(subTreeRoot, 'key');
683
- if (!subTreeRoot)
684
- return false;
685
- if (this._loopType === types_1.LoopType.RECURSIVE) {
666
+ subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
667
+ if (typeof beginRoot === 'number')
668
+ beginRoot = this.get(beginRoot);
669
+ const ans = [];
670
+ if (!beginRoot)
671
+ return ans;
672
+ if (iterationType === types_1.IterationType.RECURSIVE) {
686
673
  const _traverse = (cur) => {
687
- callback(cur);
674
+ ans.push(callback(cur));
688
675
  cur.left && _traverse(cur.left);
689
676
  cur.right && _traverse(cur.right);
690
677
  };
691
- _traverse(subTreeRoot);
678
+ _traverse(beginRoot);
692
679
  }
693
680
  else {
694
- const stack = [subTreeRoot];
681
+ const stack = [beginRoot];
695
682
  while (stack.length > 0) {
696
683
  const cur = stack.pop();
697
- callback(cur);
684
+ ans.push(callback(cur));
698
685
  cur.right && stack.push(cur.right);
699
686
  cur.left && stack.push(cur.left);
700
687
  }
701
688
  }
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);
689
+ return ans;
725
690
  }
726
691
  /**
727
692
  * The dfs function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
728
693
  * each node based on the specified pattern and property name.
694
+ * @param callback
695
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
729
696
  * @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
- * @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
697
+ * @param iterationType - The type of loop to use for the depth-first search traversal. The default value is `IterationType.ITERATIVE`.
732
698
  * @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
699
  */
734
- dfs(pattern = 'in', nodeOrPropertyName = 'key', loopType = types_1.LoopType.ITERATIVE) {
735
- this._clearResults();
736
- if (loopType === types_1.LoopType.RECURSIVE) {
700
+ dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
701
+ if (!beginRoot)
702
+ return [];
703
+ const ans = [];
704
+ if (iterationType === types_1.IterationType.RECURSIVE) {
737
705
  const _traverse = (node) => {
738
706
  switch (pattern) {
739
707
  case 'in':
740
708
  if (node.left)
741
709
  _traverse(node.left);
742
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
710
+ ans.push(callback(node));
743
711
  if (node.right)
744
712
  _traverse(node.right);
745
713
  break;
746
714
  case 'pre':
747
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
715
+ ans.push(callback(node));
748
716
  if (node.left)
749
717
  _traverse(node.left);
750
718
  if (node.right)
@@ -755,23 +723,21 @@ class BinaryTree {
755
723
  _traverse(node.left);
756
724
  if (node.right)
757
725
  _traverse(node.right);
758
- this._accumulatedByPropertyName(node, nodeOrPropertyName);
726
+ ans.push(callback(node));
759
727
  break;
760
728
  }
761
729
  };
762
- this.root && _traverse(this.root);
730
+ _traverse(beginRoot);
763
731
  }
764
732
  else {
765
- if (!this.root)
766
- return this._getResultByPropertyName(nodeOrPropertyName);
767
733
  // 0: visit, 1: print
768
- const stack = [{ opt: 0, node: this.root }];
734
+ const stack = [{ opt: 0, node: beginRoot }];
769
735
  while (stack.length > 0) {
770
736
  const cur = stack.pop();
771
737
  if (!cur || !cur.node)
772
738
  continue;
773
739
  if (cur.opt === 1) {
774
- this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
740
+ ans.push(callback(cur.node));
775
741
  }
776
742
  else {
777
743
  switch (pattern) {
@@ -799,61 +765,43 @@ class BinaryTree {
799
765
  }
800
766
  }
801
767
  }
802
- return this._getResultByPropertyName(nodeOrPropertyName);
768
+ return ans;
803
769
  }
770
+ // --- start additional methods ---
804
771
  /**
805
772
  * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
806
- * @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.
773
+ * @param callback - The `callback` parameter is a function that takes a node and a level as parameters and returns a value.
774
+ * @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.
775
+ * @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.
776
+ * @param iterationType
809
777
  */
810
- listLevels(node = this.root, nodeOrPropertyName = 'key') {
811
- if (!node)
778
+ bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
779
+ if (!beginRoot)
812
780
  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
- };
830
- if (this.loopType === types_1.LoopType.RECURSIVE) {
781
+ const ans = [];
782
+ if (iterationType === types_1.IterationType.RECURSIVE) {
831
783
  const _recursive = (node, level) => {
832
- if (!levelsNodes[level])
833
- levelsNodes[level] = [];
834
- collectByProperty(node, level);
784
+ callback && ans.push(callback(node, withLevel ? level : undefined));
835
785
  if (node.left)
836
786
  _recursive(node.left, level + 1);
837
787
  if (node.right)
838
788
  _recursive(node.right, level + 1);
839
789
  };
840
- _recursive(node, 0);
790
+ _recursive(beginRoot, 0);
841
791
  }
842
792
  else {
843
- const stack = [[node, 0]];
793
+ const stack = [[beginRoot, 0]];
844
794
  while (stack.length > 0) {
845
795
  const head = stack.pop();
846
796
  const [node, level] = head;
847
- if (!levelsNodes[level])
848
- levelsNodes[level] = [];
849
- collectByProperty(node, level);
797
+ callback && ans.push(callback(node, withLevel ? level : undefined));
850
798
  if (node.right)
851
799
  stack.push([node.right, level + 1]);
852
800
  if (node.left)
853
801
  stack.push([node.left, level + 1]);
854
802
  }
855
803
  }
856
- return levelsNodes;
804
+ return ans;
857
805
  }
858
806
  /**
859
807
  * The function returns the predecessor of a given node in a binary tree.
@@ -874,17 +822,25 @@ class BinaryTree {
874
822
  return node;
875
823
  }
876
824
  }
825
+ /**
826
+ * Time complexity is O(n)
827
+ * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
828
+ */
877
829
  /**
878
830
  * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
831
+ * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
832
+ * the tree's structure should be restored to its original state to maintain the tree's integrity.
833
+ * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
879
834
  * @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'.
835
+ * @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
836
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It represents the
881
837
  * @returns An array of BinaryTreeNodeProperties<N> objects.
882
838
  */
883
- morris(pattern = 'in', nodeOrPropertyName = 'key') {
884
- if (this.root === null)
839
+ morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
840
+ if (beginRoot === null)
885
841
  return [];
886
- this._clearResults();
887
- let cur = this.root;
842
+ const ans = [];
843
+ let cur = beginRoot;
888
844
  const _reverseEdge = (node) => {
889
845
  let pre = null;
890
846
  let next = null;
@@ -900,7 +856,7 @@ class BinaryTree {
900
856
  const tail = _reverseEdge(node);
901
857
  let cur = tail;
902
858
  while (cur) {
903
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
859
+ ans.push(callback(cur));
904
860
  cur = cur.right;
905
861
  }
906
862
  _reverseEdge(tail);
@@ -919,7 +875,7 @@ class BinaryTree {
919
875
  predecessor.right = null;
920
876
  }
921
877
  }
922
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
878
+ ans.push(callback(cur));
923
879
  cur = cur.right;
924
880
  }
925
881
  break;
@@ -929,7 +885,7 @@ class BinaryTree {
929
885
  const predecessor = this.getPredecessor(cur);
930
886
  if (!predecessor.right) {
931
887
  predecessor.right = cur;
932
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
888
+ ans.push(callback(cur));
933
889
  cur = cur.left;
934
890
  continue;
935
891
  }
@@ -938,7 +894,7 @@ class BinaryTree {
938
894
  }
939
895
  }
940
896
  else {
941
- this._accumulatedByPropertyName(cur, nodeOrPropertyName);
897
+ ans.push(callback(cur));
942
898
  }
943
899
  cur = cur.right;
944
900
  }
@@ -959,10 +915,10 @@ class BinaryTree {
959
915
  }
960
916
  cur = cur.right;
961
917
  }
962
- _printEdge(this.root);
918
+ _printEdge(beginRoot);
963
919
  break;
964
920
  }
965
- return this._getResultByPropertyName(nodeOrPropertyName);
921
+ return ans;
966
922
  }
967
923
  /**
968
924
  * The function adds a new node to a binary tree if there is an available position.
@@ -1018,98 +974,6 @@ class BinaryTree {
1018
974
  _setSize(v) {
1019
975
  this._size = v;
1020
976
  }
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
977
  }
1114
978
  exports.BinaryTree = BinaryTree;
1115
979
  //# sourceMappingURL=binary-tree.js.map