min-heap-typed 1.50.1 → 1.50.3

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 (85) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +120 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
  4. package/dist/data-structures/binary-tree/avl-tree.js +101 -72
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  8. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  9. package/dist/data-structures/binary-tree/bst.d.ts +92 -79
  10. package/dist/data-structures/binary-tree/bst.js +68 -76
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
  12. package/dist/data-structures/binary-tree/rb-tree.js +152 -99
  13. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  14. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  15. package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  16. package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
  17. package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
  18. package/dist/data-structures/graph/abstract-graph.js +3 -189
  19. package/dist/data-structures/graph/directed-graph.d.ts +73 -0
  20. package/dist/data-structures/graph/directed-graph.js +131 -0
  21. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  22. package/dist/data-structures/graph/map-graph.js +14 -0
  23. package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
  24. package/dist/data-structures/graph/undirected-graph.js +151 -18
  25. package/dist/data-structures/hash/hash-map.d.ts +254 -28
  26. package/dist/data-structures/hash/hash-map.js +347 -78
  27. package/dist/data-structures/heap/heap.d.ts +95 -25
  28. package/dist/data-structures/heap/heap.js +95 -26
  29. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  30. package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
  31. package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  32. package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
  33. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  34. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  35. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  36. package/dist/data-structures/matrix/matrix.js +50 -11
  37. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  38. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  39. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  41. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  42. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  43. package/dist/data-structures/queue/deque.d.ts +139 -35
  44. package/dist/data-structures/queue/deque.js +200 -62
  45. package/dist/data-structures/queue/queue.d.ts +103 -49
  46. package/dist/data-structures/queue/queue.js +111 -49
  47. package/dist/data-structures/stack/stack.d.ts +51 -21
  48. package/dist/data-structures/stack/stack.js +58 -22
  49. package/dist/data-structures/tree/tree.d.ts +57 -3
  50. package/dist/data-structures/tree/tree.js +77 -11
  51. package/dist/data-structures/trie/trie.d.ts +135 -34
  52. package/dist/data-structures/trie/trie.js +153 -33
  53. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  54. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  55. package/dist/types/utils/utils.d.ts +1 -0
  56. package/package.json +2 -2
  57. package/src/data-structures/base/iterable-base.ts +184 -19
  58. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  59. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  60. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  61. package/src/data-structures/binary-tree/bst.ts +127 -136
  62. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  63. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  64. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  65. package/src/data-structures/graph/abstract-graph.ts +4 -211
  66. package/src/data-structures/graph/directed-graph.ts +152 -0
  67. package/src/data-structures/graph/map-graph.ts +15 -0
  68. package/src/data-structures/graph/undirected-graph.ts +171 -19
  69. package/src/data-structures/hash/hash-map.ts +389 -96
  70. package/src/data-structures/heap/heap.ts +97 -26
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  72. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  73. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  74. package/src/data-structures/matrix/matrix.ts +52 -12
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  78. package/src/data-structures/queue/deque.ts +225 -70
  79. package/src/data-structures/queue/queue.ts +118 -49
  80. package/src/data-structures/stack/stack.ts +63 -23
  81. package/src/data-structures/tree/tree.ts +89 -15
  82. package/src/data-structures/trie/trie.ts +173 -38
  83. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  84. package/src/types/data-structures/hash/hash-map.ts +4 -3
  85. 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,47 @@ 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
37
  /**
38
- * Get the left child node.
38
+ * The function returns the value of the `_left` property.
39
+ * @returns The `_left` property of the current object is being returned.
39
40
  */
