min-heap-typed 1.42.8 → 1.42.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 (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 +388 -201
  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 +419 -204
  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
@@ -153,10 +153,23 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
153
153
  }
154
154
 
155
155
  /**
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.
156
+ * Time Complexity: O(n)
157
+ * Space Complexity: O(1)
158
+ * 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.
159
+ */
160
+
161
+ /**
162
+ * Time Complexity: O(n)
163
+ * Space Complexity: O(1)
164
+ *
165
+ * The `add` function adds a new node with a key and value to a binary tree, or updates the value of
166
+ * an existing node with the same key.
167
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
168
+ * following types:
169
+ * @param {V} [value] - The value to be associated with the key or node being added to the binary
170
+ * tree.
171
+ * @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
172
+ * tree, or `null` or `undefined` if the insertion was not successful.
160
173
  */
161
174
  add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | null | undefined {
162
175
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
@@ -204,13 +217,22 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
204
217
  }
205
218
 
206
219
  /**
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`.
220
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
221
+ * Space Complexity: O(1)
222
+ */
223
+
224
+ /**
225
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
226
+ * Space Complexity: O(1)
227
+ *
228
+ * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
229
+ * each key-value pair to a data structure.
230
+ * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
231
+ * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
232
+ * or `undefined`.
233
+ * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
234
+ * correspond to the keys or nodes being added. If provided, the values will be associated with the
235
+ * keys or nodes during the add operation.
214
236
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
215
237
  */
216
238
  addMany(keysOrNodes: (BTNKey | N |null | undefined)[], values?: (V | undefined)[]): (N | null | undefined)[] {
@@ -230,6 +252,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
230
252
  }
231
253
 
232
254
  /**
255
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
256
+ * Space Complexity: O(1)
257
+ */
258
+
259
+ /**
260
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
261
+ * Space Complexity: O(1)
262
+ *
233
263
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
234
264
  * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
235
265
  * `BTNKey` or `N` values.
@@ -250,18 +280,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
250
280
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BiTreeDeleteResult<N>[];
251
281
 
252
282
  /**
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
283
+ * Time Complexity: O(n)
284
+ * Space Complexity: O(1)
285
+ */
286
+
287
+ /**
288
+ * Time Complexity: O(n)
289
+ * Space Complexity: O(1)
290
+ *
291
+ * The function deletes a node from a binary tree and returns an array of the deleted nodes along
292
+ * with the nodes that need to be balanced.
293
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
294
+ * object that you want to delete from the binary tree. It can be of any type that is compatible with
295
+ * the callback function's return type. It can also be null or undefined if you want to delete a
296
+ * specific node based on its value or object.
297
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
298
+ * identifier of the node to be deleted. It is optional and has a default value of
299
+ * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
300
+ * @returns an array of `BiTreeDeleteResult<N>`.
265
301
  */
266
302
  delete<C extends BTNCallback<N>>(
267
303
  identifier: ReturnType<C> | null | undefined,
@@ -311,15 +347,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
311
347
  }
312
348
 
313
349
  /**
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.
350
+ * Time Complexity: O(n)
351
+ * Space Complexity: O(1)
352
+ */
353
+
354
+ /**
355
+ * Time Complexity: O(n)
356
+ * Space Complexity: O(1)
357
+ *
358
+ * The function calculates the depth of a given node in a binary tree.
359
+ * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
360
+ * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
361
+ * `undefined`.
362
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
363
+ * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
364
+ * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
323
365
  * @returns the depth of the `distNode` relative to the `beginRoot`.
324
366
  */
325
367
  getDepth(distNode: BTNKey | N | null | undefined, beginRoot: BTNKey | N | null | undefined = this.root): number {
@@ -337,15 +379,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
337
379
  }
338
380
 
