data-structure-typed 1.49.9 → 1.50.1

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 (79) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +192 -200
  3. package/benchmark/report.html +13 -13
  4. package/benchmark/report.json +158 -152
  5. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
  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 +5 -22
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
  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 +89 -27
  13. package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
  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 +0 -7
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  21. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
  25. package/dist/cjs/data-structures/hash/hash-map.js +6 -6
  26. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  28. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  30. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  31. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  32. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  33. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  34. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
  35. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
  36. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
  37. package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
  38. package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
  39. package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
  40. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
  41. package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
  42. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
  43. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
  44. package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
  45. package/dist/mjs/data-structures/hash/hash-map.js +15 -15
  46. package/dist/umd/data-structure-typed.js +143 -153
  47. package/dist/umd/data-structure-typed.min.js +2 -2
  48. package/dist/umd/data-structure-typed.min.js.map +1 -1
  49. package/package.json +12 -41
  50. package/src/data-structures/base/iterable-base.ts +6 -6
  51. package/src/data-structures/binary-tree/avl-tree.ts +7 -18
  52. package/src/data-structures/binary-tree/binary-tree.ts +111 -149
  53. package/src/data-structures/binary-tree/bst.ts +159 -56
  54. package/src/data-structures/binary-tree/rb-tree.ts +7 -18
  55. package/src/data-structures/binary-tree/tree-multimap.ts +8 -19
  56. package/src/data-structures/graph/abstract-graph.ts +14 -15
  57. package/src/data-structures/graph/directed-graph.ts +6 -7
  58. package/src/data-structures/graph/undirected-graph.ts +6 -7
  59. package/src/data-structures/hash/hash-map.ts +23 -22
  60. package/src/data-structures/heap/heap.ts +1 -1
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  62. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  63. package/src/data-structures/queue/deque.ts +3 -3
  64. package/src/data-structures/queue/queue.ts +1 -1
  65. package/src/data-structures/stack/stack.ts +1 -1
  66. package/src/data-structures/trie/trie.ts +1 -1
  67. package/src/types/data-structures/graph/abstract-graph.ts +8 -8
  68. package/test/integration/all-in-one.test.ts +1 -1
  69. package/test/integration/index.html +2 -2
  70. package/test/performance/data-structures/queue/deque.test.ts +8 -2
  71. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  72. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
  73. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
  74. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
  75. package/test/unit/data-structures/graph/abstract-graph.test.ts +2 -1
  76. package/test/unit/data-structures/hash/hash-map.test.ts +4 -2
  77. package/test/unit/data-structures/queue/deque.test.ts +8 -6
  78. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  79. package/test/unit/unrestricted-interconversion.test.ts +143 -10
@@ -135,13 +135,6 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
135
135
  * @returns a boolean value.
136
136
  */
137
137
  isNodeOrNull(node: KeyOrNodeOrEntry<K, V, N>): node is N | null;
138
- /**
139
- * The function "isNotNodeInstance" checks if a potential key is a K.
140
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
141
- * data type.
142
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
143
- */
144
- isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
145
138
  /**
146
139
  * Time Complexity O(log n) - O(n)
147
140
  * Space Complexity O(1)
@@ -344,7 +337,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
344
337
  *
345
338
  * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
346
339
  * structure, with the option to reverse the order of the nodes.
347
- * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
340
+ * @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
348
341
  * starting node from which you want to find the path to the root. It can be of type `K`, `N`,
349
342
  * `null`, or `undefined`.
350
343
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
@@ -352,7 +345,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
352
345
  * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
353
346
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
354
347
  */
355
- getPathToRoot(beginRoot: KeyOrNodeOrEntry<K, V, N>, isReverse?: boolean): N[];
348
+ getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, N>, isReverse?: boolean): N[];
356
349
  /**
357
350
  * Time Complexity: O(log n)
358
351
  * Space Complexity: O(1)
@@ -409,30 +402,20 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
409
402
  * @returns a boolean value.
410
403
  */
411
404
  isBST(beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): boolean;
412
- /**
413
- * Time complexity: O(n)
414
- * Space complexity: O(log n)
415
- */
416
- subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
417
- subTreeTraverse<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
418
- subTreeTraverse<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
419
405
  dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
