min-heap-typed 1.50.1 → 1.50.2

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 (67) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +114 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
  4. package/dist/data-structures/binary-tree/avl-tree.js +68 -71
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  6. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  7. package/dist/data-structures/binary-tree/bst.d.ts +54 -74
  8. package/dist/data-structures/binary-tree/bst.js +30 -71
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
  10. package/dist/data-structures/binary-tree/rb-tree.js +84 -89
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  12. package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
  13. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  14. package/dist/data-structures/graph/abstract-graph.js +3 -0
  15. package/dist/data-structures/graph/directed-graph.d.ts +14 -0
  16. package/dist/data-structures/graph/directed-graph.js +26 -0
  17. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  18. package/dist/data-structures/graph/map-graph.js +14 -0
  19. package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
  20. package/dist/data-structures/graph/undirected-graph.js +25 -0
  21. package/dist/data-structures/hash/hash-map.d.ts +121 -15
  22. package/dist/data-structures/hash/hash-map.js +160 -25
  23. package/dist/data-structures/heap/heap.d.ts +66 -6
  24. package/dist/data-structures/heap/heap.js +66 -6
  25. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  26. package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  28. package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  30. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  31. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  32. package/dist/data-structures/matrix/matrix.js +50 -11
  33. package/dist/data-structures/queue/deque.d.ts +49 -19
  34. package/dist/data-structures/queue/deque.js +101 -47
  35. package/dist/data-structures/queue/queue.d.ts +39 -5
  36. package/dist/data-structures/queue/queue.js +47 -5
  37. package/dist/data-structures/stack/stack.d.ts +16 -0
  38. package/dist/data-structures/stack/stack.js +22 -0
  39. package/dist/data-structures/trie/trie.d.ts +38 -1
  40. package/dist/data-structures/trie/trie.js +41 -0
  41. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  42. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  43. package/dist/types/utils/utils.d.ts +1 -0
  44. package/package.json +2 -2
  45. package/src/data-structures/base/iterable-base.ts +172 -19
  46. package/src/data-structures/binary-tree/avl-tree.ts +97 -97
  47. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  48. package/src/data-structures/binary-tree/bst.ts +89 -131
  49. package/src/data-structures/binary-tree/rb-tree.ts +127 -155
  50. package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
  51. package/src/data-structures/graph/abstract-graph.ts +4 -0
  52. package/src/data-structures/graph/directed-graph.ts +30 -0
  53. package/src/data-structures/graph/map-graph.ts +15 -0
  54. package/src/data-structures/graph/undirected-graph.ts +28 -0
  55. package/src/data-structures/hash/hash-map.ts +175 -34
  56. package/src/data-structures/heap/heap.ts +66 -6
  57. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
  58. package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
  59. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  60. package/src/data-structures/matrix/matrix.ts +52 -12
  61. package/src/data-structures/queue/deque.ts +108 -49
  62. package/src/data-structures/queue/queue.ts +51 -5
  63. package/src/data-structures/stack/stack.ts +24 -0
  64. package/src/data-structures/trie/trie.ts +45 -1
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  66. package/src/types/data-structures/hash/hash-map.ts +4 -3
  67. package/src/types/utils/utils.ts +2 -0
@@ -18,12 +18,12 @@ import { BinaryTree, BinaryTreeNode } from './binary-tree';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { Queue } from '../queue';
20
20
 
21
- export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNested<K, V>> extends BinaryTreeNode<
21
+ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<
22
22
  K,
23
23
  V,
