deque-typed 1.51.8 → 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 (50) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
  4. package/dist/data-structures/binary-tree/avl-tree.js +78 -59
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
  6. package/dist/data-structures/binary-tree/binary-tree.js +471 -361
  7. package/dist/data-structures/binary-tree/bst.d.ts +198 -200
  8. package/dist/data-structures/binary-tree/bst.js +215 -249
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
  10. package/dist/data-structures/binary-tree/rb-tree.js +107 -98
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  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/queue/deque.d.ts +2 -3
  18. package/dist/data-structures/queue/deque.js +2 -3
  19. package/dist/data-structures/trie/trie.d.ts +1 -1
  20. package/dist/data-structures/trie/trie.js +1 -1
  21. package/dist/interfaces/binary-tree.d.ts +6 -6
  22. package/dist/types/common.d.ts +1 -2
  23. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  24. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  25. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
  26. package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
  27. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  28. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  29. package/dist/utils/utils.js +3 -5
  30. package/package.json +2 -2
  31. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
  32. package/src/data-structures/binary-tree/avl-tree.ts +94 -66
  33. package/src/data-structures/binary-tree/binary-tree.ts +530 -398
  34. package/src/data-structures/binary-tree/bst.ts +251 -270
  35. package/src/data-structures/binary-tree/rb-tree.ts +121 -100
  36. package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
  37. package/src/data-structures/graph/abstract-graph.ts +10 -10
  38. package/src/data-structures/hash/hash-map.ts +42 -49
  39. package/src/data-structures/queue/deque.ts +2 -2
  40. package/src/data-structures/queue/queue.ts +1 -1
  41. package/src/data-structures/trie/trie.ts +2 -2
  42. package/src/interfaces/binary-tree.ts +8 -7
  43. package/src/types/common.ts +1 -2
  44. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  45. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  46. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
  47. package/src/types/data-structures/binary-tree/bst.ts +4 -4
  48. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  49. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
  50. package/src/utils/utils.ts +3 -3
@@ -133,32 +133,35 @@ export class BinaryTreeNode<
133
133
  export class BinaryTree<
134
134
  K extends Comparable,
135
135
  V = any,
136
+ R = BTNEntry<K, V>,
136
137
  NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
137
- 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>>
138
139
  >
139
140
  extends IterableEntryBase<K, V | undefined>
140
- implements IBinaryTree<K, V, NODE, TREE> {
141
+ implements IBinaryTree<K, V, R, NODE, TREE> {
141
142
  iterationType: IterationType = 'ITERATIVE';
142
143
 
143
144
  /**
144
- * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
145
- * @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
146
147
  * nodes to be added to the binary tree.
147
148
  * @param [options] - The `options` parameter is an optional object that can contain additional
148
149
  * configuration options for the binary tree. In this case, it is of type
149
150
  * `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
150
151
  * required.
151
152
  */
152
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BinaryTreeOptions) {
153
+ constructor(
154
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
155
+ options?: BinaryTreeOptions<K, V, R>
156
+ ) {
153
157
  super();
154
158
  if (options) {
155
- const { iterationType } = options;
159
+ const { iterationType, toEntryFn } = options;
156
160
  if (iterationType) this.iterationType = iterationType;
161
+ if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
157
162
  }
158
163
 
159
- this._size = 0;
160
-
161
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
164
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
162
165
  }
163
166
 
164
167
  protected _root?: NODE | null;
@@ -172,7 +175,7 @@ export class BinaryTree<
172
175
  return this._root;
173
176
  }
174
177
 
175
- protected _size: number;
178
+ protected _size: number = 0;
176
179
 
177
180
  /**
178
181
  * The function returns the size of an object.
@@ -192,6 +195,16 @@ export class BinaryTree<
192
195
  return this._NIL;
193
196
  }
194
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
+
195
208
  /**
196
209
  * Creates a new instance of BinaryTreeNode with the given key and value.
197
210
  * @param {K} key - The key for the new node.
@@ -209,41 +222,46 @@ export class BinaryTree<
209
222
  * you can provide only a subset of the properties defined in the `BinaryTreeOptions` interface.
210
223
  * @returns a new instance of a binary tree.
211
224
  */
212
- createTree(options?: Partial<BinaryTreeOptions>): TREE {
213
- 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;
214
227
  }
215
228
 
216
229
  /**
217
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
218
- * @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>`.
219
234
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
220
- * `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
221
- * is provided, it will be `undefined`.
222
- * @returns a value of type NODE (node), or null, or undefined.
223
- */
224
- keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | null | undefined {
225
- if (keyOrNodeOrEntry === undefined) return;
226
-
227
- let node: NODE | null | undefined;
228
- if (keyOrNodeOrEntry === null) {
229
- node = null;
230
- } else if (this.isEntry(keyOrNodeOrEntry)) {
231
- const [key, value] = keyOrNodeOrEntry;
232
- if (key === undefined) {
233
- return;
234
- } else if (key === null) {
235
- node = null;
236
- } else {
237
- node = this.createNode(key, value);
238
- }
239
- } else if (this.isNode(keyOrNodeOrEntry)) {
240
- node = keyOrNodeOrEntry;
241
- } else if (!this.isNode(keyOrNodeOrEntry)) {
242
- node = this.createNode(keyOrNodeOrEntry, value);
243
- } else {
244
- 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);
245
260
  }
246
- return node;
261
+
262
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value);
263
+
264
+ return;
247
265
  }
