min-heap-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 (49) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
  2. package/dist/data-structures/binary-tree/avl-tree.js +88 -23
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
  4. package/dist/data-structures/binary-tree/binary-tree.js +388 -201
  5. package/dist/data-structures/binary-tree/bst.d.ts +121 -66
  6. package/dist/data-structures/binary-tree/bst.js +121 -67
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
  8. package/dist/data-structures/binary-tree/rb-tree.js +95 -18
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
  10. package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
  11. package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
  12. package/dist/data-structures/graph/abstract-graph.js +147 -36
  13. package/dist/data-structures/graph/directed-graph.d.ts +126 -0
  14. package/dist/data-structures/graph/directed-graph.js +126 -0
  15. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  16. package/dist/data-structures/graph/undirected-graph.js +63 -0
  17. package/dist/data-structures/heap/heap.d.ts +175 -12
  18. package/dist/data-structures/heap/heap.js +175 -12
  19. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
  20. package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
  21. package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
  22. package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
  23. package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
  24. package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
  25. package/dist/data-structures/queue/deque.d.ts +113 -3
  26. package/dist/data-structures/queue/deque.js +113 -3
  27. package/dist/data-structures/queue/queue.d.ts +87 -0
  28. package/dist/data-structures/queue/queue.js +87 -0
  29. package/dist/data-structures/stack/stack.d.ts +42 -0
  30. package/dist/data-structures/stack/stack.js +42 -0
  31. package/dist/data-structures/trie/trie.d.ts +76 -0
  32. package/dist/data-structures/trie/trie.js +76 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree.ts +97 -23
  35. package/src/data-structures/binary-tree/binary-tree.ts +419 -204
  36. package/src/data-structures/binary-tree/bst.ts +130 -68
  37. package/src/data-structures/binary-tree/rb-tree.ts +106 -19
  38. package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
  39. package/src/data-structures/graph/abstract-graph.ts +133 -7
  40. package/src/data-structures/graph/directed-graph.ts +145 -1
  41. package/src/data-structures/graph/undirected-graph.ts +72 -0
  42. package/src/data-structures/heap/heap.ts +201 -12
  43. package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
  44. package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
  45. package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
  46. package/src/data-structures/queue/deque.ts +127 -3
  47. package/src/data-structures/queue/queue.ts +99 -0
  48. package/src/data-structures/stack/stack.ts +48 -0
  49. package/src/data-structures/trie/trie.ts +87 -4
@@ -117,10 +117,22 @@ 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) => {
@@ -174,13 +186,21 @@ class BinaryTree {
174
186
  return inserted;
175
187
  }
176
188
  /**
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`.
189
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
190
+ * Space Complexity: O(1)
191
+ */
192
+ /**
193
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
194
+ * Space Complexity: O(1)
195
+ *
196
+ * The `addMany` function takes an array of keys or nodes and an optional array of values, and adds
197
+ * each key-value pair to a data structure.
198
+ * @param {(BTNKey | N |null | undefined)[]} keysOrNodes - An array of keys or nodes to be added to
199
+ * the binary search tree. Each element can be of type `BTNKey` (a key value), `N` (a node), `null`,
200
+ * or `undefined`.
201
+ * @param {(V | undefined)[]} [values] - The `values` parameter is an optional array of values that
202
+ * correspond to the keys or nodes being added. If provided, the values will be associated with the
203
+ * keys or nodes during the add operation.
184
204
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
185
205
  */
186
206
  addMany(keysOrNodes, values) {
@@ -197,6 +217,13 @@ class BinaryTree {
197
217
  });
198
218
  }
199
219
  /**
220
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
221
+ * Space Complexity: O(1)
222
+ */
223
+ /**
224
+ * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
225
+ * Space Complexity: O(1)
226
+ *
200
227
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
201
228
  * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
202
229
  * `BTNKey` or `N` values.
@@ -210,18 +237,23 @@ class BinaryTree {
210
237
  return keysOrNodes.length === this.addMany(keysOrNodes, values).length;
211
238
  }
212
239
  /**
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
240
+ * Time Complexity: O(n)
241
+ * Space Complexity: O(1)
242
+ */
243
+ /**
244
+ * Time Complexity: O(n)
245
+ * Space Complexity: O(1)
246
+ *
247
+ * The function deletes a node from a binary tree and returns an array of the deleted nodes along
248
+ * with the nodes that need to be balanced.
249
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value or
250
+ * object that you want to delete from the binary tree. It can be of any type that is compatible with
251
+ * the callback function's return type. It can also be null or undefined if you want to delete a
252
+ * specific node based on its value or object.
253
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
254
+ * identifier of the node to be deleted. It is optional and has a default value of
255
+ * `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
256
+ * @returns an array of `BiTreeDeleteResult<N>`.
225
257
  */
