stack-typed 1.49.5 → 1.49.7

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 (100) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
  2. package/dist/data-structures/binary-tree/avl-tree.js +55 -49
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
  4. package/dist/data-structures/binary-tree/binary-tree.js +194 -153
  5. package/dist/data-structures/binary-tree/bst.d.ts +83 -71
  6. package/dist/data-structures/binary-tree/bst.js +114 -91
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
  8. package/dist/data-structures/binary-tree/rb-tree.js +62 -59
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
  11. package/dist/data-structures/hash/hash-map.d.ts +24 -27
  12. package/dist/data-structures/hash/hash-map.js +35 -35
  13. package/dist/data-structures/hash/index.d.ts +0 -1
  14. package/dist/data-structures/hash/index.js +0 -1
  15. package/dist/data-structures/heap/heap.d.ts +2 -1
  16. package/dist/data-structures/heap/heap.js +13 -13
  17. package/dist/data-structures/heap/max-heap.js +1 -1
  18. package/dist/data-structures/heap/min-heap.js +1 -1
  19. package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
  20. package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
  21. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
  22. package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
  23. package/dist/data-structures/matrix/matrix.d.ts +2 -7
  24. package/dist/data-structures/matrix/matrix.js +0 -7
  25. package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
  26. package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
  27. package/dist/data-structures/priority-queue/priority-queue.js +1 -1
  28. package/dist/data-structures/queue/deque.d.ts +2 -11
  29. package/dist/data-structures/queue/deque.js +9 -13
  30. package/dist/data-structures/queue/queue.d.ts +13 -13
  31. package/dist/data-structures/queue/queue.js +29 -25
  32. package/dist/data-structures/stack/stack.js +2 -3
  33. package/dist/data-structures/trie/trie.d.ts +2 -2
  34. package/dist/data-structures/trie/trie.js +9 -5
  35. package/dist/interfaces/binary-tree.d.ts +3 -3
  36. package/dist/types/common.d.ts +3 -3
  37. package/dist/types/common.js +2 -2
  38. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
  39. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  40. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
  41. package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
  42. package/dist/types/data-structures/hash/index.d.ts +0 -1
  43. package/dist/types/data-structures/hash/index.js +0 -1
  44. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  45. package/dist/types/data-structures/linked-list/index.d.ts +1 -0
  46. package/dist/types/data-structures/linked-list/index.js +1 -0
  47. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
  48. package/dist/types/data-structures/matrix/index.d.ts +1 -0
  49. package/dist/types/data-structures/matrix/index.js +1 -0
  50. package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
  51. package/dist/types/data-structures/queue/deque.d.ts +3 -1
  52. package/dist/types/data-structures/trie/trie.d.ts +3 -1
  53. package/package.json +2 -2
  54. package/src/data-structures/binary-tree/avl-tree.ts +58 -53
  55. package/src/data-structures/binary-tree/binary-tree.ts +255 -211
  56. package/src/data-structures/binary-tree/bst.ts +126 -107
  57. package/src/data-structures/binary-tree/rb-tree.ts +66 -64
  58. package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
  59. package/src/data-structures/hash/hash-map.ts +46 -50
  60. package/src/data-structures/hash/index.ts +0 -1
  61. package/src/data-structures/heap/heap.ts +20 -19
  62. package/src/data-structures/heap/max-heap.ts +1 -1
  63. package/src/data-structures/heap/min-heap.ts +1 -1
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  65. package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
  66. package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
  67. package/src/data-structures/matrix/matrix.ts +2 -10
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  70. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  71. package/src/data-structures/queue/deque.ts +11 -15
  72. package/src/data-structures/queue/queue.ts +29 -28
  73. package/src/data-structures/stack/stack.ts +3 -6
  74. package/src/data-structures/trie/trie.ts +10 -11
  75. package/src/interfaces/binary-tree.ts +3 -3
  76. package/src/types/common.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
  78. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  79. package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
  80. package/src/types/data-structures/hash/hash-map.ts +6 -2
  81. package/src/types/data-structures/hash/index.ts +0 -1
  82. package/src/types/data-structures/heap/heap.ts +1 -1
  83. package/src/types/data-structures/linked-list/index.ts +1 -0
  84. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
  85. package/src/types/data-structures/matrix/index.ts +1 -0
  86. package/src/types/data-structures/matrix/matrix.ts +7 -1
  87. package/src/types/data-structures/queue/deque.ts +1 -1
  88. package/src/types/data-structures/trie/trie.ts +1 -1
  89. package/dist/data-structures/hash/hash-table.d.ts +0 -108
  90. package/dist/data-structures/hash/hash-table.js +0 -281
  91. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  92. package/dist/types/data-structures/hash/hash-table.js +0 -2
  93. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
  94. package/dist/types/data-structures/matrix/matrix2d.js +0 -2
  95. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
  96. package/dist/types/data-structures/matrix/vector2d.js +0 -2
  97. package/src/data-structures/hash/hash-table.ts +0 -318
  98. package/src/types/data-structures/hash/hash-table.ts +0 -1
  99. package/src/types/data-structures/matrix/matrix2d.ts +0 -1
  100. package/src/types/data-structures/matrix/vector2d.ts +0 -1