24
- N
24
+ NODE
25
25
  > {
26
- override parent?: N;
26
+ override parent?: NODE;
27
27
 
28
28
  constructor(key: K, value?: V) {
29
29
  super(key, value);
@@ -32,42 +32,28 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
32
32
  this._right = undefined;
33
33
  }
34
34
 
35
- protected override _left?: N;
35
+ protected override _left?: NODE;
36
36
 
37
- /**
38
- * Get the left child node.
39
- */
40
- override get left(): N | undefined {
37
+ override get left(): NODE | undefined {
41
38
  return this._left;
42
39
  }
43
40
 
44
- /**
45
- * Set the left child node.
46
- * @param {N | undefined} v - The left child node.
47
- */
48
- override set left(v: N | undefined) {
41
+ override set left(v: NODE | undefined) {
49
42
  if (v) {
50
- v.parent = this as unknown as N;
43
+ v.parent = this as unknown as NODE;
51
44
  }
52
45
  this._left = v;
53
46
  }
54
47
 
55
- protected override _right?: N;
48
+ protected override _right?: NODE;
56
49
 
57
- /**
58
- * Get the right child node.
59
- */
60
- override get right(): N | undefined {
50
+ override get right(): NODE | undefined {
61
51
  return this._right;
62
52
  }
63
53
 
64
- /**
65
- * Set the right child node.
66
- * @param {N | undefined} v - The right child node.
67
- */
68
- override set right(v: N | undefined) {
54
+ override set right(v: NODE | undefined) {
69
55
  if (v) {
70
- v.parent = this as unknown as N;
56
+ v.parent = this as unknown as NODE;
71
57
  }
72
58
  this._right = v;
73
59
  }
@@ -85,11 +71,11 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
85
71
  export class BST<
86
72
  K = any,
87
73
  V = any,
88
- N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>,
89
- TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>
74
+ NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
75
+ TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>
90
76
  >
91
- extends BinaryTree<K, V, N, TREE>
92
- implements IBinaryTree<K, V, N, TREE> {
77
+ extends BinaryTree<K, V, NODE, TREE>
78
+ implements IBinaryTree<K, V, NODE, TREE> {
93
79
  /**
94
80
  * This is the constructor function for a binary search tree class in TypeScript, which initializes
95
81
  * the tree with optional keysOrNodesOrEntries and options.
@@ -98,7 +84,7 @@ export class BST<
98
84
  * @param [options] - The `options` parameter is an optional object that can contain additional
99
85
  * configuration options for the binary search tree. It can have the following properties:
100
86
  */
101
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BSTOptions<K>) {
87
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BSTOptions<K>) {
102
88
  super([], options);
103
89
 
104
90
  if (options) {
@@ -111,9 +97,9 @@ export class BST<
111
97
  if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
112
98
  }
113
99
 
114
- protected override _root?: N;
100
+ protected override _root?: NODE;
115
101
 
116
- override get root(): N | undefined {
102
+ override get root(): NODE | undefined {
117
103
  return this._root;
118
104
  }
119
105
 
@@ -131,8 +117,8 @@ export class BST<
131
117
  * represents the value associated with the node in a binary search tree.
132
118
  * @returns a new instance of the BSTNode class with the specified key and value.
133
119
  */
134
- override createNode(key: K, value?: V): N {
135
- return new BSTNode<K, V, N>(key, value) as N;
120
+ override createNode(key: K, value?: V): NODE {
121
+ return new BSTNode<K, V, NODE>(key, value) as NODE;
136
122
  }
137
123
 
138
124
  /**
@@ -143,7 +129,7 @@ export class BST<
143
129
  * @returns a new instance of the BST class with the specified options.
144
130
  */
145
131
  override createTree(options?: Partial<BSTOptions<K>>): TREE {
146
- return new BST<K, V, N, TREE>([], {
132
+ return new BST<K, V, NODE, TREE>([], {
147
133
  iterationType: this.iterationType,
148
134
  variant: this.variant,
149
135
  ...options
@@ -151,15 +137,15 @@ export class BST<
151
137
  }
152
138
 
153
139
  /**
154
- * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
140
+ * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
155
141
  * otherwise it returns undefined.
156
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
142
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
157
143
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
158
- * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
159
- * @returns a node of type N or undefined.
144
+ * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
145
+ * @returns a node of type NODE or undefined.
160
146
  */
161
- override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
162
- let node: N | undefined;
147
+ override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
148
+ let node: NODE | undefined;
163
149
  if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
164
150
  return;
165
151
  } else if (this.isNode(keyOrNodeOrEntry)) {
@@ -182,7 +168,6 @@ export class BST<
182
168
  /**
183
169
  * Time Complexity: O(log n)
184
170
  * Space Complexity: O(log n)
185
- * Average case for a balanced tree. Space for the recursive call stack in the worst case.
186
171
  */
187
172
 
188
173
  /**
@@ -191,17 +176,17 @@ export class BST<
191
176
  *
192
177
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
193
178
  * otherwise it returns the key itself.
194
- * @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
179
+ * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
195
180
  * `undefined`.
196
181
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
197
182
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
198
- * @returns either a node object (N) or undefined.
183
+ * @returns either a node object (NODE) or undefined.
199
184
  */
200
185
  override ensureNode(
201
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
186
+ keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
202
187
  iterationType = IterationType.ITERATIVE
203
- ): N | undefined {
204
- let res: N | undefined;
188
+ ): NODE | undefined {
189
+ let res: NODE | undefined;
205
190
  if (this.isRealNode(keyOrNodeOrEntry)) {
206
191
  res = keyOrNodeOrEntry;
207
192
  } else if (this.isEntry(keyOrNodeOrEntry)) {
@@ -214,17 +199,16 @@ export class BST<
214
199
 
215
200
  /**
216
201
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
217
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
202
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
218
203
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
219
204
  */
220
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
205
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
221
206
  return keyOrNodeOrEntry instanceof BSTNode;
222
207
  }
223
208
 
224
209
  /**
225
210
  * Time Complexity: O(log n)
226
211
  * Space Complexity: O(1)
227
- * - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
228
212
  */
229
213
 
230
214
  /**
@@ -239,8 +223,8 @@ export class BST<
239
223
  * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
240
224
  * node was not added.
241
225
  */
242
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
243
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
226
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
227
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
244
228
  if (newNode === undefined) return false;
245
229
 
246
230
  if (this.root === undefined) {
@@ -266,7 +250,6 @@ export class BST<
266
250
  } else if (this._compare(current.key, newNode.key) === CP.gt) {
267
251
  if (current.left === undefined) {
268
252
  current.left = newNode;
269
- newNode.parent = current;
270
253
  this._size++;
271
254
  return true;
272
255
  }
@@ -274,7 +257,6 @@ export class BST<
274
257
  } else {
275
258
  if (current.right === undefined) {
276
259
  current.right = newNode;
277
- newNode.parent = current;
278
260
  this._size++;
279
261
  return true;
280
262
  }
@@ -287,13 +269,12 @@ export class BST<
287
269
 
288
270
  /**
289
271
  * Time Complexity: O(k log n)
290
- * Space Complexity: O(k)
291
- * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
272
+ * Space Complexity: O(k + log n)
292
273
  */
293
274
 
294
275
  /**
295
276
  * Time Complexity: O(k log n)
296
- * Space Complexity: O(k)
277
+ * Space Complexity: O(k + log n)
297
278
  *
298
279
  * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
299
280
  * balancing the tree after each addition.
@@ -309,10 +290,10 @@ export class BST<
309
290
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
310
291
  * type of iteration to use when adding multiple keys or nodes. It has a default value of
311
292
  * `this.iterationType`, which suggests that it is a property of the current object.
312
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
293
+ * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
313
294
  */
314
295
  override addMany(
315
- keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>,
296
+ keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>,
316
297
  values?: Iterable<V | undefined>,
317
298
  isBalanceAdd = true,
318
299
  iterationType = this.iterationType
@@ -334,9 +315,9 @@ export class BST<
334
315
  return inserted;
335
316
  }
336
317
 
337
- const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
318
+ const realBTNExemplars: BTNodePureExemplar<K, V, NODE>[] = [];
338
319
 
339
- const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
320
+ const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, NODE>): kve is BTNodePureExemplar<K, V, NODE> => {
340
321
  if (kve === undefined || kve === null) return false;
341
322
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
342
323
  };
@@ -345,7 +326,7 @@ export class BST<
345
326
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
346
327
  }
347
328
 
348
- let sorted: BTNodePureExemplar<K, V, N>[] = [];
329
+ let sorted: BTNodePureExemplar<K, V, NODE>[] = [];
349
330
 
350
331
  sorted = realBTNExemplars.sort((a, b) => {
351
332
  let aR: number, bR: number;
@@ -360,7 +341,7 @@ export class BST<
360
341
  return aR - bR;
361
342
  });
362
343
 
363
- const _dfs = (arr: BTNodePureExemplar<K, V, N>[]) => {
344
+ const _dfs = (arr: BTNodePureExemplar<K, V, NODE>[]) => {
364
345
  if (arr.length === 0) return;
365
346
 
366
347
  const mid = Math.floor((arr.length - 1) / 2);
@@ -413,13 +394,13 @@ export class BST<
413
394
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
414
395
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
415
396
  * values:
416
- * @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
397
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
417
398
  * found in the binary tree. If no node is found, it returns `undefined`.
418
399
  */
419
- override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): N | undefined {
400
+ override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
420
401
  if (!this.root) return undefined;
421
402
  if (iterationType === IterationType.RECURSIVE) {
422
- const _dfs = (cur: N): N | undefined => {
403
+ const _dfs = (cur: NODE): NODE | undefined => {
423
404
  if (cur.key === key) return cur;
424
405
  if (!cur.left && !cur.right) return;
425
406
 
@@ -429,7 +410,7 @@ export class BST<
429
410
 
430
411
  return _dfs(this.root);
431
412
  } else {
432
- const queue = new Queue<N>([this.root]);
413
+ const queue = new Queue<NODE>([this.root]);
433
414
  while (queue.size > 0) {
434
415
  const cur = queue.shift();
435
416
  if (cur) {
@@ -443,46 +424,45 @@ export class BST<
443
424
 
444
425
  /**
445
426
  * Time Complexity: O(log n)
446
- * Space Complexity: O(log n)
447
- * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
427
+ * Space Complexity: O(k + log n)
448
428
  * /
449
429
 
450
430
  /**
451
431
  * Time Complexity: O(log n)
452
- * Space Complexity: O(log n)
432
+ * Space Complexity: O(k + log n)
453
433
  *
454
434
  * The function `getNodes` returns an array of nodes that match a given identifier, using either a
455
435
  * recursive or iterative approach.
456
436
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
457
437
  * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
458
438
  * callback function `C`.
459
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
439
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
460
440
  * argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
461
- * function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
441
+ * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
462
442
  * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
463
443
  * first node that matches the identifier. If set to true, the function will return an array
464
444
  * containing only the first matching node. If set to false (default), the function will continue
465
445
  * searching for all nodes that match the identifier and return an array containing
466
- * @param {K | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
446
+ * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
467
447
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
468
448
  * traversal will start from the root of the tree.
469
449
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
470
450
  * performed on the binary tree. It can have two possible values:
471
- * @returns The method returns an array of nodes (`N[]`).
451
+ * @returns The method returns an array of nodes (`NODE[]`).
472
452
  */
473
- override getNodes<C extends BTNCallback<N>>(
453
+ override getNodes<C extends BTNCallback<NODE>>(
474
454
  identifier: ReturnType<C> | undefined,
475
455
  callback: C = this._defaultOneParamCallback as C,
476
456
  onlyOne = false,
477
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
457
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
478
458
  iterationType = this.iterationType
479
- ): N[] {
459
+ ): NODE[] {
480
460
  beginRoot = this.ensureNode(beginRoot);
481
461
  if (!beginRoot) return [];
482
- const ans: N[] = [];
462
+ const ans: NODE[] = [];
483
463
 
484
464
  if (iterationType === IterationType.RECURSIVE) {
485
- const _traverse = (cur: N) => {
465
+ const _traverse = (cur: NODE) => {
486
466
  const callbackResult = callback(cur);
487
467
  if (callbackResult === identifier) {
488
468
  ans.push(cur);
@@ -502,7 +482,7 @@ export class BST<
502
482
 
503
483
  _traverse(beginRoot);
504
484
  } else {
505
- const queue = new Queue<N>([beginRoot]);
485
+ const queue = new Queue<NODE>([beginRoot]);
506
486
  while (queue.size > 0) {
507
487
  const cur = queue.shift();
508
488
  if (cur) {
@@ -526,26 +506,6 @@ export class BST<
526
506
  return ans;
527
507
  }
528
508
 
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
509
  /**
550
510
  * Time complexity: O(n)
551
511
  * Space complexity: O(n)
@@ -570,10 +530,10 @@ export class BST<
570
530
  * following values:
571
531
  * @returns The method is returning an array of the return type of the callback function.
572
532
  */
573
- override dfs<C extends BTNCallback<N>>(
533
+ override dfs<C extends BTNCallback<NODE>>(
574
534
  callback: C = this._defaultOneParamCallback as C,
575
535
  pattern: DFSOrderPattern = 'in',
576
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
536
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
577
537
  iterationType: IterationType = IterationType.ITERATIVE
578
538
  ): ReturnType<C>[] {
579
539
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
@@ -601,9 +561,9 @@ export class BST<
601
561
  * nodes are visited.
602
562
  * @returns The method is returning an array of the return type of the callback function.
603
563
  */
604
- override bfs<C extends BTNCallback<N>>(
564
+ override bfs<C extends BTNCallback<NODE>>(
605
565
  callback: C = this._defaultOneParamCallback as C,
606
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
566
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
607
567
  iterationType = this.iterationType
608
568
  ): ReturnType<C>[] {
609
569
  return super.bfs(callback, beginRoot, iterationType, false);
@@ -621,7 +581,7 @@ export class BST<
621
581
  * The function overrides the listLevels method and returns an array of arrays containing the return
622
582
  * type of the callback function for each level of the tree.
623
583
  * @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
584
+ * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
625
585
  * during the level listing process.
626
586
  * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
627
587
  * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
@@ -632,34 +592,33 @@ export class BST<
632
592
  * @returns The method is returning a two-dimensional array of the return type of the callback
633
593
  * function.
634
594
  */
635
- override listLevels<C extends BTNCallback<N>>(
595
+ override listLevels<C extends BTNCallback<NODE>>(
636
596
  callback: C = this._defaultOneParamCallback as C,
637
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
597
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
638
598
  iterationType = this.iterationType
639
599
  ): ReturnType<C>[][] {
640
600
  return super.listLevels(callback, beginRoot, iterationType, false);
641
601
  }
642
602
 
643
603
  /**
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.
604
+ * Time Complexity: O(log n)
605
+ * Space Complexity: O(1)
647
606
  */
648
607
 
649
608
  /**
650
- * Time Complexity: O(n log n)
651
- * Space Complexity: O(n)
609
+ * Time Complexity: O(log n)
610
+ * Space Complexity: O(1)
652
611
  *
653
612
  * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
654
613
  * 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
614
+ * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
615
+ * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
657
616
  * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
658
617
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
659
618
  * the key of the leftmost node if the comparison result is greater than, and the key of the
660
619
  * rightmost node otherwise. If no node is found, it returns 0.
661
620
  */
662
- lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
621
+ lastKey(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): K | undefined {
663
622
  let current = this.ensureNode(beginRoot);
664
623
  if (!current) return undefined;
665
624
 
@@ -680,7 +639,6 @@ export class BST<
680
639
  /**
681
640
  * Time Complexity: O(log n)
682
641
  * Space Complexity: O(log n)
683
- * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
684
642
  */
685
643
 
686
644
  /**
@@ -691,12 +649,12 @@ export class BST<
691
649
  * are either lesser or greater than a target node, depending on the specified comparison type.
692
650
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
693
651
  * that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
694
- * parameter of type `N` (the node type) and returns a value of any type.
652
+ * parameter of type `NODE` (the node type) and returns a value of any type.
695
653
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
696
654
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
697
655
  * `CP`, which is a custom type representing the comparison operator. The possible values for
698
656
  * `lesserOrGreater` are
699
- * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
657
+ * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
700
658
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
701
659
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
702
660
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -704,21 +662,21 @@ export class BST<
704
662
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
705
663
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
706
664
  */
707
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(
665
+ lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
708
666
  callback: C = this._defaultOneParamCallback as C,
709
667
  lesserOrGreater: CP = CP.lt,
710
- targetNode: KeyOrNodeOrEntry<K, V, N> = this.root,
668
+ targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
711
669
  iterationType = this.iterationType
712
670
  ): ReturnType<C>[] {
713
671
  targetNode = this.ensureNode(targetNode);
714
- const ans: ReturnType<BTNCallback<N>>[] = [];
672
+ const ans: ReturnType<BTNCallback<NODE>>[] = [];
715
673
  if (!targetNode) return ans;
716
674
  if (!this.root) return ans;
717
675
 
718
676
  const targetKey = targetNode.key;
719
677
 
720
678
  if (iterationType === IterationType.RECURSIVE) {
721
- const _traverse = (cur: N) => {
679
+ const _traverse = (cur: NODE) => {
722
680
  const compared = this._compare(cur.key, targetKey);
723
681
  if (compared === lesserOrGreater) ans.push(callback(cur));
724
682
 
@@ -730,7 +688,7 @@ export class BST<
730
688
  _traverse(this.root);
731
689
  return ans;
732
690
  } else {
733
- const queue = new Queue<N>([this.root]);
691
+ const queue = new Queue<NODE>([this.root]);
734
692
  while (queue.size > 0) {
735
693
  const cur = queue.shift();
736
694
  if (cur) {
@@ -809,8 +767,8 @@ export class BST<
809
767
  */
810
768
 
811
769
  /**
812
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
813
- * Space Complexity: O(n) - Additional space is required for the sorted array.
770
+ * Time Complexity: O(n)
771
+ * Space Complexity: O(log n)
814
772
  */
815
773
 
816
774
  /**
@@ -828,7 +786,7 @@ export class BST<
828
786
  let balanced = true;
829
787
 
830
788
  if (iterationType === IterationType.RECURSIVE) {
831
- const _height = (cur: N | undefined): number => {
789
+ const _height = (cur: NODE | undefined): number => {
832
790
  if (!cur) return 0;
833
791
  const leftHeight = _height(cur.left),
834
792
  rightHeight = _height(cur.right);
@@ -837,10 +795,10 @@ export class BST<
837
795
  };
838
796
  _height(this.root);
839
797
  } else {
840
- const stack: N[] = [];
841
- let node: N | undefined = this.root,
842
- last: N | undefined = undefined;
843
- const depths: Map<N, number> = new Map();
798
+ const stack: NODE[] = [];
799
+ let node: NODE | undefined = this.root,
800
+ last: NODE | undefined = undefined;
801
+ const depths: Map<NODE, number> = new Map();
844
802
 
845
803
  while (stack.length > 0 || node) {
846
804
  if (node) {
@@ -866,7 +824,7 @@ export class BST<
866
824
  return balanced;
867
825
  }
868
826
 
869
- protected _setRoot(v: N | undefined) {
827
+ protected _setRoot(v: NODE | undefined) {
870
828
  if (v) {
871
829
  v.parent = undefined;
872
830
  }