data-structure-typed 1.49.8 → 1.50.0

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 (54) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +1 -37
  3. package/benchmark/report.json +22 -370
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.d.ts +89 -27
  11. package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  19. package/dist/cjs/data-structures/hash/hash-map.d.ts +16 -12
  20. package/dist/cjs/data-structures/hash/hash-map.js +36 -15
  21. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  22. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +2 -1
  23. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
  24. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
  25. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
  27. package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
  28. package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
  31. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
  32. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
  33. package/dist/mjs/data-structures/hash/hash-map.d.ts +16 -12
  34. package/dist/mjs/data-structures/hash/hash-map.js +36 -15
  35. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +2 -1
  36. package/dist/umd/data-structure-typed.js +176 -165
  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 +1 -1
  40. package/src/data-structures/binary-tree/avl-tree.ts +0 -10
  41. package/src/data-structures/binary-tree/binary-tree.ts +104 -141
  42. package/src/data-structures/binary-tree/bst.ts +153 -49
  43. package/src/data-structures/binary-tree/rb-tree.ts +1 -11
  44. package/src/data-structures/binary-tree/tree-multimap.ts +2 -12
  45. package/src/data-structures/hash/hash-map.ts +42 -16
  46. package/src/types/data-structures/hash/hash-map.ts +2 -1
  47. package/test/integration/all-in-one.test.ts +1 -1
  48. package/test/integration/index.html +2 -2
  49. package/test/performance/data-structures/queue/deque.test.ts +8 -2
  50. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  51. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
  52. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
  53. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
  54. package/test/unit/data-structures/hash/hash-map.test.ts +17 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.49.8",
3
+ "version": "1.50.0",
4
4
  "description": "Data Structures of Javascript & TypeScript. 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",
@@ -98,16 +98,6 @@ export class AVLTree<
98
98
  return keyOrNodeOrEntry instanceof AVLTreeNode;
99
99
  }
100
100
 
101
- /**
102
- * The function "isNotNodeInstance" checks if a potential key is a K.
103
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
104
- * data type.
105
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
106
- */
107
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
108
- return !(potentialKey instanceof AVLTreeNode);
109
- }
110
-
111
101
  /**
112
102
  * Time Complexity: O(log n)
113
103
  * Space Complexity: O(1)
@@ -196,7 +196,7 @@ export class BinaryTree<
196
196
  }
197
197
  } else if (this.isNode(keyOrNodeOrEntry)) {
198
198
  node = keyOrNodeOrEntry;
199
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
199
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
200
200
  node = this.createNode(keyOrNodeOrEntry, value);
201
201
  } else {
202
202
  return;
@@ -292,16 +292,6 @@ export class BinaryTree<
292
292
  return this.isRealNode(node) || node === null;
293
293
  }
294
294
 
295
- /**
296
- * The function "isNotNodeInstance" checks if a potential key is a K.
297
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
298
- * data type.
299
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
300
- */
301
- isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
302
- return !(potentialKey instanceof BinaryTreeNode);
303
- }
304
-
305
295
  /**
306
296
  * Time Complexity O(log n) - O(n)
307
297
  * Space Complexity O(1)
@@ -1028,7 +1018,7 @@ export class BinaryTree<
1028
1018
  *
1029
1019
  * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
1030
1020
  * structure, with the option to reverse the order of the nodes.
1031
- * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1021
+ * @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
1032
1022
  * starting node from which you want to find the path to the root. It can be of type `K`, `N`,
1033
1023
  * `null`, or `undefined`.
1034
1024
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
@@ -1036,20 +1026,20 @@ export class BinaryTree<
1036
1026
  * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
1037
1027
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
1038
1028
  */
