data-structure-typed 1.50.6 → 1.50.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 (57) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +27 -24
  3. package/benchmark/report.html +1 -37
  4. package/benchmark/report.json +17 -395
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -4
  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.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +36 -33
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.d.ts +7 -5
  13. package/dist/cjs/data-structures/binary-tree/bst.js +68 -47
  14. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -8
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +7 -17
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -2
  19. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  21. package/dist/cjs/types/common.d.ts +5 -22
  22. package/dist/cjs/types/common.js +0 -33
  23. package/dist/cjs/types/common.js.map +1 -1
  24. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  25. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -4
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
  27. package/dist/mjs/data-structures/binary-tree/binary-tree.js +36 -33
  28. package/dist/mjs/data-structures/binary-tree/bst.d.ts +7 -5
  29. package/dist/mjs/data-structures/binary-tree/bst.js +68 -47
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -8
  31. package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -17
  32. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -2
  33. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
  34. package/dist/mjs/types/common.d.ts +5 -22
  35. package/dist/mjs/types/common.js +1 -32
  36. package/dist/umd/data-structure-typed.js +112 -138
  37. package/dist/umd/data-structure-typed.min.js +2 -2
  38. package/dist/umd/data-structure-typed.min.js.map +1 -1
  39. package/package.json +6 -6
  40. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +8 -5
  41. package/src/data-structures/binary-tree/avl-tree.ts +1 -1
  42. package/src/data-structures/binary-tree/binary-tree.ts +35 -36
  43. package/src/data-structures/binary-tree/bst.ts +67 -44
  44. package/src/data-structures/binary-tree/rb-tree.ts +11 -22
  45. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  46. package/src/types/common.ts +6 -23
  47. package/test/integration/all-in-one.test.ts +2 -2
  48. package/test/integration/avl-tree.test.ts +1 -1
  49. package/test/integration/bst.test.ts +2 -2
  50. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +13 -22
  51. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +12 -17
  52. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  53. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +118 -65
  54. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
  55. package/test/unit/data-structures/binary-tree/overall.test.ts +7 -7
  56. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +272 -267
  57. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +249 -245
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.50.6",
3
+ "version": "1.50.8",
4
4
  "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -66,11 +66,11 @@
66
66
  "@typescript-eslint/eslint-plugin": "^6.7.4",
67
67
  "@typescript-eslint/parser": "^6.7.4",
68
68
  "auto-changelog": "^2.4.0",
69
- "avl-tree-typed": "^1.44.0",
69
+ "avl-tree-typed": "^1.50.7",
70
70
  "benchmark": "^2.1.4",
71
- "binary-tree-typed": "^1.44.0",
72
- "bst-typed": "^1.44.0",
73
- "data-structure-typed": "^1.44.0",
71
+ "binary-tree-typed": "^1.50.7",
72
+ "bst-typed": "^1.50.7",
73
+ "data-structure-typed": "^1.50.7",
74
74
  "dependency-cruiser": "^14.1.0",
75
75
  "doctoc": "^2.2.1",
76
76
  "eslint": "^8.50.0",
@@ -79,7 +79,7 @@
79
79
  "eslint-import-resolver-typescript": "^3.6.1",
80
80
  "eslint-plugin-import": "^2.28.1",
81
81
  "fast-glob": "^3.3.1",
82
- "heap-typed": "^1.44.0",
82
+ "heap-typed": "^1.50.7",
83
83
  "istanbul-badges-readme": "^1.8.5",
84
84
  "jest": "^29.7.0",
85
85
  "js-sdsl": "^4.4.2",
@@ -14,7 +14,6 @@ import type {
14
14
  BTNCallback,
15
15
  KeyOrNodeOrEntry
16
16
  } from '../../types';
17
- import { FamilyPosition, IterationType } from '../../types';
18
17
  import { IBinaryTree } from '../../interfaces';
19
18
  import { AVLTree, AVLTreeNode } from './avl-tree';
20
19
 