339
381
  /**
340
- * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
341
- * iterative approach.
382
+ * Time Complexity: O(n)
383
+ * Space Complexity: O(log n)
384
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
385
+ */
386
+
387
+
388
+ /**
389
+ * Time Complexity: O(n)
390
+ * Space Complexity: O(log n)
391
+ *
392
+ * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
393
+ * iterative traversal.
342
394
  * @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 `
395
+ * starting node of the binary tree from which we want to calculate the height. It can be of type
396
+ * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
346
397
  * @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:
398
+ * height of the tree using a recursive approach or an iterative approach. It can have two possible
399
+ * values:
349
400
  * @returns the height of the binary tree.
350
401
  */
351
402
  getHeight(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): number {
@@ -383,11 +434,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
383
434
  }
384
435
 
385
436
  /**
437
+ * Time Complexity: O(n)
438
+ * Space Complexity: O(log n)
439
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
440
+ */
441
+
442
+ /**
443
+ * Time Complexity: O(n)
444
+ * Space Complexity: O(log n)
445
+ *
386
446
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
387
447
  * 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.
448
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
449
+ * starting node of the binary tree from which we want to calculate the minimum height. It can be of
450
+ * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
391
451
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
392
452
  * to calculate the minimum height of a binary tree. It can have two possible values:
393
453
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -436,11 +496,20 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
436
496
  }
437
497
 
438
498
  /**
499
+ * Time Complexity: O(n)
500
+ * Space Complexity: O(log n)
501
+ */
502
+
503
+ /**
504
+ * Time Complexity: O(n)
505
+ * Space Complexity: O(log n)
506
+ *
439
507
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
440
508
  * 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.
509
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
510
+ * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
511
+ * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
512
+ * @returns a boolean value.
444
513
  */
445
514
  isPerfectlyBalanced(beginRoot: BTNKey | N | null | undefined = this.root): boolean {
446
515
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
@@ -471,25 +540,35 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
471
540
  ): N[];
472
541
 
473
542
  /**
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.
543
+ * Time Complexity: O(n)
544
+ * Space Complexity: O(log n).
545
+ */
546
+
547
+
548
+ /**
549
+ * Time Complexity: O(n)
550
+ * Space Complexity: O(log n).
551
+ *
552
+ * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
553
+ * callback function.
554
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
555
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
556
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
557
+ * specific value.
558
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
559
+ * input and returns a value of type `C`. It is used to determine if a node matches the given
560
+ * identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
561
+ * default
562
+ * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
563
+ * matches the identifier. If set to true, the function will stop iterating once it finds a matching
564
+ * node and return that node. If set to false (default), the function will continue iterating and
565
+ * return all nodes that match the identifier.
566
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
567
+ * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
568
+ * it is `null` or `undefined`, an empty array will be returned.
490
569
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
491
570
  * traverse the binary tree. It can have two possible values:
492
- * @returns The function `getNodes` returns an array of nodes (`N[]`).
571
+ * @returns an array of nodes of type `N`.
493
572
  */
494
573
  getNodes<C extends BTNCallback<N>>(
495
574
  identifier: ReturnType<C> | null | undefined,
@@ -557,20 +636,28 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
557
636
  ): boolean;
558
637
 
559
638
  /**
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:
639
+ * Time Complexity: O(n)
640
+ * Space Complexity: O(log n).
641
+ */
642
+
643
+ /**
644
+ * Time Complexity: O(n)
645
+ *
646
+ * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
647
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
648
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
649
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
650
+ * specific identifier.
651
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
652
+ * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
653
+ * function should return a boolean value indicating whether the node should be included in the
654
+ * result or not.
655
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
656
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
657
+ * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
658
+ * @param iterationType - The `iterationType` parameter is a variable that determines the type of
659
+ * iteration to be performed on the binary tree. It is used to specify whether the iteration should
660
+ * be performed in a pre-order, in-order, or post-order manner.
574
661
  * @returns a boolean value.
575
662
  */
576
663
  has<C extends BTNCallback<N>>(
@@ -606,19 +693,30 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
606
693
  ): N | null | undefined;