226
258
  delete(identifier, callback = this._defaultOneParamCallback) {
227
259
  const deletedResult = [];
@@ -270,15 +302,20 @@ class BinaryTree {
270
302
  return deletedResult;
271
303
  }
272
304
  /**
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.
305
+ * Time Complexity: O(n)
306
+ * Space Complexity: O(1)
307
+ */
308
+ /**
309
+ * Time Complexity: O(n)
310
+ * Space Complexity: O(1)
311
+ *
312
+ * The function calculates the depth of a given node in a binary tree.
313
+ * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
314
+ * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
315
+ * `undefined`.
316
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
317
+ * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
318
+ * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
282
319
  * @returns the depth of the `distNode` relative to the `beginRoot`.
283
320
  */
284
321
  getDepth(distNode, beginRoot = this.root) {
@@ -295,15 +332,22 @@ class BinaryTree {
295
332
  return depth;
296
333
  }
297
334
  /**
298
- * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
299
- * iterative approach.
335
+ * Time Complexity: O(n)
336
+ * Space Complexity: O(log n)
337
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
338
+ */
339
+ /**
340
+ * Time Complexity: O(n)
341
+ * Space Complexity: O(log n)
342
+ *
343
+ * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
344
+ * iterative traversal.
300
345
  * @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 `
346
+ * starting node of the binary tree from which we want to calculate the height. It can be of type
347
+ * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
304
348
  * @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:
349
+ * height of the tree using a recursive approach or an iterative approach. It can have two possible
350
+ * values:
307
351
  * @returns the height of the binary tree.
308
352
  */
309
353
  getHeight(beginRoot = this.root, iterationType = this.iterationType) {
@@ -338,11 +382,19 @@ class BinaryTree {
338
382
  }
339
383
  }
340
384
  /**
385
+ * Time Complexity: O(n)
386
+ * Space Complexity: O(log n)
387
+ * Best Case - O(log n) (when using recursive iterationType), Worst Case - O(n) (when using iterative iterationType)
388
+ */
389
+ /**
390
+ * Time Complexity: O(n)
391
+ * Space Complexity: O(log n)
392
+ *
341
393
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
342
394
  * 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.
395
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
396
+ * starting node of the binary tree from which we want to calculate the minimum height. It can be of
397
+ * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
346
398
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
347
399
  * to calculate the minimum height of a binary tree. It can have two possible values:
348
400
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -393,35 +445,51 @@ class BinaryTree {
393
445
  }
394
446
  }
395
447
  /**
448
+ * Time Complexity: O(n)
449
+ * Space Complexity: O(log n)
450
+ */
451
+ /**
452
+ * Time Complexity: O(n)
453
+ * Space Complexity: O(log n)
454
+ *
396
455
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
397
456
  * 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.
457
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
458
+ * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
459
+ * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
460
+ * @returns a boolean value.
401
461
  */
402
462
  isPerfectlyBalanced(beginRoot = this.root) {
403
463
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
404
464
  }
405
465
  /**
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.
466
+ * Time Complexity: O(n)
467
+ * Space Complexity: O(log n).
468
+ */
469
+ /**
470
+ * Time Complexity: O(n)
471
+ * Space Complexity: O(log n).
472
+ *
473
+ * The function `getNodes` retrieves nodes from a binary tree based on a given identifier and
474
+ * callback function.
475
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
476
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
477
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to search for a
478
+ * specific value.
479
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as
480
+ * input and returns a value of type `C`. It is used to determine if a node matches the given
481
+ * identifier. If no callback is provided, the `_defaultOneParamCallback` function is used as the
482
+ * default
483
+ * @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
484
+ * matches the identifier. If set to true, the function will stop iterating once it finds a matching
485
+ * node and return that node. If set to false (default), the function will continue iterating and
486
+ * return all nodes that match the identifier.
487
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
488
+ * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
489
+ * it is `null` or `undefined`, an empty array will be returned.
422
490
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
423
491
  * traverse the binary tree. It can have two possible values:
424
- * @returns The function `getNodes` returns an array of nodes (`N[]`).
492
+ * @returns an array of nodes of type `N`.
425
493
  */
426
494
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
427
495
  if (!beginRoot)
@@ -464,20 +532,27 @@ class BinaryTree {
464
532
  return ans;
465
533
  }
466
534
  /**
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:
535
+ * Time Complexity: O(n)
536
+ * Space Complexity: O(log n).
537
+ */
538
+ /**
539
+ * Time Complexity: O(n)
540
+ *
541
+ * The function checks if a Binary Tree Node with a specific identifier exists in the tree.
542
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
543
+ * that you want to search for in the binary tree. It can be of any type that is returned by the
544
+ * callback function `C`. It can also be `null` or `undefined` if you don't want to specify a
545
+ * specific identifier.
546
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
547
+ * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
548
+ * function should return a boolean value indicating whether the node should be included in the
549
+ * result or not.
550
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
551
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
552
+ * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
553
+ * @param iterationType - The `iterationType` parameter is a variable that determines the type of
554
+ * iteration to be performed on the binary tree. It is used to specify whether the iteration should
555
+ * be performed in a pre-order, in-order, or post-order manner.
481
556
  * @returns a boolean value.
482
557
  */
483
558
  has(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
@@ -486,19 +561,29 @@ class BinaryTree {
486
561
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
487
562
  }
488
563
  /**
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.
564
+ * Time Complexity: O(n)
565
+ * Space Complexity: O(log n)
566
+ */
567
+ /**
568
+ * Time Complexity: O(n)
569
+ * Space Complexity: O(log n)
570
+ *
571
+ * The function `getNode` returns the first node that matches the given identifier and callback
572
+ * function.
573
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
574
+ * used to identify the node you want to retrieve. It can be of any type that is returned by the
575
+ * callback function `C`. It can also be `null` or `undefined` if you don't have a specific
576
+ * identifier.
577
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
578
+ * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
579
+ * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
580
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
581
+ * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
582
+ * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
583
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
584
+ * be performed when searching for nodes in the binary tree. It determines the order in which the
585
+ * nodes are visited during the search.
586
+ * @returns a value of type `N | null | undefined`.
502
587
  */
503
588
  getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
504
589
  var _a;
@@ -507,6 +592,13 @@ class BinaryTree {
507
592
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
508
593
  }
509
594
  /**
595
+ * Time Complexity: O(n)
596
+ * Space Complexity: O(log n)
597
+ */
598
+ /**
599
+ * Time Complexity: O(n)
600
+ * Space Complexity: O(log n)
601
+ *
510
602
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
511
603
  * recursive or iterative iteration.
512
604
  * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
@@ -561,19 +653,30 @@ class BinaryTree {
561
653
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
562
654
  }
563
655
  /**
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.
656
+ * Time Complexity: O(n)
657
+ * Space Complexity: O(log n)
658
+ */
659
+ /**
660
+ * Time Complexity: O(n)
661
+ * Space Complexity: O(log n)
662
+ *
663
+ * The function `get` retrieves the value of a node in a binary tree based on the provided identifier
664
+ * and callback function.
665
+ * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
666
+ * used to identify the node in the binary tree. It can be of any type that is the return type of the
667
+ * callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
668
+ * @param {C} callback - The `callback` parameter is a function that will be called with each node in
669
+ * the binary tree. It is used to determine whether a node matches the given identifier. The callback
670
+ * function should return a value that can be compared to the identifier to determine if it is a
671
+ * match.
672
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
673
+ * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
674
+ * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
675
+ * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
676
+ * be performed when searching for a node in the binary tree. It is an optional parameter with a
677
+ * default value specified by `this.iterationType`.
678
+ * @returns The value of the node with the given identifier is being returned. If the node is not
679
+ * found, `undefined` is returned.
577
680
  */
578
681
  get(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
579
682
  var _a, _b;
@@ -596,14 +699,22 @@ class BinaryTree {
596
699
  return this.size === 0;
597
700
  }
598
701
  /**
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.
702
+ * Time Complexity: O(log n)
703
+ * Space Complexity: O(log n)
704
+ */
705
+ /**
706
+ * Time Complexity: O(log n)
707
+ * Space Complexity: O(log n)
708
+ *
709
+ * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
710
+ * structure, with the option to reverse the order of the nodes.
711
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
712
+ * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
713
+ * `null`, or `undefined`.
603
714
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
604
715
  * 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[]`.
716
+ * reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
717
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
607
718
  */
608
719
  getPathToRoot(beginRoot, isReverse = true) {
609
720
  // TODO to support get path through passing key
@@ -621,15 +732,22 @@ class BinaryTree {
621
732
  return isReverse ? result.reverse() : result;
622
733
  }
623
734
  /**
624
- * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
625
- * iterative traversal.
735
+ * Time Complexity: O(log n)
736
+ * Space Complexity: O(1)
737
+ */
738
+ /**
739
+ * Time Complexity: O(log n)
740
+ * Space Complexity: O(1)
741
+ *
742
+ * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
743
+ * iteratively.
626
744
  * @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.
745
+ * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
746
+ * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
629
747
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
630
748
  * 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`.
749
+ * @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
750
+ * is no leftmost node, it returns `null` or `undefined` depending on the input.
633
751
  */
634
752
  getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
635
753
  beginRoot = this.ensureNotKey(beginRoot);
@@ -654,15 +772,23 @@ class BinaryTree {
654
772
  }
655
773
  }
656
774
  /**
775
+ * Time Complexity: O(log n)
776
+ * Space Complexity: O(1)
777
+ */
778
+ /**
779
+ * Time Complexity: O(log n)
780
+ * Space Complexity: O(1)
781
+ *
657
782
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
658
783
  * 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`.
784
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
785
+ * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
786
+ * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
787
+ * current object.
788
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
789
+ * type of iteration to use when finding the rightmost node. It can have one of two values:
790
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
791
+ * is no rightmost node, it returns `null` or `undefined`, depending on the input.
666
792
  */
667
793
  getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
668
794
  // TODO support get right most by passing key in
@@ -688,13 +814,20 @@ class BinaryTree {
688
814
  }
689
815
  }
690
816
  /**
817
+ * Time Complexity: O(n)
818
+ * Space Complexity: O(1)
819
+ */
820
+ /**
821
+ * Time Complexity: O(n)
822
+ * Space Complexity: O(1)
823
+ *
691
824
  * 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.
825
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
826
+ * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
694
827
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
695
828
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
696
829
  * possible values:
697
- * @returns The function `isSubtreeBST` returns a boolean value.
830
+ * @returns a boolean value.
698
831
  */
699
832
  isSubtreeBST(beginRoot, iterationType = this.iterationType) {
700
833
  // TODO there is a bug
@@ -729,11 +862,18 @@ class BinaryTree {
729
862
  }
730
863
  }
731
864
  /**
865
+ * Time Complexity: O(n)
866
+ * Space Complexity: O(1)
867
+ */
868
+ /**
869
+ * Time Complexity: O(n)
870
+ * Space Complexity: O(1)
871
+ *
732
872
  * The function checks if a binary tree is a binary search tree.
733
873
  * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
734
874
  * 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
875
+ * parameter with a default value of "this.iterationType". The value of "this.iterationType" is
876
+ * expected to be
737
877
  * @returns a boolean value.
738
878
  */
739
879
  isBST(iterationType = this.iterationType) {
@@ -742,19 +882,30 @@ class BinaryTree {
742
882
  return this.isSubtreeBST(this.root, iterationType);
743
883
  }
744
884
  /**
885
+ * Time complexity: O(n)
886
+ * Space complexity: O(log n)
887
+ */
888
+ /**
889
+ * Time complexity: O(n)
890
+ * Space complexity: O(log n)
891
+ *
745
892
  * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
746
893
  * 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.
894
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
895
+ * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
896
+ * returns a value of any type.
897
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
898
+ * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
899
+ * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
900
+ * the default value.
754
901
  * @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>>`.
902
+ * performed on the subtree. It can have two possible values:
903
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
904
+ * whether or not to include null values in the traversal. If `includeNull` is set to `true`, the
905
+ * traversal will include null values, otherwise it will skip them.
906
+ * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
907
+ * the `callback` function on each node in the subtree. The type of the array elements is determined
908
+ * by the return type of the `callback` function.
758
909
  */
759
910
  subTreeTraverse(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
760
911
  beginRoot = this.ensureNotKey(beginRoot);
@@ -831,20 +982,31 @@ class BinaryTree {
831
982
  return typeof potentialKey === 'number';
832
983
  }
833
984
  /**
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.
985
+ * Time complexity: O(n)
986
+ * Space complexity: O(n)
987
+ */
988
+ /**
989
+ * Time complexity: O(n)
990
+ * Space complexity: O(n)
991
+ *
992
+ * The `dfs` function performs a depth-first search traversal on a binary tree or graph, based on the
993
+ * specified pattern and iteration type, and returns an array of values obtained from applying a
994
+ * callback function to each visited node.
995
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
996
+ * the tree during the depth-first search. It takes a single parameter, which can be of type `N`,
997
+ * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
839
998
  * @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.
999
+ * nodes are traversed during the depth-first search. It can have one of the following values:
1000
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1001
+ * for the depth-first search traversal. It can be specified as a key, a node object, or
1002
+ * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
844
1003
  * @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.
1004
+ * iteration to use when traversing the tree. It can have one of the following values:
1005
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1006
+ * whether null or undefined nodes should be included in the traversal. If `includeNull` is set to
1007
+ * `true`, null or undefined nodes will be included in the traversal. If `includeNull` is set to
1008
+ * `false`, null or undefined
1009
+ * @returns an array of values that are the return values of the callback function.
848
1010
  */
849
1011
  dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE, includeNull = false) {
850
1012
  beginRoot = this.ensureNotKey(beginRoot);
@@ -953,18 +1115,29 @@ class BinaryTree {
953
1115
  return ans;
954
1116
  }
955
1117
  /**
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>>[]`.
1118
+ * Time complexity: O(n)
1119
+ * Space complexity: O(n)
1120
+ */
1121
+ /**
1122
+ * Time complexity: O(n)
1123
+ * Space complexity: O(n)
1124
+ *
1125
+ * The `bfs` function performs a breadth-first search traversal on a binary tree, executing a
1126
+ * callback function on each node.
1127
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1128
+ * the breadth-first search traversal. It takes a single parameter, which is the current node being
1129
+ * visited, and returns a value of any type.
1130
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1131
+ * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1132
+ * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1133
+ * the class is used as
1134
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1135
+ * performed during the breadth-first search (BFS). It can have two possible values:
1136
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean flag that determines whether
1137
+ * or not to include null values in the breadth-first search traversal. If `includeNull` is set to
1138
+ * `true`, null values will be included in the traversal, otherwise they will be skipped.
1139
+ * @returns an array of values that are the result of invoking the callback function on each node in
1140
+ * the breadth-first traversal of a binary tree.
968
1141
  */
969
1142
  bfs(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
970
1143
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1019,20 +1192,29 @@ class BinaryTree {
1019
1192
  return ans;
1020
1193
  }
1021
1194
  /**
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.
1195
+ * Time complexity: O(n)
1196
+ * Space complexity: O(n)
1197
+ */
1198
+ /**
1199
+ * Time complexity: O(n)
1200
+ * Space complexity: O(n)
1201
+ *
1202
+ * The `listLevels` function returns an array of arrays, where each inner array represents a level in
1203
+ * a binary tree and contains the values returned by a callback function applied to the nodes at that
1204
+ * level.
1205
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1206
+ * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1207
+ * returns a value of any type.
1208
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1209
+ * starting node for traversing the tree. It can be either a node object (`N`), a key value
1210
+ * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1211
+ * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1212
+ * performed on the tree. It can have two possible values:
1213
+ * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
1214
+ * whether or not to include null values in the resulting levels. If `includeNull` is set to `true`,
1215
+ * null values will be included in the levels. If `includeNull` is set to `false`, null values will
1216
+ * be excluded
1217
+ * @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
1036
1218
  */
1037
1219
  listLevels(callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
1038
1220
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1084,9 +1266,10 @@ class BinaryTree {
1084
1266
  return levelsNodes;
1085
1267
  }
1086
1268
  /**
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`.
1269
+ * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1270
+ * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1271
+ * `null`, or `undefined`.
1272
+ * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1090
1273
  */
1091
1274
  getPredecessor(node) {
1092
1275
  node = this.ensureNotKey(node);
@@ -1106,11 +1289,10 @@ class BinaryTree {
1106
1289
  }
1107
1290
  }
1108
1291
  /**
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`.
1292
+ * The function `getSuccessor` returns the next node in a binary tree given a current node.
1293
+ * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1294
+ * @returns the successor of the given node or key. The successor is the node that comes immediately
1295
+ * after the given node in the inorder traversal of the binary tree.
1114
1296
  */
1115
1297
  getSuccessor(x) {
1116
1298
  x = this.ensureNotKey(x);
@@ -1127,18 +1309,26 @@ class BinaryTree {
1127
1309
  return y;
1128
1310
  }
1129
1311
  /**
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`.
1312
+ * Time complexity: O(n)
1313
+ * Space complexity: O(1)
1314
+ */
1315
+ /**
1316
+ * Time complexity: O(n)
1317
+ * Space complexity: O(1)
1318
+ * The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
1319
+ * algorithm.
1320
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1321
+ * the tree. It takes a single parameter of type `N` (the type of the nodes in the tree) and returns
1322
+ * a value of any type.
1135
1323
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1136
1324
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1137
1325
  * 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.
1326
+ * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1327
+ * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1328
+ * the root of the tree. If no value is provided, the default value is the root of the tree.
1329
+ * @returns The function `morris` returns an array of values that are the result of invoking the
1330
+ * `callback` function on each node in the binary tree. The type of the array elements is determined
1331
+ * by the return type of the `callback` function.
1142
1332
  */
1143
1333
  morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
1144
1334
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1225,7 +1415,6 @@ class BinaryTree {
1225
1415
  }
1226
1416
  return ans;
1227
1417
  }
1228
- // --- start additional methods ---
1229
1418
  /**
1230
1419
  * The above function is an iterator for a binary tree that can be used to traverse the tree in
1231
1420
  * either an iterative or recursive manner.
@@ -1256,12 +1445,10 @@ class BinaryTree {
1256
1445
  }
1257
1446
  else {
1258
1447
  if (node.left) {
1259
- // @ts-ignore
1260
1448
  yield* this[Symbol.iterator](node.left);
1261
1449
  }
1262
1450
  yield node.key;
1263
1451
  if (node.right) {
1264
- // @ts-ignore
1265
1452
  yield* this[Symbol.iterator](node.right);
1266
1453
  }
1267
1454
  }
@@ -1341,9 +1528,9 @@ class BinaryTree {
1341
1528
  }
1342
1529
  /**
1343
1530
  * 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
1531
+ * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
1532
+ * undefined`. It represents the root node of a binary tree. The root node can have one of the
1533
+ * following types:
1347
1534
  */
1348
1535
  print(beginRoot = this.root) {
1349
1536
  beginRoot = this.ensureNotKey(beginRoot);
@@ -1356,17 +1543,17 @@ class BinaryTree {
1356
1543
  }
1357
1544
  };
1358
1545
  const _displayAux = (node) => {
1359
- if (node === undefined || node === null) {
1546
+ if (!this.isRealNode(node)) {
1360
1547
  return [[], 0, 0, 0];
1361
1548
  }
1362
- if (node && node.right === undefined && node.left === undefined) {
1549
+ if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
1363
1550
  const line = `${node.key}`;
1364
1551
  const width = line.length;
1365
1552
  const height = 1;
1366
1553
  const middle = Math.floor(width / 2);
1367
1554
  return [[line], width, height, middle];
1368
1555
  }
1369
- if (node && node.right === undefined) {
1556
+ if (this.isRealNode(node) && !this.isRealNode(node.right)) {
1370
1557
  const [lines, n, p, x] = _displayAux(node.left);
1371
1558
  const s = `${node.key}`;
1372
1559
  const u = s.length;
@@ -1375,7 +1562,7 @@ class BinaryTree {
1375
1562
  const shifted_lines = lines.map(line => line + ' '.repeat(u));
1376
1563
  return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
1377
1564
  }
1378
- if (node && node.left === undefined) {
1565
+ if (this.isRealNode(node) && !this.isRealNode(node.left)) {
1379
1566
  const [lines, n, p, u] = _displayAux(node.right);
1380
1567
  const s = `${node.key}`;
1381
1568
  const x = s.length;