@@ -83,6 +82,10 @@ export class AVLTreeMultiMap<
83
82
  * @returns the sum of the count property of all nodes in the tree.
84
83
  */
85
84
  get count(): number {
85
+ return this._count;
86
+ }
87
+
88
+ getMutableCount(): number {
86
89
  let sum = 0;
87
90
  this.dfs(node => (sum += node.count));
88
91
  return sum;
@@ -245,9 +248,9 @@ export class AVLTreeMultiMap<
245
248
  if (curr.right !== undefined) this._setRoot(curr.right);
246
249
  } else {
247
250
  const { familyPosition: fp } = curr;
248
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
251
+ if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
249
252
  parent.left = curr.right;
250
- } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
253
+ } else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
251
254
  parent.right = curr.right;
252
255
  }
253
256
  needBalanced = parent;
@@ -320,7 +323,7 @@ export class AVLTreeMultiMap<
320
323
 
321
324
  this.clear();
322
325
 
323
- if (iterationType === IterationType.RECURSIVE) {
326
+ if (iterationType === 'RECURSIVE') {
324
327
  const buildBalanceBST = (l: number, r: number) => {
325
328
  if (l > r) return;
326
329
  const m = l + Math.floor((r - l) / 2);
@@ -414,7 +417,7 @@ export class AVLTreeMultiMap<
414
417
  * @returns The method is returning the result of calling the `_replaceNode` method from the
415
418
  * superclass, after updating the `count` property of the `newNode` object.
416
419
  */
417
- protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
420
+ protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
418
421
  newNode.count = oldNode.count + newNode.count;
419
422
  return super._replaceNode(oldNode, newNode);
420
423
  }
@@ -522,7 +522,7 @@ export class AVLTree<
522
522
  * @returns the result of calling the `_replaceNode` method on the superclass, passing in the
523
523
  * `oldNode` and `newNode` as arguments.
524
524
  */
525
- protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
525
+ protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
526
526
  newNode.height = oldNode.height;
527
527
 
528
528
  return super._replaceNode(oldNode, newNode);
@@ -107,16 +107,16 @@ export class BinaryTreeNode<
107
107
  get familyPosition(): FamilyPosition {
108
108
  const that = this as unknown as NODE;
109
109
  if (!this.parent) {
110
- return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED;
110
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
111
111
  }
112
112
 
113
113
  if (this.parent.left === that) {
114
- return this.left || this.right ? FamilyPosition.ROOT_LEFT : FamilyPosition.LEFT;
114
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
115
115
  } else if (this.parent.right === that) {
116
- return this.left || this.right ? FamilyPosition.ROOT_RIGHT : FamilyPosition.RIGHT;
116
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
117
117
  }
118
118
 
119
- return FamilyPosition.MAL_NODE;
119
+ return 'MAL_NODE';
120
120
  }
121
121
  }
122
122
 
@@ -136,7 +136,7 @@ export class BinaryTree<
136
136
  >
137
137
  extends IterableEntryBase<K, V | undefined>
138
138
  implements IBinaryTree<K, V, NODE, TREE> {
139
- iterationType = IterationType.ITERATIVE;
139
+ iterationType: IterationType = 'ITERATIVE';
140
140
 
141
141
  /**
142
142
  * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
@@ -160,7 +160,7 @@ export class BinaryTree<
160
160
  if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
161
161
  }
162
162
 
163
- protected _extractor = (key: K) => Number(key);
163
+ protected _extractor = (key: K) => (typeof key === 'number' ? key : Number(key));
164
164
 
165
165
  /**
166
166
  * The function returns the value of the `_extractor` property.
@@ -260,14 +260,11 @@ export class BinaryTree<
260
260
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
261
261
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
262
262
  * type of iteration to be used when searching for a node by key. It has a default value of
263
- * `IterationType.ITERATIVE`.
263
+ * `'ITERATIVE'`.
264
264
  * @returns either the node corresponding to the given key if it is a valid node key, or the key
265
265
  * itself if it is not a valid node key.
266
266
  */
267
- ensureNode(
268
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
269
- iterationType = IterationType.ITERATIVE
270
- ): NODE | null | undefined {
267
+ ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType = 'ITERATIVE'): NODE | null | undefined {
271
268
  let res: NODE | null | undefined;
272
269
  if (this.isRealNode(keyOrNodeOrEntry)) {
273
270
  res = keyOrNodeOrEntry;
@@ -521,9 +518,9 @@ export class BinaryTree<
521
518
  }
522
519
  } else if (parent) {
523
520
  const { familyPosition: fp } = curr;
524
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
521
+ if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
525
522
  parent.left = curr.right;
526
- } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
523
+ } else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
527
524
  parent.right = curr.right;
528
525
  }
