min-heap-typed 1.42.8 → 1.43.0

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 (49) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
  2. package/dist/data-structures/binary-tree/avl-tree.js +88 -23
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
  4. package/dist/data-structures/binary-tree/binary-tree.js +415 -236
  5. package/dist/data-structures/binary-tree/bst.d.ts +121 -66
  6. package/dist/data-structures/binary-tree/bst.js +121 -67
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
  8. package/dist/data-structures/binary-tree/rb-tree.js +95 -18
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  10. package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
  11. package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
  12. package/dist/data-structures/graph/abstract-graph.js +147 -36
  13. package/dist/data-structures/graph/directed-graph.d.ts +126 -0
  14. package/dist/data-structures/graph/directed-graph.js +126 -0
  15. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  16. package/dist/data-structures/graph/undirected-graph.js +63 -0
  17. package/dist/data-structures/heap/heap.d.ts +175 -12
  18. package/dist/data-structures/heap/heap.js +175 -12
  19. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  20. package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
  21. package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  22. package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
  23. package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  24. package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
  25. package/dist/data-structures/queue/deque.d.ts +113 -3
  26. package/dist/data-structures/queue/deque.js +113 -3
  27. package/dist/data-structures/queue/queue.d.ts +87 -0
  28. package/dist/data-structures/queue/queue.js +87 -0
  29. package/dist/data-structures/stack/stack.d.ts +42 -0
  30. package/dist/data-structures/stack/stack.js +42 -0
  31. package/dist/data-structures/trie/trie.d.ts +76 -0
  32. package/dist/data-structures/trie/trie.js +76 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  35. package/src/data-structures/binary-tree/binary-tree.ts +465 -256
  36. package/src/data-structures/binary-tree/bst.ts +130 -68
  37. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  38. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  39. package/src/data-structures/graph/abstract-graph.ts +133 -7
  40. package/src/data-structures/graph/directed-graph.ts +145 -1
  41. package/src/data-structures/graph/undirected-graph.ts +72 -0
  42. package/src/data-structures/heap/heap.ts +201 -12
  43. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  44. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  45. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  46. package/src/data-structures/queue/deque.ts +127 -3
  47. package/src/data-structures/queue/queue.ts +99 -0
  48. package/src/data-structures/stack/stack.ts +48 -0
  49. package/src/data-structures/trie/trie.ts +87 -4
@@ -108,8 +108,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
108
108
  * @template N - The type of the binary tree's nodes.
109
109
  */