607
694
 
608
695
  /**
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.
696
+ * Time Complexity: O(n)
697
+ * Space Complexity: O(log n)
698
+ */
699
+
700
+ /**
701
+ * Time Complexity: O(n)
702
+ * Space Complexity: O(log n)
703
+ *
704
+ * The function `getNode` returns the first node that matches the given identifier and callback
705
+ * function.
706
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
707
+ * used to identify the node you want to retrieve. It can be of any type that is returned by the
708
+ * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
709
+ * identifier.
710
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
711
+ * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
712
+ * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
713
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
714
+ * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
715
+ * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
716
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
717
+ * be performed when searching for nodes in the binary tree. It determines the order in which the
718
+ * nodes are visited during the search.
719
+ * @returns a value of type `N | null | undefined`.
622
720
  */
623
721
  getNode<C extends BTNCallback<N>>(
624
722
  identifier: ReturnType<C> | null | undefined,
@@ -632,6 +730,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
632
730
  }
633
731
 
634
732
  /**
733
+ * Time Complexity: O(n)
734
+ * Space Complexity: O(log n)
735
+ */
736
+
737
+ /**
738
+ * Time Complexity: O(n)
739
+ * Space Complexity: O(log n)
740
+ *
635
741
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
636
742
  * recursive or iterative iteration.
637
743
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
@@ -666,7 +772,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
666
772
  }
667
773
  }
668
774
  }
669
-
775
+
670
776
  /**
671
777
  * The function `ensureNotKey` returns the node corresponding to the given key if it is a valid node
672
778
  * key, otherwise it returns the key itself.
@@ -704,19 +810,31 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
704
810
  ): V | undefined;
705
811
 
706
812
  /**
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.
813
+ * Time Complexity: O(n)
814
+ * Space Complexity: O(log n)
815
+ */
816
+
817
+ /**
818
+ * Time Complexity: O(n)
819
+ * Space Complexity: O(log n)
820
+ *
821
+ * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
822
+ * and callback function.
823
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
824
+ * used to identify the node in the binary tree. It can be of any type that is the return type of the
825
+ * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
826
+ * @param {C} callback - The `callback` parameter is a function that will be called with each node in
827
+ * the binary tree. It is used to determine whether a node matches the given identifier. The callback
828
+ * function should return a value that can be compared to the identifier to determine if it is a
829
+ * match.
830
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
831
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
832
+ * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
833
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
834
+ * be performed when searching for a node in the binary tree. It is an optional parameter with a
835
+ * default value specified by `this.iterationType`.
836
+ * @returns The value of the node with the given identifier is being returned. If the node is not
837
+ * found, `undefined` is returned.
720
838
  */
