min-heap-typed 1.42.8 → 1.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
  2. package/dist/data-structures/binary-tree/avl-tree.js +88 -23
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
  4. package/dist/data-structures/binary-tree/binary-tree.js +415 -236
  5. package/dist/data-structures/binary-tree/bst.d.ts +121 -66
  6. package/dist/data-structures/binary-tree/bst.js +121 -67
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
  8. package/dist/data-structures/binary-tree/rb-tree.js +95 -18
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  10. package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
  11. package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
  12. package/dist/data-structures/graph/abstract-graph.js +147 -36
  13. package/dist/data-structures/graph/directed-graph.d.ts +126 -0
  14. package/dist/data-structures/graph/directed-graph.js +126 -0
  15. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  16. package/dist/data-structures/graph/undirected-graph.js +63 -0
  17. package/dist/data-structures/heap/heap.d.ts +175 -12
  18. package/dist/data-structures/heap/heap.js +175 -12
  19. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  20. package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
  21. package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  22. package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
  23. package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  24. package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
  25. package/dist/data-structures/queue/deque.d.ts +113 -3
  26. package/dist/data-structures/queue/deque.js +113 -3
  27. package/dist/data-structures/queue/queue.d.ts +87 -0
  28. package/dist/data-structures/queue/queue.js +87 -0
  29. package/dist/data-structures/stack/stack.d.ts +42 -0
  30. package/dist/data-structures/stack/stack.js +42 -0
  31. package/dist/data-structures/trie/trie.d.ts +76 -0
  32. package/dist/data-structures/trie/trie.js +76 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  35. package/src/data-structures/binary-tree/binary-tree.ts +465 -256
  36. package/src/data-structures/binary-tree/bst.ts +130 -68
  37. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  38. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  39. package/src/data-structures/graph/abstract-graph.ts +133 -7
  40. package/src/data-structures/graph/directed-graph.ts +145 -1
  41. package/src/data-structures/graph/undirected-graph.ts +72 -0
  42. package/src/data-structures/heap/heap.ts +201 -12
  43. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  44. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  45. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  46. package/src/data-structures/queue/deque.ts +127 -3
  47. package/src/data-structures/queue/queue.ts +99 -0
  48. package/src/data-structures/stack/stack.ts +48 -0
  49. package/src/data-structures/trie/trie.ts +87 -4
@@ -117,33 +117,40 @@ class BinaryTree {
117
117
  return new BinaryTreeNode(key, value);
118
118
  }
119
119
  /**
120
- * Add a node with the given key and value to the binary tree.
121
- * @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
122
- * @param {V} value - The value for the new node (optional).
123
- * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
120
+ * Time Complexity: O(n)
121
+ * Space Complexity: O(1)
122
+ * 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.
123
+ */
124
+ /**
125
+ * Time Complexity: O(n)
126
+ * Space Complexity: O(1)
127
+ *
128
+ * The `add` function adds a new node with a key and value to a binary tree, or updates the value of
129
+ * an existing node with the same key.
130
+ * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
131
+ * following types:
132
+ * @param {V} [value] - The value to be associated with the key or node being added to the binary
133
+ * tree.
134
+ * @returns The function `add` returns a node (`N`) if it was successfully inserted into the binary
135
+ * tree, or `null` or `undefined` if the insertion was not successful.
124
136
  */
