data-structure-typed 1.42.7 → 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 (107) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +12 -12
  4. package/benchmark/report.json +106 -106
  5. package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
  6. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +88 -23
  7. package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
  9. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +388 -201
  10. package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +121 -66
  12. package/dist/cjs/src/data-structures/binary-tree/bst.js +121 -67
  13. package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
  15. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +95 -18
  16. package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  18. package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
  19. package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -1
  20. package/dist/cjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
  21. package/dist/cjs/src/data-structures/graph/abstract-graph.js +147 -36
  22. package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
  23. package/dist/cjs/src/data-structures/graph/directed-graph.d.ts +126 -0
  24. package/dist/cjs/src/data-structures/graph/directed-graph.js +126 -0
  25. package/dist/cjs/src/data-structures/graph/directed-graph.js.map +1 -1
  26. package/dist/cjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
  27. package/dist/cjs/src/data-structures/graph/undirected-graph.js +63 -0
  28. package/dist/cjs/src/data-structures/graph/undirected-graph.js.map +1 -1
  29. package/dist/cjs/src/data-structures/heap/heap.d.ts +175 -12
  30. package/dist/cjs/src/data-structures/heap/heap.js +175 -12
  31. package/dist/cjs/src/data-structures/heap/heap.js.map +1 -1
  32. package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  33. package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
  34. package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  35. package/dist/cjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  36. package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
  37. package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js.map +1 -1
  38. package/dist/cjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  39. package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
  40. package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js.map +1 -1
  41. package/dist/cjs/src/data-structures/queue/deque.d.ts +113 -3
  42. package/dist/cjs/src/data-structures/queue/deque.js +113 -3
  43. package/dist/cjs/src/data-structures/queue/deque.js.map +1 -1
  44. package/dist/cjs/src/data-structures/queue/queue.d.ts +87 -0
  45. package/dist/cjs/src/data-structures/queue/queue.js +87 -0
  46. package/dist/cjs/src/data-structures/queue/queue.js.map +1 -1
  47. package/dist/cjs/src/data-structures/stack/stack.d.ts +42 -0
  48. package/dist/cjs/src/data-structures/stack/stack.js +42 -0
  49. package/dist/cjs/src/data-structures/stack/stack.js.map +1 -1
  50. package/dist/cjs/src/data-structures/trie/trie.d.ts +76 -0
  51. package/dist/cjs/src/data-structures/trie/trie.js +76 -1
  52. package/dist/cjs/src/data-structures/trie/trie.js.map +1 -1
  53. package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
  54. package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +88 -23
  55. package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
  56. package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +388 -201
  57. package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +121 -66
  58. package/dist/mjs/src/data-structures/binary-tree/bst.js +121 -67
  59. package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
  60. package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +95 -18
  61. package/dist/mjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  62. package/dist/mjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
  63. package/dist/mjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
  64. package/dist/mjs/src/data-structures/graph/abstract-graph.js +147 -36
  65. package/dist/mjs/src/data-structures/graph/directed-graph.d.ts +126 -0
  66. package/dist/mjs/src/data-structures/graph/directed-graph.js +126 -0
  67. package/dist/mjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
  68. package/dist/mjs/src/data-structures/graph/undirected-graph.js +63 -0
  69. package/dist/mjs/src/data-structures/heap/heap.d.ts +175 -12
  70. package/dist/mjs/src/data-structures/heap/heap.js +175 -12
  71. package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  72. package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
  73. package/dist/mjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  74. package/dist/mjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
  75. package/dist/mjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  76. package/dist/mjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
  77. package/dist/mjs/src/data-structures/queue/deque.d.ts +113 -3
  78. package/dist/mjs/src/data-structures/queue/deque.js +113 -3
  79. package/dist/mjs/src/data-structures/queue/queue.d.ts +87 -0
  80. package/dist/mjs/src/data-structures/queue/queue.js +87 -0
  81. package/dist/mjs/src/data-structures/stack/stack.d.ts +42 -0
  82. package/dist/mjs/src/data-structures/stack/stack.js +42 -0
  83. package/dist/mjs/src/data-structures/trie/trie.d.ts +76 -0
  84. package/dist/mjs/src/data-structures/trie/trie.js +76 -1
  85. package/dist/umd/data-structure-typed.min.js +1 -1
  86. package/dist/umd/data-structure-typed.min.js.map +1 -1
  87. package/package.json +1 -1
  88. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  89. package/src/data-structures/binary-tree/binary-tree.ts +419 -204
  90. package/src/data-structures/binary-tree/bst.ts +130 -68
  91. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  92. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  93. package/src/data-structures/graph/abstract-graph.ts +133 -7
  94. package/src/data-structures/graph/directed-graph.ts +145 -1
  95. package/src/data-structures/graph/undirected-graph.ts +72 -0
  96. package/src/data-structures/heap/heap.ts +201 -12
  97. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  98. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  99. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  100. package/src/data-structures/queue/deque.ts +127 -3
  101. package/src/data-structures/queue/queue.ts +99 -0
  102. package/src/data-structures/stack/stack.ts +48 -0
  103. package/src/data-structures/trie/trie.ts +87 -4
  104. package/test/integration/index.html +24 -2
  105. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +19 -2
  106. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
  107. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -1