248
266
 
249
267
  /**
@@ -255,81 +273,122 @@ export class BinaryTree<
255
273
  * Time Complexity: O(n)
256
274
  * Space Complexity: O(log n)
257
275
  *
258
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
259
- * key, otherwise it returns the key itself.
260
- * @param {K | NODE | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`,
261
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
262
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
263
- * type of iteration to be used when searching for a node by key. It has a default value of
264
- * `'ITERATIVE'`.
265
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
266
- * 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`.
267
285
  */
268
286
  ensureNode(
269
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
287
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
270
288
  iterationType: IterationType = 'ITERATIVE'
271
289
  ): NODE | null | undefined {
272
- if (keyOrNodeOrEntry === this.NIL) return;
273
- if (this.isRealNode(keyOrNodeOrEntry)) {
274
- 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);
275
298
  }
276
- if (this.isEntry(keyOrNodeOrEntry)) {
277
- const key = keyOrNodeOrEntry[0];
299
+
300
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
301
+ const key = keyOrNodeOrEntryOrRawElement[0];
278
302
  if (key === null) return null;
279
303
  if (key === undefined) return;
280
304
  return this.getNodeByKey(key, iterationType);
281
305
  }
282
- if (keyOrNodeOrEntry === null) return null;
283
- if (keyOrNodeOrEntry === undefined) return;
284
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
306
+
307
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.getNodeByKey(keyOrNodeOrEntryOrRawElement, iterationType);
308
+ return;
285
309
  }
286
310
 
287
311
  /**
288
- * The function checks if a given node is a real node or null.
289
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
290
- * @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.
291
317
  */
292
- isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
293
- return this.isRealNode(node) || node === null;
318
+ isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
319
+ return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
294
320
  }
295
321
 
296
322
  /**
297
- * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
298
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
299
- * @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.
300
327
  */
301
- isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
302
- 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);
303
331
  }
304
332
 
305
333
  /**
306
- * The function checks if a given node is a real node by verifying if it is an instance of
307
- * BinaryTreeNode and its key is not NaN.
308
- * @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>`.
309
337
  * @returns a boolean value.
310
338
  */
311
- isRealNode(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
312
- if (node === this.NIL || node === null || node === undefined) return false;
313
- return this.isNode(node);
339
+ isNodeOrNull(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
340
+ return this.isRealNode(node) || node === null;
314
341
  }
315
342
 
316
343
  /**
317
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
318
- * @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>`.
319
347
  * @returns a boolean value.
320
348
  */
321
- isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
349
+ isNIL(node: R | KeyOrNodeOrEntry<K, V, NODE>) {
322
350
  return node === this.NIL;
323
351
  }
324
352
 
325
353
  /**
326
- * The function checks if a given value is an entry in a binary tree node.
327
- * @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
328
- * 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>`.
358
+ * @returns a boolean value.
359
+ */
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().
329
374
  * @returns a boolean value.
330
375
  */
331
- isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
332
- return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
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;
333
392
  }
334
393
 
335
394
  /**
@@ -341,14 +400,20 @@ export class BinaryTree<
341
400
  * Time Complexity O(n)
342
401
  * Space Complexity O(1)
343
402
  *
344
- * The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
345
- * existing node with the same key.
346
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
347
- * @param {V} [value] - The value to be inserted into the binary tree.
348
- * @returns The function `add` returns either a node (`NODE`), `null`, or `undefined`.
349
- */
350
- add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
351
- 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);
352
417
  if (newNode === undefined) return false;
353
418
 
354
419
  // If the tree is empty, directly set the new node as the root node
@@ -410,13 +475,20 @@ export class BinaryTree<
410
475
  * Time Complexity: O(k * n)
411
476
  * Space Complexity: O(1)
412
477
  *
413
- * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
414
- * adds each node with its corresponding value to the data structure.
415
- * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
416
- * @param [values] - An optional iterable of values that will be assigned to each node being added.
417
- * @returns The function `addMany` returns an array of `NODE`, `null`, or `undefined` values.
418
- */
419
- 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[] {
420
492
  // TODO not sure addMany not be run multi times
421
493
  const inserted: boolean[] = [];
422
494
 
@@ -425,7 +497,7 @@ export class BinaryTree<
425
497
  valuesIterator = values[Symbol.iterator]();
426
498
  }
427
499
 
428
- for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
500
+ for (const keyOrNodeOrEntryOrRawElement of keysOrNodesOrEntriesOrRawElements) {
429
501
  let value: V | undefined | null = undefined;
430
502
 
431
503
  if (valuesIterator) {
@@ -435,7 +507,7 @@ export class BinaryTree<
435
507
  }
436
508
  }
437
509
 
438
- inserted.push(this.add(keyOrNodeOrEntry, value));
510
+ inserted.push(this.add(keyOrNodeOrEntryOrRawElement, value));
439
511
  }
440
512
 