721
839
  get<C extends BTNCallback<N>>(
722
840
  identifier: ReturnType<C> | null | undefined,
@@ -744,16 +862,25 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
744
862
  isEmpty(): boolean {
745
863
  return this.size === 0;
746
864
  }
747
-
865
+
866
+ /**
867
+ * Time Complexity: O(log n)
868
+ * Space Complexity: O(log n)
869
+ */
870
+
748
871
  /**
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.
872
+ * Time Complexity: O(log n)
873
+ * Space Complexity: O(log n)
874
+ *
875
+ * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
876
+ * structure, with the option to reverse the order of the nodes.
877
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
878
+ * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
879
+ * `null`, or `undefined`.
753
880
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
754
881
  * 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[]`.
882
+ * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
883
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
757
884
  */
758
885
  getPathToRoot(beginRoot: BTNKey | N | null | undefined, isReverse = true): N[] {
759
886
  // TODO to support get path through passing key
@@ -773,15 +900,23 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
773
900
  }
774
901
 
775
902
  /**
776
- * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
777
- * iterative traversal.
903
+ * Time Complexity: O(log n)
904
+ * Space Complexity: O(1)
905
+ */
906
+
907
+ /**
908
+ * Time Complexity: O(log n)
909
+ * Space Complexity: O(1)
910
+ *
911
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
912
+ * iteratively.
778
913
  * @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.
914
+ * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
915
+ * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
781
916
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
782
917
  * 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`.
918
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
919
+ * is no leftmost node, it returns `null` or `undefined` depending on the input.
785
920
  */
786
921
  getLeftMost(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): N | null | undefined {
787
922
  beginRoot = this.ensureNotKey(beginRoot);
@@ -807,15 +942,24 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
807
942
  }
808
943
 
809
944
  /**
945
+ * Time Complexity: O(log n)
946
+ * Space Complexity: O(1)
947
+ */
948
+
949
+ /**
950
+ * Time Complexity: O(log n)
951
+ * Space Complexity: O(1)
952
+ *
810
953
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
811
954
  * 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`.
955
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
956
+ * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
957
+ * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
958
+ * current object.
959
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
960
+ * type of iteration to use when finding the rightmost node. It can have one of two values:
961
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
962
+ * is no rightmost node, it returns `null` or `undefined`, depending on the input.
819
963
  */
820
964
  getRightMost(beginRoot: BTNKey | N | null | undefined = this.root, iterationType = this.iterationType): N | null | undefined {
821
965
  // TODO support get right most by passing key in
@@ -841,13 +985,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
841
985
  }
842
986
 
843
987
  /**
988
+ * Time Complexity: O(n)
989
+ * Space Complexity: O(1)
990
+ */
991
+
992
+ /**
993
+ * Time Complexity: O(n)
994
+ * Space Complexity: O(1)
995
+ *
844
996
  * 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.
997
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
998
+ * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
847
999
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
848
1000
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
849
1001
  * possible values:
850
- * @returns The function `isSubtreeBST` returns a boolean value.
1002
+ * @returns a boolean value.
851
1003
  */
852
1004
  isSubtreeBST(beginRoot: BTNKey | N | null | undefined, iterationType = this.iterationType): boolean {
853
1005
  // TODO there is a bug
@@ -881,11 +1033,19 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
881
1033
  }
882
1034
 
883
1035
  /**
1036
+ * Time Complexity: O(n)
1037
+ * Space Complexity: O(1)
1038
+ */
1039
+
1040
+ /**
1041
+ * Time Complexity: O(n)
1042
+ * Space Complexity: O(1)
1043
+ *
884
1044
  * The function checks if a binary tree is a binary search tree.
885
1045
  * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
886
1046
  * 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
1047
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
1048
+ * expected to be
889
1049
  * @returns a boolean value.
890
1050
  */
891
1051
  isBST(iterationType = this.iterationType): boolean {
@@ -914,20 +1074,32 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
914
1074
  includeNull?: true
915
1075
  ): ReturnType<C>[];
916
1076
 
1077
+ /**
1078
+ * Time complexity: O(n)
1079
+ * Space complexity: O(log n)
1080
+ */
1081
+
917
1082
  /**
1083
+ * Time complexity: O(n)
1084
+ * Space complexity: O(log n)
1085
+ *
918
1086
  * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
919
1087
  * 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.
1088
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1089
+ * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
1090
+ * returns a value of any type.
1091
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1092
+ * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
1093
+ * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
1094
+ * the default value.
927
1095
  * @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>>`.
1096
+ * performed on the subtree. It can have two possible values:
1097
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1098
+ * whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
1099
+ * traversal will include null values, otherwise it will skip them.
1100
+ * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
1101
+ * the `callback` function on each node in the subtree. The type of the array elements is determined
1102
+ * by the return type of the `callback` function.
931
1103
  */
932
1104
  subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
933
1105
  callback: C = this._defaultOneParamCallback as C,
@@ -1038,20 +1210,32 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1038
1210
  ): ReturnType<C>[];
1039
1211
 