1039
- getPathToRoot(beginRoot: KeyOrNodeOrEntry<K, V, N>, isReverse = true): N[] {
1029
+ getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, N>, isReverse = true): N[] {
1040
1030
  // TODO to support get path through passing key
1041
1031
  const result: N[] = [];
1042
- beginRoot = this.ensureNode(beginRoot);
1032
+ beginNode = this.ensureNode(beginNode);
1043
1033
 
1044
- if (!beginRoot) return result;
1034
+ if (!beginNode) return result;
1045
1035
 
1046
- while (beginRoot.parent) {
1036
+ while (beginNode.parent) {
1047
1037
  // Array.push + Array.reverse is more efficient than Array.unshift
1048
1038
  // TODO may consider using Deque, so far this is not the performance bottleneck
1049
- result.push(beginRoot);
1050
- beginRoot = beginRoot.parent;
1039
+ result.push(beginNode);
1040
+ beginNode = beginNode.parent;
1051
1041
  }
1052
- result.push(beginRoot);
1042
+ result.push(beginNode);
1053
1043
  return isReverse ? result.reverse() : result;
1054
1044
  }
1055
1045
 
@@ -1202,99 +1192,94 @@ export class BinaryTree<
1202
1192
  }
1203
1193
  }
1204
1194
 
1205
- /**
1206
- * Time complexity: O(n)
1207
- * Space complexity: O(log n)
1208
- */
1209
-
1210
- subTreeTraverse<C extends BTNCallback<N>>(
1211
- callback?: C,
1212
- beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1213
- iterationType?: IterationType,
1214
- includeNull?: false
1215
- ): ReturnType<C>[];
1216
-
1217
- subTreeTraverse<C extends BTNCallback<N>>(
1218
- callback?: C,
1219
- beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1220
- iterationType?: IterationType,
1221
- includeNull?: undefined
1222
- ): ReturnType<C>[];
1223
-
1224
- subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1225
- callback?: C,
1226
- beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1227
- iterationType?: IterationType,
1228
- includeNull?: true
1229
- ): ReturnType<C>[];
1230
-
1231
- /**
1232
- * Time complexity: O(n)
1233
- * Space complexity: O(log n)
1234
- *
1235
- * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
1236
- * node, either recursively or iteratively.
1237
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1238
- * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
1239
- * returns a value of any type.
1240
- * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1241
- * starting node or key from which the subtree traversal should begin. It can be of type `K`,
1242
- * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
1243
- * the default value.
1244
- * @param iterationType - The `iterationType` parameter determines the type of traversal to be
1245
- * performed on the subtree. It can have two possible values:
1246
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1247
- * whether to include null values in the traversal. If `includeNull` is set to `true`, the
1248
- * traversal will include null values, otherwise it will skip them.
1249
- * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
1250
- * the `callback` function on each node in the subtree. The type of the array nodes is determined
1251
- * by the return type of the `callback` function.
1252
- */
1253
- subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1254
- callback: C = this._defaultOneParamCallback as C,
1255
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1256
- iterationType = this.iterationType,
1257
- includeNull = false
1258
- ): ReturnType<C>[] {
1259
- beginRoot = this.ensureNode(beginRoot);
1260
-
1261
- const ans: (ReturnType<BTNCallback<N>> | null | undefined)[] = [];
1262
- if (!beginRoot) return ans;
1263
-
1264
- if (iterationType === IterationType.RECURSIVE) {
1265
- const _traverse = (cur: N | null | undefined) => {
1266
- if (cur !== undefined) {
1267
- ans.push(callback(cur));
1268
- if (includeNull) {
1269
- cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
1270
- cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
1271
- } else {
1272
- cur && cur.left && _traverse(cur.left);
1273
- cur && cur.right && _traverse(cur.right);
1274
- }
1275
- }
1276
- };
1277
-
1278
- _traverse(beginRoot);
1279
- } else {
1280
- const stack: (N | null | undefined)[] = [beginRoot];
1281
-
1282
- while (stack.length > 0) {
1283
- const cur = stack.pop();
1284
- if (cur !== undefined) {
1285
- ans.push(callback(cur));
1286
- if (includeNull) {
1287
- cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
1288
- cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
1289
- } else {
1290
- cur && cur.right && stack.push(cur.right);
1291
- cur && cur.left && stack.push(cur.left);
1292
- }
1293
- }
1294
- }
1295
- }
1296
- return ans;
1297
- }
1195
+ // /**
1196
+ // * Time complexity: O(n)
1197
+ // * Space complexity: O(log n)
1198
+ // */
1199
+ //
1200
+ // subTreeTraverse<C extends BTNCallback<N>>(
1201
+ // callback?: C,
1202
+ // beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1203
+ // iterationType?: IterationType,
1204
+ // includeNull?: false
1205
+ // ): ReturnType<C>[];
1206
+ //
1207
+ // subTreeTraverse<C extends BTNCallback<N | null>>(
1208
+ // callback?: C,
1209
+ // beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1210
+ // iterationType?: IterationType,
1211
+ // includeNull?: true
1212
+ // ): ReturnType<C>[];
1213
+ //
1214
+ // /**
1215
+ // * Time complexity: O(n)
1216
+ // * Space complexity: O(log n)
1217
+ // *
1218
+ // * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
1219
+ // * node, either recursively or iteratively.
1220
+ // * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1221
+ // * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
1222
+ // * returns a value of any type.
1223
+ // * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1224
+ // * starting node or key from which the subtree traversal should begin. It can be of type `K`,
1225
+ // * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
1226
+ // * the default value.
1227
+ // * @param iterationType - The `iterationType` parameter determines the type of traversal to be
1228
+ // * performed on the subtree. It can have two possible values:
1229
+ // * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1230
+ // * whether to include null values in the traversal. If `includeNull` is set to `true`, the
1231
+ // * traversal will include null values, otherwise it will skip them.
1232
+ // * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
1233
+ // * the `callback` function on each node in the subtree. The type of the array nodes is determined
1234
+ // * by the return type of the `callback` function.
1235
+ // */
1236
+ // subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1237
+ // callback: C = this._defaultOneParamCallback as C,
1238
+ // beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1239
+ // iterationType = this.iterationType,
1240
+ // includeNull = false
1241
+ // ): ReturnType<C>[] {
1242
+ // console.warn('subTreeTraverse is unnecessary, since the dfs method can substitute it.');
1243
+ //
1244
+ // beginRoot = this.ensureNode(beginRoot);
1245
+ //
1246
+ // const ans: (ReturnType<BTNCallback<N>> | null | undefined)[] = [];
1247
+ // if (!beginRoot) return ans;
1248
+ //
1249
+ // if (iterationType === IterationType.RECURSIVE) {
1250
+ // const _traverse = (cur: N | null | undefined) => {
1251
+ // if (cur !== undefined) {
1252
+ // ans.push(callback(cur));
1253
+ // if (includeNull) {
1254
+ // cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
1255
+ // cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
1256
+ // } else {
1257
+ // cur && cur.left && _traverse(cur.left);
1258
+ // cur && cur.right && _traverse(cur.right);
1259
+ // }
1260
+ // }
1261
+ // };
1262
+ //
1263
+ // _traverse(beginRoot);
1264
+ // } else {
1265
+ // const stack: (N | null | undefined)[] = [beginRoot];
1266
+ //
1267
+ // while (stack.length > 0) {
1268
+ // const cur = stack.pop();
1269
+ // if (cur !== undefined) {
1270
+ // ans.push(callback(cur));
1271
+ // if (includeNull) {
1272
+ // cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
1273
+ // cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
1274
+ // } else {
1275
+ // cur && cur.right && stack.push(cur.right);
1276
+ // cur && cur.left && stack.push(cur.left);
1277
+ // }
1278
+ // }
1279
+ // }
1280
+ // }
1281
+ // return ans;
1282
+ // }
1298
1283
 