441
513
  return inserted;
@@ -451,17 +523,19 @@ export class BinaryTree<
451
523
  * Time Complexity: O(k * n)
452
524
  * Space Complexity: O(1)
453
525
  *
454
- * The `refill` function clears the current data and adds new key-value pairs to the data structure.
455
- * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
456
- * KeyOrNodeOrEntry<K, V, NODE>.
457
- * @param [values] - The `values` parameter is an optional iterable that contains the values to be
458
- * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
459
- * the values will be associated with the corresponding keys or nodes or entries in the
460
- * `keysOrNodesOrEntries` iterable
461
- */
462
- 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 {
463
537
  this.clear();
464
- this.addMany(keysOrNodesOrEntries, values);
538
+ this.addMany(keysOrNodesOrEntriesOrRawElements, values);
465
539
  }
466
540
 
467
541
  delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
@@ -476,21 +550,21 @@ export class BinaryTree<
476
550
  /**
477
551
  * Time Complexity: O(n)
478
552
  * Space Complexity: O(1)
479
- * /
553
+ */
480
554
 
481
- /**
555
+ /**
482
556
  * Time Complexity: O(n)
483
557
  * Space Complexity: O(1)
484
558
  *
485
- * The function deletes a node from a binary tree and returns an array of the deleted nodes along
486
- * with the nodes that need to be balanced.
487
- * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
488
- * object that you want to delete from the binary tree. It can be of any type that is compatible with
489
- * the callback function's return type. It can also be null or undefined if you want to delete a
490
- * 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.
491
564
  * @param {C} callback - The `callback` parameter is a function that is used to determine the
492
- * identifier of the node to be deleted. It is optional and has a default value of
493
- * `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
494
568
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
495
569
  */
496
570
  delete<C extends BTNCallback<NODE>>(
@@ -545,7 +619,7 @@ export class BinaryTree<
545
619
  identifier: K,
546
620
  callback?: C,
547
621
  onlyOne?: boolean,
548
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
622
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
549
623
  iterationType?: IterationType
550
624
  ): NODE[];
551
625
 
@@ -553,7 +627,7 @@ export class BinaryTree<
553
627
  identifier: NODE | null | undefined,
554
628
  callback?: C,
555
629
  onlyOne?: boolean,
556
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
630
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
557
631
  iterationType?: IterationType
558
632
  ): NODE[];
559
633
 
@@ -561,7 +635,7 @@ export class BinaryTree<
561
635
  identifier: ReturnType<C>,
562
636
  callback: C,
563
637
  onlyOne?: boolean,
564
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
638
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
565
639
  iterationType?: IterationType
566
640
  ): NODE[];
567
641
 
@@ -572,34 +646,33 @@ export class BinaryTree<
572
646
 
573
647
  /**
574
648
  * Time Complexity: O(n)
575
- * Space Complexity: O(k + log n).
649
+ * Space Complexity: O(k + log n)
576
650
  *
577
- * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
578
- * callback function.
651
+ * The function `getNodes` returns an array of nodes that match a given identifier, using either a
652
+ * recursive or iterative approach.
579
653
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
580
- * that you want to search for in the binary tree. It can be of any type that is returned by the
581
- * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
582
- * specific value.
583
- * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
584
- * input and returns a value of type `C`. It is used to determine if a node matches the given
585
- * identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
586
- * default
587
- * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
588
- * matches the identifier. If set to true, the function will stop iterating once it finds a matching
589
- * node and return that node. If set to false (default), the function will continue iterating and
590
- * return all nodes that match the identifier.
591
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
592
- * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
593
- * it is `null` or `undefined`, an empty array will be returned.
594
- * @param iterationType - The `iterationType` parameter determines the type of iteration used to
595
- * traverse the binary tree. It can have two possible values:
596
- * @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.
597
670
  */