1040
1212
  /**
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.
1213
+ * Time complexity: O(n)
1214
+ * Space complexity: O(n)
1215
+ */
1216
+
1217
+ /**
1218
+ * Time complexity: O(n)
1219
+ * Space complexity: O(n)
1220
+ *
1221
+ * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1222
+ * specified pattern and iteration type, and returns an array of values obtained from applying a
1223
+ * callback function to each visited node.
1224
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1225
+ * the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
1226
+ * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
1046
1227
  * @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.
1228
+ * nodes are traversed during the depth-first search. It can have one of the following values:
1229
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1230
+ * for the depth-first search traversal. It can be specified as a key, a node object, or
1231
+ * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1051
1232
  * @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.
1233
+ * iteration to use when traversing the tree. It can have one of the following values:
1234
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1235
+ * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1236
+ * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1237
+ * `false`, null or undefined
1238
+ * @returns an array of values that are the return values of the callback function.
1055
1239
  */
1056
1240
  dfs<C extends BTNCallback<N | null | undefined>>(
1057
1241
  callback: C = this._defaultOneParamCallback as C,
@@ -1171,18 +1355,30 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1171
1355
  ): ReturnType<C>[];
1172
1356
 
1173
1357
  /**
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>>[]`.
1358
+ * Time complexity: O(n)
1359
+ * Space complexity: O(n)
1360
+ */
1361
+
1362
+ /**
1363
+ * Time complexity: O(n)
1364
+ * Space complexity: O(n)
1365
+ *
1366
+ * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1367
+ * callback function on each node.
1368
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1369
+ * the breadth-first search traversal. It takes a single parameter, which is the current node being
1370
+ * visited, and returns a value of any type.
1371
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1372
+ * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1373
+ * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1374
+ * the class is used as
1375
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1376
+ * performed during the breadth-first search (BFS). It can have two possible values:
1377
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1378
+ * or not to include null values in the breadth-first search traversal. If `includeNull` is set to
1379
+ * `true`, null values will be included in the traversal, otherwise they will be skipped.
1380
+ * @returns an array of values that are the result of invoking the callback function on each node in
1381
+ * the breadth-first traversal of a binary tree.
1186
1382
  */
1187
1383
  bfs<C extends BTNCallback<N | null | undefined>>(
1188
1384
  callback: C = this._defaultOneParamCallback as C,
@@ -1260,20 +1456,31 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1260
1456
  ): ReturnType<C>[][];
1261
1457
 
1262
1458
  /**
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.
1459
+ * Time complexity: O(n)
1460
+ * Space complexity: O(n)
1461
+ */
1462
+
1463
+
1464
+ /**
1465
+ * Time complexity: O(n)
1466
+ * Space complexity: O(n)
1467
+ *
1468
+ * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1469
+ * a binary tree and contains the values returned by a callback function applied to the nodes at that
1470
+ * level.
1471
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1472
+ * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1473
+ * returns a value of any type.
1474
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1475
+ * starting node for traversing the tree. It can be either a node object (`N`), a key value
1476
+ * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1477
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1478
+ * performed on the tree. It can have two possible values:
1479
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1480
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1481
+ * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1482
+ * be excluded
1483
+ * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1277
1484
  */
1278
1485
  listLevels<C extends BTNCallback<N | null | undefined>>(
1279
1486
  callback: C = this._defaultOneParamCallback as C,
@@ -1324,10 +1531,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1324
1531
 
1325
1532
  getPredecessor(node: N ): N
1326
1533
 
1534
+
1327
1535
  /**
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`.
1536
+ * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1537
+ * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1538
+ * `null`, or `undefined`.
1539
+ * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1331
1540
  */
1332
1541
  getPredecessor(node: BTNKey | N | null | undefined): N | undefined{
1333
1542
  node = this.ensureNotKey(node);
@@ -1346,12 +1555,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1346
1555
  }
1347
1556
  }
1348
1557
 
