min-heap-typed 1.39.2 → 1.39.4

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 (45) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
  2. package/dist/data-structures/binary-tree/avl-tree.js +8 -6
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +50 -117
  4. package/dist/data-structures/binary-tree/binary-tree.js +43 -51
  5. package/dist/data-structures/binary-tree/bst.d.ts +23 -23
  6. package/dist/data-structures/binary-tree/bst.js +18 -18
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +15 -15
  9. package/dist/data-structures/binary-tree/tree-multiset.js +9 -9
  10. package/dist/interfaces/binary-tree.d.ts +4 -4
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  13. package/dist/types/helpers.d.ts +1 -1
  14. package/package.json +2 -2
  15. package/src/data-structures/binary-tree/avl-tree.ts +16 -14
  16. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  17. package/src/data-structures/binary-tree/binary-tree.ts +145 -84
  18. package/src/data-structures/binary-tree/bst.ts +37 -38
  19. package/src/data-structures/binary-tree/rb-tree.ts +7 -6
  20. package/src/data-structures/binary-tree/tree-multiset.ts +18 -17
  21. package/src/data-structures/graph/abstract-graph.ts +11 -10
  22. package/src/data-structures/graph/directed-graph.ts +2 -1
  23. package/src/data-structures/graph/undirected-graph.ts +5 -4
  24. package/src/data-structures/hash/hash-map.ts +1 -1
  25. package/src/data-structures/hash/tree-map.ts +1 -2
  26. package/src/data-structures/hash/tree-set.ts +1 -2
  27. package/src/data-structures/heap/heap.ts +2 -2
  28. package/src/data-structures/heap/max-heap.ts +1 -1
  29. package/src/data-structures/heap/min-heap.ts +1 -1
  30. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  31. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  32. package/src/data-structures/matrix/matrix.ts +1 -1
  33. package/src/data-structures/matrix/vector2d.ts +1 -2
  34. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  35. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  36. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  37. package/src/data-structures/queue/deque.ts +4 -5
  38. package/src/data-structures/queue/queue.ts +1 -1
  39. package/src/interfaces/binary-tree.ts +4 -4
  40. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  41. package/src/types/data-structures/binary-tree/bst.ts +2 -2
  42. package/src/types/data-structures/matrix/navigator.ts +1 -1
  43. package/src/types/helpers.ts +1 -1
  44. package/src/types/utils/utils.ts +1 -1
  45. package/src/types/utils/validate-type.ts +2 -2
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback} from '../../types';
9
+ import type {BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey} from '../../types';
10
10
  import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
11
11
  import {IBinaryTree} from '../../interfaces';
12
12
  import {trampoline} from '../../utils';
@@ -21,7 +21,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
21
21
  /**
22
22
  * The key associated with the node.
23
23
  */
24
- key: BinaryTreeNodeKey;
24
+ key: BTNKey;
25
25
 
26
26
  /**
27
27
  * The value stored in the node.
@@ -35,10 +35,10 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
35
35
 
36
36
  /**
37
37
  * Creates a new instance of BinaryTreeNode.
38
- * @param {BinaryTreeNodeKey} key - The key associated with the node.
38
+ * @param {BTNKey} key - The key associated with the node.
39
39
  * @param {V} val - The value stored in the node.
40
40
  */