420
- dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
421
- dfs<C extends BTNCallback<N | null | undefined>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
406
+ dfs<C extends BTNCallback<N | null>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
422
407
  /**
423
408
  * Time complexity: O(n)
424
409
  * Space complexity: O(n)
425
410
  */
426
411
  bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
427
- bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[];
428
- bfs<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
412
+ bfs<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
429
413
  /**
430
414
  * Time complexity: O(n)
431
415
  * Space complexity: O(n)
432
416
  */
433
417
  listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
434
- listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: undefined): ReturnType<C>[][];
435
- listLevels<C extends BTNCallback<N | null | undefined>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
418
+ listLevels<C extends BTNCallback<N | null>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
436
419
  /**
437
420
  * Time Complexity: O(log n)
438
421
  * Space Complexity: O(1)
@@ -152,7 +152,7 @@ export class BinaryTree extends IterableEntryBase {
152
152
  else if (this.isNode(keyOrNodeOrEntry)) {
153
153
  node = keyOrNodeOrEntry;
154
154
  }
155
- else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
155
+ else if (!this.isNode(keyOrNodeOrEntry)) {
156
156
  node = this.createNode(keyOrNodeOrEntry, value);
157
157
  }
158
158
  else {
@@ -243,15 +243,6 @@ export class BinaryTree extends IterableEntryBase {
243
243
  isNodeOrNull(node) {
244
244
  return this.isRealNode(node) || node === null;
245
245
  }
246
- /**
247
- * The function "isNotNodeInstance" checks if a potential key is a K.
248
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
249
- * data type.
250
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
251
- */
252
- isNotNodeInstance(potentialKey) {
253
- return !(potentialKey instanceof BinaryTreeNode);
254
- }
255
246
  /**
256
247
  * Time Complexity O(log n) - O(n)
257
248
  * Space Complexity O(1)
@@ -810,7 +801,7 @@ export class BinaryTree extends IterableEntryBase {
810
801
  *
811
802
  * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
812
803
  * structure, with the option to reverse the order of the nodes.
813
- * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
804
+ * @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
814
805
  * starting node from which you want to find the path to the root. It can be of type `K`, `N`,
815
806
  * `null`, or `undefined`.
816
807
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
@@ -818,19 +809,19 @@ export class BinaryTree extends IterableEntryBase {
818
809
  * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
819
810
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
820
811
  */