40
- override get left(): N | undefined {
41
+ override get left(): NODE | undefined {
41
42
  return this._left;
42
43
  }
43
44
 
44
45
  /**
45
- * Set the left child node.
46
- * @param {N | undefined} v - The left child node.
46
+ * The function sets the left child of a node and updates the parent reference of the child.
47
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
48
+ * instance of the `NODE` class or `undefined`.
47
49
  */
48
- override set left(v: N | undefined) {
50
+ override set left(v: NODE | undefined) {
49
51
  if (v) {
50
- v.parent = this as unknown as N;
52
+ v.parent = this as unknown as NODE;
51
53
  }
52
54
  this._left = v;
53
55
  }
54
56
 
55
- protected override _right?: N;
57
+ protected override _right?: NODE;
56
58
 
57
59
  /**
58
- * Get the right child node.
60
+ * The function returns the right node of a binary tree or undefined if there is no right node.
61
+ * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
62
+ * `undefined`.
59
63
  */
60
- override get right(): N | undefined {
64
+ override get right(): NODE | undefined {
61
65
  return this._right;
62
66
  }
63
67
 
64
68
  /**
65
- * Set the right child node.
66
- * @param {N | undefined} v - The right child node.
69
+ * The function sets the right child of a node and updates the parent reference of the child.
70
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
71
+ * `NODE` object or `undefined`.
67
72
  */
68
- override set right(v: N | undefined) {
73
+ override set right(v: NODE | undefined) {
69
74
  if (v) {
70
- v.parent = this as unknown as N;
75
+ v.parent = this as unknown as NODE;
71
76
  }
72
77
  this._right = v;
73
78
  }
@@ -85,20 +90,20 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
85
90
  export class BST<
86
91
  K = any,
87
92
  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>>
93
+ NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
94
+ TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>
90
95
  >
91
- extends BinaryTree<K, V, N, TREE>
92
- implements IBinaryTree<K, V, N, TREE> {
96
+ extends BinaryTree<K, V, NODE, TREE>
97
+ implements IBinaryTree<K, V, NODE, TREE> {
93
98
  /**
94
- * This is the constructor function for a binary search tree class in TypeScript, which initializes
95
- * the tree with optional keysOrNodesOrEntries and options.
96
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
97
- * binary search tree.
99
+ * This is the constructor function for a TypeScript class that initializes a binary search tree with
100
+ * optional keys or nodes or entries and options.
101
+ * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
102
+ * to initialize the binary search tree with the provided keys, nodes, or entries.
98
103
  * @param [options] - The `options` parameter is an optional object that can contain additional
99
104
  * configuration options for the binary search tree. It can have the following properties:
100
105
  */
101
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BSTOptions<K>) {
106
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BSTOptions<K>) {
102
107
  super([], options);
103
108
 
104
109
  if (options) {
@@ -111,39 +116,48 @@ export class BST<
111
116
  if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
112
117
  }
113
118
 
114
- protected override _root?: N;
119
+ protected override _root?: NODE;
115
120
 
116
- override get root(): N | undefined {
121
+ /**
122
+ * The function returns the root node of a tree structure.
123
+ * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
124
+ */
125
+ override get root(): NODE | undefined {
117
126
  return this._root;
118
127
  }
119
128
 
120
129
  protected _variant = BSTVariant.STANDARD;
121
130
 
131
+ /**
132
+ * The function returns the value of the _variant property.
133
+ * @returns The value of the `_variant` property.
134
+ */
122
135
  get variant() {
123
136
  return this._variant;
124
137
  }
125
138
 
126
139
  /**
127
- * The function creates a new binary search tree node with the given key and value.
128
- * @param {K} key - The key parameter is the key value that will be associated with
129
- * the new node. It is used to determine the position of the node in the binary search tree.
130
- * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
131
- * represents the value associated with the node in a binary search tree.
132
- * @returns a new instance of the BSTNode class with the specified key and value.
140
+ * The function creates a new BSTNode with the given key and value and returns it.
141
+ * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
142
+ * being created.
143
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
144
+ * value associated with the key in the node being created.
145
+ * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
133
146
  */
134
- override createNode(key: K, value?: V): N {
135
- return new BSTNode<K, V, N>(key, value) as N;
147
+ override createNode(key: K, value?: V): NODE {
148
+ return new BSTNode<K, V, NODE>(key, value) as NODE;
136
149
  }
137
150
 
138
151
  /**
139
152
  * The function creates a new binary search tree with the specified options.
140
153
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
141
- * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
142
- * that defines various options for creating a binary search tree.
143
- * @returns a new instance of the BST class with the specified options.
154
+ * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
155
+ * partial object of type `BSTOptions<K>`.
156
+ * @returns a new instance of the BST class, with the provided options merged with the default
157
+ * options. The returned value is casted as TREE.
144
158
  */
145
159
  override createTree(options?: Partial<BSTOptions<K>>): TREE {
146
- return new BST<K, V, N, TREE>([], {
160
+ return new BST<K, V, NODE, TREE>([], {
147
161
  iterationType: this.iterationType,
148
162
  variant: this.variant,
149
163
  ...options
@@ -151,15 +165,15 @@ export class BST<
151
165
  }
152
166
 
153
167
  /**
154
- * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
168
+ * The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
155
169
  * otherwise it returns undefined.
156
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
170
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
157
171
  * @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.
172
+ * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
173
+ * @returns a node of type NODE or undefined.
160
174
  */
161
- override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
162
- let node: N | undefined;
175
+ override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
176
+ let node: NODE | undefined;
163
177
  if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
164
178
  return;
165
179
  } else if (this.isNode(keyOrNodeOrEntry)) {
@@ -182,7 +196,6 @@ export class BST<
182
196
  /**
183
197
  * Time Complexity: O(log n)
184
198
  * Space Complexity: O(log n)
185
- * Average case for a balanced tree. Space for the recursive call stack in the worst case.
186
199
  */
187
200
 
188
201
  /**
@@ -191,17 +204,17 @@ export class BST<
191
204
  *
192
205
  * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
193
206
  * otherwise it returns the key itself.
194
- * @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
207
+ * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
195
208
  * `undefined`.
196
209
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
197
210
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
198
- * @returns either a node object (N) or undefined.
211
+ * @returns either a node object (NODE) or undefined.
199
212
  */
200
213
  override ensureNode(
201
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
214
+ keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
202
215
  iterationType = IterationType.ITERATIVE
203
- ): N | undefined {
204
- let res: N | undefined;
216
+ ): NODE | undefined {
217
+ let res: NODE | undefined;
205
218
  if (this.isRealNode(keyOrNodeOrEntry)) {
206
219
  res = keyOrNodeOrEntry;
207
220
  } else if (this.isEntry(keyOrNodeOrEntry)) {
@@ -214,17 +227,16 @@ export class BST<
214
227
 
215
228
  /**
216
229
  * 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>`.
230
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
218
231
  * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
219
232
  */
220
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
233
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
221
234
  return keyOrNodeOrEntry instanceof BSTNode;
222
235
  }
223
236
 
224
237
  /**
225
238
  * Time Complexity: O(log n)
226
239
  * Space Complexity: O(1)
227
- * - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
228
240
  */
229
241
 
230
242
  /**
@@ -239,8 +251,8 @@ export class BST<
239
251
  * @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
240
252
  * node was not added.
241
253
  */
242
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
243
- const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
254
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
255
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
244
256
  if (newNode === undefined) return false;
245
257
 
246
258
  if (this.root === undefined) {
@@ -266,7 +278,6 @@ export class BST<
266
278
  } else if (this._compare(current.key, newNode.key) === CP.gt) {
267
279
  if (current.left === undefined) {
268
280
  current.left = newNode;
269
- newNode.parent = current;
270
281
  this._size++;
271
282
  return true;
272
283
  }
@@ -274,7 +285,6 @@ export class BST<
274
285
  } else {
275
286
  if (current.right === undefined) {
276
287
  current.right = newNode;
277
- newNode.parent = current;
278
288
  this._size++;
279
289
  return true;
280
290
  }
@@ -287,13 +297,12 @@ export class BST<
287
297
 
288
298
  /**
289
299
  * 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.
300
+ * Space Complexity: O(k + log n)
292
301
  */
293
302
 
294
303
  /**
295
304
  * Time Complexity: O(k log n)
296
- * Space Complexity: O(k)
305
+ * Space Complexity: O(k + log n)
297
306
  *
298
307
  * The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
299
308
  * balancing the tree after each addition.
@@ -309,10 +318,10 @@ export class BST<
309
318
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
310
319
  * type of iteration to use when adding multiple keys or nodes. It has a default value of
311
320
  * `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.
321
+ * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
313
322
  */
314
323
  override addMany(
315
- keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>,
324
+ keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>,
316
325
  values?: Iterable<V | undefined>,
317
326
  isBalanceAdd = true,
318
327
  iterationType = this.iterationType
@@ -334,9 +343,9 @@ export class BST<
334
343
  return inserted;
335
344
  }
336
345
 
337
- const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
346
+ const realBTNExemplars: BTNodePureExemplar<K, V, NODE>[] = [];
338
347
 
339
- const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
348
+ const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, NODE>): kve is BTNodePureExemplar<K, V, NODE> => {
340
349
  if (kve === undefined || kve === null) return false;
341
350
  return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
342
351
  };
@@ -345,7 +354,7 @@ export class BST<
345
354
  isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
346
355
  }
347
356
 
348
- let sorted: BTNodePureExemplar<K, V, N>[] = [];
357
+ let sorted: BTNodePureExemplar<K, V, NODE>[] = [];
349
358
 
350
359
  sorted = realBTNExemplars.sort((a, b) => {
351
360
  let aR: number, bR: number;
@@ -360,7 +369,7 @@ export class BST<
360
369
  return aR - bR;
361
370
  });
362
371
 
363
- const _dfs = (arr: BTNodePureExemplar<K, V, N>[]) => {
372
+ const _dfs = (arr: BTNodePureExemplar<K, V, NODE>[]) => {
364
373
  if (arr.length === 0) return;
365
374
 
366
375
  const mid = Math.floor((arr.length - 1) / 2);
@@ -413,13 +422,13 @@ export class BST<
413
422
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
414
423
  * type of iteration to use when searching for a node in the binary tree. It can have two possible
415
424
  * values:
416
- * @returns The function `getNodeByKey` returns a node (`N`) if a node with the specified key is
425
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
417
426
  * found in the binary tree. If no node is found, it returns `undefined`.
418
427
  */
419
- override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): N | undefined {
428
+ override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
420
429
  if (!this.root) return undefined;
421
430
  if (iterationType === IterationType.RECURSIVE) {
422
- const _dfs = (cur: N): N | undefined => {
431
+ const _dfs = (cur: NODE): NODE | undefined => {
423
432
  if (cur.key === key) return cur;
424
433
  if (!cur.left && !cur.right) return;
425
434
 
@@ -429,7 +438,7 @@ export class BST<
429
438
 
430
439
  return _dfs(this.root);
431
440
  } else {
432
- const queue = new Queue<N>([this.root]);
441
+ const queue = new Queue<NODE>([this.root]);
433
442
  while (queue.size > 0) {
434
443
  const cur = queue.shift();
435
444
  if (cur) {
@@ -443,46 +452,45 @@ export class BST<
443
452
 
444
453
  /**
445
454
  * 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.
455
+ * Space Complexity: O(k + log n)
448
456
  * /
449
457
 
450
458
  /**
451
459
  * Time Complexity: O(log n)
452
- * Space Complexity: O(log n)
460
+ * Space Complexity: O(k + log n)
453
461
  *
454
462
  * The function `getNodes` returns an array of nodes that match a given identifier, using either a
455
463
  * recursive or iterative approach.
456
464
  * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
457
465
  * want to search for in the nodes of the binary tree. It can be of any type that is returned by the
458
466
  * callback function `C`.
459
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
467
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
460
468
  * 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
469
+ * function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
462
470
  * @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
463
471
  * first node that matches the identifier. If set to true, the function will return an array
464
472
  * containing only the first matching node. If set to false (default), the function will continue
465
473
  * 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
474
+ * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
467
475
  * for the traversal. It can be either a key value or a node object. If it is undefined, the
468
476
  * traversal will start from the root of the tree.
469
477
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
470
478
  * performed on the binary tree. It can have two possible values:
471
- * @returns The method returns an array of nodes (`N[]`).
479
+ * @returns The method returns an array of nodes (`NODE[]`).
472
480
  */
473
- override getNodes<C extends BTNCallback<N>>(
481
+ override getNodes<C extends BTNCallback<NODE>>(
474
482
  identifier: ReturnType<C> | undefined,
475
483
  callback: C = this._defaultOneParamCallback as C,
476
484
  onlyOne = false,
477
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
485
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
478
486
  iterationType = this.iterationType
479
- ): N[] {
487
+ ): NODE[] {
480
488
  beginRoot = this.ensureNode(beginRoot);
481
489
  if (!beginRoot) return [];
482
- const ans: N[] = [];
490
+ const ans: NODE[] = [];
483
491
 
484
492
  if (iterationType === IterationType.RECURSIVE) {
485
- const _traverse = (cur: N) => {
493
+ const _traverse = (cur: NODE) => {
486
494
  const callbackResult = callback(cur);
487
495
  if (callbackResult === identifier) {
488
496
  ans.push(cur);
@@ -502,7 +510,7 @@ export class BST<
502
510
 
503
511
  _traverse(beginRoot);
504
512
  } else {
505
- const queue = new Queue<N>([beginRoot]);
513
+ const queue = new Queue<NODE>([beginRoot]);
506
514
  while (queue.size > 0) {
507
515
  const cur = queue.shift();
508
516
  if (cur) {
@@ -526,26 +534,6 @@ export class BST<
526
534
  return ans;
527
535
  }
528
536
 
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
537
  /**
550
538
  * Time complexity: O(n)
551
539
  * Space complexity: O(n)
@@ -570,10 +558,10 @@ export class BST<
570
558
  * following values:
571
559
  * @returns The method is returning an array of the return type of the callback function.
572
560
  */
573
- override dfs<C extends BTNCallback<N>>(
561
+ override dfs<C extends BTNCallback<NODE>>(
574
562
  callback: C = this._defaultOneParamCallback as C,
575
563
  pattern: DFSOrderPattern = 'in',
576
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
564
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
577
565
  iterationType: IterationType = IterationType.ITERATIVE
578
566
  ): ReturnType<C>[] {
579
567
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
@@ -601,9 +589,9 @@ export class BST<
601
589
  * nodes are visited.
602
590
  * @returns The method is returning an array of the return type of the callback function.
603
591
  */
604
- override bfs<C extends BTNCallback<N>>(
592
+ override bfs<C extends BTNCallback<NODE>>(
605
593
  callback: C = this._defaultOneParamCallback as C,
606
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
594
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
607
595
  iterationType = this.iterationType
608
596
  ): ReturnType<C>[] {
609
597
  return super.bfs(callback, beginRoot, iterationType, false);
@@ -621,7 +609,7 @@ export class BST<
621
609
  * The function overrides the listLevels method and returns an array of arrays containing the return
622
610
  * type of the callback function for each level of the tree.
623
611
  * @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
612
+ * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
625
613
  * during the level listing process.
626
614
  * @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
627
615
  * levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
@@ -632,34 +620,33 @@ export class BST<
632
620
  * @returns The method is returning a two-dimensional array of the return type of the callback
633
621
  * function.
634
622
  */
635
- override listLevels<C extends BTNCallback<N>>(
623
+ override listLevels<C extends BTNCallback<NODE>>(
636
624
  callback: C = this._defaultOneParamCallback as C,
637
- beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
625
+ beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
638
626
  iterationType = this.iterationType
639
627
  ): ReturnType<C>[][] {
640
628
  return super.listLevels(callback, beginRoot, iterationType, false);
641
629
  }
642
630
 
643
631
  /**
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.
632
+ * Time Complexity: O(log n)
633
+ * Space Complexity: O(1)
647
634
  */
648
635
 
649
636
  /**
650
- * Time Complexity: O(n log n)
651
- * Space Complexity: O(n)
637
+ * Time Complexity: O(log n)
638
+ * Space Complexity: O(1)
652
639
  *
653
640
  * The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
654
641
  * 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
642
+ * @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
643
+ * type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
657
644
  * the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
658
645
  * @returns the key of the rightmost node in the binary tree if the comparison result is less than,
659
646
  * the key of the leftmost node if the comparison result is greater than, and the key of the
660
647
  * rightmost node otherwise. If no node is found, it returns 0.
661
648
  */
662
- lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
649
+ lastKey(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): K | undefined {
663
650
  let current = this.ensureNode(beginRoot);
664
651
  if (!current) return undefined;
665
652
 
@@ -680,7 +667,6 @@ export class BST<
680
667
  /**
681
668
  * Time Complexity: O(log n)
682
669
  * 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
670
  */
685
671
 
686
672
  /**
@@ -691,12 +677,12 @@ export class BST<
691
677
  * are either lesser or greater than a target node, depending on the specified comparison type.
692
678
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
693
679
  * 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.
680
+ * parameter of type `NODE` (the node type) and returns a value of any type.
695
681
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
696
682
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
697
683
  * `CP`, which is a custom type representing the comparison operator. The possible values for
698
684
  * `lesserOrGreater` are
699
- * @param {K | N | undefined} targetNode - The `targetNode` parameter represents the node in the
685
+ * @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
700
686
  * binary tree that you want to traverse from. It can be specified either by its key, by the node
701
687
  * object itself, or it can be left undefined to start the traversal from the root of the tree.
702
688
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -704,21 +690,21 @@ export class BST<
704
690
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
705
691
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
706
692
  */
707
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(
693
+ lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
708
694
  callback: C = this._defaultOneParamCallback as C,
709
695
  lesserOrGreater: CP = CP.lt,
710
- targetNode: KeyOrNodeOrEntry<K, V, N> = this.root,
696
+ targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
711
697
  iterationType = this.iterationType
712
698
  ): ReturnType<C>[] {
713
699
  targetNode = this.ensureNode(targetNode);
714
- const ans: ReturnType<BTNCallback<N>>[] = [];
700
+ const ans: ReturnType<BTNCallback<NODE>>[] = [];
715
701
  if (!targetNode) return ans;
716
702
  if (!this.root) return ans;
717
703
 
718
704
  const targetKey = targetNode.key;
719
705
 
720
706
  if (iterationType === IterationType.RECURSIVE) {
721
- const _traverse = (cur: N) => {
707
+ const _traverse = (cur: NODE) => {
722
708
  const compared = this._compare(cur.key, targetKey);
723
709
  if (compared === lesserOrGreater) ans.push(callback(cur));
724
710
 
@@ -730,7 +716,7 @@ export class BST<
730
716
  _traverse(this.root);
731
717
  return ans;
732
718
  } else {
733
- const queue = new Queue<N>([this.root]);
719
+ const queue = new Queue<NODE>([this.root]);
734
720
  while (queue.size > 0) {
735
721
  const cur = queue.shift();
736
722
  if (cur) {
@@ -809,8 +795,8 @@ export class BST<
809
795
  */
810
796
 
811
797
  /**
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.
798
+ * Time Complexity: O(n)
799
+ * Space Complexity: O(log n)
814
800
  */
815
801
 
816
802
  /**
@@ -828,7 +814,7 @@ export class BST<
828
814
  let balanced = true;
829
815
 
830
816
  if (iterationType === IterationType.RECURSIVE) {
831
- const _height = (cur: N | undefined): number => {
817
+ const _height = (cur: NODE | undefined): number => {
832
818
  if (!cur) return 0;
833
819
  const leftHeight = _height(cur.left),
834
820
  rightHeight = _height(cur.right);
@@ -837,10 +823,10 @@ export class BST<
837
823
  };
838
824
  _height(this.root);
839
825
  } 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();
826
+ const stack: NODE[] = [];
827
+ let node: NODE | undefined = this.root,
828
+ last: NODE | undefined = undefined;
829
+ const depths: Map<NODE, number> = new Map();
844
830
 
845
831
  while (stack.length > 0 || node) {
846
832
  if (node) {
@@ -866,7 +852,12 @@ export class BST<
866
852
  return balanced;
867
853
  }
868
854
 
869
- protected _setRoot(v: N | undefined) {
855
+ /**
856
+ * The function sets the root property of an object and updates the parent property of the new root.
857
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
858
+ * can either be an object of type `NODE` or it can be `undefined`.
859
+ */
860
+ protected _setRoot(v: NODE | undefined) {
870
861
  if (v) {
871
862
  v.parent = undefined;
872
863
  }