linked-list-typed 1.51.7 → 1.51.9

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 (55) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
  4. package/dist/data-structures/binary-tree/avl-tree.js +90 -71
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
  6. package/dist/data-structures/binary-tree/binary-tree.js +492 -392
  7. package/dist/data-structures/binary-tree/bst.d.ts +204 -251
  8. package/dist/data-structures/binary-tree/bst.js +256 -358
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
  10. package/dist/data-structures/binary-tree/rb-tree.js +111 -119
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
  13. package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
  14. package/dist/data-structures/graph/abstract-graph.js +10 -15
  15. package/dist/data-structures/hash/hash-map.d.ts +31 -38
  16. package/dist/data-structures/hash/hash-map.js +40 -55
  17. package/dist/data-structures/heap/heap.d.ts +1 -3
  18. package/dist/data-structures/queue/deque.d.ts +2 -3
  19. package/dist/data-structures/queue/deque.js +2 -3
  20. package/dist/data-structures/trie/trie.d.ts +1 -1
  21. package/dist/data-structures/trie/trie.js +1 -1
  22. package/dist/interfaces/binary-tree.d.ts +7 -7
  23. package/dist/types/common.d.ts +2 -3
  24. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
  25. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
  26. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
  27. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
  28. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
  29. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
  30. package/dist/types/utils/utils.d.ts +10 -1
  31. package/dist/utils/utils.d.ts +2 -1
  32. package/dist/utils/utils.js +27 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
  35. package/src/data-structures/binary-tree/avl-tree.ts +109 -80
  36. package/src/data-structures/binary-tree/binary-tree.ts +556 -433
  37. package/src/data-structures/binary-tree/bst.ts +286 -375
  38. package/src/data-structures/binary-tree/rb-tree.ts +132 -125
  39. package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
  40. package/src/data-structures/graph/abstract-graph.ts +10 -10
  41. package/src/data-structures/hash/hash-map.ts +42 -49
  42. package/src/data-structures/heap/heap.ts +1 -1
  43. package/src/data-structures/queue/deque.ts +2 -2
  44. package/src/data-structures/queue/queue.ts +1 -1
  45. package/src/data-structures/trie/trie.ts +2 -2
  46. package/src/interfaces/binary-tree.ts +11 -9
  47. package/src/types/common.ts +2 -3
  48. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
  49. package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
  50. package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
  51. package/src/types/data-structures/binary-tree/bst.ts +6 -5
  52. package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
  53. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
  54. package/src/types/utils/utils.ts +14 -1
  55. package/src/utils/utils.ts +20 -1
@@ -14,12 +14,14 @@ import type {
14
14
  BinaryTreePrintOptions,
15
15
  BTNCallback,
16
16
  BTNEntry,
17
+ Comparable,
17
18
  DFSOrderPattern,
18
19
  EntryCallback,
20
+ FamilyPosition,
21
+ IterationType,
19
22
  KeyOrNodeOrEntry,
20
23
  NodeDisplayLayout
21
24
  } from '../../types';
22
- import { FamilyPosition, IterationType } from '../../types';
23
25
  import { IBinaryTree } from '../../interfaces';
24
26
  import { trampoline } from '../../utils';
25
27
  import { Queue } from '../queue';
@@ -31,7 +33,7 @@ import { IterableEntryBase } from '../base';
31
33
  * @template NODE - The type of the family relationship in the binary tree.
32
34
  */
33
35
  export class BinaryTreeNode<
34
- K = any,
36
+ K extends Comparable,
35
37
  V = any,