821
- getPathToRoot(beginRoot, isReverse = true) {
812
+ getPathToRoot(beginNode, isReverse = true) {
822
813
  // TODO to support get path through passing key
823
814
  const result = [];
824
- beginRoot = this.ensureNode(beginRoot);
825
- if (!beginRoot)
815
+ beginNode = this.ensureNode(beginNode);
816
+ if (!beginNode)
826
817
  return result;
827
- while (beginRoot.parent) {
818
+ while (beginNode.parent) {
828
819
  // Array.push + Array.reverse is more efficient than Array.unshift
829
820
  // TODO may consider using Deque, so far this is not the performance bottleneck
830
- result.push(beginRoot);
831
- beginRoot = beginRoot.parent;
821
+ result.push(beginNode);
822
+ beginNode = beginNode.parent;
832
823
  }
833
- result.push(beginRoot);
824
+ result.push(beginNode);
834
825
  return isReverse ? result.reverse() : result;
835
826
  }
836
827
  /**
@@ -973,68 +964,6 @@ export class BinaryTree extends IterableEntryBase {
973
964
  return isStandardBST || isInverseBST;
974
965
  }
975
966
  }
976
- /**
977
- * Time complexity: O(n)
978
- * Space complexity: O(log n)
979
- *
980
- * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
981
- * node, either recursively or iteratively.
982
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
983
- * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
984
- * returns a value of any type.
985
- * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
986
- * starting node or key from which the subtree traversal should begin. It can be of type `K`,
987
- * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
988
- * the default value.
989
- * @param iterationType - The `iterationType` parameter determines the type of traversal to be
990
- * performed on the subtree. It can have two possible values:
991
- * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
992
- * whether to include null values in the traversal. If `includeNull` is set to `true`, the
993
- * traversal will include null values, otherwise it will skip them.
994
- * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
995
- * the `callback` function on each node in the subtree. The type of the array nodes is determined
996
- * by the return type of the `callback` function.
997
- */
998
- subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
999
- beginRoot = this.ensureNode(beginRoot);
1000
- const ans = [];
1001
- if (!beginRoot)
1002
- return ans;
1003
- if (iterationType === IterationType.RECURSIVE) {
1004
- const _traverse = (cur) => {
1005
- if (cur !== undefined) {
1006
- ans.push(callback(cur));
1007
- if (includeNull) {
1008
- cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
1009
- cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
1010
- }
1011
- else {
1012
- cur && cur.left && _traverse(cur.left);
1013
- cur && cur.right && _traverse(cur.right);
1014
- }
1015
- }
1016
- };
1017
- _traverse(beginRoot);
1018
- }
1019
- else {
1020
- const stack = [beginRoot];
1021
- while (stack.length > 0) {
1022
- const cur = stack.pop();
1023
- if (cur !== undefined) {
1024
- ans.push(callback(cur));
1025
- if (includeNull) {
1026
- cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
1027
- cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
1028
- }
1029
- else {
1030
- cur && cur.right && stack.push(cur.right);
1031
- cur && cur.left && stack.push(cur.left);
1032
- }
1033
- }
1034
- }
1035
- }
1036
- return ans;
1037
- }
1038
967
  /**
1039
968
  * Time complexity: O(n)
1040
969
  * Space complexity: O(n)
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
9
- import { BSTVariant, CP, IterationType } from '../../types';
9
+ import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, N> {
@@ -100,13 +100,6 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
100
100
  * @returns either a node object (N) or undefined.
101
101
  */
102
102
  ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined;
103
- /**
104
- * The function "isNotNodeInstance" checks if a potential key is a K.
105
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
106
- * data type.
107
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
108
- */
109
- isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
110
103
  /**
111
104
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
112
105
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
@@ -157,25 +150,6 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
157
150
  * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
158
151
  */
159
152
  addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
160
- /**
161
- * Time Complexity: O(n log n)
162
- * Space Complexity: O(n)
163
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
164
- */
165
- /**
166
- * Time Complexity: O(n log n)
167
- * Space Complexity: O(n)
168
- *
169
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
170
- * leftmost node if the comparison result is greater than.
171
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
172
- * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
173
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
174
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
175
- * the key of the leftmost node if the comparison result is greater than, and the key of the
176
- * rightmost node otherwise. If no node is found, it returns 0.
177
- */
178
- lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
179
153
  /**
180
154
  * Time Complexity: O(log n)
181
155
  * Space Complexity: O(1)
@@ -225,6 +199,94 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
225
199
  * @returns The method returns an array of nodes (`N[]`).
226
200
  */
227
201
  getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[];
202
+ /**
203
+ * Time complexity: O(n)
204
+ * Space complexity: O(n)
205
+ */
206
+ /**
207
+ * Time complexity: O(n)
208
+ * Space complexity: O(n)
209
+ *
210
+ * The function overrides the depth-first search method and returns an array of the return types of
211
+ * the callback function.
212
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
213
+ * during the depth-first search traversal. It is an optional parameter and if not provided, a
214
+ * default callback function will be used.
215
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
216
+ * nodes are visited during the depth-first search. It can have one of the following values:
217
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
218
+ * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
219
+ * value is provided, the DFS traversal will start from the root of the tree.
220
+ * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
221
+ * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
222
+ * following values:
223
+ * @returns The method is returning an array of the return type of the callback function.
224
+ */
225
+ dfs<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
226
+ /**
227
+ * Time complexity: O(n)
228
+ * Space complexity: O(n)
229
+ */
230
+ /**
231
+ * Time complexity: O(n)
232
+ * Space complexity: O(n)
233
+ *
234
+ * The function overrides the breadth-first search method and returns an array of the return types of
235
+ * the callback function.
236
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
237
+ * visited during the breadth-first search traversal. It is an optional parameter and if not
238
+ * provided, a default callback function will be used.
239
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
240
+ * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
241
+ * the tree is used as the starting point.
242
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
243
+ * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
244
+ * nodes are visited.
245
+ * @returns The method is returning an array of the return type of the callback function.
246
+ */
247
+ bfs<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
248
+ /**
249
+ * Time complexity: O(n)
250
+ * Space complexity: O(n)
251
+ */
252
+ /**
253
+ * Time complexity: O(n)
254
+ * Space complexity: O(n)
255
+ *
256
+ * The function overrides the listLevels method and returns an array of arrays containing the return
257
+ * type of the callback function for each level of the tree.
258
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends
259
+ * `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
260
+ * during the level listing process.
261
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
262
+ * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
263
+ * provided, the root of the binary tree is used as the starting point.
264
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
265
+ * be performed on the tree. It determines the order in which the nodes are visited during the
266
+ * iteration.
267
+ * @returns The method is returning a two-dimensional array of the return type of the callback
268
+ * function.
269
+ */
270
+ listLevels<C extends BTNCallback<N>>(callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[][];
271
+ /**
272
+ * Time Complexity: O(n log n)
273
+ * Space Complexity: O(n)
274
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
275
+ */
276
+ /**
277
+ * Time Complexity: O(n log n)
278
+ * Space Complexity: O(n)
279
+ *
280
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
281
+ * leftmost node if the comparison result is greater than.
282
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
283
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
284
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
285
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
286
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
287
+ * rightmost node otherwise. If no node is found, it returns 0.
288
+ */
289
+ lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
228
290
  /**
229
291
  * Time Complexity: O(log n)
230
292
  * Space Complexity: O(log n)
@@ -131,7 +131,7 @@ export class BST extends BinaryTree {
131
131
  node = this.createNode(key, value);
132
132
  }
133
133
  }
134
- else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
134
+ else if (!this.isNode(keyOrNodeOrEntry)) {
135
135
  node = this.createNode(keyOrNodeOrEntry, value);
136
136
  }
137
137
  else {
@@ -171,15 +171,6 @@ export class BST extends BinaryTree {
171
171
  }
172
172
  return res;
173
173
  }
174
- /**
175
- * The function "isNotNodeInstance" checks if a potential key is a K.
176
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
177
- * data type.
178
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
179
- */
180
- isNotNodeInstance(potentialKey) {
181
- return !(potentialKey instanceof BSTNode);
182
- }
183
174
  /**
184
175
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
185
176
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
@@ -347,42 +338,6 @@ export class BST extends BinaryTree {
347
338
  }
348
339
  return inserted;
349
340
  }
350
- /**
351
- * Time Complexity: O(n log n)
352
- * Space Complexity: O(n)
353
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
354
- */
355
- /**
356
- * Time Complexity: O(n log n)
357
- * Space Complexity: O(n)
358
- *
359
- * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
360
- * leftmost node if the comparison result is greater than.
361
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
362
- * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
363
- * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
364
- * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
365
- * the key of the leftmost node if the comparison result is greater than, and the key of the
366
- * rightmost node otherwise. If no node is found, it returns 0.
367
- */
368
- lastKey(beginRoot = this.root) {
369
- let current = this.ensureNode(beginRoot);
370
- if (!current)
371
- return undefined;
372
- if (this._variant === BSTVariant.STANDARD) {
373
- // For BSTVariant.MIN, find the rightmost node
374
- while (current.right !== undefined) {
375
- current = current.right;
376
- }
377
- }
378
- else {
379
- // For BSTVariant.MAX, find the leftmost node
380
- while (current.left !== undefined) {
381
- current = current.left;
382
- }
383
- }
384
- return current.key;
385
- }
386
341
  /**
387
342
  * Time Complexity: O(log n)
388
343
  * Space Complexity: O(1)
@@ -517,6 +472,136 @@ export class BST extends BinaryTree {
517
472
  }
518
473
  return ans;
519
474
  }
475
+ // /**
476
+ // * The function overrides the subTreeTraverse method and returns the result of calling the super
477
+ // * method with the provided arguments.
478
+ // * @param {C} callback - The `callback` parameter is a function that will be called for each node in
479
+ // * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
480
+ // * the tree. The return type of the callback function can be any type.
481
+ // * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
482
+ // * can be either a key, a node, or an entry.
483
+ // * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
484
+ // * be performed during the traversal of the subtree. It can have one of the following values:
485
+ // * @returns The method is returning an array of the return type of the callback function.
486
+ // */
487
+ // override subTreeTraverse<C extends BTNCallback<N>>(
488
+ // callback: C = this._defaultOneParamCallback as C,
489
+ // beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
490
+ // iterationType = this.iterationType
491
+ // ): ReturnType<C>[] {
492
+ // return super.subTreeTraverse(callback, beginRoot, iterationType, false);
493
+ // }
494
+ /**
495
+ * Time complexity: O(n)
496
+ * Space complexity: O(n)
497
+ */
498
+ /**
499
+ * Time complexity: O(n)
500
+ * Space complexity: O(n)
501
+ *
502
+ * The function overrides the depth-first search method and returns an array of the return types of
503
+ * the callback function.
504
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
505
+ * during the depth-first search traversal. It is an optional parameter and if not provided, a
506
+ * default callback function will be used.
507
+ * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter specifies the order in which the
508
+ * nodes are visited during the depth-first search. It can have one of the following values:
509
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for the
510
+ * Depth-First Search (DFS) traversal. It can be either a key, a node, or an entry in the tree. If no
511
+ * value is provided, the DFS traversal will start from the root of the tree.
512
+ * @param {IterationType} iterationType - The `iterationType` parameter specifies the type of
513
+ * iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the
514
+ * following values:
515
+ * @returns The method is returning an array of the return type of the callback function.
516
+ */
517
+ dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE) {
518
+ return super.dfs(callback, pattern, beginRoot, iterationType, false);
519
+ }
520
+ /**
521
+ * Time complexity: O(n)
522
+ * Space complexity: O(n)
523
+ */
524
+ /**
525
+ * Time complexity: O(n)
526
+ * Space complexity: O(n)
527
+ *
528
+ * The function overrides the breadth-first search method and returns an array of the return types of
529
+ * the callback function.
530
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
531
+ * visited during the breadth-first search traversal. It is an optional parameter and if not
532
+ * provided, a default callback function will be used.
533
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the breadth-first search
534
+ * traversal. It can be either a key, a node, or an entry in the tree. If not specified, the root of
535
+ * the tree is used as the starting point.
536
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
537
+ * be performed during the breadth-first search (BFS) traversal. It determines the order in which the
538
+ * nodes are visited.
539
+ * @returns The method is returning an array of the return type of the callback function.
540
+ */
541
+ bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
542
+ return super.bfs(callback, beginRoot, iterationType, false);
543
+ }
544
+ /**
545
+ * Time complexity: O(n)
546
+ * Space complexity: O(n)
547
+ */
548
+ /**
549
+ * Time complexity: O(n)
550
+ * Space complexity: O(n)
551
+ *
552
+ * The function overrides the listLevels method and returns an array of arrays containing the return
553
+ * type of the callback function for each level of the tree.
554
+ * @param {C} callback - The `callback` parameter is a generic type `C` that extends
555
+ * `BTNCallback<N>`. It represents a callback function that will be called for each node in the tree
556
+ * during the level listing process.
557
+ * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
558
+ * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
559
+ * provided, the root of the binary tree is used as the starting point.
560
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
561
+ * be performed on the tree. It determines the order in which the nodes are visited during the
562
+ * iteration.
563
+ * @returns The method is returning a two-dimensional array of the return type of the callback
564
+ * function.
565
+ */
566
+ listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
567
+ return super.listLevels(callback, beginRoot, iterationType, false);
568
+ }
569
+ /**
570
+ * Time Complexity: O(n log n)
571
+ * Space Complexity: O(n)
572
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
573
+ */
574
+ /**
575
+ * Time Complexity: O(n log n)
576
+ * Space Complexity: O(n)
577
+ *
578
+ * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
579
+ * leftmost node if the comparison result is greater than.
580
+ * @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
581
+ * type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
582
+ * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
583
+ * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
584
+ * the key of the leftmost node if the comparison result is greater than, and the key of the
585
+ * rightmost node otherwise. If no node is found, it returns 0.
586
+ */
587
+ lastKey(beginRoot = this.root) {
588
+ let current = this.ensureNode(beginRoot);
589
+ if (!current)
590
+ return undefined;
591
+ if (this._variant === BSTVariant.STANDARD) {
592
+ // For BSTVariant.MIN, find the rightmost node
593
+ while (current.right !== undefined) {
594
+ current = current.right;
595
+ }
596
+ }
597
+ else {
598
+ // For BSTVariant.MAX, find the leftmost node
599
+ while (current.left !== undefined) {
600
+ current = current.left;
601
+ }
602
+ }
603
+ return current.key;
604
+ }
520
605
  /**
521
606
  * Time Complexity: O(log n)
522
607
  * Space Complexity: O(log n)
@@ -79,13 +79,6 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
79
79
  * Space Complexity: O(1)
80
80
  */
