data-structure-typed 1.50.7 → 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 (53) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +27 -19
  3. package/benchmark/report.html +1 -37
  4. package/benchmark/report.json +16 -394
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -2
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -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/binary-tree.d.ts +3 -3
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +23 -24
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.d.ts +7 -5
  12. package/dist/cjs/data-structures/binary-tree/bst.js +59 -38
  13. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -7
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -2
  17. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  19. package/dist/cjs/types/common.d.ts +5 -28
  20. package/dist/cjs/types/common.js +0 -40
  21. package/dist/cjs/types/common.js.map +1 -1
  22. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -2
  23. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +3 -4
  24. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +3 -3
  25. package/dist/mjs/data-structures/binary-tree/binary-tree.js +23 -24
  26. package/dist/mjs/data-structures/binary-tree/bst.d.ts +7 -5
  27. package/dist/mjs/data-structures/binary-tree/bst.js +59 -38
  28. package/dist/mjs/data-structures/binary-tree/rb-tree.js +7 -8
  29. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -2
  30. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
  31. package/dist/mjs/types/common.d.ts +5 -28
  32. package/dist/mjs/types/common.js +1 -39
  33. package/dist/umd/data-structure-typed.js +86 -118
  34. package/dist/umd/data-structure-typed.min.js +2 -2
  35. package/dist/umd/data-structure-typed.min.js.map +1 -1
  36. package/package.json +6 -6
  37. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
  38. package/src/data-structures/binary-tree/binary-tree.ts +23 -26
  39. package/src/data-structures/binary-tree/bst.ts +59 -36
  40. package/src/data-structures/binary-tree/rb-tree.ts +6 -6
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  42. package/src/types/common.ts +5 -29
  43. package/test/integration/all-in-one.test.ts +2 -2
  44. package/test/integration/avl-tree.test.ts +1 -1
  45. package/test/integration/bst.test.ts +2 -2
  46. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +13 -22
  47. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +8 -16
  48. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  49. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +118 -65
  50. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
  51. package/test/unit/data-structures/binary-tree/overall.test.ts +7 -7
  52. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +10 -5
  53. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +7 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.50.7",
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
 
@@ -249,9 +248,9 @@ export class AVLTreeMultiMap<
249
248
  if (curr.right !== undefined) this._setRoot(curr.right);
250
249
  } else {
251
250
  const { familyPosition: fp } = curr;
252
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
251
+ if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
253
252
  parent.left = curr.right;
254
- } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
253
+ } else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
255
254
  parent.right = curr.right;
256
255
  }
257
256
  needBalanced = parent;
@@ -324,7 +323,7 @@ export class AVLTreeMultiMap<
324
323
 
325
324
  this.clear();
326
325
 