125
137
  add(keyOrNode, value) {
126
138
  const _bfs = (root, newNode) => {
127
139
  const queue = new queue_1.Queue([root]);
128
140
  while (queue.size > 0) {
129
141
  const cur = queue.shift();
130
- if (cur) {
131
- if (newNode && cur.key === newNode.key) {
132
- cur.value = newNode.value;
133
- return;
134
- }
135
- const inserted = this._addTo(newNode, cur);
136
- if (inserted !== undefined)
137
- return inserted;
138
- if (cur.left)
139
- queue.push(cur.left);
140
- if (cur.right)
141
- queue.push(cur.right);
142
- }
143
- else
142
+ if (newNode && cur.key === newNode.key) {
143
+ cur.value = newNode.value;
144
144
  return;
145
+ }
146
+ const inserted = this._addTo(newNode, cur);
147
+ if (inserted !== undefined)
148
+ return inserted;
149
+ if (cur.left)
150
+ queue.push(cur.left);
151
+ if (cur.right)
152
+ queue.push(cur.right);
145
153
  }
146
- return;
147
154
  };
148
155
  let inserted, needInsert;
149
156
  if (keyOrNode === null) {
@@ -174,13 +181,21 @@ class BinaryTree {
174
181
  return inserted;
175
182
  }
176
183
  /**
177
- * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
178
- * values, and adds them to the binary tree.
179
- * @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
180
- * objects, or null values.
181
- * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
182
- * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
183
- * the value of the nodes will be `undefined`.
184
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
185
+ * Space Complexity: O(1)
186
+ */
187
+ /**
188
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
189
+ * Space Complexity: O(1)
190
+ *
191
+ * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
192
+ * each key-value pair to a data structure.
193
+ * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
194
+ * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
195
+ * or `undefined`.
196
+ * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
197
+ * correspond to the keys or nodes being added. If provided, the values will be associated with the
198
+ * keys or nodes during the add operation.
184
199
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
185
200
  */
186
201
  addMany(keysOrNodes, values) {
@@ -197,6 +212,13 @@ class BinaryTree {
197
212
  });
198
213
  }
199
214
  /**
215
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
216
+ * Space Complexity: O(1)
217
+ */
218
+ /**
219
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
220
+ * Space Complexity: O(1)
221
+ *
200
222
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
201
223
  * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
202
224
  * `BTNKey` or `N` values.
@@ -210,24 +232,29 @@ class BinaryTree {
210
232
  return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
211
233
  }
212
234
  /**
213
- * The `delete` function removes a node from a binary search tree and returns the deleted node along
214
- * with the parent node that needs to be balanced.
215
- * a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
216
- * binary tree.
217
- * @returns an array of `BiTreeDeleteResult<N>` objects.
218
- * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
219
- * `BTNKey` or a generic type `N`. It represents the property of the node that we are
220
- * searching for. It can be a specific key value or any other property of the node.
221
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
222
- * value. This value is compared with the `identifier` parameter to determine if the node should be
223
- * included in the result. The `callback` parameter has a default value of
224
- * `this._defaultOneParamCallback`, which
235
+ * Time Complexity: O(n)
236
+ * Space Complexity: O(1)
237
+ */
238
+ /**
239
+ * Time Complexity: O(n)
240
+ * Space Complexity: O(1)
241
+ *
242
+ * The function deletes a node from a binary tree and returns an array of the deleted nodes along
243
+ * with the nodes that need to be balanced.
244
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
245
+ * object that you want to delete from the binary tree. It can be of any type that is compatible with
246
+ * the callback function's return type. It can also be null or undefined if you want to delete a
247
+ * specific node based on its value or object.
248
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
249
+ * identifier of the node to be deleted. It is optional and has a default value of
250
+ * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
251
+ * @returns an array of `BiTreeDeleteResult<N>`.
225
252
  */
226
253
  delete(identifier, callback = this._defaultOneParamCallback) {
227
254
  const deletedResult = [];
228
255
  if (!this.root)
229
256
  return deletedResult;
230
- if (identifier instanceof BinaryTreeNode)
257
+ if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
231
258
  callback = (node => node);
232
259
  const curr = this.getNode(identifier, callback);
233
260
  if (!curr)
@@ -252,16 +279,18 @@ class BinaryTree {
252
279
  }
253
280
  }
254
281
  else {
255
- const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
256
- if (leftSubTreeRightMost) {
257
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
258
- orgCurrent = this._swap(curr, leftSubTreeRightMost);
259
- if (parentOfLeftSubTreeMax) {
260
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
261
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
262
- else
263
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
264
- needBalanced = parentOfLeftSubTreeMax;
282
+ if (curr.left) {
283
+ const leftSubTreeRightMost = this.getRightMost(curr.left);
284
+ if (leftSubTreeRightMost) {
285
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
286
+ orgCurrent = this._swap(curr, leftSubTreeRightMost);
287
+ if (parentOfLeftSubTreeMax) {
288
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
289
+ parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
290
+ else
291
+ parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
292
+ needBalanced = parentOfLeftSubTreeMax;
293
+ }
265
294
  }
266
295
  }
267
296
  }
@@ -270,15 +299,20 @@ class BinaryTree {
270
299
  return deletedResult;
271
300
  }
272
301
  /**
273
- * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
274
- * specified root node.
275
- * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node
276
- * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
277
- * of the node (`BTNKey`), or `null`.
278
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
279
- * starting node from which we want to calculate the depth. It can be either a node object or the key
280
- * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
281
- * node of the binary tree.
302
+ * Time Complexity: O(n)
303
+ * Space Complexity: O(1)
304
+ */
305
+ /**
306
+ * Time Complexity: O(n)
307
+ * Space Complexity: O(1)
308
+ *
309
+ * The function calculates the depth of a given node in a binary tree.
310
+ * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
311
+ * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
312
+ * `undefined`.
313
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
314
+ * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
315
+ * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
282
316
  * @returns the depth of the `distNode` relative to the `beginRoot`.
283
317
  */
284
318
  getDepth(distNode, beginRoot = this.root) {
@@ -295,15 +329,22 @@ class BinaryTree {
295
329
  return depth;
296
330
  }
297
331
  /**
298
- * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
299
- * iterative approach.
332
+ * Time Complexity: O(n)
333
+ * Space Complexity: O(log n)
334
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
335
+ */
336
+ /**
337
+ * Time Complexity: O(n)
338
+ * Space Complexity: O(log n)
339
+ *
340
+ * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
341
+ * iterative traversal.
300
342
  * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
301
- * starting node from which the height of the binary tree is calculated. It can be either a node
302
- * object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
303
- * node is specified. If `
343
+ * starting node of the binary tree from which we want to calculate the height. It can be of type
344
+ * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
304
345
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
305
- * height of the binary tree using a recursive approach or an iterative approach. It can have two
306
- * possible values:
346
+ * height of the tree using a recursive approach or an iterative approach. It can have two possible
347
+ * values:
307
348
  * @returns the height of the binary tree.
308
349
  */
309
350
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
@@ -321,9 +362,6 @@ class BinaryTree {
321
362
  return _getMaxHeight(beginRoot);
322
363
  }
323
364
  else {
324
- if (!beginRoot) {
325
- return -1;
326
- }
327
365
  const stack = [{ node: beginRoot, depth: 0 }];
328
366
  let maxHeight = 0;
329
367
  while (stack.length > 0) {
@@ -338,11 +376,19 @@ class BinaryTree {
338
376
  }
339
377
  }
340
378
  /**
379
+ * Time Complexity: O(n)
380
+ * Space Complexity: O(log n)
381
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
382
+ */
383
+ /**
384
+ * Time Complexity: O(n)
385
+ * Space Complexity: O(log n)
386
+ *
341
387
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
342
388
  * recursive or iterative approach.
343
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
344
- * calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
345
- * not provided.
389
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
390
+ * starting node of the binary tree from which we want to calculate the minimum height. It can be of
391
+ * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
346
392
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
347
393
  * to calculate the minimum height of a binary tree. It can have two possible values:
348
394
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -393,40 +439,54 @@ class BinaryTree {
393
439
  }
394
440
  }
395
441
  /**
442
+ * Time Complexity: O(n)
443
+ * Space Complexity: O(log n)
444
+ */
445
+ /**
446
+ * Time Complexity: O(n)
447
+ * Space Complexity: O(log n)
448
+ *
396
449
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
397
450
  * height of the tree.
398
- * @param {N | null | undefined} beginRoot - The parameter `beginRoot` is of type `N | null | undefined`, which means it can
399
- * either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
400
- * @returns The method is returning a boolean value.
451
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
452
+ * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
453
+ * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
454
+ * @returns a boolean value.
401
455
  */
402
456
  isPerfectlyBalanced(beginRoot = this.root) {
403
457
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
404
458
  }
405
459
  /**
406
- * The function `getNodes` returns an array of nodes that match a given node property, using either
407
- * recursive or iterative traversal.
408
- * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
409
- * `BTNKey` or a generic type `N`. It represents the property of the node that we are
410
- * searching for. It can be a specific key value or any other property of the node.
411
- * @param callback - The `callback` parameter is a function that takes a node as input and returns a
412
- * value. This value is compared with the `identifier` parameter to determine if the node should be
413
- * included in the result. The `callback` parameter has a default value of
414
- * `this._defaultOneParamCallback`, which
415
- * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
416
- * first node that matches the identifier. If set to true, the function will return an array with
417
- * only one element (or an empty array if no matching node is found). If set to false (default), the
418
- * function will continue searching for all
419
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which the
420
- * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
421
- * tree.
460
+ * Time Complexity: O(n)
461
+ * Space Complexity: O(log n).
462
+ */
463
+ /**
464
+ * Time Complexity: O(n)
465
+ * Space Complexity: O(log n).
466
+ *
467
+ * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
468
+ * callback function.
469
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
470
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
471
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
472
+ * specific value.
473
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
474
+ * input and returns a value of type `C`. It is used to determine if a node matches the given
475
+ * identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
476
+ * default
477
+ * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
478
+ * matches the identifier. If set to true, the function will stop iterating once it finds a matching
479
+ * node and return that node. If set to false (default), the function will continue iterating and
480
+ * return all nodes that match the identifier.
481
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
482
+ * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
483
+ * it is `null` or `undefined`, an empty array will be returned.
422
484
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
423
485
  * traverse the binary tree. It can have two possible values:
424
- * @returns The function `getNodes` returns an array of nodes (`N[]`).
486
+ * @returns an array of nodes of type `N`.
425
487
  */
426
488
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
427
- if (!beginRoot)
428
- return [];
429
- if (identifier instanceof BinaryTreeNode)
489
+ if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
430
490
  callback = (node => node);
431
491
  beginRoot = this.ensureNotKey(beginRoot);
432
492
  if (!beginRoot)
@@ -464,49 +524,73 @@ class BinaryTree {
464
524
  return ans;
465
525
  }
466
526
  /**
467
- * The function checks if a binary tree has a node with a given property or key.
468
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
469
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or a
470
- * generic type `N`.
471
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
472
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
473
- * whether the node matches the criteria or not. The default callback function
474
- * `this._defaultOneParamCallback` is used if no callback function is
475
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
476
- * the node from which the search should begin. By default, it is set to `this.root`, which means the
477
- * search will start from the root node of the binary tree. However, you can provide a different node
478
- * as
479
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
480
- * performed when searching for nodes in the binary tree. It can have one of the following values:
527
+ * Time Complexity: O(n)
528
+ * Space Complexity: O(log n).
529
+ */
530
+ /**
531
+ * Time Complexity: O(n)
532
+ *
533
+ * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
534
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
535
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
536
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
537
+ * specific identifier.
538
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
539
+ * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
540
+ * function should return a boolean value indicating whether the node should be included in the
541
+ * result or not.
542
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
543
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
544
+ * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
545
+ * @param iterationType - The `iterationType` parameter is a variable that determines the type of
546
+ * iteration to be performed on the binary tree. It is used to specify whether the iteration should
547
+ * be performed in a pre-order, in-order, or post-order manner.
481
548
  * @returns a boolean value.
482
549
  */
483
550
  has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
484
- if (identifier instanceof BinaryTreeNode)
551
+ if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
485
552
  callback = (node => node);
486
553
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
487
554
  }
488
555
  /**
489
- * The function `get` returns the first node in a binary tree that matches the given property or key.
490
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
491
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
492
- * type.
493
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
494
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
495
- * whether the node matches the criteria or not. The default callback function
496
- * (`this._defaultOneParamCallback`) is used if no callback function is
497
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
498
- * the root node from which the search should begin.
499
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
500
- * performed when searching for a node in the binary tree. It can have one of the following values:
501
- * @returns either the found node (of type N) or null if no node is found.
556
+ * Time Complexity: O(n)
557
+ * Space Complexity: O(log n)
558
+ */
559
+ /**
560
+ * Time Complexity: O(n)
561
+ * Space Complexity: O(log n)
562
+ *
563
+ * The function `getNode` returns the first node that matches the given identifier and callback
564
+ * function.
565
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
566
+ * used to identify the node you want to retrieve. It can be of any type that is returned by the
567
+ * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
568
+ * identifier.
569
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
570
+ * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
571
+ * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
572
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
573
+ * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
574
+ * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
575
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
576
+ * be performed when searching for nodes in the binary tree. It determines the order in which the
577
+ * nodes are visited during the search.
578
+ * @returns a value of type `N | null | undefined`.
502
579
  */
503
580
  getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
504
581
  var _a;
505
- if (identifier instanceof BinaryTreeNode)
582
+ if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
506
583
  callback = (node => node);
507
584
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
508
585
  }
509
586
  /**
587
+ * Time Complexity: O(n)
588
+ * Space Complexity: O(log n)
589
+ */
590
+ /**
591
+ * Time Complexity: O(n)
592
+ * Space Complexity: O(log n)
593
+ *
510
594
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
511
595
  * recursive or iterative iteration.
512
596
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
@@ -561,23 +645,34 @@ class BinaryTree {
561
645
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
562
646
  }
563
647
  /**
564
- * The function `get` returns the first node value in a binary tree that matches the given property or key.
565
- * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
566
- * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
567
- * type.
568
- * @param callback - The `callback` parameter is a function that is used to determine whether a node
569
- * matches the desired criteria. It takes a node as input and returns a boolean value indicating
570
- * whether the node matches the criteria or not. The default callback function
571
- * (`this._defaultOneParamCallback`) is used if no callback function is
572
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
573
- * the root node from which the search should begin.
574
- * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
575
- * performed when searching for a node in the binary tree. It can have one of the following values:
576
- * @returns either the found value (of type V) or undefined if no node value is found.
648
+ * Time Complexity: O(n)
649
+ * Space Complexity: O(log n)
650
+ */
651
+ /**
652
+ * Time Complexity: O(n)
653
+ * Space Complexity: O(log n)
654
+ *
655
+ * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
656
+ * and callback function.
657
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
658
+ * used to identify the node in the binary tree. It can be of any type that is the return type of the
659
+ * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
660
+ * @param {C} callback - The `callback` parameter is a function that will be called with each node in
661
+ * the binary tree. It is used to determine whether a node matches the given identifier. The callback
662
+ * function should return a value that can be compared to the identifier to determine if it is a
663
+ * match.
664
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
665
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
666
+ * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
667
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
668
+ * be performed when searching for a node in the binary tree. It is an optional parameter with a
669
+ * default value specified by `this.iterationType`.
670
+ * @returns The value of the node with the given identifier is being returned. If the node is not
671
+ * found, `undefined` is returned.
577
672
  */
578
673
  get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
579
674
  var _a, _b;
580
- if (identifier instanceof BinaryTreeNode)
675
+ if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
581
676
  callback = (node => node);
582
677
  return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
583
678
  }
@@ -596,14 +691,22 @@ class BinaryTree {
596
691
  return this.size === 0;
597
692
  }
598
693
  /**
599
- * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
600
- * up to the root node, with the option to reverse the order of the nodes.
601
- * @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
602
- * to find the path to the root node.
694
+ * Time Complexity: O(log n)
695
+ * Space Complexity: O(log n)
696
+ */
697
+ /**
698
+ * Time Complexity: O(log n)
699
+ * Space Complexity: O(log n)
700
+ *
701
+ * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
702
+ * structure, with the option to reverse the order of the nodes.
703
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
704
+ * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
705
+ * `null`, or `undefined`.
603
706
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
604
707
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
605
- * reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
606
- * @returns The function `getPathToRoot` returns an array of type `N[]`.
708
+ * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
709
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
607
710
  */
608
711
  getPathToRoot(beginRoot, isReverse = true) {
609
712
  // TODO to support get path through passing key
@@ -621,15 +724,22 @@ class BinaryTree {
621
724
  return isReverse ? result.reverse() : result;
622
725
  }
623
726
  /**
624
- * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
625
- * iterative traversal.
727
+ * Time Complexity: O(log n)
728
+ * Space Complexity: O(1)
729
+ */
730
+ /**
731
+ * Time Complexity: O(log n)
732
+ * Space Complexity: O(1)
733
+ *
734
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
735
+ * iteratively.
626
736
  * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
627
- * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
628
- * of a node (`BTNKey`), or `null` if the tree is empty.
737
+ * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
738
+ * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
629
739
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
630
740
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
631
- * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
632
- * no leftmost node, it returns `null`.
741
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
742
+ * is no leftmost node, it returns `null` or `undefined` depending on the input.
633
743
  */
634
744
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
635
745
  beginRoot = this.ensureNotKey(beginRoot);
@@ -654,15 +764,23 @@ class BinaryTree {
654
764
  }
655
765
  }
656
766
  /**
767
+ * Time Complexity: O(log n)
768
+ * Space Complexity: O(1)
769
+ */
770
+ /**
771
+ * Time Complexity: O(log n)
772
+ * Space Complexity: O(1)
773
+ *
657
774
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
658
775
  * iteratively.
659
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node from which we want to
660
- * find the rightmost node. It is of type `N | null | undefined`, which means it can either be a node of type `N`
661
- * or `null`. If it is `null`, it means there is no starting node
662
- * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
663
- * be performed when finding the rightmost node in a binary tree. It can have two possible values:
664
- * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
665
- * `beginRoot` parameter is `null`, it returns `null`.
776
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
777
+ * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
778
+ * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
779
+ * current object.
780
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
781
+ * type of iteration to use when finding the rightmost node. It can have one of two values:
782
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
783
+ * is no rightmost node, it returns `null` or `undefined`, depending on the input.
666
784
  */
667
785
  getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
668
786
  // TODO support get right most by passing key in
@@ -688,13 +806,20 @@ class BinaryTree {
688
806
  }
689
807
  }
690
808
  /**
809
+ * Time Complexity: O(n)
810
+ * Space Complexity: O(1)
811
+ */
812
+ /**
813
+ * Time Complexity: O(n)
814
+ * Space Complexity: O(1)
815
+ *
691
816
  * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
692
- * @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
693
- * to check if it is a binary search tree (BST) subtree.
817
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
818
+ * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
694
819
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
695
820
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
696
821
  * possible values:
697
- * @returns The function `isSubtreeBST` returns a boolean value.
822
+ * @returns a boolean value.
698
823
  */
699
824
  isSubtreeBST(beginRoot, iterationType = this.iterationType) {
700
825
  // TODO there is a bug
@@ -729,11 +854,18 @@ class BinaryTree {
729
854
  }
730
855
  }
731
856
  /**
857
+ * Time Complexity: O(n)
858
+ * Space Complexity: O(1)
859
+ */
860
+ /**
861
+ * Time Complexity: O(n)
862
+ * Space Complexity: O(1)
863
+ *
732
864
  * The function checks if a binary tree is a binary search tree.
733
865
  * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
734
866
  * be used when checking if the binary tree is a binary search tree (BST). It is an optional
735
- * parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
736
- * provided in
867
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
868
+ * expected to be
737
869
  * @returns a boolean value.
738
870
  */
739
871
  isBST(iterationType = this.iterationType) {
@@ -742,19 +874,30 @@ class BinaryTree {
742
874
  return this.isSubtreeBST(this.root, iterationType);
743
875
  }
744
876
  /**
877
+ * Time complexity: O(n)
878
+ * Space complexity: O(log n)
879
+ */
880
+ /**
881
+ * Time complexity: O(n)
882
+ * Space complexity: O(log n)
883
+ *
745
884
  * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
746
885
  * node, either recursively or iteratively.
747
- * @param callback - The `callback` parameter is a function that will be called on each node in the
748
- * subtree traversal. It takes a single argument, which is the current node being traversed, and
749
- * returns a value. The return values from each callback invocation will be collected and returned as
750
- * an array.
751
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
752
- * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
753
- * start from the root of the tree.
886
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
887
+ * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
888
+ * returns a value of any type.
889
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
890
+ * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
891
+ * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
892
+ * the default value.
754
893
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
755
- * performed on the binary tree. It can have two possible values:
756
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
757
- * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
894
+ * performed on the subtree. It can have two possible values:
895
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
896
+ * whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
897
+ * traversal will include null values, otherwise it will skip them.
898
+ * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
899
+ * the `callback` function on each node in the subtree. The type of the array elements is determined
900
+ * by the return type of the `callback` function.
758
901
  */
759
902
  subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
760
903
  beginRoot = this.ensureNotKey(beginRoot);
@@ -831,20 +974,31 @@ class BinaryTree {
831
974
  return typeof potentialKey === 'number';
832
975
  }
833
976
  /**
834
- * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
835
- * function on each node according to a specified order pattern.
836
- * @param callback - The `callback` parameter is a function that will be called on each node during
837
- * the depth-first search traversal. It takes a node as input and returns a value. The default value
838
- * is `this._defaultOneParamCallback`, which is a callback function defined elsewhere in the code.
977
+ * Time complexity: O(n)
978
+ * Space complexity: O(n)
979
+ */
980
+ /**
981
+ * Time complexity: O(n)
982
+ * Space complexity: O(n)
983
+ *
984
+ * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
985
+ * specified pattern and iteration type, and returns an array of values obtained from applying a
986
+ * callback function to each visited node.
987
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
988
+ * the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
989
+ * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
839
990
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
840
- * nodes are visited during the depth-first search. There are three possible values for `pattern`:
841
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
842
- * search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
843
- * is `null`, an empty array will be returned.
991
+ * nodes are traversed during the depth-first search. It can have one of the following values:
992
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
993
+ * for the depth-first search traversal. It can be specified as a key, a node object, or
994
+ * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
844
995
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
845
- * iteration used in the depth-first search algorithm. It can have two possible values:
846
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
847
- * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
996
+ * iteration to use when traversing the tree. It can have one of the following values:
997
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
998
+ * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
999
+ * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1000
+ * `false`, null or undefined
1001
+ * @returns an array of values that are the return values of the callback function.
848
1002
  */
849
1003
  dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
850
1004
  beginRoot = this.ensureNotKey(beginRoot);
@@ -953,18 +1107,29 @@ class BinaryTree {
953
1107
  return ans;
954
1108
  }
955
1109
  /**
956
- * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
957
- * function on each node.
958
- * @param callback - The `callback` parameter is a function that will be called for each node in the
959
- * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
960
- * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this._defaultOneParamCallback
961
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
962
- * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
963
- * will not be performed and an empty array will be returned.
964
- * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
965
- * in the breadth-first search (BFS) algorithm. It can have two possible values:
966
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
967
- * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
1110
+ * Time complexity: O(n)
1111
+ * Space complexity: O(n)
1112
+ */
1113
+ /**
1114
+ * Time complexity: O(n)
1115
+ * Space complexity: O(n)
1116
+ *
1117
+ * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1118
+ * callback function on each node.
1119
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1120
+ * the breadth-first search traversal. It takes a single parameter, which is the current node being
1121
+ * visited, and returns a value of any type.
1122
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1123
+ * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1124
+ * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1125
+ * the class is used as
1126
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1127
+ * performed during the breadth-first search (BFS). It can have two possible values:
1128
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1129
+ * or not to include null values in the breadth-first search traversal. If `includeNull` is set to
1130
+ * `true`, null values will be included in the traversal, otherwise they will be skipped.
1131
+ * @returns an array of values that are the result of invoking the callback function on each node in
1132
+ * the breadth-first traversal of a binary tree.
968
1133
  */
969
1134
  bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
970
1135
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1019,20 +1184,29 @@ class BinaryTree {
1019
1184
  return ans;
1020
1185
  }
1021
1186
  /**
1022
- * The `listLevels` function takes a binary tree node and a callback function, and returns an array
1023
- * of arrays representing the levels of the tree.
1024
- * @param {C} callback - The `callback` parameter is a function that will be called on each node in
1025
- * the tree. It takes a node as input and returns a value. The return type of the callback function
1026
- * is determined by the generic type `C`.
1027
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
1028
- * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
1029
- * from the root node of the binary tree.
1030
- * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
1031
- * recursively or iteratively. It can have two possible values:
1032
- * @param includeNull - The choice to output null values during binary tree traversal should be provided.
1033
- * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
1034
- * level in a binary tree. Each inner array contains the return type of the provided callback
1035
- * function `C` applied to the nodes at that level.
1187
+ * Time complexity: O(n)
1188
+ * Space complexity: O(n)
1189
+ */
1190
+ /**
1191
+ * Time complexity: O(n)
1192
+ * Space complexity: O(n)
1193
+ *
1194
+ * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1195
+ * a binary tree and contains the values returned by a callback function applied to the nodes at that
1196
+ * level.
1197
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1198
+ * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1199
+ * returns a value of any type.
1200
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1201
+ * starting node for traversing the tree. It can be either a node object (`N`), a key value
1202
+ * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1203
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1204
+ * performed on the tree. It can have two possible values:
1205
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1206
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1207
+ * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1208
+ * be excluded
1209
+ * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1036
1210
  */
1037
1211
  listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1038
1212
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1084,9 +1258,10 @@ class BinaryTree {
1084
1258
  return levelsNodes;
1085
1259
  }
1086
1260
  /**
1087
- * The function returns the predecessor node of a given node in a binary tree.
1088
- * @param {N} node - The parameter "node" represents a node in a binary tree.
1089
- * @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
1261
+ * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1262
+ * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1263
+ * `null`, or `undefined`.
1264
+ * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1090
1265
  */
1091
1266
  getPredecessor(node) {
1092
1267
  node = this.ensureNotKey(node);
@@ -1106,11 +1281,10 @@ class BinaryTree {
1106
1281
  }
1107
1282
  }
1108
1283
  /**
1109
- * The function `getSuccessor` returns the next node in a binary tree given a node `x`, or `null` if
1110
- * `x` is the last node.
1111
- * @param {N} x - N - a node in a binary tree
1112
- * @returns The function `getSuccessor` returns a value of type `N` (the successor node), or `null`
1113
- * if there is no successor, or `undefined` if the input `x` is `undefined`.
1284
+ * The function `getSuccessor` returns the next node in a binary tree given a current node.
1285
+ * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1286
+ * @returns the successor of the given node or key. The successor is the node that comes immediately
1287
+ * after the given node in the inorder traversal of the binary tree.
1114
1288
  */
1115
1289
  getSuccessor(x) {
1116
1290
  x = this.ensureNotKey(x);
@@ -1127,18 +1301,26 @@ class BinaryTree {
1127
1301
  return y;
1128
1302
  }
1129
1303
  /**
1130
- * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
1131
- * algorithm and returns an array of values obtained by applying a callback function to each node.
1132
- * @param callback - The `callback` parameter is a function that will be called on each node in the
1133
- * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
1134
- * default value for this parameter is `this._defaultOneParamCallback`.
1304
+ * Time complexity: O(n)
1305
+ * Space complexity: O(1)
1306
+ */
1307
+ /**
1308
+ * Time complexity: O(n)
1309
+ * Space complexity: O(1)
1310
+ * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1311
+ * algorithm.
1312
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1313
+ * the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
1314
+ * a value of any type.
1135
1315
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1136
1316
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1137
1317
  * following values:
1138
- * @param {N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node for the Morris
1139
- * traversal. It specifies the root node of the tree from which the traversal should begin. If
1140
- * `beginRoot` is `null`, an empty array will be returned.
1141
- * @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
1318
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1319
+ * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1320
+ * the root of the tree. If no value is provided, the default value is the root of the tree.
1321
+ * @returns The function `morris` returns an array of values that are the result of invoking the
1322
+ * `callback` function on each node in the binary tree. The type of the array elements is determined
1323
+ * by the return type of the `callback` function.
1142
1324
  */
1143
1325
  morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
1144
1326
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1225,7 +1407,6 @@ class BinaryTree {
1225
1407
  }
1226
1408
  return ans;
1227
1409
  }
1228
- // --- start additional methods ---
1229
1410
  /**
1230
1411
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
1231
1412
  * either an iterative or recursive manner.
@@ -1256,12 +1437,10 @@ class BinaryTree {
1256
1437
  }
1257
1438
  else {
1258
1439
  if (node.left) {
1259
- // @ts-ignore
1260
1440
  yield* this[Symbol.iterator](node.left);
1261
1441
  }
1262
1442
  yield node.key;
1263
1443
  if (node.right) {
1264
- // @ts-ignore
1265
1444
  yield* this[Symbol.iterator](node.right);
1266
1445
  }
1267
1446
  }
@@ -1341,9 +1520,9 @@ class BinaryTree {
1341
1520
  }
1342
1521
  /**
1343
1522
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1344
- * @param {N | null | undefined} root - The `root` parameter in the `print` function represents the
1345
- * root node of a binary tree. It can have one of the following types: `BTNKey`, `N`, `null`, or
1346
- * `undefined`. The default value is `this.root`, which suggests that `this.root` is the
1523
+ * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1524
+ * undefined`. It represents the root node of a binary tree. The root node can have one of the
1525
+ * following types:
1347
1526
  */
1348
1527
  print(beginRoot = this.root) {
1349
1528
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1356,17 +1535,17 @@ class BinaryTree {
1356
1535
  }
1357
1536
  };
1358
1537
  const _displayAux = (node) => {
1359
- if (node === undefined || node === null) {
1538
+ if (!this.isRealNode(node)) {
1360
1539
  return [[], 0, 0, 0];
1361
1540
  }
1362
- if (node && node.right === undefined && node.left === undefined) {
1541
+ if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1363
1542
  const line = `${node.key}`;
1364
1543
  const width = line.length;
1365
1544
  const height = 1;
1366
1545
  const middle = Math.floor(width / 2);
1367
1546
  return [[line], width, height, middle];
1368
1547
  }
1369
- if (node && node.right === undefined) {
1548
+ if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1370
1549
  const [lines, n, p, x] = _displayAux(node.left);
1371
1550
  const s = `${node.key}`;
1372
1551
  const u = s.length;
@@ -1375,7 +1554,7 @@ class BinaryTree {
1375
1554
  const shifted_lines = lines.map(line => line + ' '.repeat(u));
1376
1555
  return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1377
1556
  }
1378
- if (node && node.left === undefined) {
1557
+ if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1379
1558
  const [lines, n, p, u] = _displayAux(node.right);
1380
1559
  const s = `${node.key}`;
1381
1560
  const x = s.length;