598
671
  getNodes<C extends BTNCallback<NODE>>(
599
672
  identifier: ReturnType<C> | null | undefined,
600
673
  callback: C = this._DEFAULT_CALLBACK as C,
601
674
  onlyOne = false,
602
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
675
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
603
676
  iterationType: IterationType = this.iterationType
604
677
  ): NODE[] {
605
678
  beginRoot = this.ensureNode(beginRoot);
@@ -641,21 +714,21 @@ export class BinaryTree<
641
714
  getNode<C extends BTNCallback<NODE, K>>(
642
715
  identifier: K,
643
716
  callback?: C,
644
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
717
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
645
718
  iterationType?: IterationType
646
719
  ): NODE | null | undefined;
647
720
 
648
721
  getNode<C extends BTNCallback<NODE, NODE>>(
649
722
  identifier: NODE | null | undefined,
650
723
  callback?: C,
651
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
724
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
652
725
  iterationType?: IterationType
653
726
  ): NODE | null | undefined;
654
727
 
655
728
  getNode<C extends BTNCallback<NODE>>(
656
729
  identifier: ReturnType<C>,
657
730
  callback: C,
658
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
731
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
659
732
  iterationType?: IterationType
660
733
  ): NODE | null | undefined;
661
734
 
@@ -666,29 +739,26 @@ export class BinaryTree<
666
739
 
667
740
  /**
668
741
  * Time Complexity: O(n)
669
- * Space Complexity: O(log n)
742
+ * Space Complexity: O(log n).
670
743
  *
671
- * The function `getNode` returns the first node that matches the given identifier and callback
672
- * 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.
673
746
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
674
- * used to identify the node you want to retrieve. It can be of any type that is returned by the
675
- * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
676
- * identifier.
677
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
678
- * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
679
- * function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
680
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
681
- * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
682
- * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
683
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
684
- * be performed when searching for nodes in the binary tree. It determines the order in which the
685
- * nodes are visited during the search.
686
- * @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.
687
757
  */
688
758
  getNode<C extends BTNCallback<NODE>>(
689
759
  identifier: ReturnType<C> | null | undefined,
690
760
  callback: C = this._DEFAULT_CALLBACK as C,
691
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
761
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
692
762
  iterationType: IterationType = this.iterationType
693
763
  ): NODE | null | undefined {
694
764
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
@@ -703,15 +773,13 @@ export class BinaryTree<
703
773
  * Time Complexity: O(n)
704
774
  * Space Complexity: O(log n)
705
775
  *
706
- * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
707
- * recursive or iterative iteration.
708
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
709
- * It is used to find the node with the matching key value.
710
- * @param iterationType - The `iterationType` parameter is used to determine whether the search for
711
- * the node with the given key should be performed iteratively or recursively. It has two possible
712
- * values:
713
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
714
- * 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.
715
783
  */
716
784
  getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
717
785
  return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
@@ -720,21 +788,21 @@ export class BinaryTree<
720
788
  override get<C extends BTNCallback<NODE, K>>(
721
789
  identifier: K,
722
790
  callback?: C,
723
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
791
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
724
792
  iterationType?: IterationType
725
793
  ): V | undefined;
726
794
 
727
795
  override get<C extends BTNCallback<NODE, NODE>>(
728
796
  identifier: NODE | null | undefined,
729
797
  callback?: C,
730
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
798
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
731
799
  iterationType?: IterationType
732
800
  ): V | undefined;
733
801
 
734
802
  override get<C extends BTNCallback<NODE>>(
735
803
  identifier: ReturnType<C>,
736
804
  callback: C,
737
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
805
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
738
806
  iterationType?: IterationType
739
807
  ): V | undefined;
740
808
 
@@ -747,28 +815,27 @@ export class BinaryTree<
747
815
  * Time Complexity: O(n)
748
816
  * Space Complexity: O(log n)
749
817
  *
750
- * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
751
- * and callback function.
818
+ * The function `get` in TypeScript overrides the base class method and returns the value associated
819
+ * with the given identifier.
752
820
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
753
- * 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
754
822
  * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
755
- * @param {C} callback - The `callback` parameter is a function that will be called with each node in
756
- * the binary tree. It is used to determine whether a node matches the given identifier. The callback
757
- * function should return a value that can be compared to the identifier to determine if it is a
758
- * match.
759
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
760
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
761
- * node), a node object of type `NODE`, or `null`/`undefined` to start the search from the root of
762
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
763
- * be performed when searching for a node in the binary tree. It is an optional parameter with a
764
- * default value specified by `this.iterationType`.
765
- * @returns The value of the node with the given identifier is being returned. If the node is not
766
- * 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.
767
834
  */
768
835
  override get<C extends BTNCallback<NODE>>(
769
836
  identifier: ReturnType<C> | null | undefined,
770
837
  callback: C = this._DEFAULT_CALLBACK as C,
771
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
838
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
772
839
  iterationType: IterationType = this.iterationType
773
840
  ): V | undefined {
774
841
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
@@ -777,54 +844,53 @@ export class BinaryTree<
777
844
  override has<C extends BTNCallback<NODE, K>>(
778
845
  identifier: K,
779
846
  callback?: C,
780
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
847
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
781
848
  iterationType?: IterationType
782
849
  ): boolean;
783
850
 
784
851
  override has<C extends BTNCallback<NODE, NODE>>(
785
852
  identifier: NODE | null | undefined,
786
853
  callback?: C,
787
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
854
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
788
855
  iterationType?: IterationType
789
856
  ): boolean;
790
857
 
791
858
  override has<C extends BTNCallback<NODE>>(
792
859
  identifier: ReturnType<C> | null | undefined,
793
860
  callback: C,
794
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
861
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
795
862
  iterationType?: IterationType
796
863
  ): boolean;
797
864
 
798
865
  /**
799
866
  * Time Complexity: O(n)
800
- * Space Complexity: O(log n).
867
+ * Space Complexity: O(log n)
801
868
  */
802
869
 
803
870
  /**
804
871
  * Time Complexity: O(n)
805
- * Space Complexity: O(log n).
872
+ * Space Complexity: O(log n)
806
873
  *
807
- * 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.
808
876
  * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
809
- * that you want to search for in the binary tree. It can be of any type that is returned by the
810
- * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
811
- * specific identifier.
812
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
813
- * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
814
- * function should return a boolean value indicating whether the node should be included in the
815
- * result or not.
816
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
817
- * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
818
- * node in the binary tree), a node object (`NODE`), or `null`/`undefined` to start the search from
819
- * @param iterationType - The `iterationType` parameter is a variable that determines the type of
820
- * iteration to be performed on the binary tree. It is used to specify whether the iteration should
821
- * be performed in a pre-order, in-order, or post-order manner.
822
- * @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.
823
889
  */
824
890
  override has<C extends BTNCallback<NODE>>(
825
891
  identifier: ReturnType<C> | null | undefined,
826
892
  callback: C = this._DEFAULT_CALLBACK as C,
827
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
893
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
828
894
  iterationType: IterationType = this.iterationType
829
895
  ): boolean {
830
896
  callback = this._ensureCallback(identifier, callback);
@@ -875,12 +941,13 @@ export class BinaryTree<
875
941
  *
876
942
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
877
943
  * height of the tree.
878
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
879
- * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
880
- * 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
881
948
  * @returns a boolean value.
882
949
  */
883
- isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
950
+ isPerfectlyBalanced(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
884
951
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
885
952
  }
886
953
 
@@ -893,16 +960,18 @@ export class BinaryTree<
893
960
  * Time Complexity: O(n)
894
961
  * Space Complexity: O(1)
895
962
  *
896
- * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
897
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the root
898
- * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
899
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
900
- * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
901
- * 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:
902
971
  * @returns a boolean value.
903
972
  */
904
973
  isBST(
905
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
974
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
906
975
  iterationType: IterationType = this.iterationType
907
976
  ): boolean {
908
977
  // TODO there is a bug
@@ -954,16 +1023,17 @@ export class BinaryTree<
954
1023
  * Time Complexity: O(n)
955
1024
  * Space Complexity: O(1)
956
1025
  *
957
- * The function calculates the depth of a given node in a binary tree.
958
- * @param {K | NODE | null | undefined} dist - The `dist` parameter represents the node in
959
- * the binary tree whose depth we want to find. It can be of type `K`, `NODE`, `null`, or
960
- * `undefined`.
961
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
962
- * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
963
- * `NODE` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
964
- * @returns the depth of the `dist` relative to the `beginRoot`.
965
- */
966
- getDepth(dist: KeyOrNodeOrEntry<K, V, NODE>, beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
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 {
967
1037
  let distEnsured = this.ensureNode(dist);
968
1038
  const beginRootEnsured = this.ensureNode(beginRoot);
969
1039
  let depth = 0;
@@ -984,20 +1054,19 @@ export class BinaryTree<
984
1054
 
985
1055
  /**
986
1056
  * Time Complexity: O(n)
987
- * Space Complexity: O(log n)
1057
+ * Space Complexity: O(1)
988
1058
  *
989
- * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
990
- * iterative traversal.
991
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
992
- * starting node of the binary tree from which we want to calculate the height. It can be of type
993
- * `K`, `NODE`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
994
- * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
995
- * height of the tree using a recursive approach or an iterative approach. It can have two possible
996
- * values:
997
- * @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.
998
1067
  */
999
1068
  getHeight(
1000
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1069
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1001
1070
  iterationType: IterationType = this.iterationType
1002
1071
  ): number {
1003
1072
  beginRoot = this.ensureNode(beginRoot);
@@ -1040,15 +1109,18 @@ export class BinaryTree<
1040
1109
  *
1041
1110
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
1042
1111
  * recursive or iterative approach.
1043
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1044
- * starting node of the binary tree from which we want to calculate the minimum height. It can be of
1045
- * type `K`, `NODE`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
1046
- * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
1047
- * to calculate the minimum height of a binary tree. It can have two possible values:
1048
- * @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.
1049
1121
  */
1050
1122
  getMinHeight(
1051
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1123
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1052
1124
  iterationType: IterationType = this.iterationType
1053
1125
  ): number {
1054
1126
  beginRoot = this.ensureNode(beginRoot);
@@ -1096,24 +1168,22 @@ export class BinaryTree<
1096
1168
  /**
1097
1169
  * Time Complexity: O(log n)
1098
1170
  * Space Complexity: O(log n)
1099
- * /
1171
+ */
1100
1172
 
1101
- /**
1173
+ /**
1102
1174
  * Time Complexity: O(log n)
1103
1175
  * Space Complexity: O(log n)
1104
1176
  *
1105
- * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
1106
- * structure, with the option to reverse the order of the nodes.
1107
- * @param {K | NODE | null | undefined} beginNode - The `beginRoot` parameter represents the
1108
- * starting node from which you want to find the path to the root. It can be of type `K`, `NODE`,
1109
- * `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>`.
1110
1181
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
1111
1182
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
1112
- * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
1113
- * @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.
1114
1185
  */
1115
- getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1116
- // TODO to support get path through passing key
1186
+ getPathToRoot(beginNode: R | KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
1117
1187
  const result: NODE[] = [];
1118
1188
  let beginNodeEnsured = this.ensureNode(beginNode);
1119
1189
 
@@ -1121,7 +1191,6 @@ export class BinaryTree<
1121
1191
 
1122
1192
  while (beginNodeEnsured.parent) {
1123
1193
  // Array.push + Array.reverse is more efficient than Array.unshift
1124
- // TODO may consider using Deque, so far this is not the performance bottleneck
1125
1194
  result.push(beginNodeEnsured);
1126
1195
  beginNodeEnsured = beginNodeEnsured.parent;
1127
1196
  }
@@ -1138,18 +1207,17 @@ export class BinaryTree<
1138
1207
  * Time Complexity: O(log n)
1139
1208
  * Space Complexity: O(1)
1140
1209
  *
1141
- * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
1142
- * iteratively.
1143
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
1144
- * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `NODE` (a
1145
- * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
1146
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
1147
- * be performed when finding the leftmost node in a binary tree. It can have two possible values:
1148
- * @returns The function `getLeftMost` returns the leftmost node (`NODE`) in the binary tree. If there
1149
- * 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.
1150
1218
  */
1151
1219
  getLeftMost(
1152
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1220
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1153
1221
  iterationType: IterationType = this.iterationType
1154
1222
  ): NODE | null | undefined {
1155
1223
  if (this.isNIL(beginRoot)) return beginRoot as NODE;
@@ -1184,19 +1252,18 @@ export class BinaryTree<
1184
1252
  * Time Complexity: O(log n)
1185
1253
  * Space Complexity: O(1)
1186
1254
  *
1187
- * 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
1188
1256
  * iteratively.
1189
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1190
- * starting node from which we want to find the rightmost node. It can be of type `K`, `NODE`,
1191
- * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
1192
- * current object.
1193
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
1194
- * type of iteration to use when finding the rightmost node. It can have one of two values:
1195
- * @returns The function `getRightMost` returns the rightmost node (`NODE`) in a binary tree. If there
1196
- * 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`.
1197
1264
  */
1198
1265
  getRightMost(
1199
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1266
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1200
1267
  iterationType: IterationType = this.iterationType
1201
1268
  ): NODE | null | undefined {
1202
1269
  if (this.isNIL(beginRoot)) return beginRoot as NODE;
@@ -1231,10 +1298,10 @@ export class BinaryTree<
1231
1298
  * Time Complexity: O(log n)
1232
1299
  * Space Complexity: O(1)
1233
1300
  *
1234
- * The function returns the predecessor of a given node in a tree.
1235
- * @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
1236
1303
  * tree.
1237
- * @returns the predecessor of the given 'node'.
1304
+ * @returns the predecessor node of the given node.
1238
1305
  */
1239
1306
  getPredecessor(node: NODE): NODE {
1240
1307
  if (this.isRealNode(node.left)) {
@@ -1261,8 +1328,8 @@ export class BinaryTree<
1261
1328
  *
1262
1329
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1263
1330
  * @param {K | NODE | null} [x] - The parameter `x` can be of type `K`, `NODE`, or `null`.
1264
- * @returns the successor of the given node or key. The successor is the node that comes immediately
1265
- * 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.
1266
1333
  */
1267
1334
  getSuccessor(x?: K | NODE | null): NODE | null | undefined {
1268
1335
  x = this.ensureNode(x);
@@ -1283,7 +1350,7 @@ export class BinaryTree<
1283
1350
  dfs<C extends BTNCallback<NODE>>(
1284
1351
  callback?: C,
1285
1352
  pattern?: DFSOrderPattern,
1286
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1353
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1287
1354
  iterationType?: IterationType,
1288
1355
  includeNull?: false
1289
1356
  ): ReturnType<C>[];
@@ -1291,7 +1358,7 @@ export class BinaryTree<
1291
1358
  dfs<C extends BTNCallback<NODE | null>>(
1292
1359
  callback?: C,
1293
1360
  pattern?: DFSOrderPattern,
1294
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1361
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1295
1362
  iterationType?: IterationType,
1296
1363
  includeNull?: true
1297
1364
  ): ReturnType<C>[];
@@ -1299,35 +1366,35 @@ export class BinaryTree<
1299
1366
  /**
1300
1367
  * Time complexity: O(n)
1301
1368
  * Space complexity: O(n)
1302
- * /
1369
+ */
1303
1370
 
1304
- /**
1371
+ /**
1305
1372
  * Time complexity: O(n)
1306
1373
  * Space complexity: O(n)
1307
1374
  *
1308
- * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1309
- * specified pattern and iteration type, and returns an array of values obtained from applying a
1310
- * callback function to each visited node.
1311
- * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1312
- * the tree during the depth-first search. It takes a single parameter, which can be of type `NODE`,
1313
- * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1314
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1315
- * nodes are traversed during the depth-first search. It can have one of the following values:
1316
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1317
- * for the depth-first search traversal. It can be specified as a key, a node object, or
1318
- * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1319
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1320
- * 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:
1321
1388
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1322
- * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1323
- * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1324
- * `false`, null or undefined
1325
- * @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.
1326
1393
  */
1327
1394
  dfs<C extends BTNCallback<NODE | null | undefined>>(
1328
1395
  callback: C = this._DEFAULT_CALLBACK as C,
1329
1396
  pattern: DFSOrderPattern = 'IN',
1330
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1397
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1331
1398
  iterationType: IterationType = 'ITERATIVE',
1332
1399
  includeNull = false
1333
1400
  ): ReturnType<C>[] {
@@ -1421,14 +1488,14 @@ export class BinaryTree<
1421
1488
 
1422
1489
  bfs<C extends BTNCallback<NODE>>(
1423
1490
  callback?: C,
1424
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1491
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1425
1492
  iterationType?: IterationType,
1426
1493
  includeNull?: false
1427
1494
  ): ReturnType<C>[];
1428
1495
 
1429
1496
  bfs<C extends BTNCallback<NODE | null>>(
1430
1497
  callback?: C,
1431
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1498
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1432
1499
  iterationType?: IterationType,
1433
1500
  includeNull?: true
1434
1501
  ): ReturnType<C>[];
@@ -1442,26 +1509,27 @@ export class BinaryTree<
1442
1509
  * Time complexity: O(n)
1443
1510
  * Space complexity: O(n)
1444
1511
  *
1445
- * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1446
- * 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.
1447
1514
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1448
- * 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
1449
1516
  * visited, and returns a value of any type.
1450
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1451
- * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1452
- * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1453
- * the class is used as
1454
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1455
- * performed during the breadth-first search (BFS). It can have two possible values:
1456
- * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1457
- * to include null values in the breadth-first search traversal. If `includeNull` is set to
1458
- * `true`, null values will be included in the traversal, otherwise they will be skipped.
1459
- * @returns an array of values that are the result of invoking the callback function on each node in
1460
- * 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.
1461
1529
  */
1462
1530
  bfs<C extends BTNCallback<NODE | null>>(
1463
1531
  callback: C = this._DEFAULT_CALLBACK as C,
1464
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1532
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1465
1533
  iterationType: IterationType = this.iterationType,
1466
1534
  includeNull = false
1467
1535
  ): ReturnType<C>[] {
@@ -1515,14 +1583,14 @@ export class BinaryTree<
1515
1583
 
1516
1584
  listLevels<C extends BTNCallback<NODE>>(
1517
1585
  callback?: C,
1518
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1586
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1519
1587
  iterationType?: IterationType,
1520
1588
  includeNull?: false
1521
1589
  ): ReturnType<C>[][];
1522
1590
 
1523
1591
  listLevels<C extends BTNCallback<NODE | null>>(
1524
1592
  callback?: C,
1525
- beginRoot?: KeyOrNodeOrEntry<K, V, NODE>,
1593
+ beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
1526
1594
  iterationType?: IterationType,
1527
1595
  includeNull?: true
1528
1596
  ): ReturnType<C>[][];
@@ -1537,25 +1605,25 @@ export class BinaryTree<
1537
1605
  * Space complexity: O(n)
1538
1606
  *
1539
1607
  * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1540
- * a binary tree and contains the values returned by a callback function applied to the nodes at that
1541
- * level.
1608
+ * a binary tree and contains the results of applying a callback function to the nodes at that level.
1542
1609
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1543
- * the tree. It takes a single parameter, which can be of type `NODE`, `null`, or `undefined`, and
1544
- * returns a value of any type.
1545
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter represents the
1546
- * starting node for traversing the tree. It can be either a node object (`NODE`), a key value
1547
- * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1548
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1549
- * 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:
1550
1618
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1551
- * 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`,
1552
1620
  * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1553
1621
  * be excluded
1554
1622
  * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1555
1623
  */
1556
1624
  listLevels<C extends BTNCallback<NODE | null>>(
1557
1625
  callback: C = this._DEFAULT_CALLBACK as C,
1558
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
1626
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
1559
1627
  iterationType: IterationType = this.iterationType,
1560
1628
  includeNull = false
1561
1629
  ): ReturnType<C>[][] {
@@ -1612,22 +1680,22 @@ export class BinaryTree<
1612
1680
  * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1613
1681
  * algorithm.
1614
1682
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1615
- * the tree. It takes a single parameter of type `NODE` (the type of the nodes in the tree) and returns
1616
- * a value of any type.
1617
- * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1618
- * 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
1619
1688
  * following values:
1620
- * @param {K | NODE | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1621
- * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1622
- * the root of the tree. If no value is provided, the default value is the root of the tree.
1623
- * @returns The function `morris` returns an array of values that are the result of invoking the
1624
- * `callback` function on each node in the binary tree. The type of the array nodes is determined
1625
- * 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`.
1626
1694
  */
1627
1695
  morris<C extends BTNCallback<NODE>>(
1628
1696
  callback: C = this._DEFAULT_CALLBACK as C,
1629
1697
  pattern: DFSOrderPattern = 'IN',
1630
- beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
1698
+ beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root
1631
1699
  ): ReturnType<C>[] {
1632
1700
  beginRoot = this.ensureNode(beginRoot);
1633
1701
  if (beginRoot === null) return [];
@@ -1719,8 +1787,7 @@ export class BinaryTree<
1719
1787
  * Time complexity: O(n)
1720
1788
  * Space complexity: O(n)
1721
1789
  *
1722
- * The `clone` function creates a new tree object and copies all the nodes from the original tree to
1723
- * the new tree.
1790
+ * The `clone` function creates a deep copy of a tree object.
1724
1791
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
1725
1792
  */
1726
1793
  clone(): TREE {
@@ -1746,16 +1813,16 @@ export class BinaryTree<
1746
1813
  * Time Complexity: O(n)
1747
1814
  * Space Complexity: O(n)
1748
1815
  *
1749
- * The `filter` function creates a new tree by iterating over the nodes of the current tree and
1750
- * adding only the nodes that satisfy the given predicate function.
1751
- * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1752
- * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1753
- * included in the filtered tree or not.
1754
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1755
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1756
- * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1757
- * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1758
- * 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.
1759
1826
  */
1760
1827
  filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
1761
1828
  const newTree = this.createTree();
@@ -1777,15 +1844,15 @@ export class BinaryTree<
1777
1844
  * Time Complexity: O(n)
1778
1845
  * Space Complexity: O(n)
1779
1846
  *
1780
- * The `map` function creates a new tree by applying a callback function to each key-value pair in
1781
- * the original tree.
1782
- * @param callback - The callback parameter is a function that will be called for each key-value pair
1783
- * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1784
- * the index of the current pair, and a reference to the tree itself. The callback function should
1785
- * return a new
1786
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1787
- * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1788
- * 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
1789
1856
  * @returns The `map` method is returning a new tree object.
1790
1857
  */
1791
1858
  map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
@@ -1816,13 +1883,17 @@ export class BinaryTree<
1816
1883
  * Time Complexity: O(n)
1817
1884
  * Space Complexity: O(n)
1818
1885
  *
1819
- * The `print` function is used to display a binary tree structure in a visually appealing way.
1820
- * @param {K | NODE | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | NODE | null |
1821
- * undefined`. It represents the root node of a binary tree. The root node can have one of the
1822
- * following types:
1823
- * @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.
1824
- */
1825
- 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 {
1826
1897
  const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
1827
1898
  beginRoot = this.ensureNode(beginRoot);
1828
1899
  if (!beginRoot) return;
@@ -1848,13 +1919,19 @@ export class BinaryTree<
1848
1919
  }
1849
1920
 
1850
1921
  /**
1851
- * The function `_getIterator` is a protected generator function that returns an iterator for the
1852
- * key-value pairs in a binary search tree.
1853
- * @param node - The `node` parameter represents the current node in the binary search tree. It is an
1854
- * optional parameter with a default value of `this.root`, which means if no node is provided, the
1855
- * root node of the tree will be used as the starting point for iteration.
1856
- * @returns The function `_getIterator` returns an `IterableIterator` of key-value pairs `[K, V |
1857
- * 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]>.
1858
1935
  */
1859
1936
  protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
1860
1937
  if (!node) return;
@@ -1888,6 +1965,14 @@ export class BinaryTree<
1888
1965
  }
1889
1966
 
1890
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
+ *
1891
1976
  * The `_displayAux` function is responsible for generating the display layout of a binary tree node,
1892
1977
  * taking into account various options such as whether to show null, undefined, or NaN nodes.
1893
1978
  * @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
@@ -1972,14 +2057,26 @@ export class BinaryTree<
1972
2057
  protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
1973
2058
 
1974
2059
  /**
1975
- * Swap the data of two nodes in the binary tree.
1976
- * @param {NODE} srcNode - The source node to swap.
1977
- * @param {NODE} destNode - The destination node to swap.
1978
- * @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.
1979
2076
  */
1980
2077
  protected _swapProperties(
1981
- srcNode: KeyOrNodeOrEntry<K, V, NODE>,
1982
- destNode: KeyOrNodeOrEntry<K, V, NODE>
2078
+ srcNode: R | KeyOrNodeOrEntry<K, V, NODE>,
2079
+ destNode: R | KeyOrNodeOrEntry<K, V, NODE>
1983
2080
  ): NODE | undefined {
1984
2081
  srcNode = this.ensureNode(srcNode);
1985
2082
  destNode = this.ensureNode(destNode);
@@ -2002,12 +2099,21 @@ export class BinaryTree<
2002
2099
  }
2003
2100
 
2004
2101
  /**
2005
- * 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.
2006
2112
  * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
2007
2113
  * tree.
2008
2114
  * @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
2009
2115
  * tree.
2010
- * @returns The method is returning the newNode.
2116
+ * @returns the newNode.
2011
2117
  */
2012
2118
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
2013
2119
  if (oldNode.parent) {
@@ -2028,10 +2134,18 @@ export class BinaryTree<
2028
2134
  }
2029
2135
 
2030
2136
  /**
2031
- * The function sets the root property of an object to a given value, and if the value is not null,
2032
- * it also sets the parent property of the value to undefined.
2033
- * @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`, which means it can either be of
2034
- * 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`.
2035
2149
  */
2036
2150
  protected _setRoot(v: NODE | null | undefined) {
2037
2151
  if (v) {
@@ -2040,6 +2154,24 @@ export class BinaryTree<
2040
2154
  this._root = v;
2041
2155
  }
2042
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
+ */
2043
2175
  protected _ensureCallback<C extends BTNCallback<NODE>>(
2044
2176
  identifier: ReturnType<C> | null | undefined,
2045
2177
  callback: C = this._DEFAULT_CALLBACK as C