529
526
  needBalanced = parent;
@@ -606,7 +603,7 @@ export class BinaryTree<
606
603
 
607
604
  const ans: NODE[] = [];
608
605
 
609
- if (iterationType === IterationType.RECURSIVE) {
606
+ if (iterationType === 'RECURSIVE') {
610
607
  const _traverse = (cur: NODE) => {
611
608
  if (callback(cur) === identifier) {
612
609
  ans.push(cur);
@@ -714,9 +711,9 @@ export class BinaryTree<
714
711
  * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
715
712
  * found in the binary tree. If no node is found, it returns `undefined`.
716
713
  */
717
- getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
714
+ getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined {
718
715
  if (!this.root) return undefined;
719
- if (iterationType === IterationType.RECURSIVE) {
716
+ if (iterationType === 'RECURSIVE') {
720
717
  const _dfs = (cur: NODE): NODE | undefined => {
721
718
  if (cur.key === key) return cur;
722
719
 
@@ -932,7 +929,7 @@ export class BinaryTree<
932
929
  beginRoot = this.ensureNode(beginRoot);
933
930
  if (!beginRoot) return true;
934
931
 
935
- if (iterationType === IterationType.RECURSIVE) {
932
+ if (iterationType === 'RECURSIVE') {
936
933
  const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
937
934
  if (!this.isRealNode(cur)) return true;
938
935
  const numKey = this.extractor(cur.key);
@@ -1021,9 +1018,9 @@ export class BinaryTree<
1021
1018
  */
1022
1019
  getHeight(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, iterationType = this.iterationType): number {
1023
1020
  beginRoot = this.ensureNode(beginRoot);
1024
- if (!beginRoot) return -1;
1021
+ if (!this.isRealNode(beginRoot)) return -1;
1025
1022
 
1026
- if (iterationType === IterationType.RECURSIVE) {
1023
+ if (iterationType === 'RECURSIVE') {
1027
1024
  const _getMaxHeight = (cur: NODE | null | undefined): number => {
1028
1025
  if (!this.isRealNode(cur)) return -1;
1029
1026
  const leftHeight = _getMaxHeight(cur.left);
@@ -1071,10 +1068,10 @@ export class BinaryTree<
1071
1068
  beginRoot = this.ensureNode(beginRoot);
1072
1069
  if (!beginRoot) return -1;
1073
1070
 
1074
- if (iterationType === IterationType.RECURSIVE) {
1071
+ if (iterationType === 'RECURSIVE') {
1075
1072
  const _getMinHeight = (cur: NODE | null | undefined): number => {
1076
- if (!cur) return 0;
1077
- if (!cur.left && !cur.right) return 0;
1073
+ if (!this.isRealNode(cur)) return 0;
1074
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
1078
1075
  const leftMinHeight = _getMinHeight(cur.left);
1079
1076
  const rightMinHeight = _getMinHeight(cur.right);
1080
1077
  return Math.min(leftMinHeight, rightMinHeight) + 1;
@@ -1088,16 +1085,16 @@ export class BinaryTree<
1088
1085
  const depths: Map<NODE, number> = new Map();
1089
1086
 
1090
1087
  while (stack.length > 0 || node) {
1091
- if (node) {
1088
+ if (this.isRealNode(node)) {
1092
1089
  stack.push(node);
1093
1090
  node = node.left;
1094
1091
  } else {
1095
1092
  node = stack[stack.length - 1];
1096
- if (!node.right || last === node.right) {
1093
+ if (!this.isRealNode(node.right) || last === node.right) {
1097
1094
  node = stack.pop();
1098
- if (node) {
1099
- const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
1100
- const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
1095
+ if (this.isRealNode(node)) {
1096
+ const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) ?? -1 : -1;
1097
+ const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) ?? -1 : -1;
1101
1098
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
1102
1099
  last = node;
1103
1100
  node = null;
@@ -1169,11 +1166,12 @@ export class BinaryTree<
1169
1166
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1170
1167
  iterationType = this.iterationType
1171
1168
  ): NODE | null | undefined {
1169
+ if (this.isNIL(beginRoot)) return beginRoot as NODE;
1172
1170
  beginRoot = this.ensureNode(beginRoot);
1173
1171
 
1174
- if (!beginRoot) return beginRoot;
1172
+ if (!this.isRealNode(beginRoot)) return beginRoot;
1175
1173
 
1176
- if (iterationType === IterationType.RECURSIVE) {
1174
+ if (iterationType === 'RECURSIVE') {
1177
1175
  const _traverse = (cur: NODE): NODE => {
1178
1176
  if (!this.isRealNode(cur.left)) return cur;
1179
1177
  return _traverse(cur.left);
@@ -1215,11 +1213,12 @@ export class BinaryTree<
1215
1213
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1216
1214
  iterationType = this.iterationType
1217
1215
  ): NODE | null | undefined {
1216
+ if (this.isNIL(beginRoot)) return beginRoot as NODE;
1218
1217
  // TODO support get right most by passing key in
1219
1218
  beginRoot = this.ensureNode(beginRoot);
1220
1219
  if (!beginRoot) return beginRoot;
1221
1220
 
1222
- if (iterationType === IterationType.RECURSIVE) {
1221
+ if (iterationType === 'RECURSIVE') {
1223
1222
  const _traverse = (cur: NODE): NODE => {
1224
1223
  if (!this.isRealNode(cur.right)) return cur;
1225
1224
  return _traverse(cur.right);
@@ -1343,13 +1342,13 @@ export class BinaryTree<
1343
1342
  callback: C = this._defaultOneParamCallback as C,
1344
1343
  pattern: DFSOrderPattern = 'in',
1345
1344
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1346
- iterationType: IterationType = IterationType.ITERATIVE,
1345
+ iterationType: IterationType = 'ITERATIVE',
1347
1346
  includeNull = false
1348
1347
  ): ReturnType<C>[] {
1349
1348
  beginRoot = this.ensureNode(beginRoot);
1350
1349
  if (!beginRoot) return [];
1351
1350
  const ans: ReturnType<C>[] = [];
1352
- if (iterationType === IterationType.RECURSIVE) {
1351
+ if (iterationType === 'RECURSIVE') {
1353
1352
  const _traverse = (node: NODE | null | undefined) => {
1354
1353
  switch (pattern) {
1355
1354
  case 'in':
@@ -1485,7 +1484,7 @@ export class BinaryTree<
1485
1484
 
1486
1485
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
1487
1486
 
1488
- if (iterationType === IterationType.RECURSIVE) {
1487
+ if (iterationType === 'RECURSIVE') {
1489
1488
  const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
1490
1489
 
1491
1490
  const traverse = (level: number) => {
@@ -1578,7 +1577,7 @@ export class BinaryTree<
1578
1577
  const levelsNodes: ReturnType<C>[][] = [];
1579
1578
  if (!beginRoot) return levelsNodes;
1580
1579
 
1581
- if (iterationType === IterationType.RECURSIVE) {
1580
+ if (iterationType === 'RECURSIVE') {
1582
1581
  const _recursive = (node: NODE | null, level: number) => {
1583
1582
  if (!levelsNodes[level]) levelsNodes[level] = [];
1584
1583
  levelsNodes[level].push(callback(node));
@@ -1837,7 +1836,7 @@ export class BinaryTree<
1837
1836
  * following types:
1838
1837
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
1839
1838
  */
1840
- print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1839
+ override print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1841
1840
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1842
1841
  beginRoot = this.ensureNode(beginRoot);
1843
1842
  if (!beginRoot) return;
@@ -1874,7 +1873,7 @@ export class BinaryTree<
1874
1873
  protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1875
1874
  if (!node) return;
1876
1875
 
1877
- if (this.iterationType === IterationType.ITERATIVE) {
1876
+ if (this.iterationType === 'ITERATIVE') {
1878
1877
  const stack: (NODE | null | undefined)[] = [];
1879
1878
  let current: NODE | null | undefined = node;
1880
1879
 
@@ -126,7 +126,7 @@ export class BST<
126
126
  return this._root;
127
127
  }
128
128
 
129
- protected _variant = BSTVariant.STANDARD;
129
+ protected _variant: BSTVariant = 'STANDARD';
130
130
 
131
131
  /**
132
132
  * The function returns the value of the _variant property.
@@ -207,13 +207,10 @@ export class BST<
207
207
  * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
208
208
  * `undefined`.
209
209
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
210
- * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
210
+ * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
211
211
  * @returns either a node object (NODE) or undefined.
212
212
  */
213
- override ensureNode(
214
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
215
- iterationType = IterationType.ITERATIVE
216
- ): NODE | undefined {
213
+ override ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType = 'ITERATIVE'): NODE | undefined {
217
214
  let res: NODE | undefined;
218
215
  if (this.isRealNode(keyOrNodeOrEntry)) {
219
216
  res = keyOrNodeOrEntry;
@@ -263,7 +260,7 @@ export class BST<
263
260
 
264
261
  let current = this.root;
265
262
  while (current !== undefined) {
266
- if (this._compare(current.key, newNode.key) === CP.eq) {
263
+ if (this._compare(current.key, newNode.key) === 'EQ') {
267
264
  // if (current !== newNode) {
268
265
  // The key value is the same but the reference is different, update the value of the existing node
269
266
  this._replaceNode(current, newNode);
@@ -275,7 +272,7 @@ export class BST<
275
272
 
276
273
  // return;
277
274
  // }
278
- } else if (this._compare(current.key, newNode.key) === CP.gt) {
275
+ } else if (this._compare(current.key, newNode.key) === 'GT') {
279
276
  if (current.left === undefined) {
280
277
  current.left = newNode;
281
278
  this._size++;
@@ -397,7 +394,7 @@ export class BST<
397
394
  }
398
395
  };
399
396
 
400
- if (iterationType === IterationType.RECURSIVE) {
397
+ if (iterationType === 'RECURSIVE') {
401
398
  _dfs(sorted);
402
399
  } else {
403
400
  _iterate();
@@ -425,15 +422,15 @@ export class BST<
425
422
  * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
426
423
  * found in the binary tree. If no node is found, it returns `undefined`.
427
424
  */
428
- override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
429
- if (!this.root) return undefined;
430
- if (iterationType === IterationType.RECURSIVE) {
425
+ override getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined {
426
+ if (!this.isRealNode(this.root)) return undefined;
427
+ if (iterationType === 'RECURSIVE') {
431
428
  const _dfs = (cur: NODE): NODE | undefined => {
432
429
  if (cur.key === key) return cur;
433
- if (!cur.left && !cur.right) return;
430
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
434
431
 
435
- if (this._compare(cur.key, key) === CP.gt && cur.left) return _dfs(cur.left);
436
- if (this._compare(cur.key, key) === CP.lt && cur.right) return _dfs(cur.right);
432
+ if (this._compare(cur.key, key) === 'GT' && this.isRealNode(cur.left)) return _dfs(cur.left);
433
+ if (this._compare(cur.key, key) === 'LT' && this.isRealNode(cur.right)) return _dfs(cur.right);
437
434
  };
438
435
 
439
436
  return _dfs(this.root);
@@ -441,10 +438,10 @@ export class BST<
441
438
  const queue = new Queue<NODE>([this.root]);
442
439
  while (queue.size > 0) {
443
440
  const cur = queue.shift();
444
- if (cur) {
445
- if (this._compare(cur.key, key) === CP.eq) return cur;
446
- if (this._compare(cur.key, key) === CP.gt) cur.left && queue.push(cur.left);
447
- if (this._compare(cur.key, key) === CP.lt) cur.right && queue.push(cur.right);
441
+ if (this.isRealNode(cur)) {
442
+ if (this._compare(cur.key, key) === 'EQ') return cur;
443
+ if (this._compare(cur.key, key) === 'GT') this.isRealNode(cur.left) && queue.push(cur.left);
444
+ if (this._compare(cur.key, key) === 'LT') this.isRealNode(cur.right) && queue.push(cur.right);
448
445
  }
449
446
  }
450
447
  }
@@ -489,7 +486,7 @@ export class BST<
489
486
  if (!beginRoot) return [];
490
487
  const ans: NODE[] = [];
491
488
 
492
- if (iterationType === IterationType.RECURSIVE) {
489
+ if (iterationType === 'RECURSIVE') {
493
490
  const _traverse = (cur: NODE) => {
494
491
  const callbackResult = callback(cur);
495
492
  if (callbackResult === identifier) {
@@ -497,23 +494,23 @@ export class BST<
497
494
  if (onlyOne) return;
498
495
  }
499
496
 
500
- if (!cur.left && !cur.right) return;
497
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
501
498
  // TODO potential bug
502
499
  if (callback === this._defaultOneParamCallback) {
503
- if (this._compare(cur.key, identifier as K) === CP.gt) cur.left && _traverse(cur.left);
504
- if (this._compare(cur.key, identifier as K) === CP.lt) cur.right && _traverse(cur.right);
500
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') _traverse(cur.left);
501
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') _traverse(cur.right);
505
502
  } else {
506
- cur.left && _traverse(cur.left);
507
- cur.right && _traverse(cur.right);
503
+ this.isRealNode(cur.left) && _traverse(cur.left);
504
+ this.isRealNode(cur.right) && _traverse(cur.right);
508
505
  }
509
506
  };
510
507
 
511
508
  _traverse(beginRoot);
512
509
  } else {
513
- const queue = new Queue<NODE>([beginRoot]);
514
- while (queue.size > 0) {
515
- const cur = queue.shift();
516
- if (cur) {
510
+ const stack = [beginRoot];
511
+ while (stack.length > 0) {
512
+ const cur = stack.pop();
513
+ if (this.isRealNode(cur)) {
517
514
  const callbackResult = callback(cur);
518
515
  if (callbackResult === identifier) {
519
516
  ans.push(cur);
@@ -521,11 +518,19 @@ export class BST<
521
518
  }
522
519
  // TODO potential bug
523
520
  if (callback === this._defaultOneParamCallback) {
524
- if (this._compare(cur.key, identifier as K) === CP.gt) cur.left && queue.push(cur.left);
525
- if (this._compare(cur.key, identifier as K) === CP.lt) cur.right && queue.push(cur.right);
521
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
522
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
523
+
524
+ // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
525
+ // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
526
+
527
+ // // @ts-ignore
528
+ // if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
529
+ // // @ts-ignore
530
+ // if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
526
531
  } else {
527
- cur.left && queue.push(cur.left);
528
- cur.right && queue.push(cur.right);
532
+ this.isRealNode(cur.right) && stack.push(cur.right);
533
+ this.isRealNode(cur.left) && stack.push(cur.left);
529
534
  }
530
535
  }
531
536
  }
@@ -562,7 +567,7 @@ export class BST<
562
567
  callback: C = this._defaultOneParamCallback as C,
563
568
  pattern: DFSOrderPattern = 'in',
564
569
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
565
- iterationType: IterationType = IterationType.ITERATIVE
570
+ iterationType: IterationType = 'ITERATIVE'
566
571
  ): ReturnType<C>[] {
567
572
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
568
573
  }
@@ -650,7 +655,7 @@ export class BST<
650
655
  let current = this.ensureNode(beginRoot);
651
656
  if (!current) return undefined;
652
657
 
653
- if (this._variant === BSTVariant.STANDARD) {
658
+ if (this._variant === 'STANDARD') {
654
659
  // For BSTVariant.MIN, find the rightmost node
655
660
  while (current.right !== undefined) {
656
661
  current = current.right;
@@ -692,7 +697,7 @@ export class BST<
692
697
  */
693
698
  lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
694
699
  callback: C = this._defaultOneParamCallback as C,
695
- lesserOrGreater: CP = CP.lt,
700
+ lesserOrGreater: CP = 'LT',
696
701
  targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
697
702
  iterationType = this.iterationType
698
703
  ): ReturnType<C>[] {
@@ -703,7 +708,7 @@ export class BST<
703
708
 
704
709
  const targetKey = targetNode.key;
705
710
 
706
- if (iterationType === IterationType.RECURSIVE) {
711
+ if (iterationType === 'RECURSIVE') {
707
712
  const _traverse = (cur: NODE) => {
708
713
  const compared = this._compare(cur.key, targetKey);
709
714
  if (compared === lesserOrGreater) ans.push(callback(cur));
@@ -752,7 +757,7 @@ export class BST<
752
757
  this.clear();
753
758
 
754
759
  if (sorted.length < 1) return false;
755
- if (iterationType === IterationType.RECURSIVE) {
760
+ if (iterationType === 'RECURSIVE') {
756
761
  const buildBalanceBST = (l: number, r: number) => {
757
762
  if (l > r) return;
758
763
  const m = l + Math.floor((r - l) / 2);
@@ -812,7 +817,7 @@ export class BST<
812
817
 
813
818
  let balanced = true;
814
819
 
815
- if (iterationType === IterationType.RECURSIVE) {
820
+ if (iterationType === 'RECURSIVE') {
816
821
  const _height = (cur: NODE | undefined): number => {
817
822
  if (!cur) return 0;
818
823
  const leftHeight = _height(cur.left),
@@ -856,7 +861,7 @@ export class BST<
856
861
  * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
857
862
  * can either be an object of type `NODE` or it can be `undefined`.
858
863
  */
859
- protected _setRoot(v: NODE | undefined) {
864
+ protected override _setRoot(v: NODE | undefined) {
860
865
  if (v) {
861
866
  v.parent = undefined;
862
867
  }
@@ -868,14 +873,32 @@ export class BST<
868
873
  * is greater than, less than, or equal to the second value.
869
874
  * @param {K} a - The parameter "a" is of type K.
870
875
  * @param {K} b - The parameter "b" in the above code represents a K.
871
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
872
- * than), CP.lt (less than), or CP.eq (equal).
876
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
877
+ * than), 'LT' (less than), or 'EQ' (equal).
873
878
  */
874
879
  protected _compare(a: K, b: K): CP {
875
880
  const extractedA = this.extractor(a);
876
881
  const extractedB = this.extractor(b);
877
- const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
882
+ const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
878
883
 
879
- return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
884
+ return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
885
+ }
886
+
887
+ protected _lt(a: K, b: K): boolean {
888
+ const extractedA = this.extractor(a);
889
+ const extractedB = this.extractor(b);
890
+ // return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
891
+ return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
892
+ // return extractedA < extractedB;
893
+ // return a < b;
894
+ }
895
+
896
+ protected _gt(a: K, b: K): boolean {
897
+ const extractedA = this.extractor(a);
898
+ const extractedB = this.extractor(b);
899
+ // return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
900
+ return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
901
+ // return extractedA > extractedB;
902
+ // return a > b;
880
903
  }
881
904
  }