min-heap-typed 1.48.1 → 1.48.3

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 (76) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -16
  6. package/dist/data-structures/binary-tree/avl-tree.js +7 -7
  7. package/dist/data-structures/binary-tree/binary-tree.d.ts +121 -152
  8. package/dist/data-structures/binary-tree/binary-tree.js +140 -182
  9. package/dist/data-structures/binary-tree/bst.d.ts +28 -47
  10. package/dist/data-structures/binary-tree/bst.js +54 -57
  11. package/dist/data-structures/binary-tree/rb-tree.d.ts +15 -15
  12. package/dist/data-structures/binary-tree/rb-tree.js +7 -7
  13. package/dist/data-structures/binary-tree/tree-multimap.d.ts +22 -22
  14. package/dist/data-structures/binary-tree/tree-multimap.js +11 -11
  15. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  16. package/dist/data-structures/graph/abstract-graph.js +50 -27
  17. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/data-structures/hash/hash-map.js +69 -173
  19. package/dist/data-structures/heap/heap.d.ts +50 -7
  20. package/dist/data-structures/heap/heap.js +60 -30
  21. package/dist/data-structures/index.d.ts +1 -0
  22. package/dist/data-structures/index.js +1 -0
  23. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  24. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  25. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  26. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  27. package/dist/data-structures/queue/deque.d.ts +29 -51
  28. package/dist/data-structures/queue/deque.js +36 -71
  29. package/dist/data-structures/queue/queue.d.ts +49 -48
  30. package/dist/data-structures/queue/queue.js +69 -82
  31. package/dist/data-structures/stack/stack.d.ts +43 -10
  32. package/dist/data-structures/stack/stack.js +50 -31
  33. package/dist/data-structures/trie/trie.d.ts +41 -6
  34. package/dist/data-structures/trie/trie.js +53 -32
  35. package/dist/interfaces/binary-tree.d.ts +6 -6
  36. package/dist/types/common.d.ts +11 -8
  37. package/dist/types/common.js +6 -1
  38. package/dist/types/data-structures/base/base.d.ts +5 -0
  39. package/dist/types/data-structures/base/base.js +2 -0
  40. package/dist/types/data-structures/base/index.d.ts +1 -0
  41. package/dist/types/data-structures/base/index.js +17 -0
  42. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -3
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +6 -6
  45. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
  46. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +3 -3
  47. package/dist/types/data-structures/index.d.ts +1 -0
  48. package/dist/types/data-structures/index.js +1 -0
  49. package/package.json +2 -2
  50. package/src/data-structures/base/index.ts +1 -0
  51. package/src/data-structures/base/iterable-base.ts +329 -0
  52. package/src/data-structures/binary-tree/avl-tree.ts +20 -21
  53. package/src/data-structures/binary-tree/binary-tree.ts +222 -267
  54. package/src/data-structures/binary-tree/bst.ts +86 -82
  55. package/src/data-structures/binary-tree/rb-tree.ts +25 -26
  56. package/src/data-structures/binary-tree/tree-multimap.ts +30 -35
  57. package/src/data-structures/graph/abstract-graph.ts +55 -28
  58. package/src/data-structures/hash/hash-map.ts +76 -185
  59. package/src/data-structures/heap/heap.ts +63 -36
  60. package/src/data-structures/index.ts +1 -0
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  63. package/src/data-structures/queue/deque.ts +40 -82
  64. package/src/data-structures/queue/queue.ts +72 -87
  65. package/src/data-structures/stack/stack.ts +53 -34
  66. package/src/data-structures/trie/trie.ts +58 -35
  67. package/src/interfaces/binary-tree.ts +5 -6
  68. package/src/types/common.ts +11 -8
  69. package/src/types/data-structures/base/base.ts +6 -0
  70. package/src/types/data-structures/base/index.ts +1 -0
  71. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -3
  72. package/src/types/data-structures/binary-tree/binary-tree.ts +6 -5
  73. package/src/types/data-structures/binary-tree/bst.ts +6 -6
  74. package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
  75. package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -3
  76. package/src/types/data-structures/index.ts +1 -0