@@ -14,10 +14,9 @@ import type {
14
14
  BinaryTreePrintOptions,
15
15
  BTNCallback,
16
16
  BTNEntry,
17
- BTNExemplar,
18
- BTNKeyOrNode,
19
17
  DFSOrderPattern,
20
18
  EntryCallback,
19
+ KeyOrNodeOrEntry,
21
20
  NodeDisplayLayout
22
21
  } from '../../types';
23
22
  import { FamilyPosition, IterationType } from '../../types';
@@ -112,29 +111,25 @@ export class BinaryTree<
112
111
  iterationType = IterationType.ITERATIVE;
113
112
 
114
113
  /**
115
- * The constructor function initializes a binary tree object with optional elements and options.
116
- * @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the
117
- * elements to be added to the binary tree.
114
+ * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
115
+ * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
116
+ * nodes to be added to the binary tree.
118
117
  * @param [options] - The `options` parameter is an optional object that can contain additional
119
118
  * configuration options for the binary tree. In this case, it is of type
120
119
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
121
120
  * required.
122
121
  */
123
- constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>) {
122
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>> = [], options?: BinaryTreeOptions<K>) {
124
123
  super();
125
124
  if (options) {
126
125
  const { iterationType, extractor } = options;
127
- if (iterationType) {
128
- this.iterationType = iterationType;
129
- }
130
- if (extractor) {
131
- this._extractor = extractor;
132
- }
126
+ if (iterationType) this.iterationType = iterationType;
127
+ if (extractor) this._extractor = extractor;
133
128
  }
134
129
 
135
130
  this._size = 0;
136
131
 
137
- if (elements) this.addMany(elements);
132
+ if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
138
133
  }
139
134
 
140
135
  protected _extractor = (key: K) => Number(key);
@@ -177,30 +172,21 @@ export class BinaryTree<
177
172
  }
178
173
 
179
174
  /**
180
- * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
181
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V,N>`.
182
- * @returns a boolean value indicating whether the exemplar is an instance of the class N.
183
- */
184
- isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
185
- return exemplar instanceof BinaryTreeNode;
186
- }
187
-
188
- /**
189
- * The function `exemplarToNode` converts an exemplar object into a node object.
190
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
175
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
176
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
191
177
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
192
- * `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
178
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
193
179
  * is provided, it will be `undefined`.
194
180
  * @returns a value of type N (node), or null, or undefined.
195
181
  */