81
81
  isRealNode(node: N | undefined): node is N;
82
- /**
83
- * The function "isNotNodeInstance" checks if a potential key is a K.
84
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
85
- * data type.
86
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
87
- */
88
- isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
89
82
  /**
90
83
  * Time Complexity: O(log n)
91
84
  * Space Complexity: O(1)
@@ -101,7 +101,7 @@ export class RedBlackTree extends BST {
101
101
  node = this.createNode(key, value, RBTNColor.RED);
102
102
  }
103
103
  }
104
- else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
104
+ else if (!this.isNode(keyOrNodeOrEntry)) {
105
105
  node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
106
106
  }
107
107
  else {
@@ -127,15 +127,6 @@ export class RedBlackTree extends BST {
127
127
  return false;
128
128
  return node instanceof RedBlackTreeNode;
129
129
  }
130
- /**
131
- * The function "isNotNodeInstance" checks if a potential key is a K.
132
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
133
- * data type.
134
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
135
- */
136
- isNotNodeInstance(potentialKey) {
137
- return !(potentialKey instanceof RedBlackTreeNode);
138
- }
139
130
  /**
140
131
  * Time Complexity: O(log n)
141
132
  * Space Complexity: O(1)
@@ -60,13 +60,6 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
60
60
  * class.
61
61
  */