@@ -132,10 +132,22 @@ class BinaryTree {
132
132
  return new BinaryTreeNode(key, value);
133
133
  }
134
134
  /**
135
- * Add a node with the given key and value to the binary tree.
136
- * @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
137
- * @param {V} value - The value for the new node (optional).
138
- * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
135
+ * Time Complexity: O(n)
136
+ * Space Complexity: O(1)
137
+ * 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.
138
+ */
139
+ /**
140
+ * Time Complexity: O(n)
141
+ * Space Complexity: O(1)
142
+ *
143
+ * The `add` function adds a new node with a key and value to a binary tree, or updates the value of
144
+ * an existing node with the same key.
145
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
146
+ * following types:
147
+ * @param {V} [value] - The value to be associated with the key or node being added to the binary
148
+ * tree.
149
+ * @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
150
+ * tree, or `null` or `undefined` if the insertion was not successful.
139
151
  */
140
152
  add(keyOrNode, value) {
141
153
  const _bfs = (root, newNode) => {
@@ -189,13 +201,21 @@ class BinaryTree {
189
201
  return inserted;
190
202
  }
191
203
  /**
192
- * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
193
- * values, and adds them to the binary tree.
194
- * @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
195
- * objects, or null values.
196
- * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
197
- * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
198
- * the value of the nodes will be `undefined`.
204
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
205
+ * Space Complexity: O(1)
206
+ */
207
+ /**
208
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
209
+ * Space Complexity: O(1)
210
+ *
211
+ * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
212
+ * each key-value pair to a data structure.
213
+ * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
214
+ * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
215
+ * or `undefined`.
216
+ * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
217
+ * correspond to the keys or nodes being added. If provided, the values will be associated with the
218
+ * keys or nodes during the add operation.
199
219
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
200
220
  */
201
221
  addMany(keysOrNodes, values) {
@@ -212,6 +232,13 @@ class BinaryTree {
212
232
  });
213
233
  }
214
234
  /**
235
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
236
+ * Space Complexity: O(1)
237
+ */
238
+ /**
239
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
240
+ * Space Complexity: O(1)
241
+ *
215
242
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
216
243
  * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
217
244
  * `BTNKey` or `N` values.
@@ -225,18 +252,23 @@ class BinaryTree {
225
252
  return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
226
253
  }
227
254
  /**
228
- * The `delete` function removes a node from a binary search tree and returns the deleted node along
229
- * with the parent node that needs to be balanced.
230
- * a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
231
- * binary tree.
232
- * @returns an array of `BiTreeDeleteResult<N>` objects.
233
- * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
234
- * `BTNKey` or a generic type `N`. It represents the property of the node that we are
235
- * searching for. It can be a specific key value or any other property of the node.
236
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
237
- * value. This value is compared with the `identifier` parameter to determine if the node should be
238
- * included in the result. The `callback` parameter has a default value of
239
- * `this._defaultOneParamCallback`, which
255
+ * Time Complexity: O(n)
256
+ * Space Complexity: O(1)
257
+ */
258
+ /**
259
+ * Time Complexity: O(n)
260
+ * Space Complexity: O(1)
261
+ *
262
+ * The function deletes a node from a binary tree and returns an array of the deleted nodes along
263
+ * with the nodes that need to be balanced.
264
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
265
+ * object that you want to delete from the binary tree. It can be of any type that is compatible with
266
+ * the callback function's return type. It can also be null or undefined if you want to delete a
267
+ * specific node based on its value or object.
268
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
269
+ * identifier of the node to be deleted. It is optional and has a default value of
270
+ * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
271
+ * @returns an array of `BiTreeDeleteResult<N>`.
240
272
  */
241
273
  delete(identifier, callback = this._defaultOneParamCallback) {
242
274
  const deletedResult = [];
@@ -285,15 +317,20 @@ class BinaryTree {
285
317
  return deletedResult;
286
318
  }
287
319
  /**
288
- * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
289
- * specified root node.
290
- * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
291
- * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
292
- * of the node (`BTNKey`), or `null`.
293
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
294
- * starting node from which we want to calculate the depth. It can be either a node object or the key
295
- * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
296
- * node of the binary tree.
320
+ * Time Complexity: O(n)
321
+ * Space Complexity: O(1)
322
+ */
323
+ /**
324
+ * Time Complexity: O(n)
325
+ * Space Complexity: O(1)
326
+ *
327
+ * The function calculates the depth of a given node in a binary tree.
328
+ * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
329
+ * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
330
+ * `undefined`.
331
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
332
+ * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
333
+ * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
297
334
  * @returns the depth of the `distNode` relative to the `beginRoot`.
298
335
  */
299
336
  getDepth(distNode, beginRoot = this.root) {
@@ -310,15 +347,22 @@ class BinaryTree {
310
347
  return depth;
311
348
  }
312
349
  /**
313
- * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
314
- * iterative approach.
350
+ * Time Complexity: O(n)
351
+ * Space Complexity: O(log n)
352
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
353
+ */
354
+ /**
355
+ * Time Complexity: O(n)
356
+ * Space Complexity: O(log n)
357
+ *
358
+ * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
359
+ * iterative traversal.
315
360
  * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
316
- * starting node from which the height of the binary tree is calculated. It can be either a node
317
- * object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
318
- * node is specified. If `
361
+ * starting node of the binary tree from which we want to calculate the height. It can be of type
362
+ * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
319
363
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
320
- * height of the binary tree using a recursive approach or an iterative approach. It can have two
321
- * possible values:
364
+ * height of the tree using a recursive approach or an iterative approach. It can have two possible
365
+ * values:
322
366
  * @returns the height of the binary tree.
323
367
  */
324
368
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
@@ -353,11 +397,19 @@ class BinaryTree {
353
397
  }
354
398
  }
355
399
  /**
400
+ * Time Complexity: O(n)
401
+ * Space Complexity: O(log n)
402
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
403
+ */
404
+ /**
405
+ * Time Complexity: O(n)
406
+ * Space Complexity: O(log n)
407
+ *
356
408
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
357
409
  * recursive or iterative approach.
358
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
359
- * calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
360
- * not provided.
410
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
411
+ * starting node of the binary tree from which we want to calculate the minimum height. It can be of
412
+ * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
361
413
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
362
414
  * to calculate the minimum height of a binary tree. It can have two possible values:
363
415
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -407,35 +459,51 @@ class BinaryTree {
407
459
  }
408
460
  }
409
461
  /**
462
+ * Time Complexity: O(n)
463
+ * Space Complexity: O(log n)
464
+ */
465
+ /**
466
+ * Time Complexity: O(n)
467
+ * Space Complexity: O(log n)
468
+ *
410
469
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
411
470
  * height of the tree.
412
- * @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
413
- * either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
414
- * @returns The method is returning a boolean value.
471
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
472
+ * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
473
+ * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
474
+ * @returns a boolean value.
415
475
  */
416
476
  isPerfectlyBalanced(beginRoot = this.root) {
417
477
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
418
478
  }
419
479
  /**
420
- * The function `getNodes` returns an array of nodes that match a given node property, using either
421
- * recursive or iterative traversal.
422
- * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
423
- * `BTNKey` or a generic type `N`. It represents the property of the node that we are
424
- * searching for. It can be a specific key value or any other property of the node.
425
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
426
- * value. This value is compared with the `identifier` parameter to determine if the node should be
427
- * included in the result. The `callback` parameter has a default value of
428
- * `this._defaultOneParamCallback`, which
429
- * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
430
- * first node that matches the identifier. If set to true, the function will return an array with
431
- * only one element (or an empty array if no matching node is found). If set to false (default), the
432
- * function will continue searching for all
433
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
434
- * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
435
- * tree.
480
+ * Time Complexity: O(n)
481
+ * Space Complexity: O(log n).
482
+ */
483
+ /**
484
+ * Time Complexity: O(n)
485
+ * Space Complexity: O(log n).
486
+ *
487
+ * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
488
+ * callback function.
489
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
490
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
491
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
492
+ * specific value.
493
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
494
+ * input and returns a value of type `C`. It is used to determine if a node matches the given
495
+ * identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
496
+ * default
497
+ * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
498
+ * matches the identifier. If set to true, the function will stop iterating once it finds a matching
499
+ * node and return that node. If set to false (default), the function will continue iterating and
500
+ * return all nodes that match the identifier.
501
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
502
+ * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
503
+ * it is `null` or `undefined`, an empty array will be returned.
436
504
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
437
505
  * traverse the binary tree. It can have two possible values:
438
- * @returns The function `getNodes` returns an array of nodes (`N[]`).
506
+ * @returns an array of nodes of type `N`.
439
507
  */
440
508
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
441
509
  if (!beginRoot)
@@ -478,20 +546,27 @@ class BinaryTree {
478
546
  return ans;
479
547
  }
480
548
  /**
481
- * The function checks if a binary tree has a node with a given property or key.
482
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
483
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or a
484
- * generic type `N`.
485
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
486
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
487
- * whether the node matches the criteria or not. The default callback function
488
- * `this._defaultOneParamCallback` is used if no callback function is
489
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
490
- * the node from which the search should begin. By default, it is set to `this.root`, which means the
491
- * search will start from the root node of the binary tree. However, you can provide a different node
492
- * as
493
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
494
- * performed when searching for nodes in the binary tree. It can have one of the following values:
549
+ * Time Complexity: O(n)
550
+ * Space Complexity: O(log n).
551
+ */
552
+ /**
553
+ * Time Complexity: O(n)
554
+ *
555
+ * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
556
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
557
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
558
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
559
+ * specific identifier.
560
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
561
+ * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
562
+ * function should return a boolean value indicating whether the node should be included in the
563
+ * result or not.
564
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
565
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
566
+ * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
567
+ * @param iterationType - The `iterationType` parameter is a variable that determines the type of
568
+ * iteration to be performed on the binary tree. It is used to specify whether the iteration should
569
+ * be performed in a pre-order, in-order, or post-order manner.
495
570
  * @returns a boolean value.
496
571
  */
497
572
  has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
@@ -500,19 +575,29 @@ class BinaryTree {
500
575
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
501
576
  }
502
577
  /**
503
- * The function `get` returns the first node in a binary tree that matches the given property or key.
504
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
505
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
506
- * type.
507
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
508
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
509
- * whether the node matches the criteria or not. The default callback function
510
- * (`this._defaultOneParamCallback`) is used if no callback function is
511
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
512
- * the root node from which the search should begin.
513
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
514
- * performed when searching for a node in the binary tree. It can have one of the following values:
515
- * @returns either the found node (of type N) or null if no node is found.
578
+ * Time Complexity: O(n)
579
+ * Space Complexity: O(log n)
580
+ */
581
+ /**
582
+ * Time Complexity: O(n)
583
+ * Space Complexity: O(log n)
584
+ *
585
+ * The function `getNode` returns the first node that matches the given identifier and callback
586
+ * function.
587
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
588
+ * used to identify the node you want to retrieve. It can be of any type that is returned by the
589
+ * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
590
+ * identifier.
591
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
592
+ * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
593
+ * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
594
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
595
+ * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
596
+ * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
597
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
598
+ * be performed when searching for nodes in the binary tree. It determines the order in which the
599
+ * nodes are visited during the search.
600
+ * @returns a value of type `N | null | undefined`.
516
601
  */
517
602
  getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
518
603
  if (identifier instanceof BinaryTreeNode)
@@ -520,6 +605,13 @@ class BinaryTree {
520
605
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
521
606
  }
522
607
  /**
608
+ * Time Complexity: O(n)
609
+ * Space Complexity: O(log n)
610
+ */
611
+ /**
612
+ * Time Complexity: O(n)
613
+ * Space Complexity: O(log n)
614
+ *
523
615
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
524
616
  * recursive or iterative iteration.
525
617
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
@@ -574,19 +666,30 @@ class BinaryTree {
574
666
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
575
667
  }
576
668
  /**
577
- * The function `get` returns the first node value in a binary tree that matches the given property or key.
578
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
579
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
580
- * type.
581
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
582
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
583
- * whether the node matches the criteria or not. The default callback function
584
- * (`this._defaultOneParamCallback`) is used if no callback function is
585
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
586
- * the root node from which the search should begin.
587
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
588
- * performed when searching for a node in the binary tree. It can have one of the following values:
589
- * @returns either the found value (of type V) or undefined if no node value is found.
669
+ * Time Complexity: O(n)
670
+ * Space Complexity: O(log n)
671
+ */
672
+ /**
673
+ * Time Complexity: O(n)
674
+ * Space Complexity: O(log n)
675
+ *
676
+ * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
677
+ * and callback function.
678
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
679
+ * used to identify the node in the binary tree. It can be of any type that is the return type of the
680
+ * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
681
+ * @param {C} callback - The `callback` parameter is a function that will be called with each node in
682
+ * the binary tree. It is used to determine whether a node matches the given identifier. The callback
683
+ * function should return a value that can be compared to the identifier to determine if it is a
684
+ * match.
685
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
686
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
687
+ * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
688
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
689
+ * be performed when searching for a node in the binary tree. It is an optional parameter with a
690
+ * default value specified by `this.iterationType`.
691
+ * @returns The value of the node with the given identifier is being returned. If the node is not
692
+ * found, `undefined` is returned.
590
693
  */
591
694
  get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
592
695
  if (identifier instanceof BinaryTreeNode)
@@ -608,14 +711,22 @@ class BinaryTree {
608
711
  return this.size === 0;
609
712
  }
610
713
  /**
611
- * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
612
- * up to the root node, with the option to reverse the order of the nodes.
613
- * @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
614
- * to find the path to the root node.
714
+ * Time Complexity: O(log n)
715
+ * Space Complexity: O(log n)
716
+ */
717
+ /**
718
+ * Time Complexity: O(log n)
719
+ * Space Complexity: O(log n)
720
+ *
721
+ * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
722
+ * structure, with the option to reverse the order of the nodes.
723
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
724
+ * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
725
+ * `null`, or `undefined`.
615
726
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
616
727
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
617
- * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
618
- * @returns The function `getPathToRoot` returns an array of type `N[]`.
728
+ * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
729
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
619
730
  */
620
731
  getPathToRoot(beginRoot, isReverse = true) {
621
732
  // TODO to support get path through passing key
@@ -633,15 +744,22 @@ class BinaryTree {
633
744
  return isReverse ? result.reverse() : result;
634
745
  }
635
746
  /**
636
- * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
637
- * iterative traversal.
747
+ * Time Complexity: O(log n)
748
+ * Space Complexity: O(1)
749
+ */
750
+ /**
751
+ * Time Complexity: O(log n)
752
+ * Space Complexity: O(1)
753
+ *
754
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
755
+ * iteratively.
638
756
  * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
639
- * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
640
- * of a node (`BTNKey`), or `null` if the tree is empty.
757
+ * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
758
+ * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
641
759
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
642
760
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
643
- * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
644
- * no leftmost node, it returns `null`.
761
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
762
+ * is no leftmost node, it returns `null` or `undefined` depending on the input.
645
763
  */
646
764
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
647
765
  beginRoot = this.ensureNotKey(beginRoot);
@@ -666,15 +784,23 @@ class BinaryTree {
666
784
  }
667
785
  }
668
786
  /**
787
+ * Time Complexity: O(log n)
788
+ * Space Complexity: O(1)
789
+ */
790
+ /**
791
+ * Time Complexity: O(log n)
792
+ * Space Complexity: O(1)
793
+ *
669
794
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
670
795
  * iteratively.
671
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
672
- * find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
673
- * or `null`. If it is `null`, it means there is no starting node
674
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
675
- * be performed when finding the rightmost node in a binary tree. It can have two possible values:
676
- * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
677
- * `beginRoot` parameter is `null`, it returns `null`.
796
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
797
+ * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
798
+ * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
799
+ * current object.
800
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
801
+ * type of iteration to use when finding the rightmost node. It can have one of two values:
802
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
803
+ * is no rightmost node, it returns `null` or `undefined`, depending on the input.
678
804
  */
679
805
  getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
680
806
  // TODO support get right most by passing key in
@@ -700,13 +826,20 @@ class BinaryTree {
700
826
  }
701
827
  }
702
828
  /**
829
+ * Time Complexity: O(n)
830
+ * Space Complexity: O(1)
831
+ */
832
+ /**
833
+ * Time Complexity: O(n)
834
+ * Space Complexity: O(1)
835
+ *
703
836
  * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
704
- * @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
705
- * to check if it is a binary search tree (BST) subtree.
837
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
838
+ * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
706
839
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
707
840
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
708
841
  * possible values:
709
- * @returns The function `isSubtreeBST` returns a boolean value.
842
+ * @returns a boolean value.
710
843
  */
711
844
  isSubtreeBST(beginRoot, iterationType = this.iterationType) {
712
845
  // TODO there is a bug
@@ -741,11 +874,18 @@ class BinaryTree {
741
874
  }
742
875
  }
743
876
  /**
877
+ * Time Complexity: O(n)
878
+ * Space Complexity: O(1)
879
+ */
880
+ /**
881
+ * Time Complexity: O(n)
882
+ * Space Complexity: O(1)
883
+ *
744
884
  * The function checks if a binary tree is a binary search tree.
745
885
  * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
746
886
  * be used when checking if the binary tree is a binary search tree (BST). It is an optional
747
- * parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
748
- * provided in
887
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
888
+ * expected to be
749
889
  * @returns a boolean value.
750
890
  */
751
891
  isBST(iterationType = this.iterationType) {
@@ -754,19 +894,30 @@ class BinaryTree {
754
894
  return this.isSubtreeBST(this.root, iterationType);
755
895
  }
756
896
  /**
897
+ * Time complexity: O(n)
898
+ * Space complexity: O(log n)
899
+ */
900
+ /**
901
+ * Time complexity: O(n)
902
+ * Space complexity: O(log n)
903
+ *
757
904
  * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
758
905
  * node, either recursively or iteratively.
759
- * @param callback - The `callback` parameter is a function that will be called on each node in the
760
- * subtree traversal. It takes a single argument, which is the current node being traversed, and
761
- * returns a value. The return values from each callback invocation will be collected and returned as
762
- * an array.
763
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
764
- * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
765
- * start from the root of the tree.
906
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
907
+ * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
908
+ * returns a value of any type.
909
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
910
+ * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
911
+ * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
912
+ * the default value.
766
913
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
767
- * performed on the binary tree. It can have two possible values:
768
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
769
- * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
914
+ * performed on the subtree. It can have two possible values:
915
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
916
+ * whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
917
+ * traversal will include null values, otherwise it will skip them.
918
+ * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
919
+ * the `callback` function on each node in the subtree. The type of the array elements is determined
920
+ * by the return type of the `callback` function.
770
921
  */
771
922
  subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
772
923
  beginRoot = this.ensureNotKey(beginRoot);
@@ -843,20 +994,31 @@ class BinaryTree {
843
994
  return typeof potentialKey === 'number';
844
995
  }
845
996
  /**
846
- * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
847
- * function on each node according to a specified order pattern.
848
- * @param callback - The `callback` parameter is a function that will be called on each node during
849
- * the depth-first search traversal. It takes a node as input and returns a value. The default value
850
- * is `this._defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
997
+ * Time complexity: O(n)
998
+ * Space complexity: O(n)
999
+ */
1000
+ /**
1001
+ * Time complexity: O(n)
1002
+ * Space complexity: O(n)
1003
+ *
1004
+ * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
1005
+ * specified pattern and iteration type, and returns an array of values obtained from applying a
1006
+ * callback function to each visited node.
1007
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1008
+ * the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
1009
+ * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
851
1010
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
852
- * nodes are visited during the depth-first search. There are three possible values for `pattern`:
853
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
854
- * search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
855
- * is `null`, an empty array will be returned.
1011
+ * nodes are traversed during the depth-first search. It can have one of the following values:
1012
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1013
+ * for the depth-first search traversal. It can be specified as a key, a node object, or
1014
+ * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
856
1015
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
857
- * iteration used in the depth-first search algorithm. It can have two possible values:
858
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
859
- * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
1016
+ * iteration to use when traversing the tree. It can have one of the following values:
1017
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1018
+ * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1019
+ * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1020
+ * `false`, null or undefined
1021
+ * @returns an array of values that are the return values of the callback function.
860
1022
  */
861
1023
  dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
862
1024
  beginRoot = this.ensureNotKey(beginRoot);
@@ -965,18 +1127,29 @@ class BinaryTree {
965
1127
  return ans;
966
1128
  }
967
1129
  /**
968
- * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
969
- * function on each node.
970
- * @param callback - The `callback` parameter is a function that will be called for each node in the
971
- * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
972
- * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this._defaultOneParamCallback
973
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
974
- * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
975
- * will not be performed and an empty array will be returned.
976
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
977
- * in the breadth-first search (BFS) algorithm. It can have two possible values:
978
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
979
- * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
1130
+ * Time complexity: O(n)
1131
+ * Space complexity: O(n)
1132
+ */
1133
+ /**
1134
+ * Time complexity: O(n)
1135
+ * Space complexity: O(n)
1136
+ *
1137
+ * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1138
+ * callback function on each node.
1139
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1140
+ * the breadth-first search traversal. It takes a single parameter, which is the current node being
1141
+ * visited, and returns a value of any type.
1142
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1143
+ * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1144
+ * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1145
+ * the class is used as
1146
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1147
+ * performed during the breadth-first search (BFS). It can have two possible values:
1148
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1149
+ * or not to include null values in the breadth-first search traversal. If `includeNull` is set to
1150
+ * `true`, null values will be included in the traversal, otherwise they will be skipped.
1151
+ * @returns an array of values that are the result of invoking the callback function on each node in
1152
+ * the breadth-first traversal of a binary tree.
980
1153
  */
981
1154
  bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
982
1155
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1031,20 +1204,29 @@ class BinaryTree {
1031
1204
  return ans;
1032
1205
  }
1033
1206
  /**
1034
- * The `listLevels` function takes a binary tree node and a callback function, and returns an array
1035
- * of arrays representing the levels of the tree.
1036
- * @param {C} callback - The `callback` parameter is a function that will be called on each node in
1037
- * the tree. It takes a node as input and returns a value. The return type of the callback function
1038
- * is determined by the generic type `C`.
1039
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
1040
- * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
1041
- * from the root node of the binary tree.
1042
- * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
1043
- * recursively or iteratively. It can have two possible values:
1044
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
1045
- * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
1046
- * level in a binary tree. Each inner array contains the return type of the provided callback
1047
- * function `C` applied to the nodes at that level.
1207
+ * Time complexity: O(n)
1208
+ * Space complexity: O(n)
1209
+ */
1210
+ /**
1211
+ * Time complexity: O(n)
1212
+ * Space complexity: O(n)
1213
+ *
1214
+ * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1215
+ * a binary tree and contains the values returned by a callback function applied to the nodes at that
1216
+ * level.
1217
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1218
+ * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1219
+ * returns a value of any type.
1220
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1221
+ * starting node for traversing the tree. It can be either a node object (`N`), a key value
1222
+ * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1223
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1224
+ * performed on the tree. It can have two possible values:
1225
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1226
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1227
+ * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1228
+ * be excluded
1229
+ * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1048
1230
  */
1049
1231
  listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1050
1232
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1096,9 +1278,10 @@ class BinaryTree {
1096
1278
  return levelsNodes;
1097
1279
  }
1098
1280
  /**
1099
- * The function returns the predecessor node of a given node in a binary tree.
1100
- * @param {N} node - The parameter "node" represents a node in a binary tree.
1101
- * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
1281
+ * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1282
+ * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1283
+ * `null`, or `undefined`.
1284
+ * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1102
1285
  */
1103
1286
  getPredecessor(node) {
1104
1287
  node = this.ensureNotKey(node);
@@ -1118,11 +1301,10 @@ class BinaryTree {
1118
1301
  }
1119
1302
  }
1120
1303
  /**
1121
- * The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
1122
- * `x` is the last node.
1123
- * @param {N} x - N - a node in a binary tree
1124
- * @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
1125
- * if there is no successor, or `undefined` if the input `x` is `undefined`.
1304
+ * The function `getSuccessor` returns the next node in a binary tree given a current node.
1305
+ * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1306
+ * @returns the successor of the given node or key. The successor is the node that comes immediately
1307
+ * after the given node in the inorder traversal of the binary tree.
1126
1308
  */
1127
1309
  getSuccessor(x) {
1128
1310
  x = this.ensureNotKey(x);
@@ -1139,18 +1321,26 @@ class BinaryTree {
1139
1321
  return y;
1140
1322
  }
1141
1323
  /**
1142
- * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
1143
- * algorithm and returns an array of values obtained by applying a callback function to each node.
1144
- * @param callback - The `callback` parameter is a function that will be called on each node in the
1145
- * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
1146
- * default value for this parameter is `this._defaultOneParamCallback`.
1324
+ * Time complexity: O(n)
1325
+ * Space complexity: O(1)
1326
+ */
1327
+ /**
1328
+ * Time complexity: O(n)
1329
+ * Space complexity: O(1)
1330
+ * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1331
+ * algorithm.
1332
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1333
+ * the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
1334
+ * a value of any type.
1147
1335
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1148
1336
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1149
1337
  * following values:
1150
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
1151
- * traversal. It specifies the root node of the tree from which the traversal should begin. If
1152
- * `beginRoot` is `null`, an empty array will be returned.
1153
- * @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
1338
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1339
+ * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1340
+ * the root of the tree. If no value is provided, the default value is the root of the tree.
1341
+ * @returns The function `morris` returns an array of values that are the result of invoking the
1342
+ * `callback` function on each node in the binary tree. The type of the array elements is determined
1343
+ * by the return type of the `callback` function.
1154
1344
  */
1155
1345
  morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
1156
1346
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1237,7 +1427,6 @@ class BinaryTree {
1237
1427
  }
1238
1428
  return ans;
1239
1429
  }
1240
- // --- start additional methods ---
1241
1430
  /**
1242
1431
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
1243
1432
  * either an iterative or recursive manner.
@@ -1268,12 +1457,10 @@ class BinaryTree {
1268
1457
  }
1269
1458
  else {
1270
1459
  if (node.left) {
1271
- // @ts-ignore
1272
1460
  yield* this[Symbol.iterator](node.left);
1273
1461
  }
1274
1462
  yield node.key;
1275
1463
  if (node.right) {
1276
- // @ts-ignore
1277
1464
  yield* this[Symbol.iterator](node.right);
1278
1465
  }
1279
1466
  }
@@ -1354,9 +1541,9 @@ class BinaryTree {
1354
1541
  }
1355
1542
  /**
1356
1543
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1357
- * @param {N | null | undefined} root - The `root` parameter in the `print` function represents the
1358
- * root node of a binary tree. It can have one of the following types: `BTNKey`, `N`, `null`, or
1359
- * `undefined`. The default value is `this.root`, which suggests that `this.root` is the
1544
+ * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1545
+ * undefined`. It represents the root node of a binary tree. The root node can have one of the
1546
+ * following types:
1360
1547
  */
1361
1548
  print(beginRoot = this.root) {
1362
1549
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1369,17 +1556,17 @@ class BinaryTree {
1369
1556
  }
1370
1557
  };
1371
1558
  const _displayAux = (node) => {
1372
- if (node === undefined || node === null) {
1559
+ if (!this.isRealNode(node)) {
1373
1560
  return [[], 0, 0, 0];
1374
1561
  }
1375
- if (node && node.right === undefined && node.left === undefined) {
1562
+ if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1376
1563
  const line = `${node.key}`;
1377
1564
  const width = line.length;
1378
1565
  const height = 1;
1379
1566
  const middle = Math.floor(width / 2);
1380
1567
  return [[line], width, height, middle];
1381
1568
  }
1382
- if (node && node.right === undefined) {
1569
+ if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1383
1570
  const [lines, n, p, x] = _displayAux(node.left);
1384
1571
  const s = `${node.key}`;
1385
1572
  const u = s.length;
@@ -1388,7 +1575,7 @@ class BinaryTree {
1388
1575
  const shifted_lines = lines.map(line => line + ' '.repeat(u));
1389
1576
  return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1390
1577
  }
1391
- if (node && node.left === undefined) {
1578
+ if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1392
1579
  const [lines, n, p, u] = _displayAux(node.right);
1393
1580
  const s = `${node.key}`;
1394
1581
  const x = s.length;