data-structure-typed 1.51.6 → 1.51.8

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 (92) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +37 -1
  4. package/benchmark/report.json +405 -3
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +2 -10
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +12 -14
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -13
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +46 -78
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +51 -96
  15. package/dist/cjs/data-structures/binary-tree/bst.js +120 -218
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -4
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +4 -2
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  22. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  23. package/dist/cjs/data-structures/heap/heap.d.ts +1 -3
  24. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  25. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  26. package/dist/cjs/types/common.d.ts +1 -1
  27. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
  28. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
  29. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  30. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +6 -5
  31. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  32. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
  33. package/dist/cjs/types/utils/utils.d.ts +10 -1
  34. package/dist/cjs/utils/utils.d.ts +2 -1
  35. package/dist/cjs/utils/utils.js +29 -1
  36. package/dist/cjs/utils/utils.js.map +1 -1
  37. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
  38. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +2 -10
  39. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -3
  40. package/dist/mjs/data-structures/binary-tree/avl-tree.js +12 -14
  41. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -13
  42. package/dist/mjs/data-structures/binary-tree/binary-tree.js +45 -77
  43. package/dist/mjs/data-structures/binary-tree/bst.d.ts +51 -96
  44. package/dist/mjs/data-structures/binary-tree/bst.js +122 -221
  45. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -4
  46. package/dist/mjs/data-structures/binary-tree/rb-tree.js +4 -2
  47. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
  48. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  49. package/dist/mjs/data-structures/heap/heap.d.ts +1 -3
  50. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  51. package/dist/mjs/types/common.d.ts +1 -1
  52. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
  53. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
  54. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  55. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +6 -5
  56. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  57. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
  58. package/dist/mjs/types/utils/utils.d.ts +10 -1
  59. package/dist/mjs/utils/utils.d.ts +2 -1
  60. package/dist/mjs/utils/utils.js +27 -0
  61. package/dist/umd/data-structure-typed.js +206 -301
  62. package/dist/umd/data-structure-typed.min.js +4 -4
  63. package/dist/umd/data-structure-typed.min.js.map +1 -1
  64. package/package.json +6 -6
  65. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -12
  66. package/src/data-structures/binary-tree/avl-tree.ts +15 -15
  67. package/src/data-structures/binary-tree/binary-tree.ts +56 -76
  68. package/src/data-structures/binary-tree/bst.ts +132 -224
  69. package/src/data-structures/binary-tree/rb-tree.ts +9 -6
  70. package/src/data-structures/binary-tree/tree-multi-map.ts +5 -3
  71. package/src/data-structures/heap/heap.ts +1 -1
  72. package/src/interfaces/binary-tree.ts +4 -3
  73. package/src/types/common.ts +1 -1
  74. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -2
  75. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -2
  76. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -5
  77. package/src/types/data-structures/binary-tree/bst.ts +6 -5
  78. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
  79. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -2
  80. package/src/types/utils/utils.ts +14 -1
  81. package/src/utils/utils.ts +20 -1
  82. package/test/integration/all-in-one.test.ts +2 -2
  83. package/test/integration/index.html +70 -0
  84. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +7 -7
  85. package/test/performance/reportor.ts +2 -3
  86. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +7 -7
  87. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  88. package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
  89. package/test/unit/data-structures/binary-tree/overall.test.ts +24 -20
  90. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +6 -6
  91. package/test/unit/utils/utils.test.ts +5 -0
  92. package/test/utils/console.ts +1 -1