327
- if (iterationType === IterationType.RECURSIVE) {
326
+ if (iterationType === 'RECURSIVE') {
328
327
  const buildBalanceBST = (l: number, r: number) => {
329
328
  if (l > r) return;
330
329
  const m = l + Math.floor((r - l) / 2);
@@ -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);
@@ -1023,7 +1020,7 @@ export class BinaryTree<
1023
1020
  beginRoot = this.ensureNode(beginRoot);
1024
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,7 +1068,7 @@ 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
1073
  if (!this.isRealNode(cur)) return 0;
1077
1074
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
@@ -1174,7 +1171,7 @@ export class BinaryTree<
1174
1171
 
1175
1172
  if (!this.isRealNode(beginRoot)) return beginRoot;
1176
1173
 
1177
- if (iterationType === IterationType.RECURSIVE) {
1174
+ if (iterationType === 'RECURSIVE') {
1178
1175
  const _traverse = (cur: NODE): NODE => {
1179
1176
  if (!this.isRealNode(cur.left)) return cur;
1180
1177
  return _traverse(cur.left);
@@ -1221,7 +1218,7 @@ export class BinaryTree<
1221
1218
  beginRoot = this.ensureNode(beginRoot);
1222
1219
  if (!beginRoot) return beginRoot;
1223
1220
 
1224
- if (iterationType === IterationType.RECURSIVE) {
1221
+ if (iterationType === 'RECURSIVE') {
1225
1222
  const _traverse = (cur: NODE): NODE => {
1226
1223
  if (!this.isRealNode(cur.right)) return cur;
1227
1224
  return _traverse(cur.right);
@@ -1345,13 +1342,13 @@ export class BinaryTree<
1345
1342
  callback: C = this._defaultOneParamCallback as C,
1346
1343
  pattern: DFSOrderPattern = 'in',
1347
1344
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1348
- iterationType: IterationType = IterationType.ITERATIVE,
1345
+ iterationType: IterationType = 'ITERATIVE',
1349
1346
  includeNull = false
1350
1347
  ): ReturnType<C>[] {
1351
1348
  beginRoot = this.ensureNode(beginRoot);
1352
1349
  if (!beginRoot) return [];
1353
1350
  const ans: ReturnType<C>[] = [];
1354
- if (iterationType === IterationType.RECURSIVE) {
1351
+ if (iterationType === 'RECURSIVE') {
1355
1352
  const _traverse = (node: NODE | null | undefined) => {
1356
1353
  switch (pattern) {
1357
1354
  case 'in':
@@ -1487,7 +1484,7 @@ export class BinaryTree<
1487
1484
 
1488
1485
  const ans: ReturnType<BTNCallback<NODE>>[] = [];
1489
1486
 
1490
- if (iterationType === IterationType.RECURSIVE) {
1487
+ if (iterationType === 'RECURSIVE') {
1491
1488
  const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
1492
1489
 
1493
1490
  const traverse = (level: number) => {
@@ -1580,7 +1577,7 @@ export class BinaryTree<
1580
1577
  const levelsNodes: ReturnType<C>[][] = [];
1581
1578
  if (!beginRoot) return levelsNodes;
1582
1579
 
1583
- if (iterationType === IterationType.RECURSIVE) {
1580
+ if (iterationType === 'RECURSIVE') {
1584
1581
  const _recursive = (node: NODE | null, level: number) => {
1585
1582
  if (!levelsNodes[level]) levelsNodes[level] = [];
1586
1583
  levelsNodes[level].push(callback(node));
@@ -1876,7 +1873,7 @@ export class BinaryTree<
1876
1873
  protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1877
1874
  if (!node) return;
1878
1875
 
1879
- if (this.iterationType === IterationType.ITERATIVE) {
1876
+ if (this.iterationType === 'ITERATIVE') {
1880
1877
  const stack: (NODE | null | undefined)[] = [];
1881
1878
  let current: NODE | null | undefined = node;
1882
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 {
425
+ override getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined {
429
426
  if (!this.isRealNode(this.root)) return undefined;
430
- if (iterationType === IterationType.RECURSIVE) {
427
+ if (iterationType === 'RECURSIVE') {
431
428
  const _dfs = (cur: NODE): NODE | undefined => {
432
429
  if (cur.key === key) return cur;
433
430
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
434
431
 
435
- if (this._compare(cur.key, key) === CP.gt && this.isRealNode(cur.left)) return _dfs(cur.left);
436
- if (this._compare(cur.key, key) === CP.lt && this.isRealNode(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);
@@ -442,9 +439,9 @@ export class BST<
442
439
  while (queue.size > 0) {
443
440
  const cur = queue.shift();
444
441
  if (this.isRealNode(cur)) {
445
- if (this._compare(cur.key, key) === CP.eq) return cur;
446
- if (this._compare(cur.key, key) === CP.gt) this.isRealNode(cur.left) && queue.push(cur.left);
447
- if (this._compare(cur.key, key) === CP.lt) this.isRealNode(cur.right) && queue.push(cur.right);
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) {
@@ -500,8 +497,8 @@ export class BST<
500
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) this.isRealNode(cur.left) && _traverse(cur.left);
504
- if (this._compare(cur.key, identifier as K) === CP.lt) this.isRealNode(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
503
  this.isRealNode(cur.left) && _traverse(cur.left);
507
504
  this.isRealNode(cur.right) && _traverse(cur.right);
@@ -510,9 +507,9 @@ export class BST<
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();
510
+ const stack = [beginRoot];
511
+ while (stack.length > 0) {
512
+ const cur = stack.pop();
516
513
  if (this.isRealNode(cur)) {
517
514
  const callbackResult = callback(cur);
518
515
  if (callbackResult === identifier) {
@@ -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) this.isRealNode(cur.left) && queue.push(cur.left);
525
- if (this._compare(cur.key, identifier as K) === CP.lt) this.isRealNode(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
- this.isRealNode(cur.left) && queue.push(cur.left);
528
- this.isRealNode(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),
@@ -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;
883
+
884
+ return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
885
+ }
878
886
 
879
- return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
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
  }
@@ -234,8 +234,8 @@ export class RedBlackTree<
234
234
  beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
235
235
  iterationType = this.iterationType
236
236
  ): NODE | null | undefined {
237
- if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
238
- return super.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
237
+ // if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
238
+ return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
239
239
  }
240
240
 
241
241
  /**
@@ -279,7 +279,7 @@ export class RedBlackTree<
279
279
 
280
280
  const insertStatus = this._insert(newNode);
281
281
 
282
- if (insertStatus === CRUD.CREATED) {
282
+ if (insertStatus === 'CREATED') {
283
283
  // Ensure the root is black
284
284
  if (this.isRealNode(this._root)) {
285
285
  this._root.color = RBTNColor.BLACK;
@@ -288,7 +288,7 @@ export class RedBlackTree<
288
288
  }
289
289
  this._size++;
290
290
  return true;
291
- } else return insertStatus === CRUD.UPDATED;
291
+ } else return insertStatus === 'UPDATED';
292
292
  }
293
293
 
294
294
  /**
@@ -435,7 +435,7 @@ export class RedBlackTree<
435
435
  current = current.right ?? this.SENTINEL;
436
436
  } else {
437
437
  this._replaceNode(current, node);
438
- return CRUD.UPDATED;
438
+ return 'UPDATED';
439
439
  }
440
440
  }
441
441
 
@@ -454,7 +454,7 @@ export class RedBlackTree<
454
454
  node.color = RBTNColor.RED;
455
455
 
456
456
  this._insertFixup(node);
457
- return CRUD.CREATED;
457
+ return 'CREATED';
458
458
  }
459
459
 
460
460
  /**
@@ -14,7 +14,7 @@ import type {
14
14
  TreeMultiMapNodeNested,
15
15
  TreeMultiMapOptions
16
16
  } from '../../types';
17
- import { IterationType, RBTNColor } from '../../types';
17
+ import { RBTNColor } from '../../types';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
20
20
 
@@ -363,7 +363,7 @@ export class TreeMultiMap<
363
363
 
364
364
  this.clear();
365
365
 
366
- if (iterationType === IterationType.RECURSIVE) {
366
+ if (iterationType === 'RECURSIVE') {
367
367
  const buildBalanceBST = (l: number, r: number) => {
368
368
  if (l > r) return;
369
369
  const m = l + Math.floor((r - l) / 2);
@@ -1,13 +1,5 @@
1
- export enum BSTVariant {
2
- STANDARD = 'STANDARD',
3
- INVERSE = 'INVERSE'
4
- }
5
-
6
- export enum CP {
7
- lt = 'lt',
8
- eq = 'eq',
9
- gt = 'gt'
10
- }
1
+ export type BSTVariant = 'STANDARD' | 'INVERSE';
2
+ export type CP = 'LT' | 'EQ' | 'GT';
11
3
 
12
4
  /**
13
5
  * Enum representing different loop types.
@@ -15,20 +7,9 @@ export enum CP {
15
7
  * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
16
8
  * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
17
9
  */
18
- export enum IterationType {
19
- ITERATIVE = 'ITERATIVE',
20
- RECURSIVE = 'RECURSIVE'
21
- }
10
+ export type IterationType = 'ITERATIVE' | 'RECURSIVE';
22
11
 
23
- export enum FamilyPosition {
24
- ROOT = 'ROOT',
25
- LEFT = 'LEFT',
26
- RIGHT = 'RIGHT',
27
- ROOT_LEFT = 'ROOT_LEFT',
28
- ROOT_RIGHT = 'ROOT_RIGHT',
29
- ISOLATED = 'ISOLATED',
30
- MAL_NODE = 'MAL_NODE'
31
- }
12
+ export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
32
13
 
33
14
  export type Comparator<K> = (a: K, b: K) => number;
34
15
 
@@ -64,9 +45,4 @@ export type BSTNKeyOrNode<K, N> = K | undefined | N;
64
45
 
65
46
  export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
66
47
 
67
- export enum CRUD {
68
- CREATED = 'CREATED',
69
- READ = 'READ',
70
- UPDATED = 'UPDATED',
71
- DELETED = 'DELETED'
72
- }
48
+ export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
@@ -1,4 +1,4 @@
1
- import { AVLTree, CP } from 'data-structure-typed';
1
+ import { AVLTree } from 'data-structure-typed';
2
2
 
3
3
  describe('AVL Tree Test from data-structure-typed', () => {
4
4
  it('should perform various operations on a AVL Tree from data-structure-typed', () => {
@@ -27,7 +27,7 @@ describe('AVL Tree Test from data-structure-typed', () => {
27
27
  expect(subTreeSum).toBe(70);
28
28
 
29
29
  let lesserSum = 0;
30
- tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), CP.lt, 10);
30
+ tree.lesserOrGreaterTraverse(node => (lesserSum += node.key), 'LT', 10);
31
31
  expect(lesserSum).toBe(45);
32
32
 
33
33
  // node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
@@ -40,7 +40,7 @@ describe('AVL Tree Test', () => {
40
40
  expect(getMinNodeBySpecificNode?.key).toBe(12);
41
41
 
42
42
  let subTreeSum = 0;
43
- node15 && tree.subTreeTraverse(node => (subTreeSum += node.key), 15);
43
+ node15 && tree.dfs(node => (subTreeSum += node.key), 'in', 15);
44
44
  expect(subTreeSum).toBe(70);
45
45
 
46
46
  let lesserSum = 0;
@@ -34,7 +34,7 @@ describe('Individual package BST operations test', () => {
34
34
  expect(minNodeBySpecificNode?.key).toBe(12);
35
35
 
36
36
  let subTreeSum = 0;
37
- node15 && bst.subTreeTraverse(node => (subTreeSum += node.key), 15);
37
+ node15 && bst.dfs(node => (subTreeSum += node.key), 'in', 15);
38
38
  expect(subTreeSum).toBe(70);
39
39
 
40
40
  let lesserSum = 0;
@@ -231,7 +231,7 @@ describe('Individual package BST operations test', () => {
231
231
  expect(minNodeBySpecificNode?.key).toBe(12);
232
232
 
233
233
  let subTreeSum = 0;
234
- node15 && objBST.subTreeTraverse(node => (subTreeSum += node.key), node15);
234
+ node15 && objBST.dfs(node => (subTreeSum += node.key), 'in', node15);
235
235
  expect(subTreeSum).toBe(70);
236
236
 
237
237
  let lesserSum = 0;