@@ -11,6 +11,7 @@ exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
11
  const types_1 = require("../../types");
12
12
  const utils_1 = require("../../utils");
13
13
  const queue_1 = require("../queue");
14
+ const base_1 = require("../base");
14
15
  /**
15
16
  * Represents a node in a binary tree.
16
17
  * @template V - The type of data stored in the node.
@@ -69,7 +70,7 @@ exports.BinaryTreeNode = BinaryTreeNode;
69
70
  * 8. Full Trees: Every node has either 0 or 2 children.
70
71
  * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
71
72
  */
72
- class BinaryTree {
73
+ class BinaryTree extends base_1.IterablePairBase {
73
74
  /**
74
75
  * The constructor function initializes a binary tree object with optional elements and options.
75
76
  * @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
@@ -80,18 +81,26 @@ class BinaryTree {
80
81
  * required.
81
82
  */
82
83
  constructor(elements, options) {
84
+ super();
83
85
  this.iterationType = types_1.IterationType.ITERATIVE;
86
+ this._extractor = (key) => Number(key);
84
87
  this._defaultOneParamCallback = (node) => node.key;
85
88
  if (options) {
86
- const { iterationType } = options;
89
+ const { iterationType, extractor } = options;
87
90
  if (iterationType) {
88
91
  this.iterationType = iterationType;
89
92
  }
93
+ if (extractor) {
94
+ this._extractor = extractor;
95
+ }
90
96
  }
91
97
  this._size = 0;
92
98
  if (elements)
93
99
  this.addMany(elements);
94
100
  }
101
+ get extractor() {
102
+ return this._extractor;
103
+ }
95
104
  get root() {
96
105
  return this._root;
97
106
  }
@@ -100,7 +109,7 @@ class BinaryTree {
100
109
  }
101
110
  /**
102
111
  * Creates a new instance of BinaryTreeNode with the given key and value.
103
- * @param {BTNKey} key - The key for the new node.
112
+ * @param {K} key - The key for the new node.
104
113
  * @param {V} value - The value for the new node.
105
114
  * @returns {N} - The newly created BinaryTreeNode.
106
115
  */
@@ -119,7 +128,7 @@ class BinaryTree {
119
128
  }
120
129
  /**
121
130
  * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
122
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<V, N>`.
131
+ * @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V,N>`.
123
132
  * @returns a boolean value indicating whether the exemplar is an instance of the class N.
124
133
  */
125
134
  isNode(exemplar) {
@@ -128,7 +137,7 @@ class BinaryTree {
128
137
  /**
129
138
  * The function `exemplarToNode` converts an exemplar of a binary tree node into an actual node
130
139
  * object.
131
- * @param exemplar - BTNodeExemplar<V, N> - A generic type representing the exemplar parameter of the
140
+ * @param exemplar - BTNodeExemplar<K, V,N> - A generic type representing the exemplar parameter of the
132
141
  * function. It can be any type.
133
142
  * @returns a value of type `N` (which represents a node), or `null`, or `undefined`.
134
143
  */
@@ -154,7 +163,7 @@ class BinaryTree {
154
163
  else if (this.isNode(exemplar)) {
155
164
  node = exemplar;
156
165
  }
157
- else if (this.isNodeKey(exemplar)) {
166
+ else if (this.isNotNodeInstance(exemplar)) {
158
167
  node = this.createNode(exemplar);
159
168
  }
160
169
  else {
@@ -164,7 +173,7 @@ class BinaryTree {
164
173
  }
165
174
  /**
166
175
  * The function checks if a given value is an entry in a binary tree node.
167
- * @param kne - BTNodeExemplar<V, N> - A generic type representing a node in a binary tree. It has
176
+ * @param kne - BTNodeExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
168
177
  * two type parameters V and N, representing the value and node type respectively.
169
178
  * @returns a boolean value.
170
179
  */
@@ -232,7 +241,7 @@ class BinaryTree {
232
241
  * The function `addMany` takes in an iterable of `BTNodeExemplar` objects, adds each object to the
233
242
  * current instance, and returns an array of the inserted nodes.
234
243
  * @param nodes - The `nodes` parameter is an iterable (such as an array or a set) of
235
- * `BTNodeExemplar<V, N>` objects.
244
+ * `BTNodeExemplar<K, V,N>` objects.
236
245
  * @returns The function `addMany` returns an array of values, where each value is either of type
237
246
  * `N`, `null`, or `undefined`.
238
247
  */
@@ -332,11 +341,11 @@ class BinaryTree {
332
341
  * Space Complexity: O(1)
333
342
  *
334
343
  * The function calculates the depth of a given node in a binary tree.
335
- * @param {BTNKey | N | null | undefined} distNode - The `distNode` parameter represents the node in
336
- * the binary tree whose depth we want to find. It can be of type `BTNKey`, `N`, `null`, or
344
+ * @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
345
+ * the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
337
346
  * `undefined`.
338
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
339
- * from which we want to calculate the depth. It can be either a `BTNKey` (binary tree node key) or
347
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
348
+ * from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
340
349
  * `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
341
350
  * @returns the depth of the `distNode` relative to the `beginRoot`.
342
351
  */
@@ -363,9 +372,9 @@ class BinaryTree {
363
372
  *
364
373
  * The function `getHeight` calculates the maximum height of a binary tree using either recursive or
365
374
  * iterative traversal.
366
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
375
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
367
376
  * starting node of the binary tree from which we want to calculate the height. It can be of type
368
- * `BTNKey`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
377
+ * `K`, `N`, `null`, or `undefined`. If not provided, it defaults to `this.root`.
369
378
  * @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
370
379
  * height of the tree using a recursive approach or an iterative approach. It can have two possible
371
380
  * values:
@@ -410,9 +419,9 @@ class BinaryTree {
410
419
  *
411
420
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
412
421
  * recursive or iterative approach.
413
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
422
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
414
423
  * starting node of the binary tree from which we want to calculate the minimum height. It can be of
415
- * type `BTNKey`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
424
+ * type `K`, `N`, `null`, or `undefined`. If no value is provided, it defaults to `this.root`.
416
425
  * @param iterationType - The `iterationType` parameter is used to determine the method of iteration
417
426
  * to calculate the minimum height of a binary tree. It can have two possible values:
418
427
  * @returns The function `getMinHeight` returns the minimum height of a binary tree.
@@ -473,8 +482,8 @@ class BinaryTree {
473
482
  *
474
483
  * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
475
484
  * height of the tree.
476
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
477
- * for calculating the height and minimum height of a binary tree. It can be either a `BTNKey` (a key
485
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
486
+ * for calculating the height and minimum height of a binary tree. It can be either a `K` (a key
478
487
  * value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
479
488
  * @returns a boolean value.
480
489
  */
@@ -499,7 +508,7 @@ class BinaryTree {
499
508
  * matches the identifier. If set to true, the function will stop iterating once it finds a matching
500
509
  * node and return that node. If set to false (default), the function will continue iterating and
501
510
  * return all nodes that match the identifier.
502
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
511
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
503
512
  * starting node for the traversal. It can be either a key, a node object, or `null`/`undefined`. If
504
513
  * it is `null` or `undefined`, an empty array will be returned.
505
514
  * @param iterationType - The `iterationType` parameter determines the type of iteration used to
@@ -556,8 +565,8 @@ class BinaryTree {
556
565
  * the binary tree. It is used to filter the nodes based on certain conditions. The `callback`
557
566
  * function should return a boolean value indicating whether the node should be included in the
558
567
  * result or not.
559
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
560
- * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
568
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
569
+ * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
561
570
  * node in the binary tree), a node object (`N`), or `null`/`undefined` to start the search from
562
571
  * @param iterationType - The `iterationType` parameter is a variable that determines the type of
563
572
  * iteration to be performed on the binary tree. It is used to specify whether the iteration should
@@ -582,7 +591,7 @@ class BinaryTree {
582
591
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
583
592
  * the binary tree. It is used to determine if a node matches the given identifier. The `callback`
584
593
  * function should take a single parameter of type `N` (the type of the nodes in the binary tree) and
585
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
594
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
586
595
  * for searching the binary tree. It can be either a key value, a node object, or `null`/`undefined`.
587
596
  * If `null` or `undefined` is passed, the search will start from the root of the binary tree.
588
597
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
@@ -606,7 +615,7 @@ class BinaryTree {
606
615
  *
607
616
  * The function `getNodeByKey` searches for a node in a binary tree by its key, using either
608
617
  * recursive or iterative iteration.
609
- * @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
618
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
610
619
  * It is used to find the node with the matching key value.
611
620
  * @param iterationType - The `iterationType` parameter is used to determine whether the search for
612
621
  * the node with the given key should be performed iteratively or recursively. It has two possible
@@ -650,7 +659,7 @@ class BinaryTree {
650
659
  /**
651
660
  * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
652
661
  * key, otherwise it returns the key itself.
653
- * @param {BTNKey | N | null | undefined} key - The `key` parameter can be of type `BTNKey`, `N`,
662
+ * @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
654
663
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
655
664
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
656
665
  * type of iteration to be used when searching for a node by key. It has a default value of
@@ -659,7 +668,7 @@ class BinaryTree {
659
668
  * itself if it is not a valid node key.
660
669
  */
661
670
  ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
662
- return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
671
+ return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
663
672
  }
664
673
  /**
665
674
  * Time Complexity: O(n)
@@ -674,8 +683,8 @@ class BinaryTree {
674
683
  * the binary tree. It is used to determine whether a node matches the given identifier. The callback
675
684
  * function should return a value that can be compared to the identifier to determine if it is a
676
685
  * match.
677
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
678
- * for the search in the binary tree. It can be specified as a `BTNKey` (a unique identifier for a
686
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
687
+ * for the search in the binary tree. It can be specified as a `K` (a unique identifier for a
679
688
  * node), a node object of type `N`, or `null`/`undefined` to start the search from the root of
680
689
  * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
681
690
  * be performed when searching for a node in the binary tree. It is an optional parameter with a
@@ -713,8 +722,8 @@ class BinaryTree {
713
722
  *
714
723
  * The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
715
724
  * structure, with the option to reverse the order of the nodes.
716
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
717
- * starting node from which you want to find the path to the root. It can be of type `BTNKey`, `N`,
725
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
726
+ * starting node from which you want to find the path to the root. It can be of type `K`, `N`,
718
727
  * `null`, or `undefined`.
719
728
  * @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
720
729
  * resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
@@ -746,8 +755,8 @@ class BinaryTree {
746
755
  *
747
756
  * The function `getLeftMost` returns the leftmost node in a binary tree, either recursively or
748
757
  * iteratively.
749
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
750
- * for finding the leftmost node in a binary tree. It can be either a `BTNKey` (a key value), `N` (a
758
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting point
759
+ * for finding the leftmost node in a binary tree. It can be either a `K` (a key value), `N` (a
751
760
  * node), `null`, or `undefined`. If not provided, it defaults to `this.root`,
752
761
  * @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
753
762
  * be performed when finding the leftmost node in a binary tree. It can have two possible values:
@@ -786,8 +795,8 @@ class BinaryTree {
786
795
  *
787
796
  * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
788
797
  * iteratively.
789
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
790
- * starting node from which we want to find the rightmost node. It can be of type `BTNKey`, `N`,
798
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
799
+ * starting node from which we want to find the rightmost node. It can be of type `K`, `N`,
791
800
  * `null`, or `undefined`. If not provided, it defaults to `this.root`, which is a property of the
792
801
  * current object.
793
802
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
@@ -827,7 +836,7 @@ class BinaryTree {
827
836
  * Space Complexity: O(1)
828
837
  *
829
838
  * The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
830
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
839
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the root
831
840
  * node of the binary search tree (BST) that you want to check if it is a subtree of another BST.
832
841
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
833
842
  * type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
@@ -843,9 +852,10 @@ class BinaryTree {
843
852
  const dfs = (cur, min, max) => {
844
853
  if (!cur)
845
854
  return true;
846
- if (cur.key <= min || cur.key >= max)
855
+ const numKey = this.extractor(cur.key);
856
+ if (numKey <= min || numKey >= max)
847
857
  return false;
848
- return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
858
+ return dfs(cur.left, min, numKey) && dfs(cur.right, numKey, max);
849
859
  };
850
860
  return dfs(beginRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
851
861
  }
@@ -858,9 +868,10 @@ class BinaryTree {
858
868
  curr = curr.left;
859
869
  }
860
870
  curr = stack.pop();
861
- if (!curr || prev >= curr.key)
871
+ const numKey = this.extractor(curr.key);
872
+ if (!curr || prev >= numKey)
862
873
  return false;
863
- prev = curr.key;
874
+ prev = numKey;
864
875
  curr = curr.right;
865
876
  }
866
877
  return true;
@@ -895,8 +906,8 @@ class BinaryTree {
895
906
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
896
907
  * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
897
908
  * returns a value of any type.
898
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
899
- * starting node or key from which the subtree traversal should begin. It can be of type `BTNKey`,
909
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
910
+ * starting node or key from which the subtree traversal should begin. It can be of type `K`,
900
911
  * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
901
912
  * the default value.
902
913
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
@@ -978,13 +989,13 @@ class BinaryTree {
978
989
  return this.isRealNode(node) || node === null;
979
990
  }
980
991
  /**
981
- * The function "isNodeKey" checks if a potential key is a number.
992
+ * The function "isNotNodeInstance" checks if a potential key is a number.
982
993
  * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
983
994
  * data type.
984
995
  * @returns a boolean value indicating whether the potentialKey is of type number or not.
985
996
  */
986
- isNodeKey(potentialKey) {
987
- return typeof potentialKey === 'number';
997
+ isNotNodeInstance(potentialKey) {
998
+ return !(potentialKey instanceof BinaryTreeNode);
988
999
  }
989
1000
  /**
990
1001
  * Time complexity: O(n)
@@ -998,7 +1009,7 @@ class BinaryTree {
998
1009
  * `null`, or `undefined`, and returns a value of any type. The default value for this parameter is
999
1010
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
1000
1011
  * nodes are traversed during the depth-first search. It can have one of the following values:
1001
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1012
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1002
1013
  * for the depth-first search traversal. It can be specified as a key, a node object, or
1003
1014
  * `null`/`undefined`. If not provided, the `beginRoot` will default to the root node of the tree.
1004
1015
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@@ -1124,7 +1135,7 @@ class BinaryTree {
1124
1135
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1125
1136
  * the breadth-first search traversal. It takes a single parameter, which is the current node being
1126
1137
  * visited, and returns a value of any type.
1127
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1138
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1128
1139
  * starting node for the breadth-first search traversal. It can be specified as a key, a node object,
1129
1140
  * or `null`/`undefined` to indicate the root of the tree. If not provided, the `root` property of
1130
1141
  * the class is used as
@@ -1198,9 +1209,9 @@ class BinaryTree {
1198
1209
  * @param {C} callback - The `callback` parameter is a function that will be called for each node in
1199
1210
  * the tree. It takes a single parameter, which can be of type `N`, `null`, or `undefined`, and
1200
1211
  * returns a value of any type.
1201
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1212
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
1202
1213
  * starting node for traversing the tree. It can be either a node object (`N`), a key value
1203
- * (`BTNKey`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1214
+ * (`K`), `null`, or `undefined`. If not provided, it defaults to the root node of the tree.
1204
1215
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be
1205
1216
  * performed on the tree. It can have two possible values:
1206
1217
  * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
@@ -1260,7 +1271,7 @@ class BinaryTree {
1260
1271
  }
1261
1272
  /**
1262
1273
  * The function `getPredecessor` returns the predecessor node of a given node in a binary tree.
1263
- * @param {BTNKey | N | null | undefined} node - The `node` parameter can be of type `BTNKey`, `N`,
1274
+ * @param {K | N | null | undefined} node - The `node` parameter can be of type `K`, `N`,
1264
1275
  * `null`, or `undefined`.
1265
1276
  * @returns The function `getPredecessor` returns a value of type `N | undefined`.
1266
1277
  */
@@ -1283,7 +1294,7 @@ class BinaryTree {
1283
1294
  }
1284
1295
  /**
1285
1296
  * The function `getSuccessor` returns the next node in a binary tree given a current node.
1286
- * @param {BTNKey | N | null} [x] - The parameter `x` can be of type `BTNKey`, `N`, or `null`.
1297
+ * @param {K | N | null} [x] - The parameter `x` can be of type `K`, `N`, or `null`.
1287
1298
  * @returns the successor of the given node or key. The successor is the node that comes immediately
1288
1299
  * after the given node in the inorder traversal of the binary tree.
1289
1300
  */
@@ -1312,7 +1323,7 @@ class BinaryTree {
1312
1323
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
1313
1324
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
1314
1325
  * following values:
1315
- * @param {BTNKey | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1326
+ * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
1316
1327
  * for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
1317
1328
  * the root of the tree. If no value is provided, the default value is the root of the tree.
1318
1329
  * @returns The function `morris` returns an array of values that are the result of invoking the
@@ -1404,43 +1415,6 @@ class BinaryTree {
1404
1415
  }
1405
1416
  return ans;
1406
1417
  }
1407
- /**
1408
- * Time complexity: O(n)
1409
- * Space complexity: O(n)
1410
- */
1411
- /**
1412
- * Time complexity: O(n)
1413
- * Space complexity: O(n)
1414
- *
1415
- * The function "keys" returns an array of keys from a given object.
1416
- * @returns an array of BTNKey objects.
1417
- */
1418
- keys() {
1419
- const keys = [];
1420
- for (const entry of this) {
1421
- keys.push(entry[0]);
1422
- }
1423
- return keys;
1424
- }
1425
- /**
1426
- * Time complexity: O(n)
1427
- * Space complexity: O(n)
1428
- */
1429
- /**
1430
- * Time complexity: O(n)
1431
- * Space complexity: O(n)
1432
- *
1433
- * The function "values" returns an array of values from a map-like object.
1434
- * @returns The `values()` method is returning an array of values (`V`) from the entries in the
1435
- * object.
1436
- */
1437
- values() {
1438
- const values = [];
1439
- for (const entry of this) {
1440
- values.push(entry[1]);
1441
- }
1442
- return values;
1443
- }
1444
1418
  /**
1445
1419
  * Time complexity: O(n)
1446
1420
  * Space complexity: O(n)
@@ -1459,117 +1433,73 @@ class BinaryTree {
1459
1433
  return cloned;
1460
1434
  }
1461
1435
  /**
1462
- * Time complexity: O(n)
1463
- * Space complexity: O(1)
1464
- */
1465
- /**
1466
- * The `forEach` function iterates over each entry in a tree and calls a callback function with the
1467
- * entry and the tree as arguments.
1468
- * @param callback - The callback parameter is a function that will be called for each entry in the
1469
- * tree. It takes two parameters: entry and tree.
1436
+ * Time Complexity: O(n)
1437
+ * Space Complexity: O(n)
1470
1438
  */
1471
- forEach(callback) {
1472
- for (const entry of this) {
1473
- callback(entry, this);
1474
- }
1475
- }
1476
1439
  /**
1477
- * The `filter` function creates a new tree by iterating over the entries of the current tree and
1478
- * adding the entries that satisfy the given predicate.
1479
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
1480
- * `tree`.
1481
- * @returns The `filter` method is returning a new tree object that contains only the entries that
1482
- * satisfy the given predicate function.
1483
- */
1484
- filter(predicate) {
1440
+ * Time Complexity: O(n)
1441
+ * Space Complexity: O(n)
1442
+ *
1443
+ * The `filter` function creates a new tree by iterating over the elements of the current tree and
1444
+ * adding only the elements that satisfy the given predicate function.
1445
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
1446
+ * `key`, and `index`. It should return a boolean value indicating whether the pair should be
1447
+ * included in the filtered tree or not.
1448
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1449
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
1450
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
1451
+ * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
1452
+ * pass the given predicate function.
1453
+ */
1454
+ filter(predicate, thisArg) {
1485
1455
  const newTree = this.createTree();
1456
+ let index = 0;
1486
1457
  for (const [key, value] of this) {
1487
- if (predicate([key, value], this)) {
1458
+ if (predicate.call(thisArg, value, key, index++, this)) {
1488
1459
  newTree.add([key, value]);
1489
1460
  }
1490
1461
  }
1491
1462
  return newTree;
1492
1463
  }
1493
1464
  /**
1494
- * The `map` function creates a new tree by applying a callback function to each entry in the current
1495
- * tree.
1496
- * @param callback - The callback parameter is a function that takes two arguments: entry and tree.
1465
+ * Time Complexity: O(n)
1466
+ * Space Complexity: O(n)
1467
+ */
1468
+ /**
1469
+ * Time Complexity: O(n)
1470
+ * Space Complexity: O(n)
1471
+ *
1472
+ * The `map` function creates a new tree by applying a callback function to each key-value pair in
1473
+ * the original tree.
1474
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
1475
+ * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
1476
+ * the index of the current pair, and a reference to the tree itself. The callback function should
1477
+ * return a new
1478
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1479
+ * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
1480
+ * will be used as the `this` value when the callback function is called. If you don't pass a value
1497
1481
  * @returns The `map` method is returning a new tree object.
1498
1482
  */
1499
- map(callback) {
1483
+ map(callback, thisArg) {
1500
1484
  const newTree = this.createTree();
1485
+ let index = 0;
1501
1486
  for (const [key, value] of this) {
1502
- newTree.add([key, callback([key, value], this)]);
1487
+ newTree.add([key, callback.call(thisArg, value, key, index++, this)]);
1503
1488
  }
1504
1489
  return newTree;
1505
1490
  }
1506
- // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1507
- // map<NV>(callback: (entry: [BTNKey, V | undefined], tree: this) => NV) {
1508
- // const newTree = this.createTree();
1509
- // for (const [key, value] of this) {
1510
- // newTree.add(key, callback([key, value], this));
1511
- // }
1512
- // return newTree;
1513
- // }
1514
- /**
1515
- * The `reduce` function iterates over the entries of a tree and applies a callback function to each
1516
- * entry, accumulating a single value.
1517
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
1518
- * entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
1519
- * based on the logic defined in the callback function.
1520
- * @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
1521
- * is the value that will be passed as the first argument to the callback function when reducing the
1522
- * elements of the tree.
1523
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
1524
- * all the entries in the tree and applying the callback function to each entry.
1525
- */
1526
- reduce(callback, initialValue) {
1527
- let accumulator = initialValue;
1528
- for (const [key, value] of this) {
1529
- accumulator = callback(accumulator, [key, value], this);
1530
- }
1531
- return accumulator;
1532
- }
1533
- /**
1534
- * The above function is an iterator for a binary tree that can be used to traverse the tree in
1535
- * either an iterative or recursive manner.
1536
- * @param node - The `node` parameter represents the current node in the binary tree from which the
1537
- * iteration starts. It is an optional parameter with a default value of `this.root`, which means
1538
- * that if no node is provided, the iteration will start from the root of the binary tree.
1539
- * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1540
- * binary tree nodes in a specific order.
1541
- */
1542
- *[Symbol.iterator](node = this.root) {
1543
- if (!node)
1544
- return;
1545
- if (this.iterationType === types_1.IterationType.ITERATIVE) {
1546
- const stack = [];
1547
- let current = node;
1548
- while (current || stack.length > 0) {
1549
- while (current && !isNaN(current.key)) {
1550
- stack.push(current);
1551
- current = current.left;
1552
- }
1553
- current = stack.pop();
1554
- if (current && !isNaN(current.key)) {
1555
- yield [current.key, current.value];
1556
- current = current.right;
1557
- }
1558
- }
1559
- }
1560
- else {
1561
- if (node.left && !isNaN(node.key)) {
1562
- yield* this[Symbol.iterator](node.left);
1563
- }
1564
- yield [node.key, node.value];
1565
- if (node.right && !isNaN(node.key)) {
1566
- yield* this[Symbol.iterator](node.right);
1567
- }
1568
- }
1569
- }
1491
+ // // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
1492
+ // // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
1493
+ // // const newTree = this.createTree();
1494
+ // // for (const [key, value] of this) {
1495
+ // // newTree.add(key, callback([key, value], this));
1496
+ // // }
1497
+ // // return newTree;
1498
+ // // }
1499
+ //
1570
1500
  /**
1571
1501
  * The `print` function is used to display a binary tree structure in a visually appealing way.
1572
- * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
1502
+ * @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
1573
1503
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
1574
1504
  * following types:
1575
1505
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
@@ -1596,6 +1526,34 @@ class BinaryTree {
1596
1526
  };
1597
1527
  display(beginRoot);
1598
1528
  }
1529
+ *_getIterator(node = this.root) {
1530
+ if (!node)
1531
+ return;
1532
+ if (this.iterationType === types_1.IterationType.ITERATIVE) {
1533
+ const stack = [];
1534
+ let current = node;
1535
+ while (current || stack.length > 0) {
1536
+ while (current && !isNaN(this.extractor(current.key))) {
1537
+ stack.push(current);
1538
+ current = current.left;
1539
+ }
1540
+ current = stack.pop();
1541
+ if (current && !isNaN(this.extractor(current.key))) {
1542
+ yield [current.key, current.value];
1543
+ current = current.right;
1544
+ }
1545
+ }
1546
+ }
1547
+ else {
1548
+ if (node.left && !isNaN(this.extractor(node.key))) {
1549
+ yield* this[Symbol.iterator](node.left);
1550
+ }
1551
+ yield [node.key, node.value];
1552
+ if (node.right && !isNaN(this.extractor(node.key))) {
1553
+ yield* this[Symbol.iterator](node.right);
1554
+ }
1555
+ }
1556
+ }
1599
1557
  _displayAux(node, options) {
1600
1558
  const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
1601
1559
  const emptyDisplayLayout = [['─'], 1, 0, 0];
@@ -1606,12 +1564,12 @@ class BinaryTree {
1606
1564
  else if (node === undefined && !isShowUndefined) {
1607
1565
  return emptyDisplayLayout;
1608
1566
  }
1609
- else if (node !== null && node !== undefined && isNaN(node.key) && !isShowRedBlackNIL) {
1567
+ else if (node !== null && node !== undefined && isNaN(this.extractor(node.key)) && !isShowRedBlackNIL) {
1610
1568
  return emptyDisplayLayout;
1611
1569
  }
1612
1570
  else if (node !== null && node !== undefined) {
1613
1571
  // Display logic of normal nodes
1614
- const key = node.key, line = isNaN(key) ? 'S' : key.toString(), width = line.length;
1572
+ const key = node.key, line = isNaN(this.extractor(key)) ? 'S' : this.extractor(key).toString(), width = line.length;
1615
1573
  return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
1616
1574
  }
1617
1575
  else {
@@ -1698,7 +1656,7 @@ class BinaryTree {
1698
1656
  * If the parent node is null, the function also returns undefined.
1699
1657
  */
1700
1658
  _addTo(newNode, parent) {
1701
- if (this.isNodeKey(parent))
1659
+ if (this.isNotNodeInstance(parent))
1702
1660
  parent = this.getNode(parent);
1703
1661
  if (parent) {
1704
1662
  // When all leaf nodes are null, it will no longer be possible to add new entity nodes to this binary tree.