41
- constructor(key: BinaryTreeNodeKey, val?: V) {
41
+ constructor(key: BTNKey, val?: V) {
42
42
  this.key = key;
43
43
  this.val = val;
44
44
  }
@@ -108,7 +108,8 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
108
108
  * @template N - The type of the binary tree's nodes.
109
109
  */
110
110
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
111
- implements IBinaryTree<V, N> {
111
+ implements IBinaryTree<V, N>
112
+ {
112
113
  /**
113
114
  * Creates a new instance of BinaryTree.
114
115
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -157,11 +158,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
157
158
 
158
159
  /**
159
160
  * Creates a new instance of BinaryTreeNode with the given key and value.
160
- * @param {BinaryTreeNodeKey} key - The key for the new node.
161
+ * @param {BTNKey} key - The key for the new node.
161
162
  * @param {V} val - The value for the new node.
162
163
  * @returns {N} - The newly created BinaryTreeNode.
163
164
  */
164
- createNode(key: BinaryTreeNodeKey, val?: V): N {
165
+ createNode(key: BTNKey, val?: V): N {
165
166
  return new BinaryTreeNode<V, N>(key, val) as N;
166
167
  }
167
168
 
@@ -183,11 +184,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
183
184
 
184
185
  /**
185
186
  * Add a node with the given key and value to the binary tree.
186
- * @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
187
+ * @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
187
188
  * @param {V} val - The value for the new node (optional).
188
189
  * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
189
190
  */
190
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
191
+ add(keyOrNode: BTNKey | N | null, val?: V): N | null | undefined {
191
192
  const _bfs = (root: N, newNode: N | null): N | undefined | null => {
192
193
  const queue = new Queue<N | null>([root]);
193
194
  while (queue.size > 0) {
@@ -216,7 +217,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
216
217
  }
217
218
 
218
219
  const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined;
219
- const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined;
220
+ const existNode = key !== undefined ? this.get(key, (node: N) => node.key) : undefined;
220
221
 
221
222
  if (this.root) {
222
223
  if (existNode) {
@@ -240,14 +241,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
240
241
  /**
241
242
  * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
242
243
  * values, and adds them to the binary tree.
243
- * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
244
+ * @param {(BTNKey | null)[] | (N | null)[]} keysOrNodes - An array of BTNKey or BinaryTreeNode
244
245
  * objects, or null values.
245
246
  * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
246
247
  * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
247
248
  * the value of the nodes will be `undefined`.
248
249
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
249
250
  */
250
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] {
251
+ addMany(keysOrNodes: (BTNKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] {
251
252
  // TODO not sure addMany not be run multi times
252
253
  return keysOrNodes.map((keyOrNode, i) => {
253
254
  if (keyOrNode instanceof BinaryTreeNode) {
@@ -265,35 +266,41 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
265
266
 
266
267
  /**
267
268
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
268
- * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
269
- * `BinaryTreeNodeKey` or `N` values.
269
+ * @param {(BTNKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
270
+ * `BTNKey` or `N` values.
270
271
  * @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
271
272
  * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
272
273
  * array. Each value in the `data` array will be assigned to the
273
274
  * @returns The method is returning a boolean value.
274
275
  */
275
- refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean {
276
+ refill(keysOrNodes: (BTNKey | null)[] | (N | null)[], data?: Array<V>): boolean {
276
277
  this.clear();
277
278
  return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
278
279
  }
279
280
 
281
+ delete<C extends BTNCallback<N, BTNKey>>(identifier: BTNKey, callback?: C): BinaryTreeDeletedResult<N>[];
282
+
283
+ delete<C extends BTNCallback<N, N>>(identifier: N | null, callback?: C): BinaryTreeDeletedResult<N>[];
284
+
285
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeletedResult<N>[];
286
+
280
287
  /**
281
288
  * The `delete` function removes a node from a binary search tree and returns the deleted node along
282
289
  * with the parent node that needs to be balanced.
283
- * a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
290
+ * a key (`BTNKey`). If it is a key, the function will find the corresponding node in the
284
291
  * binary tree.
285
292
  * @returns an array of `BinaryTreeDeletedResult<N>` objects.
286
293
  * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
287
- * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
294
+ * `BTNKey` or a generic type `N`. It represents the property of the node that we are
288
295
  * searching for. It can be a specific key value or any other property of the node.
289
296
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
290
297
  * value. This value is compared with the `identifier` parameter to determine if the node should be
291
298
  * included in the result. The `callback` parameter has a default value of
292
- * `this._defaultCallbackByKey`, which
299
+ * `((node: N) => node.key)`, which
293
300
  */
294
- delete<C extends OneParamCallback<N>>(
301
+ delete<C extends BTNCallback<N>>(
295
302
  identifier: ReturnType<C> | null,
296
- callback: C = this._defaultCallbackByKey as C
303
+ callback: C = ((node: N) => node.key) as C
297
304
  ): BinaryTreeDeletedResult<N>[] {
298
305
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
299
306
  if (!this.root) return bstDeletedResult;
@@ -341,16 +348,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
341
348
  /**
342
349
  * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
343
350
  * specified root node.
344
- * @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node
351
+ * @param {BTNKey | N | null} distNode - The `distNode` parameter represents the node
345
352
  * whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
346
- * of the node (`BinaryTreeNodeKey`), or `null`.
347
- * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
353
+ * of the node (`BTNKey`), or `null`.
354
+ * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
348
355
  * starting node from which we want to calculate the depth. It can be either a node object or the key
349
356
  * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
350
357
  * node of the binary tree.
351
358
  * @returns the depth of the `distNode` relative to the `beginRoot`.
352
359
  */
353
- getDepth(distNode: BinaryTreeNodeKey | N | null, beginRoot: BinaryTreeNodeKey | N | null = this.root): number {
360
+ getDepth(distNode: BTNKey | N | null, beginRoot: BTNKey | N | null = this.root): number {
354
361
  if (typeof distNode === 'number') distNode = this.get(distNode);
355
362
  if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
356
363
  let depth = 0;
@@ -367,16 +374,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
367
374
  /**
368
375
  * The `getHeight` function calculates the maximum height of a binary tree using either recursive or
369
376
  * iterative approach.
370
- * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the
377
+ * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter represents the
371
378
  * starting node from which the height of the binary tree is calculated. It can be either a node
372
- * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
379
+ * object (`N`), a key value of a node in the tree (`BTNKey`), or `null` if no starting
373
380
  * node is specified. If `
374
381
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
375
382
  * height of the binary tree using a recursive approach or an iterative approach. It can have two
376
383
  * possible values:
377
384
  * @returns the height of the binary tree.
378
385
  */
379
- getHeight(beginRoot: BinaryTreeNodeKey | N | null = this.root, iterationType = this.iterationType): number {
386
+ getHeight(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): number {
380
387
  if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
381
388
  if (!beginRoot) return -1;
382
389
 
@@ -394,7 +401,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
394
401
  return -1;
395
402
  }
396
403
 
397
- const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
404
+ const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
398
405
  let maxHeight = 0;
399
406
 
400
407
  while (stack.length > 0) {
@@ -478,16 +485,40 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
478
485
  return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
479
486
  }
480
487
 
488
+ getNodes<C extends BTNCallback<N, BTNKey>>(
489
+ identifier: BTNKey,
490
+ callback?: C,
491
+ onlyOne?: boolean,
492
+ beginRoot?: N | null,
493
+ iterationType?: IterationType
494
+ ): N[];
495
+
496
+ getNodes<C extends BTNCallback<N, N>>(
497
+ identifier: N | null,
498
+ callback?: C,
499
+ onlyOne?: boolean,
500
+ beginRoot?: N | null,
501
+ iterationType?: IterationType
502
+ ): N[];
503
+
504
+ getNodes<C extends BTNCallback<N>>(
505
+ identifier: ReturnType<C>,
506
+ callback: C,
507
+ onlyOne?: boolean,
508
+ beginRoot?: N | null,
509
+ iterationType?: IterationType
510
+ ): N[];
511
+
481
512
  /**
482
513
  * The function `getNodes` returns an array of nodes that match a given node property, using either
483
514
  * recursive or iterative traversal.
484
515
  * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
485
- * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
516
+ * `BTNKey` or a generic type `N`. It represents the property of the node that we are
486
517
  * searching for. It can be a specific key value or any other property of the node.
487
518
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
488
519
  * value. This value is compared with the `identifier` parameter to determine if the node should be
489
520
  * included in the result. The `callback` parameter has a default value of
490
- * `this._defaultCallbackByKey`, which
521
+ * `((node: N) => node.key)`, which
491
522
  * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
492
523
  * first node that matches the identifier. If set to true, the function will return an array with
493
524
  * only one element (or an empty array if no matching node is found). If set to false (default), the
@@ -499,9 +530,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
499
530
  * traverse the binary tree. It can have two possible values:
500
531
  * @returns The function `getNodes` returns an array of nodes (`N[]`).
501
532
  */
502
- getNodes<C extends OneParamCallback<N>>(
533
+ getNodes<C extends BTNCallback<N>>(
503
534
  identifier: ReturnType<C> | null,
504
- callback: C = this._defaultCallbackByKey as C,
535
+ callback: C = ((node: N) => node.key) as C,
505
536
  onlyOne = false,
506
537
  beginRoot: N | null = this.root,
507
538
  iterationType = this.iterationType
@@ -540,15 +571,36 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
540
571
  return ans;
541
572
  }
542
573
 
574
+ has<C extends BTNCallback<N, BTNKey>>(
575
+ identifier: BTNKey,
576
+ callback?: C,
577
+ beginRoot?: N,
578
+ iterationType?: IterationType
579
+ ): boolean;
580
+
581
+ has<C extends BTNCallback<N, N>>(
582
+ identifier: N | null,
583
+ callback?: C,
584
+ beginRoot?: N,
585
+ iterationType?: IterationType
586
+ ): boolean;
587
+
588
+ has<C extends BTNCallback<N>>(
589
+ identifier: ReturnType<C> | null,
590
+ callback: C,
591
+ beginRoot?: N,
592
+ iterationType?: IterationType
593
+ ): boolean;
594
+
543
595
  /**
544
596
  * The function checks if a binary tree has a node with a given property or key.
545
- * @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
546
- * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
597
+ * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
598
+ * the node that you want to find in the binary tree. It can be either a `BTNKey` or a
547
599
  * generic type `N`.
548
600
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
549
601
  * matches the desired criteria. It takes a node as input and returns a boolean value indicating
550
602
  * whether the node matches the criteria or not. The default callback function
551
- * `this._defaultCallbackByKey` is used if no callback function is
603
+ * `((node: N) => node.key)` is used if no callback function is
552
604
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
553
605
  * the node from which the search should begin. By default, it is set to `this.root`, which means the
554
606
  * search will start from the root node of the binary tree. However, you can provide a different node
@@ -557,9 +609,9 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
557
609
  * performed when searching for nodes in the binary tree. It can have one of the following values:
558
610
  * @returns a boolean value.
559
611
  */
560
- has<C extends OneParamCallback<N>>(
612
+ has<C extends BTNCallback<N>>(
561
613
  identifier: ReturnType<C> | null,
562
- callback: C = this._defaultCallbackByKey as C,
614
+ callback: C = ((node: N) => node.key) as C,
563
615
  beginRoot = this.root,
564
616
  iterationType = this.iterationType
565
617
  ): boolean {
@@ -568,24 +620,45 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
568
620
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
569
621
  }
570
622
 
623
+ get<C extends BTNCallback<N, BTNKey>>(
624
+ identifier: BTNKey,
625
+ callback?: C,
626
+ beginRoot?: N,
627
+ iterationType?: IterationType
628
+ ): N | null;
629
+
630
+ get<C extends BTNCallback<N, N>>(
631
+ identifier: N | null,
632
+ callback?: C,
633
+ beginRoot?: N,
634
+ iterationType?: IterationType
635
+ ): N | null;
636
+
637
+ get<C extends BTNCallback<N>>(
638
+ identifier: ReturnType<C>,
639
+ callback: C,
640
+ beginRoot?: N,
641
+ iterationType?: IterationType
642
+ ): N | null;
643
+
571
644
  /**
572
645
  * The function `get` returns the first node in a binary tree that matches the given property or key.
573
- * @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
574
- * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
646
+ * @param {BTNKey | N} identifier - The `identifier` parameter is the key or value of
647
+ * the node that you want to find in the binary tree. It can be either a `BTNKey` or `N`
575
648
  * type.
576
649
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
577
650
  * matches the desired criteria. It takes a node as input and returns a boolean value indicating
578
651
  * whether the node matches the criteria or not. The default callback function
579
- * (`this._defaultCallbackByKey`) is used if no callback function is
652
+ * (`((node: N) => node.key)`) is used if no callback function is
580
653
  * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
581
654
  * the root node from which the search should begin.
582
655
  * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
583
656
  * performed when searching for a node in the binary tree. It can have one of the following values:
584
657
  * @returns either the found node (of type N) or null if no node is found.
585
658
  */
586
- get<C extends OneParamCallback<N>>(
659
+ get<C extends BTNCallback<N>>(
587
660
  identifier: ReturnType<C> | null,
588
- callback: C = this._defaultCallbackByKey as C,
661
+ callback: C = ((node: N) => node.key) as C,
589
662
  beginRoot = this.root,
590
663
  iterationType = this.iterationType
591
664
  ): N | null {
@@ -620,15 +693,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
620
693
  /**
621
694
  * The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
622
695
  * iterative traversal.
623
- * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
696
+ * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
624
697
  * for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
625
- * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
698
+ * of a node (`BTNKey`), or `null` if the tree is empty.
626
699
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
627
700
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
628
701
  * @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
629
702
  * no leftmost node, it returns `null`.
630
703
  */
631
- getLeftMost(beginRoot: BinaryTreeNodeKey | N | null = this.root, iterationType = this.iterationType): N | null {
704
+ getLeftMost(beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType): N | null {
632
705
  if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
633
706
 
634
707
  if (!beginRoot) return beginRoot;
@@ -698,7 +771,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
698
771
  if (!beginRoot) return true;
699
772
 
700
773
  if (iterationType === IterationType.RECURSIVE) {
701
- const dfs = (cur: N | null | undefined, min: BinaryTreeNodeKey, max: BinaryTreeNodeKey): boolean => {
774
+ const dfs = (cur: N | null | undefined, min: BTNKey, max: BTNKey): boolean => {
702
775
  if (!cur) return true;
703
776
  if (cur.key <= min || cur.key >= max) return false;
704
777
  return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
@@ -743,21 +816,21 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
743
816
  * subtree traversal. It takes a single argument, which is the current node being traversed, and
744
817
  * returns a value. The return values from each callback invocation will be collected and returned as
745
818
  * an array.
746
- * @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
819
+ * @param {BTNKey | N | null} beginRoot - The `beginRoot` parameter is the starting point
747
820
  * for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
748
821
  * start from the root of the tree.
749
822
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
750
823
  * performed on the binary tree. It can have two possible values:
751
- * @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
824
+ * @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
752
825
  */
753
- subTreeTraverse<C extends OneParamCallback<N>>(
754
- callback: C = this._defaultCallbackByKey as C,
755
- beginRoot: BinaryTreeNodeKey | N | null = this.root,
826
+ subTreeTraverse<C extends BTNCallback<N>>(
827
+ callback: C = ((node: N) => node.key) as C,
828
+ beginRoot: BTNKey | N | null = this.root,
756
829
  iterationType = this.iterationType
757
830
  ): ReturnType<C>[] {
758
831
  if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
759
832
 
760
- const ans: ReturnType<OneParamCallback<N>>[] = [];
833
+ const ans: ReturnType<BTNCallback<N>>[] = [];
761
834
  if (!beginRoot) return ans;
762
835
 
763
836
  if (iterationType === IterationType.RECURSIVE) {
@@ -787,7 +860,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
787
860
  * function on each node according to a specified order pattern.
788
861
  * @param callback - The `callback` parameter is a function that will be called on each node during
789
862
  * the depth-first search traversal. It takes a node as input and returns a value. The default value
790
- * is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
863
+ * is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
791
864
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
792
865
  * nodes are visited during the depth-first search. There are three possible values for `pattern`:
793
866
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
@@ -795,16 +868,16 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
795
868
  * is `null`, an empty array will be returned.
796
869
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
797
870
  * iteration used in the depth-first search algorithm. It can have two possible values:
798
- * @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
871
+ * @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
799
872
  */
800
- dfs<C extends OneParamCallback<N>>(
801
- callback: C = this._defaultCallbackByKey as C,
873
+ dfs<C extends BTNCallback<N>>(
874
+ callback: C = ((node: N) => node.key) as C,
802
875
  pattern: DFSOrderPattern = 'in',
803
876
  beginRoot: N | null = this.root,
804
877
  iterationType: IterationType = IterationType.ITERATIVE
805
878
  ): ReturnType<C>[] {
806
879
  if (!beginRoot) return [];
807
- const ans: ReturnType<OneParamCallback<N>>[] = [];
880
+ const ans: ReturnType<BTNCallback<N>>[] = [];
808
881
  if (iterationType === IterationType.RECURSIVE) {
809
882
  const _traverse = (node: N) => {
810
883
  switch (pattern) {
@@ -831,7 +904,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
831
904
  _traverse(beginRoot);
832
905
  } else {
833
906
  // 0: visit, 1: print
834
- const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
907
+ const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
835
908
 
836
909
  while (stack.length > 0) {
837
910
  const cur = stack.pop();
@@ -873,22 +946,22 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
873
946
  * function on each node.
874
947
  * @param callback - The `callback` parameter is a function that will be called for each node in the
875
948
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
876
- * `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
949
+ * `ReturnType<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
877
950
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
878
951
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
879
952
  * will not be performed and an empty array will be returned.
880
953
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
881
954
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
882
- * @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
955
+ * @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
883
956
  */
884
- bfs<C extends OneParamCallback<N>>(
885
- callback: C = this._defaultCallbackByKey as C,
957
+ bfs<C extends BTNCallback<N>>(
958
+ callback: C = ((node: N) => node.key) as C,
886
959
  beginRoot: N | null = this.root,
887
960
  iterationType = this.iterationType
888
961
  ): ReturnType<C>[] {
889
962
  if (!beginRoot) return [];
890
963
 
891
- const ans: ReturnType<OneParamCallback<N>>[] = [];
964
+ const ans: ReturnType<BTNCallback<N>>[] = [];
892
965
 
893
966
  if (iterationType === IterationType.RECURSIVE) {
894
967
  const queue = new Queue<N>([beginRoot]);
@@ -938,8 +1011,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
938
1011
  * level in a binary tree. Each inner array contains the return type of the provided callback
939
1012
  * function `C` applied to the nodes at that level.
940
1013
  */
941
- listLevels<C extends OneParamCallback<N>>(
942
- callback: C = this._defaultCallbackByKey as C,
1014
+ listLevels<C extends BTNCallback<N>>(
1015
+ callback: C = ((node: N) => node.key) as C,
943
1016
  beginRoot: N | null = this.root,
944
1017
  iterationType = this.iterationType
945
1018
  ): ReturnType<C>[][] {
@@ -997,23 +1070,23 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
997
1070
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
998
1071
  * algorithm and returns an array of values obtained by applying a callback function to each node.
999
1072
  * @param callback - The `callback` parameter is a function that will be called on each node in the
1000
- * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
1001
- * default value for this parameter is `this._defaultCallbackByKey`.
1073
+ * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<BTNCallback<N>>`. The
1074
+ * default value for this parameter is `((node: N) => node.key)`.
1002
1075
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1003
1076
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1004
1077
  * following values:
1005
1078
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
1006
1079
  * traversal. It specifies the root node of the tree from which the traversal should begin. If
1007
1080
  * `beginRoot` is `null`, an empty array will be returned.
1008
- * @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
1081
+ * @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
1009
1082
  */
1010
- morris<C extends OneParamCallback<N>>(
1011
- callback: C = this._defaultCallbackByKey as C,
1083
+ morris<C extends BTNCallback<N>>(
1084
+ callback: C = ((node: N) => node.key) as C,
1012
1085
  pattern: DFSOrderPattern = 'in',
1013
1086
  beginRoot: N | null = this.root
1014
1087
  ): ReturnType<C>[] {
1015
1088
  if (beginRoot === null) return [];
1016
- const ans: ReturnType<OneParamCallback<N>>[] = [];
1089
+ const ans: ReturnType<BTNCallback<N>>[] = [];
1017
1090
 
1018
1091
  let cur: N | null | undefined = beginRoot;
1019
1092
  const _reverseEdge = (node: N | null | undefined) => {
@@ -1101,7 +1174,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1101
1174
  * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1102
1175
  * binary tree nodes in a specific order.
1103
1176
  */
1104
- * [Symbol.iterator](node = this.root): Generator<BinaryTreeNodeKey, void, undefined> {
1177
+ *[Symbol.iterator](node = this.root): Generator<BTNKey, void, undefined> {
1105
1178
  if (!node) {
1106
1179
  return;
1107
1180
  }
@@ -1122,7 +1195,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1122
1195
  if (current) current = current.right;
1123
1196
  }
1124
1197
  } else {
1125
-
1126
1198
  if (node.left) {
1127
1199
  yield* this[Symbol.iterator](node.left);
1128
1200
  }
@@ -1131,10 +1203,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1131
1203
  yield* this[Symbol.iterator](node.right);
1132
1204
  }
1133
1205
  }
1134
-
1135
1206
  }
1136
1207
 
1137
-
1138
1208
  /**
1139
1209
  * Swap the data of two nodes in the binary tree.
1140
1210
  * @param {N} srcNode - The source node to swap.
@@ -1156,15 +1226,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1156
1226
  return destNode;
1157
1227
  }
1158
1228
 
1159
- /**
1160
- * Time complexity is O(n)
1161
- * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
1162
- * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
1163
- * the tree's structure should be restored to its original state to maintain the tree's integrity.
1164
- * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
1165
- */
1166
- protected _defaultCallbackByKey: OneParamCallback<N, BinaryTreeNodeKey> = node => node.key;
1167
-
1168
1229
  /**
1169
1230
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
1170
1231
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to