110
110
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
111
- implements IBinaryTree<V, N>
112
- {
111
+ implements IBinaryTree<V, N> {
113
112
  iterationType: IterationType = IterationType.ITERATIVE;
114
113
 
115
114
  /**
@@ -153,28 +152,38 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
153
152
  }
154
153
 
155
154
  /**
156
- * Add a node with the given key and value to the binary tree.
157
- * @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
158
- * @param {V} value - The value for the new node (optional).
159
- * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
155
+ * Time Complexity: O(n)
156
+ * Space Complexity: O(1)
157
+ * Comments: The time complexity for adding a node depends on the depth of the tree. In the best case (when the tree is empty), it's O(1). In the worst case (when the tree is a degenerate tree), it's O(n). The space complexity is constant.
158
+ */
159
+
160
+ /**
161
+ * Time Complexity: O(n)
162
+ * Space Complexity: O(1)
163
+ *
164
+ * The `add` function adds a new node with a key and value to a binary tree, or updates the value of
165
+ * an existing node with the same key.
166
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
167
+ * following types:
168
+ * @param {V} [value] - The value to be associated with the key or node being added to the binary
169
+ * tree.
170
+ * @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
171
+ * tree, or `null` or `undefined` if the insertion was not successful.
160
172
  */
161
173
  add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | null | undefined {
162
174
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
163
- const queue = new Queue<N | null>([root]);
175
+ const queue = new Queue<N>([root]);
164
176
  while (queue.size > 0) {
165
- const cur = queue.shift();
166
- if (cur) {
167
- if (newNode && cur.key === newNode.key) {
168
- cur.value = newNode.value;
169
- return;
170
- }
171
- const inserted = this._addTo(newNode, cur);
172
- if (inserted !== undefined) return inserted;
173
- if (cur.left) queue.push(cur.left);
174
- if (cur.right) queue.push(cur.right);
175
- } else return;
177
+ const cur = queue.shift()!;
178
+ if (newNode && cur.key === newNode.key) {
179
+ cur.value = newNode.value;
180
+ return;
181
+ }
182
+ const inserted = this._addTo(newNode, cur);
183
+ if (inserted !== undefined) return inserted;
184
+ if (cur.left) queue.push(cur.left);
185
+ if (cur.right) queue.push(cur.right);
176
186
  }
177
- return;
178
187
  };
179
188
 
180
189
  let inserted: N | null | undefined, needInsert: N | null | undefined;
@@ -204,16 +213,25 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
204
213
  }
205
214
 
206
215
  /**
207
- * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
208
- * values, and adds them to the binary tree.
209
- * @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
210
- * objects, or null values.
211
- * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
212
- * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
213
- * the value of the nodes will be `undefined`.
216
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
217
+ * Space Complexity: O(1)
218
+ */
219
+
220
+ /**
221
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
222
+ * Space Complexity: O(1)
223
+ *
224
+ * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
225
+ * each key-value pair to a data structure.
226
+ * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
227
+ * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
228
+ * or `undefined`.
229
+ * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
230
+ * correspond to the keys or nodes being added. If provided, the values will be associated with the
231
+ * keys or nodes during the add operation.
214
232
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
215
233
  */
216
- addMany(keysOrNodes: (BTNKey | N |null | undefined)[], values?: (V | undefined)[]): (N | null | undefined)[] {
234
+ addMany(keysOrNodes: (BTNKey | N | null | undefined)[], values?: (V | undefined)[]): (N | null | undefined)[] {
217
235
  // TODO not sure addMany not be run multi times
218
236
  return keysOrNodes.map((keyOrNode, i) => {
219
237
  if (keyOrNode instanceof BinaryTreeNode) {
@@ -230,6 +248,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
230
248
  }
231
249
 
232
250
  /**
251
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
252
+ * Space Complexity: O(1)
253
+ */
254
+
255
+ /**
256
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
257
+ * Space Complexity: O(1)
258
+ *
233
259
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
234
260
  * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
235
261
  * `BTNKey` or `N` values.
@@ -250,18 +276,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
250
276
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BiTreeDeleteResult<N>[];
251
277
 
252
278
  /**
253
- * The `delete` function removes a node from a binary search tree and returns the deleted node along
254
- * with the parent node that needs to be balanced.
255
- * a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
256
- * binary tree.
257
- * @returns an array of `BiTreeDeleteResult<N>` objects.
258
- * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
259
- * `BTNKey` or a generic type `N`. It represents the property of the node that we are
260
- * searching for. It can be a specific key value or any other property of the node.
261
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
262
- * value. This value is compared with the `identifier` parameter to determine if the node should be
263
- * included in the result. The `callback` parameter has a default value of
264
- * `this._defaultOneParamCallback`, which
279
+ * Time Complexity: O(n)
280
+ * Space Complexity: O(1)
281
+ */
282
+
283
+ /**
284
+ * Time Complexity: O(n)
285
+ * Space Complexity: O(1)
286
+ *
287
+ * The function deletes a node from a binary tree and returns an array of the deleted nodes along
288
+ * with the nodes that need to be balanced.
289
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
290
+ * object that you want to delete from the binary tree. It can be of any type that is compatible with
291
+ * the callback function's return type. It can also be null or undefined if you want to delete a
292
+ * specific node based on its value or object.
293
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
294
+ * identifier of the node to be deleted. It is optional and has a default value of
295
+ * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
296
+ * @returns an array of `BiTreeDeleteResult<N>`.
265
297
  */
266
298
  delete<C extends BTNCallback<N>>(
267
299
  identifier: ReturnType<C> | null | undefined,
@@ -269,7 +301,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
269
301
  ): BiTreeDeleteResult<N>[] {
270
302
  const deletedResult: BiTreeDeleteResult<N>[] = [];
271
303
  if (!this.root) return deletedResult;
272
- if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
304
+ if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
273
305
 
274
306
  const curr = this.getNode(identifier, callback);
275
307
  if (!curr) return deletedResult;
@@ -292,17 +324,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
292
324
  needBalanced = parent;
293
325
  }
294
326
  } else {
295
- const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
296
- if (leftSubTreeRightMost) {
297
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
298
- orgCurrent = this._swap(curr, leftSubTreeRightMost);
299
- if (parentOfLeftSubTreeMax) {
300
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
301
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
302
- else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
303
- needBalanced = parentOfLeftSubTreeMax;
327
+ if (curr.left) {
328
+ const leftSubTreeRightMost = this.getRightMost(curr.left);
329
+ if (leftSubTreeRightMost) {
330
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
331
+ orgCurrent = this._swap(curr, leftSubTreeRightMost);
332
+ if (parentOfLeftSubTreeMax) {
333
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
334
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
335
+ else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
336
+ needBalanced = parentOfLeftSubTreeMax;
337
+ }
304
338
  }
305
339
  }
340
+
306
341
  }
307
342
  this._size = this.size - 1;
308
343
 
@@ -311,15 +346,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
311
346
  }
312
347
 
313
348
  /**
314
- * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
315
- * specified root node.
316
- * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
317
- * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
318
- * of the node (`BTNKey`), or `null`.
319
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
320
- * starting node from which we want to calculate the depth. It can be either a node object or the key
321
- * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
322
- * node of the binary tree.
349
+ * Time Complexity: O(n)
350
+ * Space Complexity: O(1)
351
+ */
352
+
353
+ /**
354
+ * Time Complexity: O(n)
355
+ * Space Complexity: O(1)
356
+ *
357
+ * The function calculates the depth of a given node in a binary tree.
358
+ * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
359
+ * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
360
+ * `undefined`.
361
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
362
+ * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
363
+ * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
323
364
  * @returns the depth of the `distNode` relative to the `beginRoot`.
324
365
  */
325
366
  getDepth(distNode: BTNKey | N | null | undefined, beginRoot: BTNKey | N | null | undefined = this.root): number {
@@ -337,15 +378,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
337
378
  }
338
379
 
339
380
  /**
340
- * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
341
- * iterative approach.
381
+ * Time Complexity: O(n)
382
+ * Space Complexity: O(log n)
383
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
384
+ */
385
+
386
+
387
+ /**
388
+ * Time Complexity: O(n)
389
+ * Space Complexity: O(log n)
390
+ *
391
+ * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
392
+ * iterative traversal.
342
393
  * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
343
- * starting node from which the height of the binary tree is calculated. It can be either a node
344
- * object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
345
- * node is specified. If `
394
+ * starting node of the binary tree from which we want to calculate the height. It can be of type
395
+ * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
346
396
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
347
- * height of the binary tree using a recursive approach or an iterative approach. It can have two
348
- * possible values:
397
+ * height of the tree using a recursive approach or an iterative approach. It can have two possible
398
+ * values:
349
399
  * @returns the height of the binary tree.
350
400
  */
351
401
  getHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
@@ -362,11 +412,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
362
412
 
363
413
  return _getMaxHeight(beginRoot);
364
414
  } else {
365
- if (!beginRoot) {
366
- return -1;
367
- }
368
-
369
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
415
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
370
416
  let maxHeight = 0;
371
417
 
372
418
  while (stack.length > 0) {
@@ -374,7 +420,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
374
420
 
375
421
  if (node.left) stack.push({node: node.left, depth: depth + 1});
376
422
  if (node.right) stack.push({node: node.right, depth: depth + 1});
377
-
423
+
378
424
  maxHeight = Math.max(maxHeight, depth);
379
425
  }
380
426
 
@@ -383,11 +429,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
383
429
  }
384
430
 
385
431
  /**
432
+ * Time Complexity: O(n)
433
+ * Space Complexity: O(log n)
434
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
435
+ */
436
+
437
+ /**
438
+ * Time Complexity: O(n)
439
+ * Space Complexity: O(log n)
440
+ *
386
441
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
387
442
  * recursive or iterative approach.
388
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
389
- * calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
390
- * not provided.
443
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
444
+ * starting node of the binary tree from which we want to calculate the minimum height. It can be of
445
+ * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
391
446
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
392
447
  * to calculate the minimum height of a binary tree. It can have two possible values:
393
448
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -395,7 +450,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
395
450
  getMinHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
396
451
  beginRoot = this.ensureNotKey(beginRoot);
397
452
  if (!beginRoot) return -1;
398
-
453
+
399
454
  if (iterationType === IterationType.RECURSIVE) {
400
455
  const _getMinHeight = (cur: N | null | undefined): number => {
401
456
  if (!cur) return 0;
@@ -436,11 +491,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
436
491
  }
437
492
 
438
493
  /**
494
+ * Time Complexity: O(n)
495
+ * Space Complexity: O(log n)
496
+ */
497
+
498
+ /**
499
+ * Time Complexity: O(n)
500
+ * Space Complexity: O(log n)
501
+ *
439
502
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
440
503
  * height of the tree.
441
- * @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
442
- * either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
443
- * @returns The method is returning a boolean value.
504
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
505
+ * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
506
+ * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
507
+ * @returns a boolean value.
444
508
  */
445
509
  isPerfectlyBalanced(beginRoot: BTNKey | N | null | undefined = this.root): boolean {
446
510
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
@@ -471,25 +535,35 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
471
535
  ): N[];
472
536
 
473
537
  /**
474
- * The function `getNodes` returns an array of nodes that match a given node property, using either
475
- * recursive or iterative traversal.
476
- * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
477
- * `BTNKey` or a generic type `N`. It represents the property of the node that we are
478
- * searching for. It can be a specific key value or any other property of the node.
479
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
480
- * value. This value is compared with the `identifier` parameter to determine if the node should be
481
- * included in the result. The `callback` parameter has a default value of
482
- * `this._defaultOneParamCallback`, which
483
- * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
484
- * first node that matches the identifier. If set to true, the function will return an array with
485
- * only one element (or an empty array if no matching node is found). If set to false (default), the
486
- * function will continue searching for all
487
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
488
- * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
489
- * tree.
538
+ * Time Complexity: O(n)
539
+ * Space Complexity: O(log n).
540
+ */
541
+
542
+
543
+ /**
544
+ * Time Complexity: O(n)
545
+ * Space Complexity: O(log n).
546
+ *
547
+ * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
548
+ * callback function.
549
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
550
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
551
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
552
+ * specific value.
553
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
554
+ * input and returns a value of type `C`. It is used to determine if a node matches the given
555
+ * identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
556
+ * default
557
+ * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
558
+ * matches the identifier. If set to true, the function will stop iterating once it finds a matching
559
+ * node and return that node. If set to false (default), the function will continue iterating and
560
+ * return all nodes that match the identifier.
561
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
562
+ * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
563
+ * it is `null` or `undefined`, an empty array will be returned.
490
564
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
491
565
  * traverse the binary tree. It can have two possible values:
492
- * @returns The function `getNodes` returns an array of nodes (`N[]`).
566
+ * @returns an array of nodes of type `N`.
493
567
  */
494
568
  getNodes<C extends BTNCallback<N>>(
495
569
  identifier: ReturnType<C> | null | undefined,
@@ -498,11 +572,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
498
572
  beginRoot: BTNKey | N | null | undefined = this.root,
499
573
  iterationType = this.iterationType
500
574
  ): N[] {
501
- if (!beginRoot) return [];
502
- if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
575
+ if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
503
576
  beginRoot = this.ensureNotKey(beginRoot);
504
577
  if (!beginRoot) return [];
505
-
578
+
506
579
  const ans: N[] = [];
507
580
 
508
581
  if (iterationType === IterationType.RECURSIVE) {
@@ -557,20 +630,28 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
557
630
  ): boolean;
558
631
 
559
632
  /**
560
- * The function checks if a binary tree has a node with a given property or key.
561
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
562
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or a
563
- * generic type `N`.
564
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
565
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
566
- * whether the node matches the criteria or not. The default callback function
567
- * `this._defaultOneParamCallback` is used if no callback function is
568
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
569
- * the node from which the search should begin. By default, it is set to `this.root`, which means the
570
- * search will start from the root node of the binary tree. However, you can provide a different node
571
- * as
572
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
573
- * performed when searching for nodes in the binary tree. It can have one of the following values:
633
+ * Time Complexity: O(n)
634
+ * Space Complexity: O(log n).
635
+ */
636
+
637
+ /**
638
+ * Time Complexity: O(n)
639
+ *
640
+ * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
641
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
642
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
643
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
644
+ * specific identifier.
645
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
646
+ * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
647
+ * function should return a boolean value indicating whether the node should be included in the
648
+ * result or not.
649
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
650
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
651
+ * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
652
+ * @param iterationType - The `iterationType` parameter is a variable that determines the type of
653
+ * iteration to be performed on the binary tree. It is used to specify whether the iteration should
654
+ * be performed in a pre-order, in-order, or post-order manner.
574
655
  * @returns a boolean value.
575
656
  */
576
657
  has<C extends BTNCallback<N>>(
@@ -579,7 +660,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
579
660
  beginRoot: BTNKey | N | null | undefined = this.root,
580
661
  iterationType = this.iterationType
581
662
  ): boolean {
582
- if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
663
+ if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
583
664
 
584
665
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
585
666
  }
@@ -606,19 +687,30 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
606
687
  ): N | null | undefined;
607
688
 
608
689
  /**
609
- * The function `get` returns the first node in a binary tree that matches the given property or key.
610
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
611
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
612
- * type.
613
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
614
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
615
- * whether the node matches the criteria or not. The default callback function
616
- * (`this._defaultOneParamCallback`) is used if no callback function is
617
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
618
- * the root node from which the search should begin.
619
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
620
- * performed when searching for a node in the binary tree. It can have one of the following values:
621
- * @returns either the found node (of type N) or null if no node is found.
690
+ * Time Complexity: O(n)
691
+ * Space Complexity: O(log n)
692
+ */
693
+
694
+ /**
695
+ * Time Complexity: O(n)
696
+ * Space Complexity: O(log n)
697
+ *
698
+ * The function `getNode` returns the first node that matches the given identifier and callback
699
+ * function.
700
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
701
+ * used to identify the node you want to retrieve. It can be of any type that is returned by the
702
+ * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
703
+ * identifier.
704
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
705
+ * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
706
+ * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
707
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
708
+ * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
709
+ * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
710
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
711
+ * be performed when searching for nodes in the binary tree. It determines the order in which the
712
+ * nodes are visited during the search.
713
+ * @returns a value of type `N | null | undefined`.
622
714
  */
623
715
  getNode<C extends BTNCallback<N>>(
624
716
  identifier: ReturnType<C> | null | undefined,
@@ -626,12 +718,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
626
718
  beginRoot: BTNKey | N | null | undefined = this.root,
627
719
  iterationType = this.iterationType
628
720
  ): N | null | undefined {
629
- if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
721
+ if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
630
722
 
631
723
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
632
724
  }
633
725
 
634
726
  /**
727
+ * Time Complexity: O(n)
728
+ * Space Complexity: O(log n)
729
+ */
730
+
731
+ /**
732
+ * Time Complexity: O(n)
733
+ * Space Complexity: O(log n)
734
+ *
635
735
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
636
736
  * recursive or iterative iteration.
637
737
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
@@ -681,7 +781,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
681
781
  ensureNotKey(key: BTNKey | N | null | undefined, iterationType = IterationType.ITERATIVE): N | null | undefined {
682
782
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
683
783
  }
684
-
784
+
685
785
  get<C extends BTNCallback<N, BTNKey>>(
686
786
  identifier: BTNKey,
687
787
  callback?: C,
@@ -704,31 +804,43 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
704
804
  ): V | undefined;
705
805
 
706
806
  /**
707
- * The function `get` returns the first node value in a binary tree that matches the given property or key.
708
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
709
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
710
- * type.
711
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
712
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
713
- * whether the node matches the criteria or not. The default callback function
714
- * (`this._defaultOneParamCallback`) is used if no callback function is
715
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
716
- * the root node from which the search should begin.
717
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
718
- * performed when searching for a node in the binary tree. It can have one of the following values:
719
- * @returns either the found value (of type V) or undefined if no node value is found.
807
+ * Time Complexity: O(n)
808
+ * Space Complexity: O(log n)
809
+ */
810
+
811
+ /**
812
+ * Time Complexity: O(n)
813
+ * Space Complexity: O(log n)
814
+ *
815
+ * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
816
+ * and callback function.
817
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
818
+ * used to identify the node in the binary tree. It can be of any type that is the return type of the
819
+ * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
820
+ * @param {C} callback - The `callback` parameter is a function that will be called with each node in
821
+ * the binary tree. It is used to determine whether a node matches the given identifier. The callback
822
+ * function should return a value that can be compared to the identifier to determine if it is a
823
+ * match.
824
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
825
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
826
+ * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
827
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
828
+ * be performed when searching for a node in the binary tree. It is an optional parameter with a
829
+ * default value specified by `this.iterationType`.
830
+ * @returns The value of the node with the given identifier is being returned. If the node is not
831
+ * found, `undefined` is returned.
720
832
  */
721
833
  get<C extends BTNCallback<N>>(
722
834
  identifier: ReturnType<C> | null | undefined,
723
835
  callback: C = this._defaultOneParamCallback as C,
724
- beginRoot:BTNKey | N | null | undefined = this.root,
836
+ beginRoot: BTNKey | N | null | undefined = this.root,
725
837
  iterationType = this.iterationType
726
838
  ): V | undefined {
727
- if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
839
+ if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
728
840
 
729
841
  return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
730
842
  }
731
-
843
+
732
844
  /**
733
845
  * Clear the binary tree, removing all nodes.
734
846
  */
@@ -744,24 +856,33 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
744
856
  isEmpty(): boolean {
745
857
  return this.size === 0;
746
858
  }
747
-
859
+
860
+ /**
861
+ * Time Complexity: O(log n)
862
+ * Space Complexity: O(log n)
863
+ */
864
+
748
865
  /**
749
- * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
750
- * up to the root node, with the option to reverse the order of the nodes.
751
- * @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
752
- * to find the path to the root node.
866
+ * Time Complexity: O(log n)
867
+ * Space Complexity: O(log n)
868
+ *
869
+ * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
870
+ * structure, with the option to reverse the order of the nodes.
871
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
872
+ * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
873
+ * `null`, or `undefined`.
753
874
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
754
875
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
755
- * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
756
- * @returns The function `getPathToRoot` returns an array of type `N[]`.
876
+ * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
877
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
757
878
  */
758
879
  getPathToRoot(beginRoot: BTNKey | N | null | undefined, isReverse = true): N[] {
759
880
  // TODO to support get path through passing key
760
881
  const result: N[] = [];
761
882
  beginRoot = this.ensureNotKey(beginRoot);
762
-
883
+
763
884
  if (!beginRoot) return result;
764
-
885
+
765
886
  while (beginRoot.parent) {
766
887
  // Array.push + Array.reverse is more efficient than Array.unshift
767
888
  // TODO may consider using Deque, so far this is not the performance bottleneck
@@ -773,15 +894,23 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
773
894
  }
774
895
 
775
896
  /**
776
- * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
777
- * iterative traversal.
897
+ * Time Complexity: O(log n)
898
+ * Space Complexity: O(1)
899
+ */
900
+
901
+ /**
902
+ * Time Complexity: O(log n)
903
+ * Space Complexity: O(1)
904
+ *
905
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
906
+ * iteratively.
778
907
  * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
779
- * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
780
- * of a node (`BTNKey`), or `null` if the tree is empty.
908
+ * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
909
+ * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
781
910
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
782
911
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
783
- * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
784
- * no leftmost node, it returns `null`.
912
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
913
+ * is no leftmost node, it returns `null` or `undefined` depending on the input.
785
914
  */
786
915
  getLeftMost(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): N | null | undefined {
787
916
  beginRoot = this.ensureNotKey(beginRoot);
@@ -807,15 +936,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
807
936
  }
808
937
 
809
938
  /**
939
+ * Time Complexity: O(log n)
940
+ * Space Complexity: O(1)
941
+ */
942
+
943
+ /**
944
+ * Time Complexity: O(log n)
945
+ * Space Complexity: O(1)
946
+ *
810
947
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
811
948
  * iteratively.
812
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
813
- * find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
814
- * or `null`. If it is `null`, it means there is no starting node
815
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
816
- * be performed when finding the rightmost node in a binary tree. It can have two possible values:
817
- * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
818
- * `beginRoot` parameter is `null`, it returns `null`.
949
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
950
+ * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
951
+ * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
952
+ * current object.
953
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
954
+ * type of iteration to use when finding the rightmost node. It can have one of two values:
955
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
956
+ * is no rightmost node, it returns `null` or `undefined`, depending on the input.
819
957
  */
820
958
  getRightMost(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): N | null | undefined {
821
959
  // TODO support get right most by passing key in
@@ -841,13 +979,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
841
979
  }
842
980
 
843
981
  /**
982
+ * Time Complexity: O(n)
983
+ * Space Complexity: O(1)
984
+ */
985
+
986
+ /**
987
+ * Time Complexity: O(n)
988
+ * Space Complexity: O(1)
989
+ *
844
990
  * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
845
- * @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
846
- * to check if it is a binary search tree (BST) subtree.
991
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
992
+ * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
847
993
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
848
994
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
849
995
  * possible values:
850
- * @returns The function `isSubtreeBST` returns a boolean value.
996
+ * @returns a boolean value.
851
997
  */
852
998
  isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType = this.iterationType): boolean {
853
999
  // TODO there is a bug
@@ -881,11 +1027,19 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
881
1027
  }
882
1028
 
883
1029
  /**
1030
+ * Time Complexity: O(n)
1031
+ * Space Complexity: O(1)
1032
+ */
1033
+
1034
+ /**
1035
+ * Time Complexity: O(n)
1036
+ * Space Complexity: O(1)
1037
+ *
884
1038
  * The function checks if a binary tree is a binary search tree.
885
1039
  * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
886
1040
  * be used when checking if the binary tree is a binary search tree (BST). It is an optional
887
- * parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
888
- * provided in
1041
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
1042
+ * expected to be
889
1043
  * @returns a boolean value.
890
1044
  */
891
1045
  isBST(iterationType = this.iterationType): boolean {
@@ -915,19 +1069,31 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
915
1069
  ): ReturnType<C>[];
916
1070
 
917
1071
  /**
1072
+ * Time complexity: O(n)
1073
+ * Space complexity: O(log n)
1074
+ */
1075
+
1076
+ /**
1077
+ * Time complexity: O(n)
1078
+ * Space complexity: O(log n)
1079
+ *
918
1080
  * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
919
1081
  * node, either recursively or iteratively.
920
- * @param callback - The `callback` parameter is a function that will be called on each node in the
921
- * subtree traversal. It takes a single argument, which is the current node being traversed, and
922
- * returns a value. The return values from each callback invocation will be collected and returned as
923
- * an array.
924
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
925
- * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
926
- * start from the root of the tree.
1082
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1083
+ * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
1084
+ * returns a value of any type.
1085
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1086
+ * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
1087
+ * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
1088
+ * the default value.
927
1089
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
928
- * performed on the binary tree. It can have two possible values:
929
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
930
- * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
1090
+ * performed on the subtree. It can have two possible values:
1091
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1092
+ * whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
1093
+ * traversal will include null values, otherwise it will skip them.
1094
+ * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
1095
+ * the `callback` function on each node in the subtree. The type of the array elements is determined
1096
+ * by the return type of the `callback` function.
931
1097
  */
932
1098
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
933
1099
  callback: C = this._defaultOneParamCallback as C,
@@ -974,7 +1140,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
974
1140
  }
975
1141
  return ans;
976
1142
  }
977
-
1143
+
978
1144
  /**
979
1145
  * The function checks if a given node is a real node by verifying if it is an instance of
980
1146
  * BinaryTreeNode and its key is not NaN.
@@ -999,7 +1165,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
999
1165
  * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
1000
1166
  * @returns a boolean value.
1001
1167
  */
1002
- isNodeOrNull(node: any): node is (N | null){
1168
+ isNodeOrNull(node: any): node is (N | null) {
1003
1169
  return this.isRealNode(node) || node === null;
1004
1170
  }
1005
1171
 
@@ -1009,7 +1175,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1009
1175
  * data type.
1010
1176
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
1011
1177
  */
1012
- isNodeKey(potentialKey: any) : potentialKey is number {
1178
+ isNodeKey(potentialKey: any): potentialKey is number {
1013
1179
  return typeof potentialKey === 'number';
1014
1180
  }
1015
1181
 
@@ -1038,20 +1204,32 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1038
1204
  ): ReturnType<C>[];
1039
1205
 
1040
1206
  /**
1041
- * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
1042
- * function on each node according to a specified order pattern.
1043
- * @param callback - The `callback` parameter is a function that will be called on each node during
1044
- * the depth-first search traversal. It takes a node as input and returns a value. The default value
1045
- * is `this._defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
1207
+ * Time complexity: O(n)
1208
+ * Space complexity: O(n)
1209
+ */
1210
+
1211
+ /**
1212
+ * Time complexity: O(n)
1213
+ * Space complexity: O(n)
1214
+ *
1215
+ * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1216
+ * specified pattern and iteration type, and returns an array of values obtained from applying a
1217
+ * callback function to each visited node.
1218
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1219
+ * the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
1220
+ * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1046
1221
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1047
- * nodes are visited during the depth-first search. There are three possible values for `pattern`:
1048
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
1049
- * search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
1050
- * is `null`, an empty array will be returned.
1222
+ * nodes are traversed during the depth-first search. It can have one of the following values:
1223
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1224
+ * for the depth-first search traversal. It can be specified as a key, a node object, or
1225
+ * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1051
1226
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
1052
- * iteration used in the depth-first search algorithm. It can have two possible values:
1053
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
1054
- * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
1227
+ * iteration to use when traversing the tree. It can have one of the following values:
1228
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1229
+ * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1230
+ * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1231
+ * `false`, null or undefined
1232
+ * @returns an array of values that are the return values of the callback function.
1055
1233
  */
1056
1234
  dfs<C extends BTNCallback<N | null | undefined>>(
1057
1235
  callback: C = this._defaultOneParamCallback as C,
@@ -1107,7 +1285,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1107
1285
  _traverse(beginRoot);
1108
1286
  } else {
1109
1287
  // 0: visit, 1: print
1110
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
1288
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
1111
1289
 
1112
1290
  while (stack.length > 0) {
1113
1291
  const cur = stack.pop();
@@ -1171,18 +1349,30 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1171
1349
  ): ReturnType<C>[];
1172
1350
 
1173
1351
  /**
1174
- * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
1175
- * function on each node.
1176
- * @param callback - The `callback` parameter is a function that will be called for each node in the
1177
- * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
1178
- * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this._defaultOneParamCallback
1179
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
1180
- * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
1181
- * will not be performed and an empty array will be returned.
1182
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
1183
- * in the breadth-first search (BFS) algorithm. It can have two possible values:
1184
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
1185
- * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
1352
+ * Time complexity: O(n)
1353
+ * Space complexity: O(n)
1354
+ */
1355
+
1356
+ /**
1357
+ * Time complexity: O(n)
1358
+ * Space complexity: O(n)
1359
+ *
1360
+ * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1361
+ * callback function on each node.
1362
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1363
+ * the breadth-first search traversal. It takes a single parameter, which is the current node being
1364
+ * visited, and returns a value of any type.
1365
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1366
+ * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1367
+ * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1368
+ * the class is used as
1369
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1370
+ * performed during the breadth-first search (BFS). It can have two possible values:
1371
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1372
+ * or not to include null values in the breadth-first search traversal. If `includeNull` is set to
1373
+ * `true`, null values will be included in the traversal, otherwise they will be skipped.
1374
+ * @returns an array of values that are the result of invoking the callback function on each node in
1375
+ * the breadth-first traversal of a binary tree.
1186
1376
  */
1187
1377
  bfs<C extends BTNCallback<N | null | undefined>>(
1188
1378
  callback: C = this._defaultOneParamCallback as C,
@@ -1260,20 +1450,31 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1260
1450
  ): ReturnType<C>[][];
1261
1451
 
1262
1452
  /**
1263
- * The `listLevels` function takes a binary tree node and a callback function, and returns an array
1264
- * of arrays representing the levels of the tree.
1265
- * @param {C} callback - The `callback` parameter is a function that will be called on each node in
1266
- * the tree. It takes a node as input and returns a value. The return type of the callback function
1267
- * is determined by the generic type `C`.
1268
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
1269
- * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
1270
- * from the root node of the binary tree.
1271
- * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
1272
- * recursively or iteratively. It can have two possible values:
1273
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
1274
- * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
1275
- * level in a binary tree. Each inner array contains the return type of the provided callback
1276
- * function `C` applied to the nodes at that level.
1453
+ * Time complexity: O(n)
1454
+ * Space complexity: O(n)
1455
+ */
1456
+
1457
+
1458
+ /**
1459
+ * Time complexity: O(n)
1460
+ * Space complexity: O(n)
1461
+ *
1462
+ * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1463
+ * a binary tree and contains the values returned by a callback function applied to the nodes at that
1464
+ * level.
1465
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1466
+ * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1467
+ * returns a value of any type.
1468
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1469
+ * starting node for traversing the tree. It can be either a node object (`N`), a key value
1470
+ * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1471
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1472
+ * performed on the tree. It can have two possible values:
1473
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1474
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1475
+ * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1476
+ * be excluded
1477
+ * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1277
1478
  */
1278
1479
  listLevels<C extends BTNCallback<N | null | undefined>>(
1279
1480
  callback: C = this._defaultOneParamCallback as C,
@@ -1322,14 +1523,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1322
1523
  return levelsNodes;
1323
1524
  }
1324
1525
 
1325
- getPredecessor(node: N ): N
1526
+ getPredecessor(node: N): N
1527
+
1326
1528
 
1327
1529
  /**
1328
- * The function returns the predecessor node of a given node in a binary tree.
1329
- * @param {N} node - The parameter "node" represents a node in a binary tree.
1330
- * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
1530
+ * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1531
+ * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1532
+ * `null`, or `undefined`.
1533
+ * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1331
1534
  */
1332
- getPredecessor(node: BTNKey | N | null | undefined): N | undefined{
1535
+ getPredecessor(node: BTNKey | N | null | undefined): N | undefined {
1333
1536
  node = this.ensureNotKey(node);
1334
1537
  if (!this.isRealNode(node)) return undefined;
1335
1538
 
@@ -1346,12 +1549,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1346
1549
  }
1347
1550
  }
1348
1551
 
1552
+
1349
1553
  /**
1350
- * The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
1351
- * `x` is the last node.
1352
- * @param {N} x - N - a node in a binary tree
1353
- * @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
1354
- * if there is no successor, or `undefined` if the input `x` is `undefined`.
1554
+ * The function `getSuccessor` returns the next node in a binary tree given a current node.
1555
+ * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1556
+ * @returns the successor of the given node or key. The successor is the node that comes immediately
1557
+ * after the given node in the inorder traversal of the binary tree.
1355
1558
  */
1356
1559
  getSuccessor(x?: BTNKey | N | null): N | null | undefined {
1357
1560
  x = this.ensureNotKey(x);
@@ -1370,18 +1573,27 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1370
1573
  }
1371
1574
 
1372
1575
  /**
1373
- * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
1374
- * algorithm and returns an array of values obtained by applying a callback function to each node.
1375
- * @param callback - The `callback` parameter is a function that will be called on each node in the
1376
- * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
1377
- * default value for this parameter is `this._defaultOneParamCallback`.
1576
+ * Time complexity: O(n)
1577
+ * Space complexity: O(1)
1578
+ */
1579
+
1580
+ /**
1581
+ * Time complexity: O(n)
1582
+ * Space complexity: O(1)
1583
+ * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1584
+ * algorithm.
1585
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1586
+ * the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
1587
+ * a value of any type.
1378
1588
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1379
1589
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1380
1590
  * following values:
1381
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
1382
- * traversal. It specifies the root node of the tree from which the traversal should begin. If
1383
- * `beginRoot` is `null`, an empty array will be returned.
1384
- * @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
1591
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1592
+ * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1593
+ * the root of the tree. If no value is provided, the default value is the root of the tree.
1594
+ * @returns The function `morris` returns an array of values that are the result of invoking the
1595
+ * `callback` function on each node in the binary tree. The type of the array elements is determined
1596
+ * by the return type of the `callback` function.
1385
1597
  */
1386
1598
  morris<C extends BTNCallback<N>>(
1387
1599
  callback: C = this._defaultOneParamCallback as C,
@@ -1469,7 +1681,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1469
1681
  return ans;
1470
1682
  }
1471
1683
 
1472
- // --- start additional methods ---
1473
1684
 
1474
1685
  /**
1475
1686
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
@@ -1480,7 +1691,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1480
1691
  * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1481
1692
  * binary tree nodes in a specific order.
1482
1693
  */
1483
- *[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1694
+ * [Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1484
1695
  if (!node) {
1485
1696
  return;
1486
1697
  }
@@ -1502,12 +1713,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1502
1713
  }
1503
1714
  } else {
1504
1715
  if (node.left) {
1505
- // @ts-ignore
1506
1716
  yield* this[Symbol.iterator](node.left);
1507
1717
  }
1508
1718
  yield node.key;
1509
1719
  if (node.right) {
1510
- // @ts-ignore
1511
1720
  yield* this[Symbol.iterator](node.right);
1512
1721
  }
1513
1722
  }
@@ -1521,7 +1730,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1521
1730
  * @param {N} destNode - The destination node to swap.
1522
1731
  * @returns {N} - The destination node after the swap.
1523
1732
  */
1524
- protected _swap(srcNode: BTNKey | N | null | undefined, destNode:BTNKey | N | null | undefined): N | undefined{
1733
+ protected _swap(srcNode: BTNKey | N | null | undefined, destNode: BTNKey | N | null | undefined): N | undefined {
1525
1734
  srcNode = this.ensureNotKey(srcNode);
1526
1735
  destNode = this.ensureNotKey(destNode);
1527
1736
 
@@ -1593,11 +1802,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1593
1802
  this._root = v;
1594
1803
  }
1595
1804
 
1805
+
1596
1806
  /**
1597
1807
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1598
- * @param {N | null | undefined} root - The `root` parameter in the `print` function represents the
1599
- * root node of a binary tree. It can have one of the following types: `BTNKey`, `N`, `null`, or
1600
- * `undefined`. The default value is `this.root`, which suggests that `this.root` is the
1808
+ * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1809
+ * undefined`. It represents the root node of a binary tree. The root node can have one of the
1810
+ * following types:
1601
1811
  */
1602
1812
  print(beginRoot: BTNKey | N | null | undefined = this.root): void {
1603
1813
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1610,12 +1820,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1610
1820
  }
1611
1821
  };
1612
1822
 
1613
- const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
1614
- if (node === undefined || node === null) {
1823
+ const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
1824
+ if (!this.isRealNode(node)) {
1615
1825
  return [[], 0, 0, 0];
1616
1826
  }
1617
1827
 
1618
- if (node && node.right === undefined && node.left === undefined) {
1828
+ if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1619
1829
  const line = `${node.key}`;
1620
1830
  const width = line.length;
1621
1831
  const height = 1;
@@ -1623,7 +1833,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1623
1833
  return [[line], width, height, middle];
1624
1834
  }
1625
1835
 
1626
- if (node && node.right === undefined) {
1836
+ if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1627
1837
  const [lines, n, p, x] = _displayAux(node.left);
1628
1838
  const s = `${node.key}`;
1629
1839
  const u = s.length;
@@ -1633,7 +1843,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1633
1843
  return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1634
1844
  }
1635
1845
 
1636
- if (node && node.left === undefined) {
1846
+ if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1637
1847
  const [lines, n, p, u] = _displayAux(node.right);
1638
1848
  const s = `${node.key}`;
1639
1849
  const x = s.length;
@@ -1660,5 +1870,4 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1660
1870
 
1661
1871
  display(beginRoot);
1662
1872
  }
1663
- // --- end additional methods ---
1664
1873
  }