196
- exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | null | undefined {
197
- if (exemplar === undefined) return;
182
+ exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | null | undefined {
183
+ if (keyOrNodeOrEntry === undefined) return;
198
184
 
199
185
  let node: N | null | undefined;
200
- if (exemplar === null) {
186
+ if (keyOrNodeOrEntry === null) {
201
187
  node = null;
202
- } else if (this.isEntry(exemplar)) {
203
- const [key, value] = exemplar;
188
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
189
+ const [key, value] = keyOrNodeOrEntry;
204
190
  if (key === undefined) {
205
191
  return;
206
192
  } else if (key === null) {
@@ -208,24 +194,112 @@ export class BinaryTree<
208
194
  } else {
209
195
  node = this.createNode(key, value);
210
196
  }
211
- } else if (this.isNode(exemplar)) {
212
- node = exemplar;
213
- } else if (this.isNotNodeInstance(exemplar)) {
214
- node = this.createNode(exemplar, value);
197
+ } else if (this.isNode(keyOrNodeOrEntry)) {
198
+ node = keyOrNodeOrEntry;
199
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
200
+ node = this.createNode(keyOrNodeOrEntry, value);
215
201
  } else {
216
202
  return;
217
203
  }
218
204
  return node;
219
205
  }
220
206
 
207
+ /**
208
+ * Time Complexity: O(n)
209
+ * Space Complexity: O(log n)
210
+ */
211
+
212
+ /**
213
+ * Time Complexity: O(n)
214
+ * Space Complexity: O(log n)
215
+ *
216
+ * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
217
+ * key, otherwise it returns the key itself.
218
+ * @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
219
+ * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
220
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
221
+ * type of iteration to be used when searching for a node by key. It has a default value of
222
+ * `IterationType.ITERATIVE`.
223
+ * @returns either the node corresponding to the given key if it is a valid node key, or the key
224
+ * itself if it is not a valid node key.
225
+ */
226
+ ensureNode(
227
+ keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
228
+ iterationType = IterationType.ITERATIVE
229
+ ): N | null | undefined {
230
+ let res: N | null | undefined;
231
+ if (this.isRealNode(keyOrNodeOrEntry)) {
232
+ res = keyOrNodeOrEntry;
233
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
234
+ if (keyOrNodeOrEntry[0] === null) res = null;
235
+ else if (keyOrNodeOrEntry[0] !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
236
+ } else {
237
+ if (keyOrNodeOrEntry === null) res = null;
238
+ else if (keyOrNodeOrEntry !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
239
+ }
240
+ return res;
241
+ }
242
+
243
+ /**
244
+ * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
245
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,N>`.
246
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
247
+ */
248
+ isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
249
+ return keyOrNodeOrEntry instanceof BinaryTreeNode;
250
+ }
251
+
221
252
  /**
222
253
  * The function checks if a given value is an entry in a binary tree node.
223
- * @param kne - BTNExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
254
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,N> - A generic type representing a node in a binary tree. It has
224
255
  * two type parameters V and N, representing the value and node type respectively.
225
256
  * @returns a boolean value.
226
257
  */
227
- isEntry(kne: BTNExemplar<K, V, N>): kne is BTNEntry<K, V> {
228
- return Array.isArray(kne) && kne.length === 2;
258
+ isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is BTNEntry<K, V> {
259
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
260
+ }
261
+
262
+ /**
263
+ * Time complexity: O(n)
264
+ * Space complexity: O(log n)
265
+ */
266
+
267
+ /**
268
+ * The function checks if a given node is a real node by verifying if it is an instance of
269
+ * BinaryTreeNode and its key is not NaN.
270
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
271
+ * @returns a boolean value.
272
+ */
273
+ isRealNode(node: KeyOrNodeOrEntry<K, V, N>): node is N {
274
+ return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
275
+ }
276
+
277
+ /**
278
+ * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
279
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
280
+ * @returns a boolean value.
281
+ */
282
+ isNIL(node: KeyOrNodeOrEntry<K, V, N>) {
283
+ return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
284
+ }
285
+
286
+ /**
287
+ * The function checks if a given node is a real node or null.
288
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
289
+ * @returns a boolean value.
290
+ */
291
+ isNodeOrNull(node: KeyOrNodeOrEntry<K, V, N>): node is N | null {
292
+ return this.isRealNode(node) || node === null;
293
+ }
294
+
295
+ /**
296
+ * The function "isNotNodeInstance" checks if a potential key is a K.
297
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
298
+ * data type.
299
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
300
+ */
301
+ isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
302
+ return !(potentialKey instanceof BinaryTreeNode);
229
303
  }
230
304
 
231
305
  /**
@@ -243,15 +317,15 @@ export class BinaryTree<
243
317
  * @param {V} [value] - The value to be inserted into the binary tree.
244
318
  * @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
245
319
  */
246
- add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | null | undefined {
320
+ add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
247
321
  const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
248
- if (newNode === undefined) return;
322
+ if (newNode === undefined) return false;
249
323
 
250
324
  // If the tree is empty, directly set the new node as the root node
251
325
  if (!this.root) {
252
326
  this._root = newNode;
253
327
  this._size = 1;
254
- return newNode;
328
+ return true;
255
329
  }
256
330
 
257
331
  const queue = new Queue<N>([this.root]);
@@ -265,7 +339,7 @@ export class BinaryTree<
265
339
  // Check for duplicate keys when newNode is not null
266
340
  if (newNode !== null && cur.key === newNode.key) {
267
341
  this._replaceNode(cur, newNode);
268
- return newNode; // If duplicate keys are found, no insertion is performed
342
+ return true; // If duplicate keys are found, no insertion is performed
269
343
  }
270
344
 
271
345
  // Record the first possible insertion location found
@@ -290,10 +364,10 @@ export class BinaryTree<
290
364
  potentialParent.right = newNode;
291
365
  }
292
366
  this._size++;
293
- return newNode;
367
+ return true;
294
368
  }
295
369
 
296
- return undefined; // If the insertion position cannot be found, return undefined
370
+ return false; // If the insertion position cannot be found, return undefined
297
371
  }
298
372
 
299
373
  /**
@@ -306,22 +380,22 @@ export class BinaryTree<
306
380
  * Time Complexity: O(k log n) - O(k * n)
307
381
  * Space Complexity: O(1)
308
382
  *
309
- * The `addMany` function takes in a collection of nodes and an optional collection of values, and
383
+ * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
310
384
  * adds each node with its corresponding value to the data structure.
311
- * @param nodes - An iterable collection of BTNExemplar objects.
385
+ * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
312
386
  * @param [values] - An optional iterable of values that will be assigned to each node being added.
313
387
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
314
388
  */
315
- addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
389
+ addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[] {
316
390
  // TODO not sure addMany not be run multi times
317
- const inserted: (N | null | undefined)[] = [];
391
+ const inserted: boolean[] = [];
318
392
 
319
393
  let valuesIterator: Iterator<V | undefined> | undefined;
320
394
  if (values) {
321
395
  valuesIterator = values[Symbol.iterator]();
322
396
  }
323
397
 
324
- for (const kne of nodes) {
398
+ for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
325
399
  let value: V | undefined | null = undefined;
326
400
 
327
401
  if (valuesIterator) {
@@ -331,25 +405,39 @@ export class BinaryTree<
331
405
  }
332
406
  }
333
407
 
334
- inserted.push(this.add(kne, value));
408
+ inserted.push(this.add(keyOrNodeOrEntry, value));
335
409
  }
336
410
 
337
411
  return inserted;
338
412
  }
339
413
 
340
414
  /**
341
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
415
+ * Time Complexity: O(k * n)
342
416
  * Space Complexity: O(1)
417
+ * "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
343
418
  */
344
419
 