1299
1284
  dfs<C extends BTNCallback<N>>(
1300
1285
  callback?: C,
@@ -1304,15 +1289,7 @@ export class BinaryTree<
1304
1289
  includeNull?: false
1305
1290
  ): ReturnType<C>[];
1306
1291
 
1307
- dfs<C extends BTNCallback<N>>(
1308
- callback?: C,
1309
- pattern?: DFSOrderPattern,
1310
- beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1311
- iterationType?: IterationType,
1312
- includeNull?: undefined
1313
- ): ReturnType<C>[];
1314
-
1315
- dfs<C extends BTNCallback<N | null | undefined>>(
1292
+ dfs<C extends BTNCallback<N | null>>(
1316
1293
  callback?: C,
1317
1294
  pattern?: DFSOrderPattern,
1318
1295
  beginRoot?: KeyOrNodeOrEntry<K, V, N>,
@@ -1450,14 +1427,7 @@ export class BinaryTree<
1450
1427
  includeNull?: false
1451
1428
  ): ReturnType<C>[];
1452
1429
 
1453
- bfs<C extends BTNCallback<N>>(
1454
- callback?: C,
1455
- beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1456
- iterationType?: IterationType,
1457
- includeNull?: undefined
1458
- ): ReturnType<C>[];
1459
-
1460
- bfs<C extends BTNCallback<N | null | undefined>>(
1430
+ bfs<C extends BTNCallback<N | null>>(
1461
1431
  callback?: C,
1462
1432
  beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1463
1433
  iterationType?: IterationType,
@@ -1485,7 +1455,7 @@ export class BinaryTree<
1485
1455
  * @returns an array of values that are the result of invoking the callback function on each node in
1486
1456
  * the breadth-first traversal of a binary tree.
1487
1457
  */
1488
- bfs<C extends BTNCallback<N | null | undefined>>(
1458
+ bfs<C extends BTNCallback<N | null>>(
1489
1459
  callback: C = this._defaultOneParamCallback as C,
1490
1460
  beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1491
1461
  iterationType = this.iterationType,
@@ -1551,14 +1521,7 @@ export class BinaryTree<
1551
1521
  includeNull?: false
1552
1522
  ): ReturnType<C>[][];
1553
1523
 
1554
- listLevels<C extends BTNCallback<N>>(
1555
- callback?: C,
1556
- beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1557
- iterationType?: IterationType,
1558
- includeNull?: undefined
1559
- ): ReturnType<C>[][];
1560
-
1561
- listLevels<C extends BTNCallback<N | null | undefined>>(
1524
+ listLevels<C extends BTNCallback<N | null>>(
1562
1525
  callback?: C,
1563
1526
  beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1564
1527
  iterationType?: IterationType,
@@ -1586,7 +1549,7 @@ export class BinaryTree<
1586
1549
  * be excluded
1587
1550
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1588
1551
  */
1589
- listLevels<C extends BTNCallback<N | null | undefined>>(
1552
+ listLevels<C extends BTNCallback<N | null>>(
1590
1553
  callback: C = this._defaultOneParamCallback as C,
1591
1554
  beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1592
1555
  iterationType = this.iterationType,
@@ -1597,7 +1560,7 @@ export class BinaryTree<
1597
1560
  if (!beginRoot) return levelsNodes;
1598
1561
 
1599
1562
  if (iterationType === IterationType.RECURSIVE) {
1600
- const _recursive = (node: N | null | undefined, level: number) => {
1563
+ const _recursive = (node: N | null, level: number) => {
1601
1564
  if (!levelsNodes[level]) levelsNodes[level] = [];
1602
1565
  levelsNodes[level].push(callback(node));
1603
1566
  if (includeNull) {
@@ -1611,7 +1574,7 @@ export class BinaryTree<
1611
1574
 
1612
1575
  _recursive(beginRoot, 0);
1613
1576
  } else {
1614
- const stack: [N | null | undefined, number][] = [[beginRoot, 0]];
1577
+ const stack: [N | null, number][] = [[beginRoot, 0]];
1615
1578
 
1616
1579
  while (stack.length > 0) {
1617
1580
  const head = stack.pop()!;
@@ -13,7 +13,7 @@ import type {
13
13
  BTNodePureExemplar,
14
14
  KeyOrNodeOrEntry
15
15
  } from '../../types';
16
- import { BSTVariant, CP, IterationType } from '../../types';
16
+ import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
17
17
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { Queue } from '../queue';
@@ -171,7 +171,7 @@ export class BST<
171
171
  } else {
172
172
  node = this.createNode(key, value);
173
173
  }
174
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
174
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
175
175
  node = this.createNode(keyOrNodeOrEntry, value);
176
176
  } else {
177
177
  return;
@@ -212,16 +212,6 @@ export class BST<
212
212
  return res;
213
213
  }
214
214
 
215
- /**
216
- * The function "isNotNodeInstance" checks if a potential key is a K.
217
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
218
- * data type.
219
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
220
- */
221
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
222
- return !(potentialKey instanceof BSTNode);
223
- }
224
-
225
215
  /**
226
216
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
227
217
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
@@ -407,43 +397,6 @@ export class BST<
407
397
  return inserted;
408
398
  }
409
399
 
410
- /**
411
- * Time Complexity: O(n log n)
412
- * Space Complexity: O(n)
413
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
414
- */
415
-
416
- /**
417
- * Time Complexity: O(n log n)
418
- * Space Complexity: O(n)
419
- *
420
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
421
- * leftmost node if the comparison result is greater than.
422
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
423
- * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
424
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
425
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
426
- * the key of the leftmost node if the comparison result is greater than, and the key of the
427
- * rightmost node otherwise. If no node is found, it returns 0.
428
- */
429
- lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
430
- let current = this.ensureNode(beginRoot);
431
- if (!current) return undefined;
432
-
433
- if (this._variant === BSTVariant.STANDARD) {
434
- // For BSTVariant.MIN, find the rightmost node
435
- while (current.right !== undefined) {
436
- current = current.right;
437
- }
438
- } else {
439
- // For BSTVariant.MAX, find the leftmost node
440
- while (current.left !== undefined) {
441
- current = current.left;
442
- }
443
- }
444
- return current.key;
445
- }
446
-
447
400
  /**
448
401
  * Time Complexity: O(log n)
449
402
  * Space Complexity: O(1)
@@ -573,6 +526,157 @@ export class BST<
573
526
  return ans;
574
527
  }
575
528
 
529
+ // /**
530
+ // * The function overrides the subTreeTraverse method and returns the result of calling the super
531
+ // * method with the provided arguments.
532
+ // * @param {C} callback - The `callback` parameter is a function that will be called for each node in
533
+ // * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
534
+ // * the tree. The return type of the callback function can be any type.
535
+ // * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
536
+ // * can be either a key, a node, or an entry.
537
+ // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
538
+ // * be performed during the traversal of the subtree. It can have one of the following values:
539
+ // * @returns The method is returning an array of the return type of the callback function.
540
+ // */
541
+ // override subTreeTraverse<C extends BTNCallback<N>>(
542
+ // callback: C = this._defaultOneParamCallback as C,
543
+ // beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
544
+ // iterationType = this.iterationType
545
+ // ): ReturnType<C>[] {
546
+ // return super.subTreeTraverse(callback, beginRoot, iterationType, false);
547
+ // }
548
+
549
+ /**
550
+ * Time complexity: O(n)
551
+ * Space complexity: O(n)
552
+ */
553
+
554
+ /**
555
+ * Time complexity: O(n)
556
+ * Space complexity: O(n)
557
+ *
558
+ * The function overrides the depth-first search method and returns an array of the return types of
559
+ * the callback function.
560
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
561
+ * during the depth-first search traversal. It is an optional parameter and if not provided, a
562
+ * default callback function will be used.
563
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
564
+ * nodes are visited during the depth-first search. It can have one of the following values:
565
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
566
+ * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
567
+ * value is provided, the DFS traversal will start from the root of the tree.
568
+ * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
569
+ * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
570
+ * following values:
571
+ * @returns The method is returning an array of the return type of the callback function.
572
+ */
573
+ override dfs<C extends BTNCallback<N>>(
574
+ callback: C = this._defaultOneParamCallback as C,
575
+ pattern: DFSOrderPattern = 'in',
576
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
577
+ iterationType: IterationType = IterationType.ITERATIVE
578
+ ): ReturnType<C>[] {
579
+ return super.dfs(callback, pattern, beginRoot, iterationType, false);
580
+ }
581
+
582
+ /**
583
+ * Time complexity: O(n)
584
+ * Space complexity: O(n)
585
+ */
586
+
587
+ /**
588
+ * Time complexity: O(n)
589
+ * Space complexity: O(n)
590
+ *
591
+ * The function overrides the breadth-first search method and returns an array of the return types of
592
+ * the callback function.
593
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
594
+ * visited during the breadth-first search traversal. It is an optional parameter and if not
595
+ * provided, a default callback function will be used.
596
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
597
+ * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
598
+ * the tree is used as the starting point.
599
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
600
+ * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
601
+ * nodes are visited.
602
+ * @returns The method is returning an array of the return type of the callback function.
603
+ */
604
+ override bfs<C extends BTNCallback<N>>(
605
+ callback: C = this._defaultOneParamCallback as C,
606
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
607
+ iterationType = this.iterationType
608
+ ): ReturnType<C>[] {
609
+ return super.bfs(callback, beginRoot, iterationType, false);
610
+ }
611
+
612
+ /**
613
+ * Time complexity: O(n)
614
+ * Space complexity: O(n)
615
+ */
616
+
617
+ /**
618
+ * Time complexity: O(n)
619
+ * Space complexity: O(n)
620
+ *
621
+ * The function overrides the listLevels method and returns an array of arrays containing the return
622
+ * type of the callback function for each level of the tree.
623
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends
624
+ * `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
625
+ * during the level listing process.
626
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
627
+ * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
628
+ * provided, the root of the binary tree is used as the starting point.
629
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
630
+ * be performed on the tree. It determines the order in which the nodes are visited during the
631
+ * iteration.
632
+ * @returns The method is returning a two-dimensional array of the return type of the callback
633
+ * function.
634
+ */
635
+ override listLevels<C extends BTNCallback<N>>(
636
+ callback: C = this._defaultOneParamCallback as C,
637
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
638
+ iterationType = this.iterationType
639
+ ): ReturnType<C>[][] {
640
+ return super.listLevels(callback, beginRoot, iterationType, false);
641
+ }
642
+
643
+ /**
644
+ * Time Complexity: O(n log n)
645
+ * Space Complexity: O(n)
646
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
647
+ */
648
+
649
+ /**
650
+ * Time Complexity: O(n log n)
651
+ * Space Complexity: O(n)
652
+ *
653
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
654
+ * leftmost node if the comparison result is greater than.
655
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
656
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
657
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
658
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
659
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
660
+ * rightmost node otherwise. If no node is found, it returns 0.
661
+ */
662
+ lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
663
+ let current = this.ensureNode(beginRoot);
664
+ if (!current) return undefined;
665
+
666
+ if (this._variant === BSTVariant.STANDARD) {
667
+ // For BSTVariant.MIN, find the rightmost node
668
+ while (current.right !== undefined) {
669
+ current = current.right;
670
+ }
671
+ } else {
672
+ // For BSTVariant.MAX, find the leftmost node
673
+ while (current.left !== undefined) {
674
+ current = current.left;
675
+ }
676
+ }
677
+ return current.key;
678
+ }
679
+
576
680
  /**
577
681
  * Time Complexity: O(log n)
578
682
  * Space Complexity: O(log n)
@@ -132,7 +132,7 @@ export class RedBlackTree<
132
132
  } else {
133
133
  node = this.createNode(key, value, RBTNColor.RED);
134
134
  }
135
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
135
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
136
136
  node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
137
137
  } else {
138
138
  return;
@@ -160,16 +160,6 @@ export class RedBlackTree<
160
160
  return node instanceof RedBlackTreeNode;
161
161
  }
162
162
 
163
- /**
164
- * The function "isNotNodeInstance" checks if a potential key is a K.
165
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
166
- * data type.
167
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
168
- */
169
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
170
- return !(potentialKey instanceof RedBlackTreeNode);
171
- }
172
-
173
163
  /**
174
164
  * Time Complexity: O(log n)
175
165
  * Space Complexity: O(1)
@@ -62,7 +62,7 @@ export class TreeMultimap<
62
62
  // TODO the _count is not accurate after nodes count modified
63
63
  get count(): number {
64
64
  let sum = 0;
65
- this.subTreeTraverse(node => (sum += node.count));
65
+ this.dfs(node => (sum += node.count));
66
66
  return sum;
67
67
  }
68
68
 
@@ -111,7 +111,7 @@ export class TreeMultimap<
111
111
  } else {
112
112
  node = this.createNode(key, value, count);
113
113
  }
114
- } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
114
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
115
115
  node = this.createNode(keyOrNodeOrEntry, value, count);
116
116
  } else {
117
117
  return;
@@ -129,16 +129,6 @@ export class TreeMultimap<
129
129
  return keyOrNodeOrEntry instanceof TreeMultimapNode;
130
130
  }
131
131
 
132
- /**
133
- * The function "isNotNodeInstance" checks if a potential key is a K.
134
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
135
- * data type.
136
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
137
- */
138
- override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
139
- return !(potentialKey instanceof TreeMultimapNode);
140
- }
141
-
142
132
  /**
143
133
  * Time Complexity: O(log n)
144
134
  * Space Complexity: O(1)