62
62
  isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
63
- /**
64
- * The function "isNotNodeInstance" checks if a potential key is a K.
65
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
66
- * data type.
67
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
68
- */
69
- isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
70
63
  /**
71
64
  * Time Complexity: O(log n)
72
65
  * Space Complexity: O(1)
@@ -30,7 +30,7 @@ export class TreeMultimap extends AVLTree {
30
30
  // TODO the _count is not accurate after nodes count modified
31
31
  get count() {
32
32
  let sum = 0;
33
- this.subTreeTraverse(node => (sum += node.count));
33
+ this.dfs(node => (sum += node.count));
34
34
  return sum;
35
35
  }
36
36
  /**
@@ -80,7 +80,7 @@ export class TreeMultimap extends AVLTree {
80
80
  node = this.createNode(key, value, count);
81
81
  }
82
82
  }
83
- else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
83
+ else if (!this.isNode(keyOrNodeOrEntry)) {
84
84
  node = this.createNode(keyOrNodeOrEntry, value, count);
85
85
  }
86
86
  else {
@@ -97,15 +97,6 @@ export class TreeMultimap extends AVLTree {
97
97
  isNode(keyOrNodeOrEntry) {
98
98
  return keyOrNodeOrEntry instanceof TreeMultimapNode;
99
99
  }
100
- /**
101
- * The function "isNotNodeInstance" checks if a potential key is a K.
102
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
103
- * data type.
104
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
105
- */
106
- isNotNodeInstance(potentialKey) {
107
- return !(potentialKey instanceof TreeMultimapNode);
108
- }
109
100
  /**
110
101
  * Time Complexity: O(log n)
111
102
  * Space Complexity: O(1)
@@ -18,9 +18,6 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
18
18
  [key: string]: HashMapStoreItem<K, V>;
19
19
  };
20
20
  protected _objMap: Map<object, V>;
21
- protected _toEntryFn: (rawElement: R) => [K, V];
22
- get toEntryFn(): (rawElement: R) => [K, V];
23
- isEntry(rawElement: any): rawElement is [K, V];
24
21
  /**
25
22
  * The constructor function initializes a HashMap object with an optional initial collection and
26
23
  * options.
@@ -29,8 +26,11 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
29
26
  * @param [options] - The `options` parameter is an optional object that can contain two properties:
30
27
  */
31
28
  constructor(rawCollection?: Iterable<R>, options?: HashMapOptions<K, V, R>);
29
+ protected _toEntryFn: (rawElement: R) => [K, V];
30
+ get toEntryFn(): (rawElement: R) => [K, V];
32
31
  protected _size: number;
33
32
  get size(): number;
33
+ isEntry(rawElement: any): rawElement is [K, V];
34
34
  isEmpty(): boolean;
35
35
  clear(): void;
36
36
  /**