data-structure-typed 1.54.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/LICENSE +2 -2
  3. package/README.md +14 -1
  4. package/README_zh-CN.md +1 -1
  5. package/benchmark/report.html +28 -1
  6. package/benchmark/report.json +327 -18
  7. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
  8. package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
  9. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  10. package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
  11. package/dist/cjs/data-structures/base/linear-base.js +553 -0
  12. package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
  13. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +8 -7
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  17. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  18. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +89 -21
  20. package/dist/cjs/data-structures/binary-tree/avl-tree.js +76 -8
  21. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  22. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +173 -225
  23. package/dist/cjs/data-structures/binary-tree/binary-tree.js +244 -149
  24. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  25. package/dist/cjs/data-structures/binary-tree/bst.d.ts +62 -56
  26. package/dist/cjs/data-structures/binary-tree/bst.js +89 -133
  27. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  28. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  29. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +7 -13
  30. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  31. package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +19 -19
  32. package/dist/cjs/data-structures/binary-tree/tree-counter.js +12 -12
  33. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
  34. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  35. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +211 -41
  36. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  37. package/dist/cjs/data-structures/graph/abstract-graph.js +2 -2
  38. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
  40. package/dist/cjs/data-structures/heap/heap.js +0 -10
  41. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  42. package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
  43. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  44. package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
  45. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  46. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  49. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +217 -169
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  52. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  53. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  54. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  55. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  56. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
  57. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  58. package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
  59. package/dist/cjs/data-structures/queue/deque.js +269 -169
  60. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  61. package/dist/cjs/data-structures/queue/queue.d.ts +84 -40
  62. package/dist/cjs/data-structures/queue/queue.js +134 -50
  63. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  64. package/dist/cjs/data-structures/stack/stack.d.ts +3 -11
  65. package/dist/cjs/data-structures/stack/stack.js +0 -10
  66. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  67. package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
  68. package/dist/cjs/data-structures/trie/trie.js +3 -0
  69. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  70. package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
  71. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  72. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  73. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
  74. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  75. package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  76. package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  77. package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
  78. package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
  79. package/dist/cjs/utils/utils.d.ts +2 -2
  80. package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
  81. package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
  82. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  83. package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
  84. package/dist/esm/data-structures/base/linear-base.js +549 -0
  85. package/dist/esm/data-structures/base/linear-base.js.map +1 -0
  86. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  87. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +9 -8
  88. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  89. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  90. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +52 -38
  91. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  92. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +89 -21
  93. package/dist/esm/data-structures/binary-tree/avl-tree.js +77 -9
  94. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  95. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +173 -225
  96. package/dist/esm/data-structures/binary-tree/binary-tree.js +248 -153
  97. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  98. package/dist/esm/data-structures/binary-tree/bst.d.ts +62 -56
  99. package/dist/esm/data-structures/binary-tree/bst.js +90 -132
  100. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  101. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  102. package/dist/esm/data-structures/binary-tree/red-black-tree.js +8 -14
  103. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  104. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +19 -19
  105. package/dist/esm/data-structures/binary-tree/tree-counter.js +13 -13
  106. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
  107. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  108. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +212 -41
  109. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  110. package/dist/esm/data-structures/graph/abstract-graph.js +2 -2
  111. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  112. package/dist/esm/data-structures/heap/heap.d.ts +3 -11
  113. package/dist/esm/data-structures/heap/heap.js +0 -10
  114. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  115. package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
  116. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  117. package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
  118. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  119. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  120. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
  121. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  122. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  123. package/dist/esm/data-structures/linked-list/singly-linked-list.js +217 -170
  124. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  125. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  126. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  127. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  128. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  129. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
  130. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  131. package/dist/esm/data-structures/queue/deque.d.ts +130 -91
  132. package/dist/esm/data-structures/queue/deque.js +269 -169
  133. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  134. package/dist/esm/data-structures/queue/queue.d.ts +84 -40
  135. package/dist/esm/data-structures/queue/queue.js +135 -51
  136. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  137. package/dist/esm/data-structures/stack/stack.d.ts +3 -11
  138. package/dist/esm/data-structures/stack/stack.js +0 -10
  139. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  140. package/dist/esm/data-structures/trie/trie.d.ts +4 -3
  141. package/dist/esm/data-structures/trie/trie.js +3 -0
  142. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  143. package/dist/esm/types/data-structures/base/base.d.ts +9 -4
  144. package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  145. package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  146. package/dist/esm/types/data-structures/binary-tree/bst.d.ts +1 -1
  147. package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  148. package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  149. package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  150. package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
  151. package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
  152. package/dist/esm/utils/utils.d.ts +2 -2
  153. package/dist/umd/data-structure-typed.js +1548 -920
  154. package/dist/umd/data-structure-typed.min.js +5 -12
  155. package/dist/umd/data-structure-typed.min.js.map +1 -1
  156. package/package.json +7 -7
  157. package/src/data-structures/base/iterable-element-base.ts +29 -20
  158. package/src/data-structures/base/linear-base.ts +649 -0
  159. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  160. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  161. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  162. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  163. package/src/data-structures/binary-tree/bst.ts +150 -152
  164. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  165. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  166. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  167. package/src/data-structures/graph/abstract-graph.ts +2 -2
  168. package/src/data-structures/heap/heap.ts +3 -14
  169. package/src/data-structures/heap/max-heap.ts +2 -2
  170. package/src/data-structures/heap/min-heap.ts +2 -2
  171. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  172. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  173. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  174. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  175. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  176. package/src/data-structures/queue/deque.ts +286 -183
  177. package/src/data-structures/queue/queue.ts +149 -63
  178. package/src/data-structures/stack/stack.ts +3 -18
  179. package/src/data-structures/trie/trie.ts +7 -3
  180. package/src/types/data-structures/base/base.ts +17 -8
  181. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  182. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  183. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  184. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  185. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  186. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  187. package/src/types/data-structures/queue/deque.ts +2 -3
  188. package/src/types/data-structures/queue/queue.ts +2 -2
  189. package/src/utils/utils.ts +2 -2
  190. package/test/integration/all-in-one.test.ts +1 -1
  191. package/test/integration/avl-tree.test.ts +1 -1
  192. package/test/integration/bst.test.ts +2 -2
  193. package/test/integration/compile.mjs +21 -21
  194. package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +71 -0
  195. package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +81 -0
  196. package/test/performance/{reportor.js → reportor.mjs} +264 -8
  197. package/test/performance/reportor.ts +1 -1
  198. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +7 -7
  199. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +172 -5
  200. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +72 -4
  201. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +132 -82
  202. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
  203. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +5 -13
  204. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -4
  205. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +168 -10
  206. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
  207. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +111 -11
  208. package/test/unit/data-structures/queue/deque.test.ts +241 -60
  209. package/test/unit/data-structures/queue/queue.test.ts +118 -19
  210. package/test/unit/unrestricted-interconversion.test.ts +1 -1
  211. package/test/utils/json2html.ts +0 -154
  212. package/test/performance/data-structures/binary-tree/avl-tree.test.js +0 -45
  213. /package/test/performance/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +0 -0