@@ -82,7 +82,7 @@ export class AVLTree extends BST {
82
82
  createTree(options) {
83
83
  return new AVLTree([], {
84
84
  iterationType: this.iterationType,
85
- variant: this.variant,
85
+ comparator: this.comparator,
86
86
  ...options
87
87
  });
88
88
  }
@@ -139,8 +139,6 @@ export class AVLTree extends BST {
139
139
  * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
140
140
  */
141
141
  delete(identifier, callback = this._DEFAULT_CALLBACK) {
142
- if (identifier instanceof AVLTreeNode)
143
- callback = (node => node);
144
142
  const deletedResults = super.delete(identifier, callback);
145
143
  for (const { needBalanced } of deletedResults) {
146
144
  if (needBalanced) {
@@ -160,21 +158,21 @@ export class AVLTree extends BST {
160
158
  * if either `srcNode` or `destNode` is undefined.
161
159
  */
162
160
  _swapProperties(srcNode, destNode) {
163
- srcNode = this.ensureNode(srcNode);
164
- destNode = this.ensureNode(destNode);
165
- if (srcNode && destNode) {
166
- const { key, value, height } = destNode;
161
+ const srcNodeEnsured = this.ensureNode(srcNode);
162
+ const destNodeEnsured = this.ensureNode(destNode);
163
+ if (srcNodeEnsured && destNodeEnsured) {
164
+ const { key, value, height } = destNodeEnsured;
167
165
  const tempNode = this.createNode(key, value);
168
166
  if (tempNode) {
169
167
  tempNode.height = height;
170
- destNode.key = srcNode.key;
171
- destNode.value = srcNode.value;
172
- destNode.height = srcNode.height;
173
- srcNode.key = tempNode.key;
174
- srcNode.value = tempNode.value;
175
- srcNode.height = tempNode.height;
168
+ destNodeEnsured.key = srcNodeEnsured.key;
169
+ destNodeEnsured.value = srcNodeEnsured.value;
170
+ destNodeEnsured.height = srcNodeEnsured.height;
171
+ srcNodeEnsured.key = tempNode.key;
172
+ srcNodeEnsured.value = tempNode.value;
173
+ srcNodeEnsured.height = tempNode.height;
176
174
  }
177
- return destNode;
175
+ return destNodeEnsured;
178
176
  }
179
177
  return undefined;
180
178
  }
@@ -5,8 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNCallback, BTNEntry, DFSOrderPattern, EntryCallback, KeyOrNodeOrEntry, NodeDisplayLayout } from '../../types';
9
- import { FamilyPosition, IterationType } from '../../types';
8
+ import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNCallback, BTNEntry, Comparable, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, KeyOrNodeOrEntry, NodeDisplayLayout } from '../../types';
10
9
  import { IBinaryTree } from '../../interfaces';
11
10
  import { IterableEntryBase } from '../base';
12
11
  /**
@@ -14,7 +13,7 @@ import { IterableEntryBase } from '../base';
14
13
  * @template V - The type of data stored in the node.
15
14
  * @template NODE - The type of the family relationship in the binary tree.
16
15
  */
17
- export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> {
16
+ export declare class BinaryTreeNode<K extends Comparable, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> {
18
17
  key: K;
19
18
  value?: V;
20
19
  parent?: NODE;
@@ -66,7 +65,7 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
66
65
  * 4. Subtrees: Each child of a node forms the root of a subtree.
67
66
  * 5. Leaf Nodes: Nodes without children are leaves.
68
67
  */
69
- export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, NODE, TREE> {
68
+ export declare class BinaryTree<K extends Comparable, V = any, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, NODE, TREE> {
70
69
  iterationType: IterationType;
71
70
  /**
72
71
  * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
@@ -77,13 +76,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
77
76
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
78
77
  * required.
79
78
  */
80
- constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: BinaryTreeOptions<K>);
81
- protected _extractor: (key: K) => number;
82
- /**
83
- * The function returns the value of the `_extractor` property.
84
- * @returns The `_extractor` property is being returned.
85
- */
86
- get extractor(): (key: K) => number;
79
+ constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: BinaryTreeOptions);
87
80
  protected _root?: NODE | null;
88
81
  /**
89
82
  * The function returns the root node, which can be of type NODE, null, or undefined.
@@ -117,7 +110,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
117
110
  * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
118
111
  * @returns a new instance of a binary tree.
119
112
  */
120
- createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
113
+ createTree(options?: Partial<BinaryTreeOptions>): TREE;
121
114
  /**
122
115
  * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
123
116
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
@@ -254,7 +247,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
254
247
  * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
255
248
  * found in the binary tree. If no node is found, it returns `undefined`.
256
249
  */
257
- getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
250
+ getNodeByKey(key: K, iterationType?: IterationType): NODE | null | undefined;
258
251
  get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
259
252
  get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
260
253
  get<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
@@ -606,4 +599,5 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
606
599
  * type `NODE` or `null`.
607
600
  */
608
601
  protected _setRoot(v: NODE | null | undefined): void;
602
+ protected _ensureCallback<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): C;
609
603
  }
@@ -108,24 +108,14 @@ export class BinaryTree extends IterableEntryBase {
108
108
  constructor(keysOrNodesOrEntries = [], options) {
109
109
  super();
110
110
  if (options) {
111
- const { iterationType, extractor } = options;
111
+ const { iterationType } = options;
112
112
  if (iterationType)
113
113
  this.iterationType = iterationType;
114
- if (extractor)
115
- this._extractor = extractor;
116
114
  }
117
115
  this._size = 0;
118
116
  if (keysOrNodesOrEntries)
119
117
  this.addMany(keysOrNodesOrEntries);
120
118
  }
121
- _extractor = (key) => (typeof key === 'number' ? key : Number(key));
122
- /**
123
- * The function returns the value of the `_extractor` property.
124
- * @returns The `_extractor` property is being returned.
125
- */
126
- get extractor() {
127
- return this._extractor;
128
- }
129
119
  _root;
130
120
  /**
131
121
  * The function returns the root node, which can be of type NODE, null, or undefined.
@@ -227,23 +217,24 @@ export class BinaryTree extends IterableEntryBase {
227
217
  * itself if it is not a valid node key.
228
218
  */
229
219
  ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
220
+ if (keyOrNodeOrEntry === this.NIL)
221
+ return;
230
222
  if (this.isRealNode(keyOrNodeOrEntry)) {
231
223
  return keyOrNodeOrEntry;
232
224
  }
233
- else if (this.isEntry(keyOrNodeOrEntry)) {
234
- if (keyOrNodeOrEntry[0] === null)
235
- return null;
236
- if (keyOrNodeOrEntry[0] === undefined)
237
- return;
238
- return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
239
- }
240
- else {
241
- if (keyOrNodeOrEntry === null)
225
+ if (this.isEntry(keyOrNodeOrEntry)) {
226
+ const key = keyOrNodeOrEntry[0];
227
+ if (key === null)
242
228
  return null;
243
- if (keyOrNodeOrEntry === undefined)
229
+ if (key === undefined)
244
230
  return;
245
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
231
+ return this.getNodeByKey(key, iterationType);
246
232
  }
233
+ if (keyOrNodeOrEntry === null)
234
+ return null;
235
+ if (keyOrNodeOrEntry === undefined)
236
+ return;
237
+ return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
247
238
  }
248
239
  /**
249
240
  * The function checks if a given node is a real node or null.
@@ -428,8 +419,7 @@ export class BinaryTree extends IterableEntryBase {
428
419
  const deletedResult = [];
429
420
  if (!this.root)
430
421
  return deletedResult;
431
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
432
- callback = (node => node);
422
+ callback = this._ensureCallback(identifier, callback);
433
423
  const curr = this.getNode(identifier, callback);
434
424
  if (!curr)
435
425
  return deletedResult;
@@ -501,11 +491,10 @@ export class BinaryTree extends IterableEntryBase {
501
491
  * @returns an array of nodes of type `NODE`.
502
492
  */
503
493
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
504
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
505
- callback = (node => node);
506
494
  beginRoot = this.ensureNode(beginRoot);
507
495
  if (!beginRoot)
508
496
  return [];
497
+ callback = this._ensureCallback(identifier, callback);
509
498
  const ans = [];
510
499
  if (iterationType === 'RECURSIVE') {
511
500
  const dfs = (cur) => {
@@ -585,33 +574,7 @@ export class BinaryTree extends IterableEntryBase {
585
574
  * found in the binary tree. If no node is found, it returns `undefined`.
586
575
  */
587
576
  getNodeByKey(key, iterationType = 'ITERATIVE') {
588
- if (!this.root)
589
- return undefined;
590
- if (iterationType === 'RECURSIVE') {
591
- const dfs = (cur) => {
592
- if (cur.key === key)
593
- return cur;
594
- if (!cur.left && !cur.right)
595
- return;
596
- if (cur.left)
597
- return dfs(cur.left);
598
- if (cur.right)
599
- return dfs(cur.right);
600
- };
601
- return dfs(this.root);
602
- }
603
- else {
604
- const stack = [this.root];
605
- while (stack.length > 0) {
606
- const cur = stack.pop();
607
- if (cur) {
608
- if (cur.key === key)
609
- return cur;
610
- cur.left && stack.push(cur.left);
611
- cur.right && stack.push(cur.right);
612
- }
613
- }
614
- }
577
+ return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
615
578
  }
616
579
  /**
617
580
  * Time Complexity: O(n)
@@ -640,7 +603,7 @@ export class BinaryTree extends IterableEntryBase {
640
603
  * found, `undefined` is returned.
641
604
  */
642
605
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
643
- return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
606
+ return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
644
607
  }
645
608
  /**
646
609
  * Time Complexity: O(n)
@@ -668,8 +631,7 @@ export class BinaryTree extends IterableEntryBase {
668
631
  * @returns a boolean value.
669
632
  */
670
633
  has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
671
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
672
- callback = (node => node);
634
+ callback = this._ensureCallback(identifier, callback);
673
635
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
674
636
  }
675
637
  /**
@@ -743,7 +705,7 @@ export class BinaryTree extends IterableEntryBase {
743
705
  const dfs = (cur, min, max) => {
744
706
  if (!this.isRealNode(cur))
745
707
  return true;
746
- const numKey = this.extractor(cur.key);
708
+ const numKey = Number(cur.key);
747
709
  if (numKey <= min || numKey >= max)
748
710
  return false;
749
711
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
@@ -764,7 +726,7 @@ export class BinaryTree extends IterableEntryBase {
764
726
  curr = curr.left;
765
727
  }
766
728
  curr = stack.pop();
767
- const numKey = this.extractor(curr.key);
729
+ const numKey = Number(curr.key);
768
730
  if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
769
731
  return false;
770
732
  prev = numKey;
@@ -794,15 +756,15 @@ export class BinaryTree extends IterableEntryBase {
794
756
  * @returns the depth of the `dist` relative to the `beginRoot`.
795
757
  */
796
758
  getDepth(dist, beginRoot = this.root) {
797
- dist = this.ensureNode(dist);
798
- beginRoot = this.ensureNode(beginRoot);
759
+ let distEnsured = this.ensureNode(dist);
760
+ const beginRootEnsured = this.ensureNode(beginRoot);
799
761
  let depth = 0;
800
- while (dist?.parent) {
801
- if (dist === beginRoot) {
762
+ while (distEnsured?.parent) {
763
+ if (distEnsured === beginRootEnsured) {
802
764
  return depth;
803
765
  }
804
766
  depth++;
805
- dist = dist.parent;
767
+ distEnsured = distEnsured.parent;
806
768
  }
807
769
  return depth;
808
770
  }
@@ -935,16 +897,16 @@ export class BinaryTree extends IterableEntryBase {
935
897
  getPathToRoot(beginNode, isReverse = true) {
936
898
  // TODO to support get path through passing key
937
899
  const result = [];
938
- beginNode = this.ensureNode(beginNode);
939
- if (!beginNode)
900
+ let beginNodeEnsured = this.ensureNode(beginNode);
901
+ if (!beginNodeEnsured)
940
902
  return result;
941
- while (beginNode.parent) {
903
+ while (beginNodeEnsured.parent) {
942
904
  // Array.push + Array.reverse is more efficient than Array.unshift
943
905
  // TODO may consider using Deque, so far this is not the performance bottleneck
944
- result.push(beginNode);
945
- beginNode = beginNode.parent;
906
+ result.push(beginNodeEnsured);
907
+ beginNodeEnsured = beginNodeEnsured.parent;
946
908
  }
947
- result.push(beginNode);
909
+ result.push(beginNodeEnsured);
948
910
  return isReverse ? result.reverse() : result;
949
911
  }
950
912
  /**
@@ -1590,10 +1552,10 @@ export class BinaryTree extends IterableEntryBase {
1590
1552
  console.log(`U for undefined
1591
1553
  `);
1592
1554
  if (opts.isShowNull)
1593
- console.log(`NODE for null
1555
+ console.log(`N for null
1594
1556
  `);
1595
1557
  if (opts.isShowRedBlackNIL)
1596
- console.log(`S for Sentinel Node
1558
+ console.log(`S for Sentinel Node(NIL)
1597
1559
  `);
1598
1560
  const display = (root) => {
1599
1561
  const [lines, , ,] = this._displayAux(root, opts);
@@ -1619,23 +1581,23 @@ export class BinaryTree extends IterableEntryBase {
1619
1581
  const stack = [];
1620
1582
  let current = node;
1621
1583
  while (current || stack.length > 0) {
1622
- while (current && !isNaN(this.extractor(current.key))) {
1584
+ while (this.isRealNode(current)) {
1623
1585
  stack.push(current);
1624
1586
  current = current.left;
1625
1587
  }
1626
1588
  current = stack.pop();
1627
- if (current && !isNaN(this.extractor(current.key))) {
1589
+ if (this.isRealNode(current)) {
1628
1590
  yield [current.key, current.value];
1629
1591
  current = current.right;
1630
1592
  }
1631
1593
  }
1632
1594
  }
1633
1595
  else {
1634
- if (node.left && !isNaN(this.extractor(node.key))) {
1596
+ if (node.left && this.isRealNode(node)) {
1635
1597
  yield* this[Symbol.iterator](node.left);
1636
1598
  }
1637
1599
  yield [node.key, node.value];
1638
- if (node.right && !isNaN(this.extractor(node.key))) {
1600
+ if (node.right && this.isRealNode(node)) {
1639
1601
  yield* this[Symbol.iterator](node.right);
1640
1602
  }
1641
1603
  }
@@ -1664,12 +1626,12 @@ export class BinaryTree extends IterableEntryBase {
1664
1626
  else if (node === undefined && !isShowUndefined) {
1665
1627
  return emptyDisplayLayout;
1666
1628
  }
1667
- else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
1629
+ else if (this.isNIL(node) && !isShowRedBlackNIL) {
1668
1630
  return emptyDisplayLayout;
1669
1631
  }
1670
1632
  else if (node !== null && node !== undefined) {
1671
1633
  // Display logic of normal nodes
1672
- const key = node.key, line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(), width = line.length;
1634
+ const key = node.key, line = this.isNIL(node) ? 'S' : key.toString(), width = line.length;
1673
1635
  return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
1674
1636
  }
1675
1637
  else {
@@ -1766,4 +1728,10 @@ export class BinaryTree extends IterableEntryBase {
1766
1728
  }
1767
1729
  this._root = v;
1768
1730
  }
1731
+ _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
1732
+ if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
1733
+ callback = (node => node);
1734
+ }
1735
+ return callback;
1736
+ }
1769
1737
  }
@@ -5,11 +5,10 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
9
- import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, Comparable, Comparator, CP, DFSOrderPattern, IterationType, KeyOrNodeOrEntry } from '../../types';
10
9
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
10
  import { IBinaryTree } from '../../interfaces';
12
- export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
11
+ export declare class BSTNode<K extends Comparable, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
13
12
  parent?: NODE;
14
13
  constructor(key: K, value?: V);
15
14
  protected _left?: NODE;
@@ -47,12 +46,13 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
47
46
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
48
47
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
49
48
  */
50
- export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>> extends BinaryTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
49
+ export declare class BST<K extends Comparable, V = any, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>> extends BinaryTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
51
50
  /**
52
- * This is the constructor function for a TypeScript class that initializes a binary search tree with
53
- * optional keys or nodes or entries and options.
54
- * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
55
- * to initialize the binary search tree with the provided keys, nodes, or entries.
51
+ * This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
52
+ * the tree with keys, nodes, or entries and optional options.
53
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
54
+ * contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
55
+ * keys, nodes, or entries.
56
56
  * @param [options] - The `options` parameter is an optional object that can contain additional
57
57
  * configuration options for the binary search tree. It can have the following properties:
58
58
  */
@@ -63,12 +63,12 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
63
63
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
64
64
  */
65
65
  get root(): NODE | undefined;
66
- protected _variant: BSTVariant;
66
+ protected _comparator: Comparator<K>;
67
67
  /**
68
- * The function returns the value of the _variant property.
69
- * @returns The value of the `_variant` property.
68
+ * The function returns the value of the _comparator property.
69
+ * @returns The `_comparator` property is being returned.
70
70
  */
71
- get variant(): BSTVariant;
71
+ get comparator(): Comparator<K>;
72
72
  /**
73
73
  * The function creates a new BSTNode with the given key and value and returns it.
74
74
  * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
@@ -127,13 +127,11 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
127
127
  * Time Complexity: O(log n)
128
128
  * Space Complexity: O(1)
129
129
  *
130
- * The `add` function adds a new node to a binary tree, updating the value if the key already exists
131
- * or inserting a new node if the key is unique.
132
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
133
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
134
- * being added to the binary tree.
135
- * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
136
- * node was not added.
130
+ * The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
131
+ * updating the value if the key already exists.
132
+ * @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
133
+ * @param {V} [value] - The value to be added to the binary search tree.
134
+ * @returns The method returns a boolean value.
137
135
  */
138
136
  add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
139
137
  /**
@@ -144,42 +142,26 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
144
142
  * Time Complexity: O(k log n)
145
143
  * Space Complexity: O(k + log n)
146
144
  *
147
- * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
148
- * balancing the tree after each addition.
149
- * @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
150
- * the binary tree.
145
+ * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
146
+ * the structure if specified, and returns an array indicating whether each key or node was
147
+ * successfully inserted.
148
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
149
+ * data structure.
151
150
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
152
151
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
153
152
  * order. If not provided, undefined will be assigned as the value for each key or node.
154
- * @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
155
- * balanced or not. If set to true, the add operation will be balanced using a binary search tree
156
- * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
157
- * in the order they appear in the input.
158
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
159
- * type of iteration to use when adding multiple keys or nodes. It has a default value of
160
- * `this.iterationType`, which suggests that it is a property of the current object.
161
- * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
153
+ * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
154
+ * adding the elements. If set to true, the tree will be balanced using a binary search tree
155
+ * algorithm. If set to false, the elements will be added without balancing the tree. The default
156
+ * value is true.
157
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
158
+ * specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
159
+ * has a default value of `this.iterationType`, which means it will use the iteration type specified
160
+ * in the binary tree instance.
161
+ * @returns The function `addMany` returns an array of booleans indicating whether each key or node
162
+ * or entry was successfully inserted into the data structure.
162
163
  */
163
164
  addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
164
- /**
165
- * Time Complexity: O(log n)
166
- * Space Complexity: O(1)
167
- */
168
- /**
169
- * Time Complexity: O(log n)
170
- * Space Complexity: O(1)
171
- *
172
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
173
- * either recursive or iterative methods.
174
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
175
- * It is used to identify the node that we want to retrieve.
176
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
177
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
178
- * values:
179
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
180
- * found in the binary tree. If no node is found, it returns `undefined`.
181
- */
182
- getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
183
165
  /**
184
166
  * Time Complexity: O(log n)
185
167
  * Space Complexity: O(k + log n)
@@ -235,6 +217,25 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
235
217
  * @returns The method is returning a value of type `NODE | null | undefined`.
236
218
  */
237
219
  getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | undefined;
220
+ /**
221
+ * Time Complexity: O(log n)
222
+ * Space Complexity: O(1)
223
+ */
224
+ /**
225
+ * Time Complexity: O(log n)
226
+ * Space Complexity: O(1)
227
+ *
228
+ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
229
+ * either recursive or iterative methods.
230
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
231
+ * It is used to identify the node that we want to retrieve.
232
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
233
+ * type of iteration to use when searching for a node in the binary tree. It can have two possible
234
+ * values:
235
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
236
+ * found in the binary tree. If no node is found, it returns `undefined`.
237
+ */
238
+ getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
238
239
  /**
239
240
  * Time complexity: O(n)
240
241
  * Space complexity: O(n)
@@ -304,24 +305,6 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
304
305
  * function.
305
306
  */
306
307
  listLevels<C extends BTNCallback<NODE>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): ReturnType<C>[][];
307
- /**
308
- * Time Complexity: O(log n)
309
- * Space Complexity: O(1)
310
- */
311
- /**
312
- * Time Complexity: O(log n)
313
- * Space Complexity: O(1)
314
- *
315
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
316
- * leftmost node if the comparison result is greater than.
317
- * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
318
- * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
319
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
320
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
321
- * the key of the leftmost node if the comparison result is greater than, and the key of the
322
- * rightmost node otherwise. If no node is found, it returns 0.
323
- */
324
- lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, NODE>): K | undefined;
325
308
  /**
326
309
  * Time Complexity: O(log n)
327
310
  * Space Complexity: O(log n)
@@ -393,32 +376,4 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
393
376
  * can either be an object of type `NODE` or it can be `undefined`.
394
377
  */
395
378
  protected _setRoot(v: NODE | undefined): void;
396
- /**
397
- * The function compares two values using a comparator function and returns whether the first value
398
- * is greater than, less than, or equal to the second value.
399
- * @param {K} a - The parameter "a" is of type K.
400
- * @param {K} b - The parameter "b" in the above code represents a K.
401
- * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
402
- * than), 'LT' (less than), or 'EQ' (equal).
403
- */
404
- protected _compare(a: K, b: K): CP;
405
- /**
406
- * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
407
- * `a` is less than `b` based on the specified variant.
408
- * @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
409
- * first value to be compared in the function.
410
- * @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
411
- * of the arguments for the comparison in the `_lt` function.
412
- * @returns a boolean value.
413
- */
414
- protected _lt(a: K, b: K): boolean;
415
- /**
416
- * The function compares two values using a custom extractor function and returns true if the first
417
- * value is greater than the second value.
418
- * @param {K} a - The parameter "a" is of type K, which means it can be any type.
419
- * @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
420
- * of the arguments for the comparison in the function.
421
- * @returns a boolean value.
422
- */
423
- protected _gt(a: K, b: K): boolean;
424
379
  }