1558
+
1349
1559
  /**
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`.
1560
+ * The function `getSuccessor` returns the next node in a binary tree given a current node.
1561
+ * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1562
+ * @returns the successor of the given node or key. The successor is the node that comes immediately
1563
+ * after the given node in the inorder traversal of the binary tree.
1355
1564
  */
1356
1565
  getSuccessor(x?: BTNKey | N | null): N | null | undefined {
1357
1566
  x = this.ensureNotKey(x);
@@ -1370,18 +1579,27 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1370
1579
  }
1371
1580
 
1372
1581
  /**
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`.
1582
+ * Time complexity: O(n)
1583
+ * Space complexity: O(1)
1584
+ */
1585
+
1586
+ /**
1587
+ * Time complexity: O(n)
1588
+ * Space complexity: O(1)
1589
+ * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1590
+ * algorithm.
1591
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1592
+ * the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
1593
+ * a value of any type.
1378
1594
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1379
1595
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1380
1596
  * 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.
1597
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1598
+ * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1599
+ * the root of the tree. If no value is provided, the default value is the root of the tree.
1600
+ * @returns The function `morris` returns an array of values that are the result of invoking the
1601
+ * `callback` function on each node in the binary tree. The type of the array elements is determined
1602
+ * by the return type of the `callback` function.
1385
1603
  */
1386
1604
  morris<C extends BTNCallback<N>>(
1387
1605
  callback: C = this._defaultOneParamCallback as C,
@@ -1469,7 +1687,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1469
1687
  return ans;
1470
1688
  }
1471
1689
 
1472
- // --- start additional methods ---
1473
1690
 
1474
1691
  /**
1475
1692
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
@@ -1502,12 +1719,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1502
1719
  }
1503
1720
  } else {
1504
1721
  if (node.left) {
1505
- // @ts-ignore
1506
1722
  yield* this[Symbol.iterator](node.left);
1507
1723
  }
1508
1724
  yield node.key;
1509
1725
  if (node.right) {
1510
- // @ts-ignore
1511
1726
  yield* this[Symbol.iterator](node.right);
1512
1727
  }
1513
1728
  }
@@ -1593,11 +1808,12 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1593
1808
  this._root = v;
1594
1809
  }
1595
1810
 
1811
+
1596
1812
  /**
1597
1813
  * 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
1814
+ * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1815
+ * undefined`. It represents the root node of a binary tree. The root node can have one of the
1816
+ * following types:
1601
1817
  */
1602
1818
  print(beginRoot: BTNKey | N | null | undefined = this.root): void {
1603
1819
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1611,11 +1827,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1611
1827
  };
1612
1828
 
1613
1829
  const _displayAux = (node: N | null | undefined): [string[], number, number, number] => {
1614
- if (node === undefined || node === null) {
1830
+ if (!this.isRealNode(node)) {
1615
1831
  return [[], 0, 0, 0];
1616
1832
  }
1617
1833
 
1618
- if (node && node.right === undefined && node.left === undefined) {
1834
+ if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1619
1835
  const line = `${node.key}`;
1620
1836
  const width = line.length;
1621
1837
  const height = 1;
@@ -1623,7 +1839,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1623
1839
  return [[line], width, height, middle];
1624
1840
  }
1625
1841
 
1626
- if (node && node.right === undefined) {
1842
+ if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1627
1843
  const [lines, n, p, x] = _displayAux(node.left);
1628
1844
  const s = `${node.key}`;
1629
1845
  const u = s.length;
@@ -1633,7 +1849,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1633
1849
  return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1634
1850
  }
1635
1851
 
1636
- if (node && node.left === undefined) {
1852
+ if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1637
1853
  const [lines, n, p, u] = _displayAux(node.right);
1638
1854
  const s = `${node.key}`;
1639
1855
  const x = s.length;
@@ -1660,5 +1876,4 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1660
1876
 
1661
1877
  display(beginRoot);
1662
1878
  }
1663
- // --- end additional methods ---
1664
1879
  }