@@ -15,6 +15,9 @@ import { DFSOperation, Range } from '../../common';
15
15
  * @template BinaryTreeNode<K, V> - The type of the family relationship in the binary tree.
16
16
  */
17
17
  export class BinaryTreeNode {
18
+ key;
19
+ value;
20
+ parent = undefined;
18
21
  /**
19
22
  * The constructor function initializes an object with a key and an optional value in TypeScript.
20
23
  * @param {K} key - The `key` parameter in the constructor function is used to store the key value
@@ -27,9 +30,6 @@ export class BinaryTreeNode {
27
30
  this.key = key;
28
31
  this.value = value;
29
32
  }
30
- key;
31
- value;
32
- parent = undefined;
33
33
  _left = undefined;
34
34
  get left() {
35
35
  return this._left;
@@ -90,13 +90,76 @@ export class BinaryTreeNode {
90
90
  * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
91
91
  * 4. Subtrees: Each child of a node forms the root of a subtree.
92
92
  * 5. Leaf Nodes: Nodes without children are leaves.
93
+ * @example
94
+ * // determine loan approval using a decision tree
95
+ * // Decision tree structure
96
+ * const loanDecisionTree = new BinaryTree<string>(
97
+ * ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
98
+ * { isDuplicate: true }
99
+ * );
100
+ *
101
+ * function determineLoanApproval(
102
+ * node?: BinaryTreeNode<string> | null,
103
+ * conditions?: { [key: string]: boolean }
104
+ * ): string {
105
+ * if (!node) throw new Error('Invalid node');
106
+ *
107
+ * // If it's a leaf node, return the decision result
108
+ * if (!node.left && !node.right) return node.key;
109
+ *
110
+ * // Check if a valid condition exists for the current node's key
111
+ * return conditions?.[node.key]
112
+ * ? determineLoanApproval(node.left, conditions)
113
+ * : determineLoanApproval(node.right, conditions);
114
+ * }
115
+ *
116
+ * // Test case 1: Stable income and good credit score
117
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
118
+ *
119
+ * // Test case 2: Stable income but poor credit score
120
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
121
+ *
122
+ * // Test case 3: No stable income
123
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
124
+ *
125
+ * // Test case 4: No stable income and poor credit score
126
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
127
+ * @example
128
+ * // evaluate the arithmetic expression represented by the binary tree
129
+ * const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
130
+ *
131
+ * function evaluate(node?: BinaryTreeNode<number | string> | null): number {
132
+ * if (!node) return 0;
133
+ *
134
+ * if (typeof node.key === 'number') return node.key;
135
+ *
136
+ * const leftValue = evaluate(node.left); // Evaluate the left subtree
137
+ * const rightValue = evaluate(node.right); // Evaluate the right subtree
138
+ *
139
+ * // Perform the operation based on the current node's operator
140
+ * switch (node.key) {
141
+ * case '+':
142
+ * return leftValue + rightValue;
143
+ * case '-':
144
+ * return leftValue - rightValue;
145
+ * case '*':
146
+ * return leftValue * rightValue;
147
+ * case '/':
148
+ * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
149
+ * default:
150
+ * throw new Error(`Unsupported operator: ${node.key}`);
151
+ * }
152
+ * }
153
+ *
154
+ * console.log(evaluate(expressionTree.root)); // -27
93
155
  */
94
156
  export class BinaryTree extends IterableEntryBase {
157
+ iterationType = 'ITERATIVE';
95
158
  /**
96
159
  * This TypeScript constructor function initializes a binary tree with optional options and adds
97
160
  * elements based on the provided input.
98
161
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
99
- * iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
162
+ * iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
100
163
  * is used to initialize the binary tree with keys, nodes, entries, or raw data.
101
164
  * @param [options] - The `options` parameter in the constructor is an optional object that can
102
165
  * contain the following properties:
@@ -104,11 +167,13 @@ export class BinaryTree extends IterableEntryBase {
104
167
  constructor(keysNodesEntriesOrRaws = [], options) {
105
168
  super();
106
169
  if (options) {
107
- const { iterationType, toEntryFn, isMapMode } = options;
170
+ const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
108
171
  if (iterationType)
109
172
  this.iterationType = iterationType;
110
173
  if (isMapMode !== undefined)
111
174
  this._isMapMode = isMapMode;
175
+ if (isDuplicate !== undefined)
176
+ this._isDuplicate = isDuplicate;
112
177
  if (typeof toEntryFn === 'function')
113
178
  this._toEntryFn = toEntryFn;
114
179
  else if (toEntryFn)
@@ -117,11 +182,14 @@ export class BinaryTree extends IterableEntryBase {
117
182
  if (keysNodesEntriesOrRaws)
118
183
  this.addMany(keysNodesEntriesOrRaws);
119
184
  }
120
- iterationType = 'ITERATIVE';
121
185
  _isMapMode = true;
122
186
  get isMapMode() {
123
187
  return this._isMapMode;
124
188
  }
189
+ _isDuplicate = false;
190
+ get isDuplicate() {
191
+ return this._isDuplicate;
192
+ }
125
193
  _store = new Map();
126
194
  get store() {
127
195
  return this._store;
@@ -182,8 +250,8 @@ export class BinaryTree extends IterableEntryBase {
182
250
  *
183
251
  * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
184
252
  * value and returns the corresponding node or null.
185
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
186
- * parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
253
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
254
+ * parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
187
255
  * is used to determine whether the input is a key, node, entry, or raw data. The
188
256
  * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
189
257
  * is used to specify the type of iteration to be performed. It has a default value of
@@ -215,7 +283,7 @@ export class BinaryTree extends IterableEntryBase {
215
283
  * Space Complexity: O(1)
216
284
  *
217
285
  * The function isNode checks if the input is an instance of BinaryTreeNode.
218
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
286
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
219
287
  * `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
220
288
  * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
221
289
  * accordingly.
@@ -232,7 +300,7 @@ export class BinaryTree extends IterableEntryBase {
232
300
  * Space Complexity: O(1)
233
301
  *
234
302
  * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
235
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>>
303
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R} keyNodeEntryOrRaw - K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
236
304
  * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
237
305
  * checking if it is an object. If the parameter is an object, the function will return `true`,
238
306
  * indicating that it is of type `R`.
@@ -245,8 +313,8 @@ export class BinaryTree extends IterableEntryBase {
245
313
  * Space Complexity: O(1)
246
314
  *
247
315
  * The function `isRealNode` checks if a given input is a valid node in a binary tree.
248
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
249
- * parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
316
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
317
+ * parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
250
318
  * The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
251
319
  * @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
252
320
  * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
@@ -263,7 +331,7 @@ export class BinaryTree extends IterableEntryBase {
263
331
  * Space Complexity: O(1)
264
332
  *
265
333
  * The function checks if a given input is a valid node or null.
266
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
334
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
267
335
  * `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
268
336
  * V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
269
337
  * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
@@ -278,7 +346,7 @@ export class BinaryTree extends IterableEntryBase {
278
346
  * Space Complexity: O(1)
279
347
  *
280
348
  * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
281
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V,
349
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
282
350
  * BinaryTreeNode<K, V>>
283
351
  * @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
284
352
  * property of the current object and returning a boolean value based on that comparison.
@@ -291,9 +359,9 @@ export class BinaryTree extends IterableEntryBase {
291
359
  * Space Complexity: O(1)
292
360
  *
293
361
  * The function `isRange` checks if the input parameter is an instance of the `Range` class.
294
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>}
295
- * keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
296
- * of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or
362
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
363
+ * - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
364
+ * of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
297
365
  * `Range<K>`. The function checks if the `keyNodeEntry
298
366
  * @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
299
367
  * instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
@@ -309,8 +377,8 @@ export class BinaryTree extends IterableEntryBase {
309
377
  *
310
378
  * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
311
379
  * tree.
312
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
313
- * `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a
380
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
381
+ * `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
314
382
  * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
315
383
  * provided
316
384
  * @returns The function `isLeaf` returns a boolean value indicating whether the input
@@ -330,8 +398,8 @@ export class BinaryTree extends IterableEntryBase {
330
398
  *
331
399
  * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
332
400
  * with a length of 2.
333
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
334
- * parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`.
401
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
402
+ * parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
335
403
  * The function checks if the provided `keyNodeOrEntry` is of type `BTN
336
404
  * @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
337
405
  * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
@@ -362,7 +430,7 @@ export class BinaryTree extends IterableEntryBase {
362
430
  *
363
431
  * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
364
432
  * and finding the correct insertion position.
365
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided
433
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
366
434
  * seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
367
435
  * parameter in the method can accept different types of values:
368
436
  * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
@@ -387,16 +455,18 @@ export class BinaryTree extends IterableEntryBase {
387
455
  }
388
456
  const queue = new Queue([this._root]);
389
457
  let potentialParent; // Record the parent node of the potential insertion location
390
- while (queue.size > 0) {
458
+ while (queue.length > 0) {
391
459
  const cur = queue.shift();
392
460
  if (!cur)
393
461
  continue;
394
- // Check for duplicate keys when newNode is not null
395
- if (newNode !== null && cur.key === newNode.key) {
396
- this._replaceNode(cur, newNode);
397
- if (this._isMapMode)
398
- this._setValue(cur.key, newValue);
399
- return true; // If duplicate keys are found, no insertion is performed
462
+ if (!this._isDuplicate) {
463
+ // Check for duplicate keys when newNode is not null
464
+ if (newNode !== null && cur.key === newNode.key) {
465
+ this._replaceNode(cur, newNode);
466
+ if (this._isMapMode)
467
+ this._setValue(cur.key, newValue);
468
+ return true; // If duplicate keys are found, no insertion is performed
469
+ }
400
470
  }
401
471
  // Record the first possible insertion location found
402
472
  if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
@@ -436,7 +506,7 @@ export class BinaryTree extends IterableEntryBase {
436
506
  * each insertion was successful.
437
507
  * @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
438
508
  * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
439
- * `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
509
+ * `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
440
510
  * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
441
511
  * accepts an iterable of values. These values correspond to the keys or nodes being added in the
442
512
  * `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
@@ -484,7 +554,7 @@ export class BinaryTree extends IterableEntryBase {
484
554
  * The `refill` function clears the existing data structure and then adds new key-value pairs based
485
555
  * on the provided input.
486
556
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
487
- * method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R`
557
+ * method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
488
558
  * objects.
489
559
  * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
490
560
  * accepts an iterable of values of type `V` or `undefined`.
@@ -499,7 +569,7 @@ export class BinaryTree extends IterableEntryBase {
499
569
  *
500
570
  * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
501
571
  * the deleted node along with information for tree balancing.
502
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry
572
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
503
573
  * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
504
574
  * node, entry or raw data. The method returns an array of
505
575
  * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
@@ -561,15 +631,15 @@ export class BinaryTree extends IterableEntryBase {
561
631
  *
562
632
  * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
563
633
  * structure based on a given predicate or key, with options to return multiple results or just one.
564
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
634
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
565
635
  * `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
566
636
  * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
567
637
  * determines whether the search should stop after finding the first matching node. If `onlyOne` is
568
638
  * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
569
639
  * @param {C} callback - The `callback` parameter in the `search` function is a callback function
570
640
  * that will be called on each node that matches the search criteria. It is of type `C`, which
571
- * extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
572
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is
641
+ * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
642
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
573
643
  * used to specify the node from which the search operation should begin. It represents the starting
574
644
  * point in the binary tree where the search will be performed. If no specific `startNode` is
575
645
  * provided, the search operation will start from the root
@@ -630,12 +700,12 @@ export class BinaryTree extends IterableEntryBase {
630
700
  *
631
701
  * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
632
702
  * or predicate, with options for recursive or iterative traversal.
633
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
703
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
634
704
  * - The `getNodes` function you provided takes several parameters:
635
705
  * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
636
706
  * determines whether to return only the first node that matches the criteria specified by the
637
707
  * `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
638
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
708
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
639
709
  * `getNodes` function is used to specify the starting point for traversing the binary tree. It
640
710
  * represents the root node of the binary tree or the node from which the traversal should begin. If
641
711
  * not provided, the default value is set to `this._root
@@ -654,10 +724,10 @@ export class BinaryTree extends IterableEntryBase {
654
724
  *
655
725
  * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
656
726
  * predicate.
657
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
727
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
658
728
  * - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
659
729
  * node, entry, raw data, or a predicate function.
660
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
730
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
661
731
  * `getNode` function is used to specify the starting point for searching for a node in a binary
662
732
  * tree. If no specific starting point is provided, the default value is set to `this._root`, which
663
733
  * is typically the root node of the binary tree.
@@ -677,10 +747,10 @@ export class BinaryTree extends IterableEntryBase {
677
747
  *
678
748
  * This function overrides the `get` method to retrieve the value associated with a specified key,
679
749
  * node, entry, raw data, or predicate in a data structure.
680
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
750
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
681
751
  * - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
682
752
  * following types:
683
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get`
753
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
684
754
  * method is used to specify the starting point for searching for a key or node in the binary tree.
685
755
  * If no specific starting point is provided, the default starting point is the root of the binary
686
756
  * tree (`this._root`).
@@ -708,10 +778,10 @@ export class BinaryTree extends IterableEntryBase {
708
778
  *
709
779
  * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
710
780
  * exists in the data structure.
711
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
781
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
712
782
  * - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
713
783
  * the following types:
714
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
784
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
715
785
  * `override` method is used to specify the starting point for the search operation within the data
716
786
  * structure. It defaults to `this._root` if not provided explicitly.
717
787
  * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
@@ -755,7 +825,7 @@ export class BinaryTree extends IterableEntryBase {
755
825
  *
756
826
  * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
757
827
  * its height.
758
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
828
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
759
829
  * point for checking if the binary tree is perfectly balanced. It represents the root node of the
760
830
  * binary tree or a specific node from which the balance check should begin.
761
831
  * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
@@ -773,7 +843,7 @@ export class BinaryTree extends IterableEntryBase {
773
843
  *
774
844
  * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
775
845
  * or iterative methods.
776
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST`
846
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
777
847
  * function represents the starting point for checking whether a binary search tree (BST) is valid.
778
848
  * It can be a node in the BST or a reference to the root of the BST. If no specific node is
779
849
  * provided, the function will default to
@@ -832,10 +902,10 @@ export class BinaryTree extends IterableEntryBase {
832
902
  * Space Complexity: O(log n)
833
903
  *
834
904
  * The `getDepth` function calculates the depth between two nodes in a binary tree.
835
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth`
905
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
836
906
  * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
837
907
  * It is the target node for which you want to calculate the depth from the `startNode` node.
838
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
908
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
839
909
  * `getDepth` function represents the starting point from which you want to calculate the depth of a
840
910
  * given node or entry in a binary tree. If no specific starting point is provided, the default value
841
911
  * for `startNode` is set to the root of the binary
@@ -862,7 +932,7 @@ export class BinaryTree extends IterableEntryBase {
862
932
  *
863
933
  * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
864
934
  * or iterative approach in TypeScript.
865
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
935
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
866
936
  * point from which the height of the binary tree will be calculated. It can be a node in the binary
867
937
  * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
868
938
  * binary tree data structure.
@@ -907,7 +977,7 @@ export class BinaryTree extends IterableEntryBase {
907
977
  *
908
978
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
909
979
  * recursive or iterative approach in TypeScript.
910
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
980
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
911
981
  * `getMinHeight` function represents the starting node from which the minimum height of the binary
912
982
  * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
913
983
  * tree. If not provided, the default value is the root
@@ -973,7 +1043,7 @@ export class BinaryTree extends IterableEntryBase {
973
1043
  * the path to the root. It is expected to be a function that takes a node as an argument and returns
974
1044
  * a value based on that node. The return type of the callback function is determined by the generic
975
1045
  * type `C
976
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the
1046
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
977
1047
  * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
978
1048
  * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
979
1049
  * whether the resulting path from the given `beginNode` to the root should be in reverse order or
@@ -1005,7 +1075,7 @@ export class BinaryTree extends IterableEntryBase {
1005
1075
  * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
1006
1076
  * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
1007
1077
  * value of `_DEFAULT_NODE_CALLBACK` if not specified.
1008
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1078
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1009
1079
  * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
1010
1080
  * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1011
1081
  * starting point is provided, the function will default
@@ -1051,7 +1121,7 @@ export class BinaryTree extends IterableEntryBase {
1051
1121
  * of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
1052
1122
  * which means it is a callback function that can accept either an optional binary tree node or null
1053
1123
  * as
1054
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1124
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1055
1125
  * `getRightMost` function represents the starting point for finding the rightmost node in a binary
1056
1126
  * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1057
1127
  * starting point is provided, the function will default
@@ -1145,33 +1215,35 @@ export class BinaryTree extends IterableEntryBase {
1145
1215
  * Time complexity: O(n)
1146
1216
  * Space complexity: O(n)
1147
1217
  *
1148
- * The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
1149
- * specified parameters.
1150
- * @param {C} callback - The `callback` parameter is a generic type `C` that extends the
1151
- * `NodeCallback` interface with a type parameter of `OptNodeOrNull<BinaryTreeNode<K, V>>`. It has a default value of
1152
- * `this._DEFAULT_NODE_CALLBACK as C`.
1153
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
1154
- * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
1155
- * possible values for the `pattern` parameter are:
1156
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `dfs`
1157
- * method is used to specify the starting point for the Depth-First Search traversal. It can be
1158
- * either a `BTNRep` object representing a key, node, or entry in the binary tree map,
1159
- * or it can be a
1160
- * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
1161
- * the type of iteration to be performed during the depth-first search traversal. It is used to
1162
- * determine the order in which nodes are visited during the traversal.
1163
- * @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
1164
- * that determines whether null values should be included in the traversal or not. If `includeNull`
1165
- * is set to `true`, then null values will be included in the traversal process. If it is set to
1166
- * `false`,
1167
- * @returns The `dfs` method is returning an array of the return type specified by the generic type
1168
- * parameter `C`. The return type is determined by the callback function provided to the method.
1218
+ * The function performs a depth-first search on a binary tree structure based on the specified
1219
+ * parameters.
1220
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
1221
+ * visited during the depth-first search. It should accept a `BinaryTreeNode` as an argument and
1222
+ * return an optional node or null. The default value for this parameter is `_DEFAULT_NODE_CALLBACK`.
1223
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` function specifies
1224
+ * the order in which the nodes are visited during a depth-first search traversal. The possible
1225
+ * values for the `pattern` parameter are:
1226
+ * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `dfs` function is a boolean flag
1227
+ * that determines whether the depth-first search should stop after finding the first matching node
1228
+ * or continue searching for all matching nodes. If `onlyOne` is set to `true`, the search will stop
1229
+ * after finding the first matching node
1230
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
1231
+ * startNode - The `startNode` parameter in the `dfs` function can be one of the following types:
1232
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` function
1233
+ * specifies the type of iteration to be performed during the Depth-First Search traversal. It is
1234
+ * used to determine the order in which nodes are visited during the traversal. The possible values
1235
+ * for `iterationType` are typically defined as an enum or a
1236
+ * @param [includeNull=false] - The `includeNull` parameter in the `dfs` function determines whether
1237
+ * null nodes should be included in the depth-first search traversal. If `includeNull` is set to
1238
+ * `true`, null nodes will be included in the traversal process. If it is set to `false`, null nodes
1239
+ * will be skipped
1240
+ * @returns The `dfs` method is returning an array of the return type of the callback function `C`.
1169
1241
  */
1170
- dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1242
+ dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1171
1243
  startNode = this.ensureNode(startNode);
1172
1244
  if (!startNode)
1173
1245
  return [];
1174
- return this._dfs(callback, pattern, startNode, iterationType, includeNull);
1246
+ return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
1175
1247
  }
1176
1248
  /**
1177
1249
  * Time complexity: O(n)
@@ -1182,7 +1254,7 @@ export class BinaryTree extends IterableEntryBase {
1182
1254
  * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1183
1255
  * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1184
1256
  * that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
1185
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `bfs`
1257
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `bfs`
1186
1258
  * function represents the starting point for the breadth-first search traversal in a binary tree. It
1187
1259
  * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1188
1260
  * default value is the root node of the binary
@@ -1206,7 +1278,7 @@ export class BinaryTree extends IterableEntryBase {
1206
1278
  startNode
1207
1279
  ]);
1208
1280
  const dfs = (level) => {
1209
- if (queue.size === 0)
1281
+ if (queue.length === 0)
1210
1282
  return;
1211
1283
  const current = queue.shift();
1212
1284
  ans.push(callback(current));
@@ -1228,8 +1300,8 @@ export class BinaryTree extends IterableEntryBase {
1228
1300
  }
1229
1301
  else {
1230
1302
  const queue = new Queue([startNode]);
1231
- while (queue.size > 0) {
1232
- const levelSize = queue.size;
1303
+ while (queue.length > 0) {
1304
+ const levelSize = queue.length;
1233
1305
  for (let i = 0; i < levelSize; i++) {
1234
1306
  const current = queue.shift();
1235
1307
  ans.push(callback(current));
@@ -1258,7 +1330,7 @@ export class BinaryTree extends IterableEntryBase {
1258
1330
  * structure based on a specified callback and iteration type.
1259
1331
  * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
1260
1332
  * in the binary tree. It is optional and defaults to a default callback function if not provided.
1261
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves`
1333
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
1262
1334
  * method is used to specify the starting point for finding and processing the leaves of a binary
1263
1335
  * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1264
1336
  * explicitly provided, the default value
@@ -1289,7 +1361,7 @@ export class BinaryTree extends IterableEntryBase {
1289
1361
  }
1290
1362
  else {
1291
1363
  const queue = new Queue([startNode]);
1292
- while (queue.size > 0) {
1364
+ while (queue.length > 0) {
1293
1365
  const cur = queue.shift();
1294
1366
  if (this.isRealNode(cur)) {
1295
1367
  if (this.isLeaf(cur)) {
@@ -1313,7 +1385,7 @@ export class BinaryTree extends IterableEntryBase {
1313
1385
  * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1314
1386
  * the binary tree during the traversal. It is used to process each node and determine what
1315
1387
  * information to include in the output for each level of the tree.
1316
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1388
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1317
1389
  * `listLevels` function represents the starting point for traversing the binary tree. It can be
1318
1390
  * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1319
1391
  * root of the binary tree.
@@ -1385,11 +1457,11 @@ export class BinaryTree extends IterableEntryBase {
1385
1457
  * Morris Traversal algorithm with different order patterns.
1386
1458
  * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1387
1459
  * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1388
- * `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT
1460
+ * `NodeCallback<BinaryTreeNode<K, V> | null>` type. The default value for `callback` is `this._DEFAULT
1389
1461
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1390
1462
  * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1391
1463
  * values for the `pattern` parameter are:
1392
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris`
1464
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `morris`
1393
1465
  * function is the starting point for the Morris traversal algorithm. It represents the root node of
1394
1466
  * the binary tree or the node from which the traversal should begin. It can be provided as either a
1395
1467
  * key, a node, an entry, or a reference
@@ -1498,20 +1570,6 @@ export class BinaryTree extends IterableEntryBase {
1498
1570
  this._clone(cloned);
1499
1571
  return cloned;
1500
1572
  }
1501
- _clone(cloned) {
1502
- this.bfs(node => {
1503
- if (node === null)
1504
- cloned.add(null);
1505
- else {
1506
- if (this._isMapMode)
1507
- cloned.add([node.key, this._store.get(node.key)]);
1508
- else
1509
- cloned.add([node.key, node.value]);
1510
- }
1511
- }, this._root, this.iterationType, true);
1512
- if (this._isMapMode)
1513
- cloned._store = this._store;
1514
- }
1515
1573
  /**
1516
1574
  * Time Complexity: O(n)
1517
1575
  * Space Complexity: O(n)
@@ -1571,7 +1629,7 @@ export class BinaryTree extends IterableEntryBase {
1571
1629
  *
1572
1630
  * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1573
1631
  * customizable options for displaying undefined, null, and sentinel nodes.
1574
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1632
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1575
1633
  * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1576
1634
  * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1577
1635
  * the default is set to the root
@@ -1616,7 +1674,7 @@ export class BinaryTree extends IterableEntryBase {
1616
1674
  * printing options for the binary tree. It is an optional parameter that allows you to customize how
1617
1675
  * the binary tree is printed, such as choosing between different traversal orders or formatting
1618
1676
  * options.
1619
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1677
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1620
1678
  * `override print` method is used to specify the starting point for printing the binary tree. It can
1621
1679
  * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
1622
1680
  * provided, the default value is set to
@@ -1624,21 +1682,35 @@ export class BinaryTree extends IterableEntryBase {
1624
1682
  print(options, startNode = this._root) {
1625
1683
  console.log(this.toVisual(startNode, options));
1626
1684
  }
1685
+ _clone(cloned) {
1686
+ this.bfs(node => {
1687
+ if (node === null)
1688
+ cloned.add(null);
1689
+ else {
1690
+ if (this._isMapMode)
1691
+ cloned.add([node.key, this._store.get(node.key)]);
1692
+ else
1693
+ cloned.add([node.key, node.value]);
1694
+ }
1695
+ }, this._root, this.iterationType, true);
1696
+ if (this._isMapMode)
1697
+ cloned._store = this._store;
1698
+ }
1627
1699
  /**
1628
1700
  * Time Complexity: O(1)
1629
1701
  * Space Complexity: O(1)
1630
1702
  *
1631
1703
  * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
1632
1704
  * or returns null.
1633
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The
1705
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
1634
1706
  * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
1635
- * can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a
1707
+ * can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
1636
1708
  * node, an entry
1637
1709
  * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
1638
1710
  * an optional parameter of type `V`. It represents the value associated with the key in the node
1639
1711
  * being created. If a `value` is provided, it will be used when creating the node. If
1640
1712
  * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
1641
- * (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the
1713
+ * (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
1642
1714
  * input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
1643
1715
  * value.
1644
1716
  */
@@ -1664,45 +1736,48 @@ export class BinaryTree extends IterableEntryBase {
1664
1736
  * Time complexity: O(n)
1665
1737
  * Space complexity: O(n)
1666
1738
  *
1667
- * The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
1668
- * the specified order pattern and callback function.
1739
+ * The `_dfs` function performs a depth-first search traversal on a binary tree, with customizable
1740
+ * options for traversal order and node processing.
1669
1741
  * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1670
- * called on each node visited during the depth-first search traversal. It is of type `C`, which
1671
- * extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT
1742
+ * called on each node visited during the depth-first search traversal. It is a generic type `C` that
1743
+ * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback`
1672
1744
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
1673
- * order in which the nodes are visited during the Depth-First Search traversal. It can have one of
1674
- * the following values:
1675
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs`
1676
- * method is used to specify the starting point for the depth-first search traversal in a binary
1677
- * tree. It can be provided as either a `BTNRep` object or a reference to the root node
1678
- * of the tree. If no specific
1745
+ * order in which the nodes are visited during a depth-first search traversal. It can have one of the
1746
+ * following values:
1747
+ * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `_dfs` method is a boolean flag
1748
+ * that determines whether the traversal should stop after processing a single node. If `onlyOne` is
1749
+ * set to `true`, the traversal will return as soon as a single node is processed. If it is set to
1750
+ * `false
1751
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
1752
+ * startNode - The `startNode` parameter in the `_dfs` method is used to specify the starting node
1753
+ * for the depth-first search traversal. It can be provided in different forms:
1679
1754
  * @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
1680
- * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
1681
- * binary tree. It can have two possible values:
1682
- * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
1683
- * that determines whether null nodes should be included in the depth-first search traversal. If
1684
- * `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
1685
- * is set to `false`,
1686
- * @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
1687
- * and returns a boolean value. It is used to determine whether the left child of a node should be
1688
- * visited during the depth-first search traversal. By default, it checks if the node is truthy (not
1689
- * null or undefined
1690
- * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
1691
- * argument and returns a boolean value. It is used to determine whether the right child of a node
1692
- * should be visited during the depth-first search traversal. The default implementation checks if
1693
- * the node is truthy before visiting the right child
1694
- * @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
1695
- * argument and returns a boolean value. It is used to determine whether the root node should be
1696
- * visited during the depth-first search traversal based on certain conditions. The default
1697
- * implementation checks if the node is a real node or null based
1698
- * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
1699
- * argument and returns a boolean value indicating whether the node should be processed during the
1700
- * depth-first search traversal. The default implementation checks if the node is a real node or null
1701
- * based on the `includeNull` flag. If `
1702
- * @returns The function `_dfs` returns an array of the return type of the callback function provided
1703
- * as input.
1755
+ * specifies whether the traversal should be done recursively or iteratively. It can have two
1756
+ * possible values:
1757
+ * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method determines whether
1758
+ * null nodes should be included in the traversal process. If `includeNull` is set to `true`, the
1759
+ * method will consider null nodes as valid nodes to visit or process. If `includeNull` is set to
1760
+ * `false`,
1761
+ * @param shouldVisitLeft - The `shouldVisitLeft` parameter in the `_dfs` method is a function that
1762
+ * determines whether the left child of a node should be visited during the Depth-First Search
1763
+ * traversal. By default, it checks if the node is not null or undefined before visiting the left
1764
+ * child. You can customize this behavior
1765
+ * @param shouldVisitRight - The `shouldVisitRight` parameter in the `_dfs` method is a function that
1766
+ * determines whether to visit the right child node of the current node during a depth-first search
1767
+ * traversal. The default implementation of this function checks if the node is not null or undefined
1768
+ * before deciding to visit it.
1769
+ * @param shouldVisitRoot - The `shouldVisitRoot` parameter in the `_dfs` method is a function that
1770
+ * determines whether a given node should be visited during the depth-first search traversal. The
1771
+ * function takes a node as an argument and returns a boolean value indicating whether the node
1772
+ * should be visited.
1773
+ * @param shouldProcessRoot - The `shouldProcessRoot` parameter in the `_dfs` method is a function
1774
+ * that determines whether the root node should be processed during the Depth-First Search traversal.
1775
+ * It takes a node (BinaryTreeNode<K, V> | null | undefined) as input and returns a boolean value. If
1776
+ * the function
1777
+ * @returns The `_dfs` method returns an array of the return type of the provided callback function
1778
+ * `C`.
1704
1779
  */
1705
- _dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
1780
+ _dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
1706
1781
  if (includeNull)
1707
1782
  return this.isRealNodeOrNull(node);
1708
1783
  return this.isRealNode(node);
@@ -1716,31 +1791,40 @@ export class BinaryTree extends IterableEntryBase {
1716
1791
  if (!shouldVisitRoot(node))
1717
1792
  return;
1718
1793
  const visitLeft = () => {
1719
- if (shouldVisitLeft(node))
1794
+ if (shouldVisitLeft(node) && node?.left !== undefined)
1720
1795
  dfs(node?.left);
1721
1796
  };
1722
1797
  const visitRight = () => {
1723
- if (shouldVisitRight(node))
1798
+ if (shouldVisitRight(node) && node?.right !== undefined)
1724
1799
  dfs(node?.right);
1725
1800
  };
1726
1801
  switch (pattern) {
1727
1802
  case 'IN':
1728
1803
  visitLeft();
1729
- if (shouldProcessRoot(node))
1804
+ if (shouldProcessRoot(node)) {
1730
1805
  ans.push(callback(node));
1806
+ if (onlyOne)
1807
+ return;
1808
+ }
1731
1809
  visitRight();
1732
1810
  break;
1733
1811
  case 'PRE':
1734
- if (shouldProcessRoot(node))
1812
+ if (shouldProcessRoot(node)) {
1735
1813
  ans.push(callback(node));
1814
+ if (onlyOne)
1815
+ return;
1816
+ }
1736
1817
  visitLeft();
1737
1818
  visitRight();
1738
1819
  break;
1739
1820
  case 'POST':
1740
1821
  visitLeft();
1741
1822
  visitRight();
1742
- if (shouldProcessRoot(node))
1823
+ if (shouldProcessRoot(node)) {
1743
1824
  ans.push(callback(node));
1825
+ if (onlyOne)
1826
+ return;
1827
+ }
1744
1828
  break;
1745
1829
  }
1746
1830
  };
@@ -1767,8 +1851,11 @@ export class BinaryTree extends IterableEntryBase {
1767
1851
  if (!shouldVisitRoot(cur.node))
1768
1852
  continue;
1769
1853
  if (cur.opt === DFSOperation.PROCESS) {
1770
- if (shouldProcessRoot(cur.node))
1854
+ if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
1771
1855
  ans.push(callback(cur.node));
1856
+ if (onlyOne)
1857
+ return ans;
1858
+ }
1772
1859
  }
1773
1860
  else {
1774
1861
  switch (pattern) {
@@ -1915,12 +2002,12 @@ export class BinaryTree extends IterableEntryBase {
1915
2002
  * Space Complexity: O(1)
1916
2003
  *
1917
2004
  * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
1918
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the
2005
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
1919
2006
  * `_swapProperties` method can be either a BTNRep object containing key and value
1920
2007
  * properties, or it can be of type R.
1921
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the
2008
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
1922
2009
  * `_swapProperties` method represents the node or entry where the properties will be swapped with
1923
- * the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that
2010
+ * the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
1924
2011
  * both `srcNode
1925
2012
  * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
1926
2013
  * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
@@ -1980,7 +2067,7 @@ export class BinaryTree extends IterableEntryBase {
1980
2067
  *
1981
2068
  * The function _setRoot sets the root node of a data structure while updating the parent reference
1982
2069
  * of the previous root node.
1983
- * @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means
2070
+ * @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
1984
2071
  * it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
1985
2072
  */
1986
2073
  _setRoot(v) {
@@ -1995,7 +2082,7 @@ export class BinaryTree extends IterableEntryBase {
1995
2082
  *
1996
2083
  * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
1997
2084
  * predicate function for a binary tree node.
1998
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
2085
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
1999
2086
  * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
2000
2087
  * parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
2001
2088
  * used for filtering nodes in a binary tree.
@@ -2010,9 +2097,17 @@ export class BinaryTree extends IterableEntryBase {
2010
2097
  return (node) => node === keyNodeEntryOrPredicate;
2011
2098
  if (this.isEntry(keyNodeEntryOrPredicate)) {
2012
2099
  const [key] = keyNodeEntryOrPredicate;
2013
- return (node) => node.key === key;
2100
+ return (node) => {
2101
+ if (!node)
2102
+ return false;
2103
+ return node.key === key;
2104
+ };
2014
2105
  }
2015
- return (node) => node.key === keyNodeEntryOrPredicate;
2106
+ return (node) => {
2107
+ if (!node)
2108
+ return false;
2109
+ return node.key === keyNodeEntryOrPredicate;
2110
+ };
2016
2111
  }
2017
2112
  /**
2018
2113
  * Time Complexity: O(1)
@@ -2035,8 +2130,8 @@ export class BinaryTree extends IterableEntryBase {
2035
2130
  *
2036
2131
  * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
2037
2132
  * entry, raw data, or null/undefined.
2038
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a
2039
- * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`,
2133
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
2134
+ * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
2040
2135
  * where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
2041
2136
  * @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
2042
2137
  * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on