345
- refill(nodesOrKeysOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
420
+ /**
421
+ * Time Complexity: O(k * n)
422
+ * Space Complexity: O(1)
423
+ *
424
+ * The `refill` function clears the current data and adds new key-value pairs to the data structure.
425
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
426
+ * KeyOrNodeOrEntry<K, V, N>.
427
+ * @param [values] - The `values` parameter is an optional iterable that contains the values to be
428
+ * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
429
+ * the values will be associated with the corresponding keys or nodes or entries in the
430
+ * `keysOrNodesOrEntries` iterable
431
+ */
432
+ refill(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): void {
346
433
  this.clear();
347
- this.addMany(nodesOrKeysOrEntries, values);
434
+ this.addMany(keysOrNodesOrEntries, values);
348
435
  }
349
436
 
350
437
  /**
351
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
438
+ * Time Complexity: O(k * n)
352
439
  * Space Complexity: O(1)
440
+ * "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
353
441
  */
354
442
 
355
443
  delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<N>[];
@@ -433,24 +521,24 @@ export class BinaryTree<
433
521
  * Space Complexity: O(1)
434
522
  *
435
523
  * The function calculates the depth of a given node in a binary tree.
436
- * @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
524
+ * @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
437
525
  * the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
438
526
  * `undefined`.
439
527
  * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
440
528
  * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
441
529
  * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
442
- * @returns the depth of the `distNode` relative to the `beginRoot`.
530
+ * @returns the depth of the `dist` relative to the `beginRoot`.
443
531
  */
444
- getDepth(distNode: BTNKeyOrNode<K, N>, beginRoot: BTNKeyOrNode<K, N> = this.root): number {
445
- distNode = this.ensureNode(distNode);
532
+ getDepth(dist: KeyOrNodeOrEntry<K, V, N>, beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): number {
533
+ dist = this.ensureNode(dist);
446
534
  beginRoot = this.ensureNode(beginRoot);
447
535
  let depth = 0;
448
- while (distNode?.parent) {
449
- if (distNode === beginRoot) {
536
+ while (dist?.parent) {
537
+ if (dist === beginRoot) {
450
538
  return depth;
451
539
  }
452
540
  depth++;
453
- distNode = distNode.parent;
541
+ dist = dist.parent;
454
542
  }
455
543
  return depth;
456
544
  }
@@ -474,7 +562,7 @@ export class BinaryTree<
474
562
  * values:
475
563
  * @returns the height of the binary tree.
476
564
  */
477
- getHeight(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
565
+ getHeight(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, iterationType = this.iterationType): number {
478
566
  beginRoot = this.ensureNode(beginRoot);
479
567
  if (!beginRoot) return -1;
480
568
 
@@ -523,7 +611,7 @@ export class BinaryTree<
523
611
  * to calculate the minimum height of a binary tree. It can have two possible values:
524
612
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
525
613
  */
526
- getMinHeight(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
614
+ getMinHeight(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, iterationType = this.iterationType): number {
527
615
  beginRoot = this.ensureNode(beginRoot);
528
616
  if (!beginRoot) return -1;
529
617
 
@@ -583,7 +671,7 @@ export class BinaryTree<
583
671
  * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
584
672
  * @returns a boolean value.
585
673
  */
586
- isPerfectlyBalanced(beginRoot: BTNKeyOrNode<K, N> = this.root): boolean {
674
+ isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): boolean {
587
675
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
588
676
  }
589
677
 
@@ -596,7 +684,7 @@ export class BinaryTree<
596
684
  identifier: K,
597
685
  callback?: C,
598
686
  onlyOne?: boolean,
599
- beginRoot?: BTNKeyOrNode<K, N>,
687
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
600
688
  iterationType?: IterationType
601
689
  ): N[];
602
690
 
@@ -604,7 +692,7 @@ export class BinaryTree<
604
692
  identifier: N | null | undefined,
605
693
  callback?: C,
606
694
  onlyOne?: boolean,
607
- beginRoot?: BTNKeyOrNode<K, N>,
695
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
608
696
  iterationType?: IterationType
609
697
  ): N[];
610
698
 
@@ -612,7 +700,7 @@ export class BinaryTree<
612
700
  identifier: ReturnType<C>,
613
701
  callback: C,
614
702
  onlyOne?: boolean,
615
- beginRoot?: BTNKeyOrNode<K, N>,
703
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
616
704
  iterationType?: IterationType
617
705
  ): N[];
618
706
 
@@ -645,7 +733,7 @@ export class BinaryTree<
645
733
  identifier: ReturnType<C> | null | undefined,
646
734
  callback: C = this._defaultOneParamCallback as C,
647
735
  onlyOne = false,
648
- beginRoot: BTNKeyOrNode<K, N> = this.root,
736
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
649
737
  iterationType = this.iterationType
650
738
  ): N[] {
651
739
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -693,26 +781,27 @@ export class BinaryTree<
693
781
  has<C extends BTNCallback<N, K>>(
694
782
  identifier: K,
695
783
  callback?: C,
696
- beginRoot?: BTNKeyOrNode<K, N>,
784
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
697
785
  iterationType?: IterationType
698
786
  ): boolean;
699
787
 
700
788
  has<C extends BTNCallback<N, N>>(
701
789
  identifier: N | null | undefined,
702
790
  callback?: C,
703
- beginRoot?: BTNKeyOrNode<K, N>,
791
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
704
792
  iterationType?: IterationType
705
793
  ): boolean;
706
794
 
707
795
  has<C extends BTNCallback<N>>(
708
796
  identifier: ReturnType<C> | null | undefined,
709
797
  callback: C,
710
- beginRoot?: BTNKeyOrNode<K, N>,
798
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
711
799
  iterationType?: IterationType
712
800
  ): boolean;
713
801
 
714
802
  /**
715
803
  * Time Complexity: O(n)
804
+ * Space Complexity: O(log n).
716
805
  *
717
806
  * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
718
807
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
@@ -734,7 +823,7 @@ export class BinaryTree<
734
823
  has<C extends BTNCallback<N>>(
735
824
  identifier: ReturnType<C> | null | undefined,
736
825
  callback: C = this._defaultOneParamCallback as C,
737
- beginRoot: BTNKeyOrNode<K, N> = this.root,
826
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
738
827
  iterationType = this.iterationType
739
828
  ): boolean {
740
829
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -751,21 +840,21 @@ export class BinaryTree<
751
840
  getNode<C extends BTNCallback<N, K>>(
752
841
  identifier: K,
753
842
  callback?: C,
754
- beginRoot?: BTNKeyOrNode<K, N>,
843
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
755
844
  iterationType?: IterationType
756
845
  ): N | null | undefined;
757
846
 
758
847
  getNode<C extends BTNCallback<N, N>>(
759
848
  identifier: N | null | undefined,
760
849
  callback?: C,
761
- beginRoot?: BTNKeyOrNode<K, N>,
850
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
762
851
  iterationType?: IterationType
763
852
  ): N | null | undefined;
764
853
 
765
854
  getNode<C extends BTNCallback<N>>(
766
855
  identifier: ReturnType<C>,
767
856
  callback: C,
768
- beginRoot?: BTNKeyOrNode<K, N>,
857
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
769
858
  iterationType?: IterationType
770
859
  ): N | null | undefined;
771
860
 
@@ -793,7 +882,7 @@ export class BinaryTree<
793
882
  getNode<C extends BTNCallback<N>>(
794
883
  identifier: ReturnType<C> | null | undefined,
795
884
  callback: C = this._defaultOneParamCallback as C,
796
- beginRoot: BTNKeyOrNode<K, N> = this.root,
885
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
797
886
  iterationType = this.iterationType
798
887
  ): N | null | undefined {
799
888
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -846,44 +935,24 @@ export class BinaryTree<
846
935
  }
847
936
  }
848
937
 
849
- /**
850
- * Time Complexity: O(n)
851
- * Space Complexity: O(log n)
852
- */
853
-
854
- /**
855
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
856
- * key, otherwise it returns the key itself.
857
- * @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
858
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
859
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
860
- * type of iteration to be used when searching for a node by key. It has a default value of
861
- * `IterationType.ITERATIVE`.
862
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
863
- * itself if it is not a valid node key.
864
- */
865
- ensureNode(key: BTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | null | undefined {
866
- return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
867
- }
868
-
869
938
  get<C extends BTNCallback<N, K>>(
870
939
  identifier: K,
871
940
  callback?: C,
872
- beginRoot?: BTNKeyOrNode<K, N>,
941
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
873
942
  iterationType?: IterationType
874
943
  ): V | undefined;
875
944
 
876
945
  get<C extends BTNCallback<N, N>>(
877
946
  identifier: N | null | undefined,
878
947
  callback?: C,
879
- beginRoot?: BTNKeyOrNode<K, N>,
948
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
880
949
  iterationType?: IterationType
881
950
  ): V | undefined;
882
951
 
883
952
  get<C extends BTNCallback<N>>(
884
953
  identifier: ReturnType<C>,
885
954
  callback: C,
886
- beginRoot?: BTNKeyOrNode<K, N>,
955
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
887
956
  iterationType?: IterationType
888
957
  ): V | undefined;
889
958
 
@@ -912,7 +981,7 @@ export class BinaryTree<
912
981
  get<C extends BTNCallback<N>>(
913
982
  identifier: ReturnType<C> | null | undefined,
914
983
  callback: C = this._defaultOneParamCallback as C,
915
- beginRoot: BTNKeyOrNode<K, N> = this.root,
984
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
916
985
  iterationType = this.iterationType
917
986
  ): V | undefined {
918
987
  if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -922,11 +991,14 @@ export class BinaryTree<
922
991
  }
923
992
 
924
993
  /**
925
- * Time Complexity: O(n)
926
- * Space Complexity: O(log n)
994
+ * Time Complexity: O(1)
995
+ * Space Complexity: O(1)
927
996
  */
928
997
 
929
998
  /**
999
+ * Time Complexity: O(1)
1000
+ * Space Complexity: O(1)
1001
+ *
930
1002
  * Clear the binary tree, removing all nodes.
931
1003
  */
932
1004
  clear() {
@@ -935,6 +1007,14 @@ export class BinaryTree<
935
1007
  }
936
1008
 
937
1009
  /**
1010
+ * Time Complexity: O(1)
1011
+ * Space Complexity: O(1)
1012
+ */
1013
+
1014
+ /**
1015
+ * Time Complexity: O(1)
1016
+ * Space Complexity: O(1)
1017
+ *
938
1018
  * Check if the binary tree is empty.
939
1019
  * @returns {boolean} - True if the binary tree is empty, false otherwise.
940
1020
  */
@@ -956,7 +1036,7 @@ export class BinaryTree<
956
1036
  * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
957
1037
  * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
958
1038
  */
959
- getPathToRoot(beginRoot: BTNKeyOrNode<K, N>, isReverse = true): N[] {
1039
+ getPathToRoot(beginRoot: KeyOrNodeOrEntry<K, V, N>, isReverse = true): N[] {
960
1040
  // TODO to support get path through passing key
961
1041
  const result: N[] = [];
962
1042
  beginRoot = this.ensureNode(beginRoot);
@@ -975,7 +1055,7 @@ export class BinaryTree<
975
1055
 
976
1056
  /**
977
1057
  * Time Complexity: O(log n)
978
- * Space Complexity: O(log n)
1058
+ * Space Complexity: O(1)
979
1059
  */
980
1060
 
981
1061
  /**
@@ -992,7 +1072,10 @@ export class BinaryTree<
992
1072
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
993
1073
  * is no leftmost node, it returns `null` or `undefined` depending on the input.
994
1074
  */
995
- getLeftMost(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): N | null | undefined {
1075
+ getLeftMost(
1076
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1077
+ iterationType = this.iterationType
1078
+ ): N | null | undefined {
996
1079
  beginRoot = this.ensureNode(beginRoot);
997
1080
 
998
1081
  if (!beginRoot) return beginRoot;
@@ -1035,7 +1118,10 @@ export class BinaryTree<
1035
1118
  * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
1036
1119
  * is no rightmost node, it returns `null` or `undefined`, depending on the input.
1037
1120
  */
1038
- getRightMost(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): N | null | undefined {
1121
+ getRightMost(
1122
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1123
+ iterationType = this.iterationType
1124
+ ): N | null | undefined {
1039
1125
  // TODO support get right most by passing key in
1040
1126
  beginRoot = this.ensureNode(beginRoot);
1041
1127
  if (!beginRoot) return beginRoot;
@@ -1075,7 +1161,7 @@ export class BinaryTree<
1075
1161
  * possible values:
1076
1162
  * @returns a boolean value.
1077
1163
  */
1078
- isSubtreeBST(beginRoot: BTNKeyOrNode<K, N>, iterationType = this.iterationType): boolean {
1164
+ isBST(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, iterationType = this.iterationType): boolean {
1079
1165
  // TODO there is a bug
1080
1166
  beginRoot = this.ensureNode(beginRoot);
1081
1167
  if (!beginRoot) return true;
@@ -1088,69 +1174,56 @@ export class BinaryTree<
1088
1174
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
1089
1175
  };
1090
1176
 
1091
- return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
1177
+ const isStandardBST = dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
1178
+ const isInverseBST = dfs(beginRoot, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
1179
+ return isStandardBST || isInverseBST;
1092
1180
  } else {
1093
- const stack = [];
1094
- let prev = Number.MIN_SAFE_INTEGER,
1095
- curr: N | null | undefined = beginRoot;
1096
- while (curr || stack.length > 0) {
1097
- while (curr) {
1098
- stack.push(curr);
1099
- curr = curr.left;
1181
+ const checkBST = (checkMax = false) => {
1182
+ const stack = [];
1183
+ let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
1184
+ // @ts-ignore
1185
+ let curr: N | null | undefined = beginRoot;
1186
+ while (curr || stack.length > 0) {
1187
+ while (curr) {
1188
+ stack.push(curr);
1189
+ curr = curr.left;
1190
+ }
1191
+ curr = stack.pop()!;
1192
+ const numKey = this.extractor(curr.key);
1193
+ if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
1194
+ prev = numKey;
1195
+ curr = curr.right;
1100
1196
  }
1101
- curr = stack.pop()!;
1102
- const numKey = this.extractor(curr.key);
1103
- if (!curr || prev >= numKey) return false;
1104
- prev = numKey;
1105
- curr = curr.right;
1106
- }
1107
- return true;
1197
+ return true;
1198
+ };
1199
+ const isStandardBST = checkBST(false),
1200
+ isInverseBST = checkBST(true);
1201
+ return isStandardBST || isInverseBST;
1108
1202
  }
1109
1203
  }
1110
1204
 
1111
1205
  /**
1112
- * Time Complexity: O(n)
1113
- * Space Complexity: O(1)
1114
- */
1115
-
1116
- /**
1117
- * Time Complexity: O(n)
1118
- * Space Complexity: O(1)
1119
- *
1120
- * The function checks if a binary tree is a binary search tree.
1121
- * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
1122
- * be used when checking if the binary tree is a binary search tree (BST). It is an optional
1123
- * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
1124
- * expected to be
1125
- * @returns a boolean value.
1126
- */
1127
- isBST(iterationType = this.iterationType): boolean {
1128
- if (this.root === null) return true;
1129
- return this.isSubtreeBST(this.root, iterationType);
1130
- }
1131
-
1132
- /**
1133
- * Time Complexity: O(n)
1134
- * Space Complexity: O(1)
1206
+ * Time complexity: O(n)
1207
+ * Space complexity: O(log n)
1135
1208
  */
1136
1209
 
1137
1210
  subTreeTraverse<C extends BTNCallback<N>>(
1138
1211
  callback?: C,
1139
- beginRoot?: BTNKeyOrNode<K, N>,
1212
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1140
1213
  iterationType?: IterationType,
1141
1214
  includeNull?: false
1142
1215
  ): ReturnType<C>[];
1143
1216
 
1144
1217
  subTreeTraverse<C extends BTNCallback<N>>(
1145
1218
  callback?: C,
1146
- beginRoot?: BTNKeyOrNode<K, N>,
1219
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1147
1220
  iterationType?: IterationType,
1148
1221
  includeNull?: undefined
1149
1222
  ): ReturnType<C>[];
1150
1223
 
1151
1224
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1152
1225
  callback?: C,
1153
- beginRoot?: BTNKeyOrNode<K, N>,
1226
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1154
1227
  iterationType?: IterationType,
1155
1228
  includeNull?: true
1156
1229
  ): ReturnType<C>[];
@@ -1174,12 +1247,12 @@ export class BinaryTree<
1174
1247
  * whether to include null values in the traversal. If `includeNull` is set to `true`, the
1175
1248
  * traversal will include null values, otherwise it will skip them.
1176
1249
  * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
1177
- * the `callback` function on each node in the subtree. The type of the array elements is determined
1250
+ * the `callback` function on each node in the subtree. The type of the array nodes is determined
1178
1251
  * by the return type of the `callback` function.
1179
1252
  */
1180
1253
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
1181
1254
  callback: C = this._defaultOneParamCallback as C,
1182
- beginRoot: BTNKeyOrNode<K, N> = this.root,
1255
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1183
1256
  iterationType = this.iterationType,
1184
1257
  includeNull = false
1185
1258
  ): ReturnType<C>[] {
@@ -1223,53 +1296,10 @@ export class BinaryTree<
1223
1296
  return ans;
1224
1297
  }
1225
1298
 
1226
- /**
1227
- * Time complexity: O(n)
1228
- * Space complexity: O(log n)
1229
- */
1230
-
1231
- /**
1232
- * The function checks if a given node is a real node by verifying if it is an instance of
1233
- * BinaryTreeNode and its key is not NaN.
1234
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1235
- * @returns a boolean value.
1236
- */
1237
- isRealNode(node: BTNExemplar<K, V, N>): node is N {
1238
- return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
1239
- }
1240
-
1241
- /**
1242
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
1243
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1244
- * @returns a boolean value.
1245
- */
1246
- isNIL(node: BTNExemplar<K, V, N>) {
1247
- return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
1248
- }
1249
-
1250
- /**
1251
- * The function checks if a given node is a real node or null.
1252
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1253
- * @returns a boolean value.
1254
- */
1255
- isNodeOrNull(node: BTNExemplar<K, V, N>): node is N | null {
1256
- return this.isRealNode(node) || node === null;
1257
- }
1258
-
1259
- /**
1260
- * The function "isNotNodeInstance" checks if a potential key is a K.
1261
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
1262
- * data type.
1263
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
1264
- */
1265
- isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
1266
- return !(potentialKey instanceof BinaryTreeNode);
1267
- }
1268
-
1269
1299
  dfs<C extends BTNCallback<N>>(
1270
1300
  callback?: C,
1271
1301
  pattern?: DFSOrderPattern,
1272
- beginRoot?: BTNKeyOrNode<K, N>,
1302
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1273
1303
  iterationType?: IterationType,
1274
1304
  includeNull?: false
1275
1305
  ): ReturnType<C>[];
@@ -1277,7 +1307,7 @@ export class BinaryTree<
1277
1307
  dfs<C extends BTNCallback<N>>(
1278
1308
  callback?: C,
1279
1309
  pattern?: DFSOrderPattern,
1280
- beginRoot?: BTNKeyOrNode<K, N>,
1310
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1281
1311
  iterationType?: IterationType,
1282
1312
  includeNull?: undefined
1283
1313
  ): ReturnType<C>[];
@@ -1285,7 +1315,7 @@ export class BinaryTree<
1285
1315
  dfs<C extends BTNCallback<N | null | undefined>>(
1286
1316
  callback?: C,
1287
1317
  pattern?: DFSOrderPattern,
1288
- beginRoot?: BTNKeyOrNode<K, N>,
1318
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1289
1319
  iterationType?: IterationType,
1290
1320
  includeNull?: true
1291
1321
  ): ReturnType<C>[];
@@ -1316,7 +1346,7 @@ export class BinaryTree<
1316
1346
  dfs<C extends BTNCallback<N | null | undefined>>(
1317
1347
  callback: C = this._defaultOneParamCallback as C,
1318
1348
  pattern: DFSOrderPattern = 'in',
1319
- beginRoot: BTNKeyOrNode<K, N> = this.root,
1349
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1320
1350
  iterationType: IterationType = IterationType.ITERATIVE,
1321
1351
  includeNull = false
1322
1352
  ): ReturnType<C>[] {
@@ -1415,21 +1445,21 @@ export class BinaryTree<
1415
1445
 
1416
1446
  bfs<C extends BTNCallback<N>>(
1417
1447
  callback?: C,
1418
- beginRoot?: BTNKeyOrNode<K, N>,
1448
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1419
1449
  iterationType?: IterationType,
1420
1450
  includeNull?: false
1421
1451
  ): ReturnType<C>[];
1422
1452
 
1423
1453
  bfs<C extends BTNCallback<N>>(
1424
1454
  callback?: C,
1425
- beginRoot?: BTNKeyOrNode<K, N>,
1455
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1426
1456
  iterationType?: IterationType,
1427
1457
  includeNull?: undefined
1428
1458
  ): ReturnType<C>[];
1429
1459
 
1430
1460
  bfs<C extends BTNCallback<N | null | undefined>>(
1431
1461
  callback?: C,
1432
- beginRoot?: BTNKeyOrNode<K, N>,
1462
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1433
1463
  iterationType?: IterationType,
1434
1464
  includeNull?: true
1435
1465
  ): ReturnType<C>[];
@@ -1457,7 +1487,7 @@ export class BinaryTree<
1457
1487
  */
1458
1488
  bfs<C extends BTNCallback<N | null | undefined>>(
1459
1489
  callback: C = this._defaultOneParamCallback as C,
1460
- beginRoot: BTNKeyOrNode<K, N> = this.root,
1490
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1461
1491
  iterationType = this.iterationType,
1462
1492
  includeNull = false
1463
1493
  ): ReturnType<C>[] {
@@ -1516,21 +1546,21 @@ export class BinaryTree<
1516
1546
 
1517
1547
  listLevels<C extends BTNCallback<N>>(
1518
1548
  callback?: C,
1519
- beginRoot?: BTNKeyOrNode<K, N>,
1549
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1520
1550
  iterationType?: IterationType,
1521
1551
  includeNull?: false
1522
1552
  ): ReturnType<C>[][];
1523
1553
 
1524
1554
  listLevels<C extends BTNCallback<N>>(
1525
1555
  callback?: C,
1526
- beginRoot?: BTNKeyOrNode<K, N>,
1556
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1527
1557
  iterationType?: IterationType,
1528
1558
  includeNull?: undefined
1529
1559
  ): ReturnType<C>[][];
1530
1560
 
1531
1561
  listLevels<C extends BTNCallback<N | null | undefined>>(
1532
1562
  callback?: C,
1533
- beginRoot?: BTNKeyOrNode<K, N>,
1563
+ beginRoot?: KeyOrNodeOrEntry<K, V, N>,
1534
1564
  iterationType?: IterationType,
1535
1565
  includeNull?: true
1536
1566
  ): ReturnType<C>[][];
@@ -1558,7 +1588,7 @@ export class BinaryTree<
1558
1588
  */
1559
1589
  listLevels<C extends BTNCallback<N | null | undefined>>(
1560
1590
  callback: C = this._defaultOneParamCallback as C,
1561
- beginRoot: BTNKeyOrNode<K, N> = this.root,
1591
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
1562
1592
  iterationType = this.iterationType,
1563
1593
  includeNull = false
1564
1594
  ): ReturnType<C>[][] {
@@ -1655,7 +1685,13 @@ export class BinaryTree<
1655
1685
 
1656
1686
  /**
1657
1687
  * Time complexity: O(n)
1658
- * Space complexity: O(1)
1688
+ * Space complexity: O(n)
1689
+ */
1690
+
1691
+ /**
1692
+ * Time complexity: O(n)
1693
+ * Space complexity: O(n)
1694
+ *
1659
1695
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1660
1696
  * algorithm.
1661
1697
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
@@ -1668,13 +1704,13 @@ export class BinaryTree<
1668
1704
  * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1669
1705
  * the root of the tree. If no value is provided, the default value is the root of the tree.
1670
1706
  * @returns The function `morris` returns an array of values that are the result of invoking the
1671
- * `callback` function on each node in the binary tree. The type of the array elements is determined
1707
+ * `callback` function on each node in the binary tree. The type of the array nodes is determined
1672
1708
  * by the return type of the `callback` function.
1673
1709
  */
1674
1710
  morris<C extends BTNCallback<N>>(
1675
1711
  callback: C = this._defaultOneParamCallback as C,
1676
1712
  pattern: DFSOrderPattern = 'in',
1677
- beginRoot: BTNKeyOrNode<K, N> = this.root
1713
+ beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root
1678
1714
  ): ReturnType<C>[] {
1679
1715
  beginRoot = this.ensureNode(beginRoot);
1680
1716
  if (beginRoot === null) return [];
@@ -1785,8 +1821,8 @@ export class BinaryTree<
1785
1821
  * Time Complexity: O(n)
1786
1822
  * Space Complexity: O(n)
1787
1823
  *
1788
- * The `filter` function creates a new tree by iterating over the elements of the current tree and
1789
- * adding only the elements that satisfy the given predicate function.
1824
+ * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1825
+ * adding only the nodes that satisfy the given predicate function.
1790
1826
  * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1791
1827
  * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1792
1828
  * included in the filtered tree or not.
@@ -1847,13 +1883,21 @@ export class BinaryTree<
1847
1883
  //
1848
1884
 
1849
1885
  /**
1886
+ * Time Complexity: O(n)
1887
+ * Space Complexity: O(n)
1888
+ */
1889
+
1890
+ /**
1891
+ * Time Complexity: O(n)
1892
+ * Space Complexity: O(n)
1893
+ *
1850
1894
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1851
1895
  * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
1852
1896
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
1853
1897
  * following types:
1854
1898
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
1855
1899
  */
1856
- print(beginRoot: BTNKeyOrNode<K, N> = this.root, options?: BinaryTreePrintOptions): void {
1900
+ print(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root, options?: BinaryTreePrintOptions): void {
1857
1901
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1858
1902
  beginRoot = this.ensureNode(beginRoot);
1859
1903
  if (!beginRoot) return;
@@ -1985,7 +2029,7 @@ export class BinaryTree<
1985
2029
  * @param {N} destNode - The destination node to swap.
1986
2030
  * @returns {N} - The destination node after the swap.
1987
2031
  */
1988
- protected _swapProperties(srcNode: BTNKeyOrNode<K, N>, destNode: BTNKeyOrNode<K, N>): N | undefined {
2032
+ protected _swapProperties(srcNode: KeyOrNodeOrEntry<K, V, N>, destNode: KeyOrNodeOrEntry<K, V, N>): N | undefined {
1989
2033
  srcNode = this.ensureNode(srcNode);
1990
2034
  destNode = this.ensureNode(destNode);
1991
2035