36
38
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
37
39
  > {
@@ -129,45 +131,37 @@ export class BinaryTreeNode<
129
131
  */
130
132
 
131
133
  export class BinaryTree<
132
- K = any,
134
+ K extends Comparable,
133
135
  V = any,
136
+ R = BTNEntry<K, V>,
134
137
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
135
- TREE extends BinaryTree<K, V, NODE, TREE> = BinaryTree<K, V, NODE, BinaryTreeNested<K, V, NODE>>
138
+ TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
136
139
  >
137
140
  extends IterableEntryBase<K, V | undefined>
138
- implements IBinaryTree<K, V, NODE, TREE> {
141
+ implements IBinaryTree<K, V, R, NODE, TREE> {
139
142
  iterationType: IterationType = 'ITERATIVE';
140
143
 
141
144
  /**
142
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
143
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
145
+ * The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
146
+ * @param [keysOrNodesOrEntriesOrRawElements] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
144
147
  * nodes to be added to the binary tree.
145
148
  * @param [options] - The `options` parameter is an optional object that can contain additional
146
149
  * configuration options for the binary tree. In this case, it is of type
147
150
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
148
151
  * required.
149
152
  */
150
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BinaryTreeOptions<K>) {
153
+ constructor(
154
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
155
+ options?: BinaryTreeOptions<K, V, R>
156
+ ) {
151
157
  super();
152
158
  if (options) {
153
- const { iterationType, extractor } = options;
159
+ const { iterationType, toEntryFn } = options;
154
160
  if (iterationType) this.iterationType = iterationType;
155
- if (extractor) this._extractor = extractor;
161
+ if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
156
162
  }
157
163
 
158
- this._size = 0;
159
-
160
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
161
- }
162
-
163
- protected _extractor = (key: K) => (typeof key === 'number' ? key : Number(key));
164
-
165
- /**
166
- * The function returns the value of the `_extractor` property.
167
- * @returns The `_extractor` property is being returned.
168
- */
169
- get extractor() {
170
- return this._extractor;
164
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
171
165
  }
172
166
 
173
167
  protected _root?: NODE | null;
@@ -181,7 +175,7 @@ export class BinaryTree<
181
175
  return this._root;
182
176
  }
183
177
 
184
- protected _size: number;
178
+ protected _size: number = 0;
185
179
 
186
180
  /**
187
181
  * The function returns the size of an object.
@@ -201,6 +195,16 @@ export class BinaryTree<
201
195
  return this._NIL;
202
196
  }
203
197
 
198
+ protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
199
+
200
+ /**
201
+ * The function returns the value of the _toEntryFn property.
202
+ * @returns The function being returned is `this._toEntryFn`.
203
+ */
204
+ get toEntryFn() {
205
+ return this._toEntryFn;
206
+ }
207
+
204
208
  /**
205
209
  * Creates a new instance of BinaryTreeNode with the given key and value.
206
210
  * @param {K} key - The key for the new node.
@@ -218,41 +222,46 @@ export class BinaryTree<
218
222
  * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
219
223
  * @returns a new instance of a binary tree.
220
224
  */
221
- createTree(options?: Partial<BinaryTreeOptions<K>>): TREE {
222
- return new BinaryTree<K, V, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
225
+ createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE {
226
+ return new BinaryTree<K, V, R, NODE, TREE>([], { iterationType: this.iterationType, ...options }) as TREE;
223
227
  }
224
228
 
225
229
  /**
226
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
227
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
230
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
231
+ * into a node object.
232
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
233
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
228
234
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
229
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
230
- * is provided, it will be `undefined`.
231
- * @returns a value of type NODE (node), or null, or undefined.
232
- */
233
- keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | null | undefined {
234
- if (keyOrNodeOrEntry === undefined) return;
235
-
236
- let node: NODE | null | undefined;
237
- if (keyOrNodeOrEntry === null) {
238
- node = null;
239
- } else if (this.isEntry(keyOrNodeOrEntry)) {
240
- const [key, value] = keyOrNodeOrEntry;
241
- if (key === undefined) {
242
- return;
243
- } else if (key === null) {
244
- node = null;
245
- } else {
246
- node = this.createNode(key, value);
247
- }
248
- } else if (this.isNode(keyOrNodeOrEntry)) {
249
- node = keyOrNodeOrEntry;
250
- } else if (!this.isNode(keyOrNodeOrEntry)) {
251
- node = this.createNode(keyOrNodeOrEntry, value);
252
- } else {
253
- return;
235
+ * `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
236
+ * key-value pair. If provided, it will be used to create a node with the specified key and value.
237
+ * @returns The function `keyValueOrEntryOrRawElementToNode` returns either a `NODE` object, `null`,
238
+ * or `undefined`.
239
+ */
240
+ keyValueOrEntryOrRawElementToNode(
241
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
242
+ value?: V
243
+ ): NODE | null | undefined {
244
+ if (keyOrNodeOrEntryOrRawElement === undefined) return;
245
+ if (keyOrNodeOrEntryOrRawElement === null) return null;
246
+
247
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
248
+
249
+ if (this.toEntryFn) {
250
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
251
+ if (key) return this.createNode(key, entryValue ?? value);
252
+ else return;
253
+ }
254
+
255
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
256
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
257
+ if (key === undefined) return;
258
+ else if (key === null) return null;
259
+ else return this.createNode(key, value);
254
260
  }
255
- return node;
261
+
262
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value);
263
+
264
+ return;
256
265
  }
257
266
 
258
267
  /**
@@ -264,81 +273,122 @@ export class BinaryTree<
264
273
  * Time Complexity: O(n)
265
274
  * Space Complexity: O(log n)
266
275
  *
267
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
268
- * key, otherwise it returns the key itself.
269
- * @param {K | NODE | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`,
270
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
271
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
272
- * type of iteration to be used when searching for a node by key. It has a default value of
273
- * `'ITERATIVE'`.
274
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
275
- * itself if it is not a valid node key.
276
+ * The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
277
+ * node if it is a key or entry.
278
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
279
+ * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
280
+ * a raw element.
281
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
282
+ * parameter that specifies the type of iteration to be used when searching for a node. It has a
283
+ * default value of `'ITERATIVE'`.
284
+ * @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
276
285
  */
277
286
  ensureNode(
278
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
287
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
279
288
  iterationType: IterationType = 'ITERATIVE'
280
289
  ): NODE | null | undefined {
281
- if (keyOrNodeOrEntry === this.NIL) return;
282
- if (this.isRealNode(keyOrNodeOrEntry)) {
283
- return keyOrNodeOrEntry;
290
+ if (keyOrNodeOrEntryOrRawElement === null) return null;
291
+ if (keyOrNodeOrEntryOrRawElement === undefined) return;
292
+ if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
293
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
294
+
295
+ if (this.toEntryFn) {
296
+ const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
297
+ if (key) return this.getNodeByKey(key);
284
298
  }
285
- if (this.isEntry(keyOrNodeOrEntry)) {
286
- const key = keyOrNodeOrEntry[0];
299
+
300
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
301
+ const key = keyOrNodeOrEntryOrRawElement[0];
287
302
  if (key === null) return null;
288
303
  if (key === undefined) return;
289
304
  return this.getNodeByKey(key, iterationType);
290
305
  }
291
- if (keyOrNodeOrEntry === null) return null;
292
- if (keyOrNodeOrEntry === undefined) return;
293
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
306
+
307
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
308
+ return;
294
309
  }
295
310
 
296
311
  /**
297
- * The function checks if a given node is a real node or null.
298
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
299
- * @returns a boolean value.
312
+ * The function checks if the input is an instance of the BinaryTreeNode class.
313
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
314
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
315
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
316
+ * an instance of the `BinaryTreeNode` class.
300
317
  */
301
- isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
302
- return this.isRealNode(node) || node === null;
318
+ isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
319
+ return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
303
320
  }
304
321
 
305
322
  /**
306
- * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
307
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
308
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
323
+ * The function checks if a given node is a valid node in a binary search tree.
324
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
325
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
326
+ * @returns a boolean value.
309
327
  */
310
- isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
311
- return keyOrNodeOrEntry instanceof BinaryTreeNode;
328
+ isRealNode(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
329
+ if (node === this.NIL || node === null || node === undefined) return false;
330
+ return this.isNode(node);
312
331
  }
313
332
 
314
333
  /**
315
- * The function checks if a given node is a real node by verifying if it is an instance of
316
- * BinaryTreeNode and its key is not NaN.
317
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
334
+ * The function checks if a given node is a real node or null.
335
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
336
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
318
337
  * @returns a boolean value.
319
338
  */
320
- isRealNode(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
321
- if (node === this.NIL || node === null || node === undefined) return false;
322
- return this.isNode(node);
339
+ isNodeOrNull(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
340
+ return this.isRealNode(node) || node === null;
323
341
  }
324
342
 
325
343
  /**
326
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
327
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
344
+ * The function checks if a given node is equal to the NIL value.
345
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
346
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
328
347
  * @returns a boolean value.
329
348
  */
330
- isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
349
+ isNIL(node: R | KeyOrNodeOrEntry<K, V, NODE>) {
331
350
  return node === this.NIL;
332
351
  }
333
352
 
334
353
  /**
335
- * The function checks if a given value is an entry in a binary tree node.
336
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
337
- * two type parameters V and NODE, representing the value and node type respectively.
354
+ * The function checks if the input is an array with two elements, indicating it is a binary tree
355
+ * node entry.
356
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
357
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
338
358
  * @returns a boolean value.
339
359
  */
340
- isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
341
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
360
+ isEntry(
361
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
362
+ ): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
363
+ return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
364
+ }
365
+
366
+ /**
367
+ * The function checks if a given value is a valid key by evaluating its type and value.
368
+ * @param {any} key - The `key` parameter can be of any type. It is the value that we want to check
369
+ * if it is a valid key.
370
+ * @param [isCheckValueOf=true] - The `isCheckValueOf` parameter is a boolean flag that determines
371
+ * whether the function should check the valueOf() method of an object when the key is of type
372
+ * 'object'. If `isCheckValueOf` is true, the function will recursively call itself with the value
373
+ * returned by key.valueOf().
374
+ * @returns a boolean value.
375
+ */
376
+ isKey(key: any, isCheckValueOf = true): key is K {
377
+ if (key === null) return true;
378
+ const keyType = typeof key;
379
+ if (keyType === 'string' || keyType === 'bigint' || keyType === 'boolean') return true;
380
+ if (keyType === 'number') return !isNaN(key);
381
+ if (keyType === 'symbol' || keyType === 'undefined') return false;
382
+ if (keyType === 'function') return this.isKey(key());
383
+ if (keyType === 'object') {
384
+ if (typeof key.toString === 'function') return true;
385
+ if (isCheckValueOf && typeof key.valueOf === 'function') {
386
+ this.isKey(key.valueOf(), false);
387
+ }
388
+ return false;
389
+ }
390
+
391
+ return false;
342
392
  }
343
393
 
344
394
  /**
@@ -350,14 +400,20 @@ export class BinaryTree<
350
400
  * Time Complexity O(n)
351
401
  * Space Complexity O(1)
352
402
  *
353
- * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
354
- * existing node with the same key.
355
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
356
- * @param {V} [value] - The value to be inserted into the binary tree.
357
- * @returns The function `add` returns either a node (`NODE`), `null`, or `undefined`.
358
- */
359
- add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
360
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
403
+ * The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
404
+ * and finding the appropriate insertion position.
405
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
406
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
407
+ * node, entry, or raw element to be added to the tree. It can also accept a value of type
408
+ * `KeyOrNodeOrEntry<K, V, NODE>
409
+ * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
410
+ * key being added to the tree. It represents the value that will be stored in the tree for the given
411
+ * key.
412
+ * @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
413
+ * insertion position cannot be found or if there are duplicate keys.
414
+ */
415
+ add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
416
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
361
417
  if (newNode === undefined) return false;
362
418
 
363
419
  // If the tree is empty, directly set the new node as the root node
@@ -419,13 +475,20 @@ export class BinaryTree<
419
475
  * Time Complexity: O(k * n)
420
476
  * Space Complexity: O(1)
421
477
  *
422
- * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
423
- * adds each node with its corresponding value to the data structure.
424
- * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
425
- * @param [values] - An optional iterable of values that will be assigned to each node being added.
426
- * @returns The function `addMany` returns an array of `NODE`, `null`, or `undefined` values.
427
- */
428
- addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[] {
478
+ * The `addMany` function takes in an iterable of keys or nodes or entries or raw elements, and an
479
+ * optional iterable of values, and adds each key or node or entry with its corresponding value to a
480
+ * data structure, returning an array of booleans indicating whether each insertion was successful.
481
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
482
+ * elements. These elements will be added to the data structure.
483
+ * @param [values] - An optional iterable of values that correspond to the keys or nodes or entries
484
+ * in the `keysOrNodesOrEntriesOrRawElements` parameter.
485
+ * @returns The function `addMany` returns an array of booleans indicating whether each element was
486
+ * successfully added to the data structure.
487
+ */
488
+ addMany(
489
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
490
+ values?: Iterable<V | undefined>
491
+ ): boolean[] {
429
492
  // TODO not sure addMany not be run multi times
430
493
  const inserted: boolean[] = [];
431
494
 
@@ -434,7 +497,7 @@ export class BinaryTree<
434
497
  valuesIterator = values[Symbol.iterator]();
435
498
  }
436
499
 
437
- for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
500
+ for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
438
501
  let value: V | undefined | null = undefined;
439
502
 
440
503
  if (valuesIterator) {
@@ -444,7 +507,7 @@ export class BinaryTree<
444
507
  }
445
508
  }
446
509
 
447
- inserted.push(this.add(keyOrNodeOrEntry, value));
510
+ inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
448
511
  }
449
512
 
450
513
  return inserted;
@@ -460,17 +523,19 @@ export class BinaryTree<
460
523
  * Time Complexity: O(k * n)
461
524
  * Space Complexity: O(1)
462
525
  *
463
- * The `refill` function clears the current data and adds new key-value pairs to the data structure.
464
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
465
- * KeyOrNodeOrEntry<K, V, NODE>.
466
- * @param [values] - The `values` parameter is an optional iterable that contains the values to be
467
- * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
468
- * the values will be associated with the corresponding keys or nodes or entries in the
469
- * `keysOrNodesOrEntries` iterable
470
- */
471
- refill(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): void {
526
+ * The `refill` function clears the current data and adds new data to the collection.
527
+ * @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
528
+ * elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
529
+ * @param [values] - The `values` parameter is an optional iterable of values that will be associated
530
+ * with the keys or nodes being added. If provided, the values will be assigned to the corresponding
531
+ * keys or nodes. If not provided, the values will be set to `undefined`.
532
+ */
533
+ refill(
534
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
535
+ values?: Iterable<V | undefined>
536
+ ): void {
472
537
  this.clear();
473
- this.addMany(keysOrNodesOrEntries, values);
538
+ this.addMany(keysOrNodesOrEntriesOrRawElements, values);
474
539
  }
475
540
 
476
541
  delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
@@ -485,21 +550,21 @@ export class BinaryTree<
485
550
  /**
486
551
  * Time Complexity: O(n)
487
552
  * Space Complexity: O(1)
488
- * /
553
+ */
489
554
 
490
- /**
555
+ /**
491
556
  * Time Complexity: O(n)
492
557
  * Space Complexity: O(1)
493
558
  *
494
- * The function deletes a node from a binary tree and returns an array of the deleted nodes along
495
- * with the nodes that need to be balanced.
496
- * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
497
- * object that you want to delete from the binary tree. It can be of any type that is compatible with
498
- * the callback function's return type. It can also be null or undefined if you want to delete a
499
- * specific node based on its value or object.
559
+ * The above function is a TypeScript implementation of deleting a node from a binary tree, returning
560
+ * the deleted node and the node that needs to be balanced.
561
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
562
+ * used to identify the node that needs to be deleted from the binary tree. It can be of any type
563
+ * that is returned by the callback function.
500
564
  * @param {C} callback - The `callback` parameter is a function that is used to determine the
501
- * identifier of the node to be deleted. It is optional and has a default value of
502
- * `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
565
+ * identifier of the node to be deleted. It is of type `C`, which extends the `BTNCallback<NODE>`
566
+ * interface. The `BTNCallback<NODE>` interface represents a callback function that takes a node of
567
+ * type `NODE
503
568
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
504
569
  */
505
570
  delete<C extends BTNCallback<NODE>>(
@@ -554,7 +619,7 @@ export class BinaryTree<
554
619
  identifier: K,
555
620
  callback?: C,
556
621
  onlyOne?: boolean,
557
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
622
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
558
623
  iterationType?: IterationType
559
624
  ): NODE[];
560
625
 
@@ -562,7 +627,7 @@ export class BinaryTree<
562
627
  identifier: NODE | null | undefined,
563
628
  callback?: C,
564
629
  onlyOne?: boolean,
565
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
630
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
566
631
  iterationType?: IterationType
567
632
  ): NODE[];
568
633
 
@@ -570,7 +635,7 @@ export class BinaryTree<
570
635
  identifier: ReturnType<C>,
571
636
  callback: C,
572
637
  onlyOne?: boolean,
573
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
638
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
574
639
  iterationType?: IterationType
575
640
  ): NODE[];
576
641
 
@@ -581,34 +646,33 @@ export class BinaryTree<
581
646
 
582
647
  /**
583
648
  * Time Complexity: O(n)
584
- * Space Complexity: O(k + log n).
649
+ * Space Complexity: O(k + log n)
585
650
  *
586
- * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
587
- * callback function.
651
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
652
+ * recursive or iterative approach.
588
653
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
589
- * that you want to search for in the binary tree. It can be of any type that is returned by the
590
- * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
591
- * specific value.
592
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
593
- * input and returns a value of type `C`. It is used to determine if a node matches the given
594
- * identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
595
- * default
596
- * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
597
- * matches the identifier. If set to true, the function will stop iterating once it finds a matching
598
- * node and return that node. If set to false (default), the function will continue iterating and
599
- * return all nodes that match the identifier.
600
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
601
- * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
602
- * it is `null` or `undefined`, an empty array will be returned.
603
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
604
- * traverse the binary tree. It can have two possible values:
605
- * @returns an array of nodes of type `NODE`.
654
+ * that is used to identify the nodes. It can be of any type and is used to match against the result
655
+ * of the callback function for each node.
656
+ * @param {C} callback - The `callback` parameter is a function that takes a node as input and
657
+ * returns a value. This value is used to identify the nodes that match the given identifier. The
658
+ * `callback` function is optional and defaults to a default callback function
659
+ * (`this._DEFAULT_CALLBACK`) if not provided.
660
+ * @param [onlyOne=false] - A boolean value indicating whether to return only one node that matches
661
+ * the identifier or all nodes that match the identifier. If set to true, only the first matching
662
+ * node will be returned. If set to false, all matching nodes will be returned. The default value is
663
+ * false.
664
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
665
+ * point for the search. It can be either a node object, a key-value pair, or a key. If it is not
666
+ * provided, the `root` of the data structure is used as the starting point.
667
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
668
+ * iteration to be performed on the nodes of a binary tree. It can have two possible values:
669
+ * @returns an array of NODE objects.
606
670
  */
607
671
  getNodes<C extends BTNCallback<NODE>>(
608
672
  identifier: ReturnType<C> | null | undefined,
609
673
  callback: C = this._DEFAULT_CALLBACK as C,
610
674
  onlyOne = false,
611
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
675
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
612
676
  iterationType: IterationType = this.iterationType
613
677
  ): NODE[] {
614
678
  beginRoot = this.ensureNode(beginRoot);
@@ -650,21 +714,21 @@ export class BinaryTree<
650
714
  getNode<C extends BTNCallback<NODE, K>>(
651
715
  identifier: K,
652
716
  callback?: C,
653
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
717
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
654
718
  iterationType?: IterationType
655
719
  ): NODE | null | undefined;
656
720
 
657
721
  getNode<C extends BTNCallback<NODE, NODE>>(
658
722
  identifier: NODE | null | undefined,
659
723
  callback?: C,
660
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
724
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
661
725
  iterationType?: IterationType
662
726
  ): NODE | null | undefined;
663
727
 
664
728
  getNode<C extends BTNCallback<NODE>>(
665
729
  identifier: ReturnType<C>,
666
730
  callback: C,
667
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
731
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
668
732
  iterationType?: IterationType
669
733
  ): NODE | null | undefined;
670
734
 
@@ -675,29 +739,26 @@ export class BinaryTree<
675
739
 
676
740
  /**
677
741
  * Time Complexity: O(n)
678
- * Space Complexity: O(log n)
742
+ * Space Complexity: O(log n).
679
743
  *
680
- * The function `getNode` returns the first node that matches the given identifier and callback
681
- * function.
744
+ * The function `getNode` returns the first node that matches the given identifier and callback,
745
+ * starting from the specified root node and using the specified iteration type.
682
746
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
683
- * used to identify the node you want to retrieve. It can be of any type that is returned by the
684
- * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
685
- * identifier.
686
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
687
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
688
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
689
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
690
- * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
691
- * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
692
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
693
- * be performed when searching for nodes in the binary tree. It determines the order in which the
694
- * nodes are visited during the search.
695
- * @returns a value of type `NODE | null | undefined`.
747
+ * used to identify the node you want to retrieve. It can be of any type that is the return type of
748
+ * the `C` callback function, or it can be `null` or `undefined`.
749
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
750
+ * node matches the desired criteria. It should return a value that can be used to identify the node.
751
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
752
+ * point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
753
+ * a node entry. If not provided, the search will start from the root of the tree.
754
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
755
+ * of iteration to be performed when searching for nodes. It can have one of the following values:
756
+ * @returns The method is returning a NODE object, or null, or undefined.
696
757
  */
697
758
  getNode<C extends BTNCallback<NODE>>(
698
759
  identifier: ReturnType<C> | null | undefined,
699
760
  callback: C = this._DEFAULT_CALLBACK as C,
700
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
761
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
701
762
  iterationType: IterationType = this.iterationType
702
763
  ): NODE | null | undefined {
703
764
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
@@ -712,15 +773,13 @@ export class BinaryTree<
712
773
  * Time Complexity: O(n)
713
774
  * Space Complexity: O(log n)
714
775
  *
715
- * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
716
- * recursive or iterative iteration.
717
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
718
- * It is used to find the node with the matching key value.
719
- * @param iterationType - The `iterationType` parameter is used to determine whether the search for
720
- * the node with the given key should be performed iteratively or recursively. It has two possible
721
- * values:
722
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
723
- * found in the binary tree. If no node is found, it returns `undefined`.
776
+ * The function `getNodeByKey` returns a node with a specific key value from a tree structure.
777
+ * @param {K} key - The key parameter is the value that you want to search for in the tree. It is
778
+ * used to find the node with the matching key value.
779
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
780
+ * parameter that specifies the type of iteration to be used when searching for a node in the tree.
781
+ * It has a default value of `'ITERATIVE'`.
782
+ * @returns a value of type NODE, null, or undefined.
724
783
  */
725
784
  getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
726
785
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
@@ -729,21 +788,21 @@ export class BinaryTree<
729
788
  override get<C extends BTNCallback<NODE, K>>(
730
789
  identifier: K,
731
790
  callback?: C,
732
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
791
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
733
792
  iterationType?: IterationType
734
793
  ): V | undefined;
735
794
 
736
795
  override get<C extends BTNCallback<NODE, NODE>>(
737
796
  identifier: NODE | null | undefined,
738
797
  callback?: C,
739
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
798
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
740
799
  iterationType?: IterationType
741
800
  ): V | undefined;
742
801
 
743
802
  override get<C extends BTNCallback<NODE>>(
744
803
  identifier: ReturnType<C>,
745
804
  callback: C,
746
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
805
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
747
806
  iterationType?: IterationType
748
807
  ): V | undefined;
749
808
 
@@ -756,28 +815,27 @@ export class BinaryTree<
756
815
  * Time Complexity: O(n)
757
816
  * Space Complexity: O(log n)
758
817
  *
759
- * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
760
- * and callback function.
818
+ * The function `get` in TypeScript overrides the base class method and returns the value associated
819
+ * with the given identifier.
761
820
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
762
- * used to identify the node in the binary tree. It can be of any type that is the return type of the
821
+ * used to identify the node in the binary tree. It can be of any type that is returned by the
763
822
  * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
764
- * @param {C} callback - The `callback` parameter is a function that will be called with each node in
765
- * the binary tree. It is used to determine whether a node matches the given identifier. The callback
766
- * function should return a value that can be compared to the identifier to determine if it is a
767
- * match.
768
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
769
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
770
- * node), a node object of type `NODE`, or `null`/`undefined` to start the search from the root of
771
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
772
- * be performed when searching for a node in the binary tree. It is an optional parameter with a
773
- * default value specified by `this.iterationType`.
774
- * @returns The value of the node with the given identifier is being returned. If the node is not
775
- * found, `undefined` is returned.
823
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
824
+ * node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
825
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
826
+ * point for the search in the binary tree. It can be either a root node of the tree or a key, node,
827
+ * or entry object that exists in the tree. If no specific starting point is provided, the search
828
+ * will begin from the root of the
829
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
830
+ * of iteration to be performed when searching for a node in the tree. It can have one of the
831
+ * following values:
832
+ * @returns The method is returning the value associated with the specified identifier in the binary
833
+ * tree.
776
834
  */
777
835
  override get<C extends BTNCallback<NODE>>(
778
836
  identifier: ReturnType<C> | null | undefined,
779
837
  callback: C = this._DEFAULT_CALLBACK as C,
780
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
838
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
781
839
  iterationType: IterationType = this.iterationType
782
840
  ): V | undefined {
783
841
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
@@ -786,54 +844,53 @@ export class BinaryTree<
786
844
  override has<C extends BTNCallback<NODE, K>>(
787
845
  identifier: K,
788
846
  callback?: C,
789
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
847
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
790
848
  iterationType?: IterationType
791
849
  ): boolean;
792
850
 
793
851
  override has<C extends BTNCallback<NODE, NODE>>(
794
852
  identifier: NODE | null | undefined,
795
853
  callback?: C,
796
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
854
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
797
855
  iterationType?: IterationType
798
856
  ): boolean;
799
857
 
800
858
  override has<C extends BTNCallback<NODE>>(
801
859
  identifier: ReturnType<C> | null | undefined,
802
860
  callback: C,
803
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
861
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
804
862
  iterationType?: IterationType
805
863
  ): boolean;
806
864
 
807
865
  /**
808
866
  * Time Complexity: O(n)
809
- * Space Complexity: O(log n).
867
+ * Space Complexity: O(log n)
810
868
  */
811
869
 
812
870
  /**
813
871
  * Time Complexity: O(n)
814
- * Space Complexity: O(log n).
872
+ * Space Complexity: O(log n)
815
873
  *
816
- * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
874
+ * The `has` function checks if a given identifier exists in the data structure and returns a boolean
875
+ * value.
817
876
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
818
- * that you want to search for in the binary tree. It can be of any type that is returned by the
819
- * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
820
- * specific identifier.
821
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
822
- * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
823
- * function should return a boolean value indicating whether the node should be included in the
824
- * result or not.
825
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
826
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
827
- * node in the binary tree), a node object (`NODE`), or `null`/`undefined` to start the search from
828
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
829
- * iteration to be performed on the binary tree. It is used to specify whether the iteration should
830
- * be performed in a pre-order, in-order, or post-order manner.
831
- * @returns a boolean value.
877
+ * used to identify a specific node or entry in the data structure. It can be of any type that is
878
+ * returned by the callback function `C`. It can also be `null` or `undefined` if no specific
879
+ * identifier is provided.
880
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine
881
+ * whether a node should be included in the result or not. It is of type `C`, which extends the
882
+ * `BTNCallback<NODE>` type.
883
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
884
+ * point for the iteration in the data structure. It can be either a root node, a key-value pair, or
885
+ * a node entry. If not specified, it defaults to the root of the data structure.
886
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
887
+ * of iteration to be performed. It is an optional parameter with a default value of `IterationType`.
888
+ * @returns The method is returning a boolean value.
832
889
  */
833
890
  override has<C extends BTNCallback<NODE>>(
834
891
  identifier: ReturnType<C> | null | undefined,
835
892
  callback: C = this._DEFAULT_CALLBACK as C,
836
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
893
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
837
894
  iterationType: IterationType = this.iterationType
838
895
  ): boolean {
839
896
  callback = this._ensureCallback(identifier, callback);
@@ -884,12 +941,13 @@ export class BinaryTree<
884
941
  *
885
942
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
886
943
  * height of the tree.
887
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
888
- * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
889
- * value of a binary tree node), `NODE` (a node of a binary tree), `null`, or `undefined`. If
944
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
945
+ * has a default value of `this.root`. It represents the starting point for checking if the tree is
946
+ * perfectly balanced. It can be either a root node (`R`), a key or node or entry
947
+ * (`KeyOrNodeOrEntry<K, V, NODE
890
948
  * @returns a boolean value.
891
949
  */
892
- isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
950
+ isPerfectlyBalanced(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
893
951
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
894
952
  }
895
953
 
@@ -902,16 +960,18 @@ export class BinaryTree<
902
960
  * Time Complexity: O(n)
903
961
  * Space Complexity: O(1)
904
962
  *
905
- * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
906
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the root
907
- * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
908
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
909
- * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
910
- * possible values:
963
+ * The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
964
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
965
+ * starting point for checking if a binary search tree (BST) is valid. It can be either a root node
966
+ * of the BST, a key value of a node in the BST, or an entry object containing both the key and value
967
+ * of a node in the BST
968
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to determine the type
969
+ * of iteration to be performed while checking if the binary search tree (BST) is valid. It can have
970
+ * two possible values:
911
971
  * @returns a boolean value.
912
972
  */
913
973
  isBST(
914
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
974
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
915
975
  iterationType: IterationType = this.iterationType
916
976
  ): boolean {
917
977
  // TODO there is a bug
@@ -921,7 +981,7 @@ export class BinaryTree<
921
981
  if (iterationType === 'RECURSIVE') {
922
982
  const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
923
983
  if (!this.isRealNode(cur)) return true;
924
- const numKey = this.extractor(cur.key);
984
+ const numKey = Number(cur.key);
925
985
  if (numKey <= min || numKey >= max) return false;
926
986
  return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
927
987
  };
@@ -941,7 +1001,7 @@ export class BinaryTree<
941
1001
  curr = curr.left;
942
1002
  }
943
1003
  curr = stack.pop()!;
944
- const numKey = this.extractor(curr.key);
1004
+ const numKey = Number(curr.key);
945
1005
  if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey)) return false;
946
1006
  prev = numKey;
947
1007
  curr = curr.right;
@@ -963,25 +1023,26 @@ export class BinaryTree<
963
1023
  * Time Complexity: O(n)
964
1024
  * Space Complexity: O(1)
965
1025
  *
966
- * The function calculates the depth of a given node in a binary tree.
967
- * @param {K | NODE | null | undefined} dist - The `dist` parameter represents the node in
968
- * the binary tree whose depth we want to find. It can be of type `K`, `NODE`, `null`, or
969
- * `undefined`.
970
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
971
- * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
972
- * `NODE` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
973
- * @returns the depth of the `dist` relative to the `beginRoot`.
974
- */
975
- getDepth(dist: KeyOrNodeOrEntry<K, V, NODE>, beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
976
- dist = this.ensureNode(dist);
977
- beginRoot = this.ensureNode(beginRoot);
1026
+ * The function calculates the depth of a given node or key in a tree-like data structure.
1027
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
1028
+ * (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
1029
+ * entry).
1030
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
1031
+ * represents the starting point from which to calculate the depth. It can be either a reference to a
1032
+ * node in the tree or a key-value pair or an entry object. If not provided, the default value is
1033
+ * `this.root`, which refers to the root node
1034
+ * @returns the depth of a node in a tree structure.
1035
+ */
1036
+ getDepth(dist: R | KeyOrNodeOrEntry<K, V, NODE>, beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
1037
+ let distEnsured = this.ensureNode(dist);
1038
+ const beginRootEnsured = this.ensureNode(beginRoot);
978
1039
  let depth = 0;
979
- while (dist?.parent) {
980
- if (dist === beginRoot) {
1040
+ while (distEnsured?.parent) {
1041
+ if (distEnsured === beginRootEnsured) {
981
1042
  return depth;
982
1043
  }
983
1044
  depth++;
984
- dist = dist.parent;
1045
+ distEnsured = distEnsured.parent;
985
1046
  }
986
1047
  return depth;
987
1048
  }
@@ -993,20 +1054,19 @@ export class BinaryTree<
993
1054
 
994
1055
  /**
995
1056
  * Time Complexity: O(n)
996
- * Space Complexity: O(log n)
1057
+ * Space Complexity: O(1)
997
1058
  *
998
- * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
999
- * iterative traversal.
1000
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1001
- * starting node of the binary tree from which we want to calculate the height. It can be of type
1002
- * `K`, `NODE`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
1003
- * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
1004
- * height of the tree using a recursive approach or an iterative approach. It can have two possible
1005
- * values:
1006
- * @returns the height of the binary tree.
1059
+ * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
1060
+ * or iterative approach.
1061
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1062
+ * starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
1063
+ * node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
1064
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1065
+ * iteration used to calculate the height of the tree. It can have two possible values:
1066
+ * @returns the maximum height of the binary tree.
1007
1067
  */
1008
1068
  getHeight(
1009
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1069
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1010
1070
  iterationType: IterationType = this.iterationType
1011
1071
  ): number {
1012
1072
  beginRoot = this.ensureNode(beginRoot);
@@ -1049,15 +1109,18 @@ export class BinaryTree<
1049
1109
  *
1050
1110
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
1051
1111
  * recursive or iterative approach.
1052
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1053
- * starting node of the binary tree from which we want to calculate the minimum height. It can be of
1054
- * type `K`, `NODE`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
1055
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
1056
- * to calculate the minimum height of a binary tree. It can have two possible values:
1057
- * @returns The function `getMinHeight` returns the minimum height of a binary tree.
1112
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1113
+ * starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
1114
+ * key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
1115
+ * tree.
1116
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1117
+ * iteration to be used when calculating the minimum height of the tree. It can have two possible
1118
+ * values:
1119
+ * @returns The function `getMinHeight` returns a number, which represents the minimum height of the
1120
+ * binary tree.
1058
1121
  */
1059
1122
  getMinHeight(
1060
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1123
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1061
1124
  iterationType: IterationType = this.iterationType
1062
1125
  ): number {
1063
1126
  beginRoot = this.ensureNode(beginRoot);
@@ -1105,36 +1168,33 @@ export class BinaryTree<
1105
1168
  /**
1106
1169
  * Time Complexity: O(log n)
1107
1170
  * Space Complexity: O(log n)
1108
- * /
1171
+ */
1109
1172
 
1110
- /**
1173
+ /**
1111
1174
  * Time Complexity: O(log n)
1112
1175
  * Space Complexity: O(log n)
1113
1176
  *
1114
- * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
1115
- * structure, with the option to reverse the order of the nodes.
1116
- * @param {K | NODE | null | undefined} beginNode - The `beginRoot` parameter represents the
1117
- * starting node from which you want to find the path to the root. It can be of type `K`, `NODE`,
1118
- * `null`, or `undefined`.
1177
+ * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
1178
+ * up to the root node, with an option to reverse the order of the nodes.
1179
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
1180
+ * type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
1119
1181
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
1120
1182
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
1121
- * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
1122
- * @returns The function `getPathToRoot` returns an array of nodes (`NODE[]`).
1183
+ * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
1184
+ * @returns The function `getPathToRoot` returns an array of `NODE` objects.
1123
1185
  */
1124
- getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1125
- // TODO to support get path through passing key
1186
+ getPathToRoot(beginNode: R | KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1126
1187
  const result: NODE[] = [];
1127
- beginNode = this.ensureNode(beginNode);
1188
+ let beginNodeEnsured = this.ensureNode(beginNode);
1128
1189
 
1129
- if (!beginNode) return result;
1190
+ if (!beginNodeEnsured) return result;
1130
1191
 
1131
- while (beginNode.parent) {
1192
+ while (beginNodeEnsured.parent) {
1132
1193
  // Array.push + Array.reverse is more efficient than Array.unshift
1133
- // TODO may consider using Deque, so far this is not the performance bottleneck
1134
- result.push(beginNode);
1135
- beginNode = beginNode.parent;
1194
+ result.push(beginNodeEnsured);
1195
+ beginNodeEnsured = beginNodeEnsured.parent;
1136
1196
  }
1137
- result.push(beginNode);
1197
+ result.push(beginNodeEnsured);
1138
1198
  return isReverse ? result.reverse() : result;
1139
1199
  }
1140
1200
 
@@ -1147,18 +1207,17 @@ export class BinaryTree<
1147
1207
  * Time Complexity: O(log n)
1148
1208
  * Space Complexity: O(1)
1149
1209
  *
1150
- * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
1151
- * iteratively.
1152
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
1153
- * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `NODE` (a
1154
- * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
1155
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
1156
- * be performed when finding the leftmost node in a binary tree. It can have two possible values:
1157
- * @returns The function `getLeftMost` returns the leftmost node (`NODE`) in the binary tree. If there
1158
- * is no leftmost node, it returns `null` or `undefined` depending on the input.
1210
+ * The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
1211
+ * iterative traversal.
1212
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1213
+ * starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
1214
+ * a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
1215
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
1216
+ * of iteration to be performed. It can have two possible values:
1217
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree.
1159
1218
  */
1160
1219
  getLeftMost(
1161
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1220
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1162
1221
  iterationType: IterationType = this.iterationType
1163
1222
  ): NODE | null | undefined {
1164
1223
  if (this.isNIL(beginRoot)) return beginRoot as NODE;
@@ -1193,19 +1252,18 @@ export class BinaryTree<
1193
1252
  * Time Complexity: O(log n)
1194
1253
  * Space Complexity: O(1)
1195
1254
  *
1196
- * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
1255
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
1197
1256
  * iteratively.
1198
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1199
- * starting node from which we want to find the rightmost node. It can be of type `K`, `NODE`,
1200
- * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
1201
- * current object.
1202
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
1203
- * type of iteration to use when finding the rightmost node. It can have one of two values:
1204
- * @returns The function `getRightMost` returns the rightmost node (`NODE`) in a binary tree. If there
1205
- * is no rightmost node, it returns `null` or `undefined`, depending on the input.
1257
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1258
+ * starting point for finding the rightmost node in a binary tree. It can be either a root node
1259
+ * (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
1260
+ * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
1261
+ * of iteration to be performed when finding the rightmost node in a binary tree. It can have two
1262
+ * possible values:
1263
+ * @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
1206
1264
  */
1207
1265
  getRightMost(
1208
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1266
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1209
1267
  iterationType: IterationType = this.iterationType
1210
1268
  ): NODE | null | undefined {
1211
1269
  if (this.isNIL(beginRoot)) return beginRoot as NODE;
@@ -1240,10 +1298,10 @@ export class BinaryTree<
1240
1298
  * Time Complexity: O(log n)
1241
1299
  * Space Complexity: O(1)
1242
1300
  *
1243
- * The function returns the predecessor of a given node in a tree.
1244
- * @param {NODE} node - The parameter `node` is of type `RedBlackTreeNode`, which represents a node in a
1301
+ * The function returns the predecessor node of a given node in a binary tree.
1302
+ * @param {NODE} node - The parameter "node" is of type "NODE", which represents a node in a binary
1245
1303
  * tree.
1246
- * @returns the predecessor of the given 'node'.
1304
+ * @returns the predecessor node of the given node.
1247
1305
  */
1248
1306
  getPredecessor(node: NODE): NODE {
1249
1307
  if (this.isRealNode(node.left)) {
@@ -1270,8 +1328,8 @@ export class BinaryTree<
1270
1328
  *
1271
1329
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1272
1330
  * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1273
- * @returns the successor of the given node or key. The successor is the node that comes immediately
1274
- * after the given node in the inorder traversal of the binary tree.
1331
+ * @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
1332
+ * there is no successor, and `undefined` if the input `x` is not a valid node.
1275
1333
  */
1276
1334
  getSuccessor(x?: K | NODE | null): NODE | null | undefined {
1277
1335
  x = this.ensureNode(x);
@@ -1292,7 +1350,7 @@ export class BinaryTree<
1292
1350
  dfs<C extends BTNCallback<NODE>>(
1293
1351
  callback?: C,
1294
1352
  pattern?: DFSOrderPattern,
1295
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1353
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1296
1354
  iterationType?: IterationType,
1297
1355
  includeNull?: false
1298
1356
  ): ReturnType<C>[];
@@ -1300,7 +1358,7 @@ export class BinaryTree<
1300
1358
  dfs<C extends BTNCallback<NODE | null>>(
1301
1359
  callback?: C,
1302
1360
  pattern?: DFSOrderPattern,
1303
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1361
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1304
1362
  iterationType?: IterationType,
1305
1363
  includeNull?: true
1306
1364
  ): ReturnType<C>[];
@@ -1308,35 +1366,35 @@ export class BinaryTree<
1308
1366
  /**
1309
1367
  * Time complexity: O(n)
1310
1368
  * Space complexity: O(n)
1311
- * /
1369
+ */
1312
1370
 
1313
- /**
1371
+ /**
1314
1372
  * Time complexity: O(n)
1315
1373
  * Space complexity: O(n)
1316
1374
  *
1317
- * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1318
- * specified pattern and iteration type, and returns an array of values obtained from applying a
1319
- * callback function to each visited node.
1320
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1321
- * the tree during the depth-first search. It takes a single parameter, which can be of type `NODE`,
1322
- * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1323
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1324
- * nodes are traversed during the depth-first search. It can have one of the following values:
1325
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1326
- * for the depth-first search traversal. It can be specified as a key, a node object, or
1327
- * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1328
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1329
- * iteration to use when traversing the tree. It can have one of the following values:
1375
+ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1376
+ * function on each node according to a specified pattern and iteration type.
1377
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
1378
+ * visited during the depth-first search. It takes a node as an argument and returns a value. The
1379
+ * return type of the callback function is determined by the generic type `C`.
1380
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
1381
+ * nodes are visited during the depth-first search. It can have one of the following values:
1382
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1383
+ * point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
1384
+ * is a key or key-value pair, the method will find the corresponding node in the tree and start the
1385
+ * search from there.
1386
+ * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter determines the
1387
+ * type of iteration to use during the depth-first search. It can have two possible values:
1330
1388
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1331
- * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1332
- * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1333
- * `false`, null or undefined
1334
- * @returns an array of values that are the return values of the callback function.
1389
+ * whether or not to include null values in the depth-first search traversal. If `includeNull` is set
1390
+ * to `true`, null values will be included in the traversal. If `includeNull` is set to `false`, null
1391
+ * values will
1392
+ * @returns an array of the return types of the callback function.
1335
1393
  */
1336
1394
  dfs<C extends BTNCallback<NODE | null | undefined>>(
1337
1395
  callback: C = this._DEFAULT_CALLBACK as C,
1338
1396
  pattern: DFSOrderPattern = 'IN',
1339
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1397
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1340
1398
  iterationType: IterationType = 'ITERATIVE',
1341
1399
  includeNull = false
1342
1400
  ): ReturnType<C>[] {
@@ -1430,14 +1488,14 @@ export class BinaryTree<
1430
1488
 
1431
1489
  bfs<C extends BTNCallback<NODE>>(
1432
1490
  callback?: C,
1433
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1491
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1434
1492
  iterationType?: IterationType,
1435
1493
  includeNull?: false
1436
1494
  ): ReturnType<C>[];
1437
1495
 
1438
1496
  bfs<C extends BTNCallback<NODE | null>>(
1439
1497
  callback?: C,
1440
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1498
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1441
1499
  iterationType?: IterationType,
1442
1500
  includeNull?: true
1443
1501
  ): ReturnType<C>[];
@@ -1451,26 +1509,27 @@ export class BinaryTree<
1451
1509
  * Time complexity: O(n)
1452
1510
  * Space complexity: O(n)
1453
1511
  *
1454
- * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1455
- * callback function on each node.
1512
+ * The `bfs` function performs a breadth-first search on a binary tree, calling a callback function
1513
+ * on each node and returning an array of the results.
1456
1514
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1457
- * the breadth-first search traversal. It takes a single parameter, which is the current node being
1515
+ * the breadth-first search traversal. It takes a single argument, which is the current node being
1458
1516
  * visited, and returns a value of any type.
1459
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1460
- * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1461
- * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1462
- * the class is used as
1463
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1464
- * performed during the breadth-first search (BFS). It can have two possible values:
1465
- * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1466
- * to include null values in the breadth-first search traversal. If `includeNull` is set to
1467
- * `true`, null values will be included in the traversal, otherwise they will be skipped.
1468
- * @returns an array of values that are the result of invoking the callback function on each node in
1469
- * the breadth-first traversal of a binary tree.
1517
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1518
+ * starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
1519
+ * or entry object. If no value is provided, the `root` property of the class is used as the default
1520
+ * starting point.
1521
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1522
+ * iteration to be performed. It can have two possible values:
1523
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1524
+ * whether or not to include null values in the breadth-first search (BFS) traversal. If
1525
+ * `includeNull` is set to `true`, null values will be included in the traversal. If `includeNull` is
1526
+ * set to `false
1527
+ * @returns The function `bfs` returns an array of values that are the result of invoking the
1528
+ * `callback` function on each node in the breadth-first order traversal of the binary tree.
1470
1529
  */
1471
1530
  bfs<C extends BTNCallback<NODE | null>>(
1472
1531
  callback: C = this._DEFAULT_CALLBACK as C,
1473
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1532
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1474
1533
  iterationType: IterationType = this.iterationType,
1475
1534
  includeNull = false
1476
1535
  ): ReturnType<C>[] {
@@ -1524,14 +1583,14 @@ export class BinaryTree<
1524
1583
 
1525
1584
  listLevels<C extends BTNCallback<NODE>>(
1526
1585
  callback?: C,
1527
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1586
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1528
1587
  iterationType?: IterationType,
1529
1588
  includeNull?: false
1530
1589
  ): ReturnType<C>[][];
1531
1590
 
1532
1591
  listLevels<C extends BTNCallback<NODE | null>>(
1533
1592
  callback?: C,
1534
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1593
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1535
1594
  iterationType?: IterationType,
1536
1595
  includeNull?: true
1537
1596
  ): ReturnType<C>[][];
@@ -1546,25 +1605,25 @@ export class BinaryTree<
1546
1605
  * Space complexity: O(n)
1547
1606
  *
1548
1607
  * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1549
- * a binary tree and contains the values returned by a callback function applied to the nodes at that
1550
- * level.
1608
+ * a binary tree and contains the results of applying a callback function to the nodes at that level.
1551
1609
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1552
- * the tree. It takes a single parameter, which can be of type `NODE`, `null`, or `undefined`, and
1553
- * returns a value of any type.
1554
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1555
- * starting node for traversing the tree. It can be either a node object (`NODE`), a key value
1556
- * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1557
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1558
- * performed on the tree. It can have two possible values:
1610
+ * the tree. It takes a node as an argument and returns a value. The return type of the callback
1611
+ * function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
1612
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
1613
+ * starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
1614
+ * entry. If no value is provided, the `root` property of the class is used as the default starting
1615
+ * point.
1616
+ * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1617
+ * iteration to be performed on the binary tree. It can have two possible values:
1559
1618
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1560
- * whether to include null values in the resulting levels. If `includeNull` is set to `true`,
1619
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1561
1620
  * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1562
1621
  * be excluded
1563
1622
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1564
1623
  */
1565
1624
  listLevels<C extends BTNCallback<NODE | null>>(
1566
1625
  callback: C = this._DEFAULT_CALLBACK as C,
1567
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1626
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1568
1627
  iterationType: IterationType = this.iterationType,
1569
1628
  includeNull = false
1570
1629
  ): ReturnType<C>[][] {
@@ -1621,22 +1680,22 @@ export class BinaryTree<
1621
1680
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1622
1681
  * algorithm.
1623
1682
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1624
- * the tree. It takes a single parameter of type `NODE` (the type of the nodes in the tree) and returns
1625
- * a value of any type.
1626
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1627
- * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1683
+ * the tree. It takes a single argument, which is the current node, and can return any value. The
1684
+ * return type of the `callback` function is determined by the `ReturnType<C>` type, which represents
1685
+ * the return
1686
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
1687
+ * to specify the order in which the nodes of a binary tree are traversed. It can take one of the
1628
1688
  * following values:
1629
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1630
- * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1631
- * the root of the tree. If no value is provided, the default value is the root of the tree.
1632
- * @returns The function `morris` returns an array of values that are the result of invoking the
1633
- * `callback` function on each node in the binary tree. The type of the array nodes is determined
1634
- * by the return type of the `callback` function.
1689
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1690
+ * point for the traversal. It can be either a node object, a key, or an entry object. If no value is
1691
+ * provided, the `root` of the tree is used as the starting point.
1692
+ * @returns The function `morris` returns an array of values that are the return values of the
1693
+ * callback function `callback`.
1635
1694
  */
1636
1695
  morris<C extends BTNCallback<NODE>>(
1637
1696
  callback: C = this._DEFAULT_CALLBACK as C,
1638
1697
  pattern: DFSOrderPattern = 'IN',
1639
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
1698
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root
1640
1699
  ): ReturnType<C>[] {
1641
1700
  beginRoot = this.ensureNode(beginRoot);
1642
1701
  if (beginRoot === null) return [];
@@ -1728,8 +1787,7 @@ export class BinaryTree<
1728
1787
  * Time complexity: O(n)
1729
1788
  * Space complexity: O(n)
1730
1789
  *
1731
- * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1732
- * the new tree.
1790
+ * The `clone` function creates a deep copy of a tree object.
1733
1791
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1734
1792
  */
1735
1793
  clone(): TREE {
@@ -1755,16 +1813,16 @@ export class BinaryTree<
1755
1813
  * Time Complexity: O(n)
1756
1814
  * Space Complexity: O(n)
1757
1815
  *
1758
- * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1759
- * adding only the nodes that satisfy the given predicate function.
1760
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1761
- * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1762
- * included in the filtered tree or not.
1763
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1764
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1765
- * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1766
- * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1767
- * pass the given predicate function.
1816
+ * The `filter` function creates a new tree with entries that pass a given predicate function.
1817
+ * @param predicate - The `predicate` parameter is a callback function that is used to test each
1818
+ * element in the tree. It takes three arguments: `value`, `key`, and `index`. The `value` argument
1819
+ * represents the value of the current element being processed, the `key` argument represents the key
1820
+ * of the
1821
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1822
+ * specify the value of `this` within the `predicate` function. When the `predicate` function is
1823
+ * called, `thisArg` will be used as the value of `this` within the function. If `thisArg`
1824
+ * @returns The `filter` method is returning a new tree object that contains the entries that pass
1825
+ * the given predicate function.
1768
1826
  */
1769
1827
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
1770
1828
  const newTree = this.createTree();
@@ -1786,15 +1844,15 @@ export class BinaryTree<
1786
1844
  * Time Complexity: O(n)
1787
1845
  * Space Complexity: O(n)
1788
1846
  *
1789
- * The `map` function creates a new tree by applying a callback function to each key-value pair in
1790
- * the original tree.
1791
- * @param callback - The callback parameter is a function that will be called for each key-value pair
1792
- * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1793
- * the index of the current pair, and a reference to the tree itself. The callback function should
1794
- * return a new
1795
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1796
- * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1797
- * will be used as the `this` value when the callback function is called. If you don't pass a value
1847
+ * The `map` function creates a new tree by applying a callback function to each entry in the current
1848
+ * tree.
1849
+ * @param callback - The callback parameter is a function that will be called for each entry in the
1850
+ * tree. It takes three arguments: value, key, and index. The value argument represents the value of
1851
+ * the current entry, the key argument represents the key of the current entry, and the index
1852
+ * argument represents the index of the
1853
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1854
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1855
+ * passed as the `this` value to the `callback` function. If `thisArg` is
1798
1856
  * @returns The `map` method is returning a new tree object.
1799
1857
  */
1800
1858
  map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
@@ -1825,13 +1883,17 @@ export class BinaryTree<
1825
1883
  * Time Complexity: O(n)
1826
1884
  * Space Complexity: O(n)
1827
1885
  *
1828
- * The `print` function is used to display a binary tree structure in a visually appealing way.
1829
- * @param {K | NODE | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | NODE | null |
1830
- * undefined`. It represents the root node of a binary tree. The root node can have one of the
1831
- * following types:
1832
- * @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.
1833
- */
1834
- override print(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1886
+ * The `print` function in TypeScript prints the binary tree structure with customizable options.
1887
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
1888
+ * point for printing the binary tree. It can be either a node of the binary tree or a key or entry
1889
+ * that exists in the binary tree. If no value is provided, the root of the binary tree will be used
1890
+ * as the starting point.
1891
+ * @param {BinaryTreePrintOptions} [options] - The `options` parameter is an optional object that
1892
+ * allows you to customize the printing behavior. It has the following properties:
1893
+ * @returns Nothing is being returned. The function has a return type of `void`, which means it does
1894
+ * not return any value.
1895
+ */
1896
+ override print(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
1835
1897
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1836
1898
  beginRoot = this.ensureNode(beginRoot);
1837
1899
  if (!beginRoot) return;
@@ -1840,10 +1902,10 @@ export class BinaryTree<
1840
1902
  console.log(`U for undefined
1841
1903
  `);
1842
1904
  if (opts.isShowNull)
1843
- console.log(`NODE for null
1905
+ console.log(`N for null
1844
1906
  `);
1845
1907
  if (opts.isShowRedBlackNIL)
1846
- console.log(`S for Sentinel Node
1908
+ console.log(`S for Sentinel Node(NIL)
1847
1909
  `);
1848
1910
 
1849
1911
  const display = (root: NODE | null | undefined): void => {
@@ -1857,13 +1919,19 @@ export class BinaryTree<
1857
1919
  }
1858
1920
 
1859
1921
  /**
1860
- * The function `_getIterator` is a protected generator function that returns an iterator for the
1861
- * key-value pairs in a binary search tree.
1862
- * @param node - The `node` parameter represents the current node in the binary search tree. It is an
1863
- * optional parameter with a default value of `this.root`, which means if no node is provided, the
1864
- * root node of the tree will be used as the starting point for iteration.
1865
- * @returns The function `_getIterator` returns an `IterableIterator` of key-value pairs `[K, V |
1866
- * undefined]`.
1922
+ * Time Complexity: O(1)
1923
+ * Space Complexity: O(1)
1924
+ */
1925
+
1926
+ /**
1927
+ * Time Complexity: O(1)
1928
+ * Space Complexity: O(1)
1929
+ *
1930
+ * The function `_getIterator` is a generator function that returns an iterator for the key-value
1931
+ * pairs in a binary search tree.
1932
+ * @param node - The `node` parameter represents the current node in the binary search tree. It is
1933
+ * initially set to the root node of the tree.
1934
+ * @returns an IterableIterator<[K, V | undefined]>.
1867
1935
  */
1868
1936
  protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1869
1937
  if (!node) return;
@@ -1873,30 +1941,38 @@ export class BinaryTree<
1873
1941
  let current: NODE | null | undefined = node;
1874
1942
 
1875
1943
  while (current || stack.length > 0) {
1876
- while (current && !isNaN(this.extractor(current.key))) {
1944
+ while (this.isRealNode(current)) {
1877
1945
  stack.push(current);
1878
1946
  current = current.left;
1879
1947
  }
1880
1948
 
1881
1949
  current = stack.pop();
1882
1950
 
1883
- if (current && !isNaN(this.extractor(current.key))) {
1951
+ if (this.isRealNode(current)) {
1884
1952
  yield [current.key, current.value];
1885
1953
  current = current.right;
1886
1954
  }
1887
1955
  }
1888
1956
  } else {
1889
- if (node.left && !isNaN(this.extractor(node.key))) {
1957
+ if (node.left && this.isRealNode(node)) {
1890
1958
  yield* this[Symbol.iterator](node.left);
1891
1959
  }
1892
1960
  yield [node.key, node.value];
1893
- if (node.right && !isNaN(this.extractor(node.key))) {
1961
+ if (node.right && this.isRealNode(node)) {
1894
1962
  yield* this[Symbol.iterator](node.right);
1895
1963
  }
1896
1964
  }
1897
1965
  }
1898
1966
 
1899
1967
  /**
1968
+ * Time Complexity: O(n)
1969
+ * Space Complexity: O(n)
1970
+ */
1971
+
1972
+ /**
1973
+ * Time Complexity: O(n)
1974
+ * Space Complexity: O(n)
1975
+ *
1900
1976
  * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1901
1977
  * taking into account various options such as whether to show null, undefined, or NaN nodes.
1902
1978
  * @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
@@ -1919,13 +1995,13 @@ export class BinaryTree<
1919
1995
  return emptyDisplayLayout;
1920
1996
  } else if (node === undefined && !isShowUndefined) {
1921
1997
  return emptyDisplayLayout;
1922
- } else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
1998
+ } else if (this.isNIL(node) && !isShowRedBlackNIL) {
1923
1999
  return emptyDisplayLayout;
1924
2000
  } else if (node !== null && node !== undefined) {
1925
2001
  // Display logic of normal nodes
1926
2002
 
1927
2003
  const key = node.key,
1928
- line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(),
2004
+ line = this.isNIL(node) ? 'S' : key.toString(),
1929
2005
  width = line.length;
1930
2006
 
1931
2007
  return _buildNodeDisplay(
@@ -1981,14 +2057,26 @@ export class BinaryTree<
1981
2057
  protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
1982
2058
 
1983
2059
  /**
1984
- * Swap the data of two nodes in the binary tree.
1985
- * @param {NODE} srcNode - The source node to swap.
1986
- * @param {NODE} destNode - The destination node to swap.
1987
- * @returns {NODE} - The destination node after the swap.
2060
+ * Time Complexity: O(1)
2061
+ * Space Complexity: O(1)
2062
+ */
2063
+
2064
+ /**
2065
+ * Time Complexity: O(1)
2066
+ * Space Complexity: O(1)
2067
+ *
2068
+ * The function `_swapProperties` swaps the key-value properties between two nodes.
2069
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
2070
+ * destination node. It can be either an instance of the class `R`, or an object of type
2071
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
2072
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
2073
+ * the properties will be swapped with the `srcNode`.
2074
+ * @returns either the `destNode` object with its properties swapped with the `srcNode` object's
2075
+ * properties, or `undefined` if either `srcNode` or `destNode` is falsy.
1988
2076
  */
1989
2077
  protected _swapProperties(
1990
- srcNode: KeyOrNodeOrEntry<K, V, NODE>,
1991
- destNode: KeyOrNodeOrEntry<K, V, NODE>
2078
+ srcNode: R | KeyOrNodeOrEntry<K, V, NODE>,
2079
+ destNode: R | KeyOrNodeOrEntry<K, V, NODE>
1992
2080
  ): NODE | undefined {
1993
2081
  srcNode = this.ensureNode(srcNode);
1994
2082
  destNode = this.ensureNode(destNode);
@@ -2011,12 +2099,21 @@ export class BinaryTree<
2011
2099
  }
2012
2100
 
2013
2101
  /**
2014
- * The function replaces an old node with a new node in a binary tree.
2102
+ * Time Complexity: O(1)
2103
+ * Space Complexity: O(1)
2104
+ */
2105
+
2106
+ /**
2107
+ * Time Complexity: O(1)
2108
+ * Space Complexity: O(1)
2109
+ *
2110
+ * The function replaces a node in a binary tree with a new node, updating the parent, left child,
2111
+ * right child, and root if necessary.
2015
2112
  * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
2016
2113
  * tree.
2017
2114
  * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
2018
2115
  * tree.
2019
- * @returns The method is returning the newNode.
2116
+ * @returns the newNode.
2020
2117
  */
2021
2118
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
2022
2119
  if (oldNode.parent) {
@@ -2037,10 +2134,18 @@ export class BinaryTree<
2037
2134
  }
2038
2135
 
2039
2136
  /**
2040
- * The function sets the root property of an object to a given value, and if the value is not null,
2041
- * it also sets the parent property of the value to undefined.
2042
- * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
2043
- * type `NODE` or `null`.
2137
+ * Time Complexity: O(1)
2138
+ * Space Complexity: O(1)
2139
+ */
2140
+
2141
+ /**
2142
+ * Time Complexity: O(1)
2143
+ * Space Complexity: O(1)
2144
+ *
2145
+ * The function sets the root property of an object to the provided value, and also updates the
2146
+ * parent property of the new root.
2147
+ * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
2148
+ * means that it can accept a value of type `NODE`, `null`, or `undefined`.
2044
2149
  */
2045
2150
  protected _setRoot(v: NODE | null | undefined) {
2046
2151
  if (v) {
@@ -2049,6 +2154,24 @@ export class BinaryTree<
2049
2154
  this._root = v;
2050
2155
  }
2051
2156
 
2157
+ /**
2158
+ * Time Complexity: O(1)
2159
+ * Space Complexity: O(1)
2160
+ */
2161
+
2162
+ /**
2163
+ * Time Complexity: O(1)
2164
+ * Space Complexity: O(1)
2165
+ *
2166
+ * The function `_ensureCallback` ensures that a callback function is provided and returns it.
2167
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is of type
2168
+ * `ReturnType<C> | null | undefined`. This means it can accept a value that is the return type of
2169
+ * the generic type `C`, or it can be `null` or `undefined`.
2170
+ * @param {C} callback - The `callback` parameter is a function that takes a `node` as an argument
2171
+ * and returns a value. It is of type `C`, which is a generic type that extends the
2172
+ * `BTNCallback<NODE>` type.
2173
+ * @returns the callback parameter.
2174
+ */
2052
2175
  protected _ensureCallback<C extends BTNCallback<NODE>>(
2053
2176
  identifier: ReturnType<C> | null | undefined,
2054
2177
  callback: C = this._DEFAULT_CALLBACK as C