linked-list-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 (85) hide show
  1. package/README.md +7 -7
  2. package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
  3. package/dist/data-structures/base/iterable-element-base.js +14 -11
  4. package/dist/data-structures/base/linear-base.d.ts +277 -0
  5. package/dist/data-structures/base/linear-base.js +552 -0
  6. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  7. package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  10. package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
  11. package/dist/data-structures/binary-tree/avl-tree.js +76 -8
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
  13. package/dist/data-structures/binary-tree/binary-tree.js +244 -149
  14. package/dist/data-structures/binary-tree/bst.d.ts +62 -56
  15. package/dist/data-structures/binary-tree/bst.js +89 -133
  16. package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  17. package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
  18. package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
  19. package/dist/data-structures/binary-tree/tree-counter.js +12 -12
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
  22. package/dist/data-structures/graph/abstract-graph.js +2 -2
  23. package/dist/data-structures/heap/heap.d.ts +3 -11
  24. package/dist/data-structures/heap/heap.js +0 -10
  25. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  26. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  30. package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  32. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  33. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  34. package/dist/data-structures/queue/deque.d.ts +130 -91
  35. package/dist/data-structures/queue/deque.js +269 -169
  36. package/dist/data-structures/queue/queue.d.ts +84 -40
  37. package/dist/data-structures/queue/queue.js +134 -50
  38. package/dist/data-structures/stack/stack.d.ts +3 -11
  39. package/dist/data-structures/stack/stack.js +0 -10
  40. package/dist/data-structures/trie/trie.d.ts +4 -3
  41. package/dist/data-structures/trie/trie.js +3 -0
  42. package/dist/types/data-structures/base/base.d.ts +9 -4
  43. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  44. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  45. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  46. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  47. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  48. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  49. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  50. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  51. package/dist/utils/utils.d.ts +2 -2
  52. package/package.json +2 -2
  53. package/src/data-structures/base/iterable-element-base.ts +29 -20
  54. package/src/data-structures/base/linear-base.ts +649 -0
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  57. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  58. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  59. package/src/data-structures/binary-tree/bst.ts +150 -152
  60. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  61. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  62. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  63. package/src/data-structures/graph/abstract-graph.ts +2 -2
  64. package/src/data-structures/heap/heap.ts +3 -14
  65. package/src/data-structures/heap/max-heap.ts +2 -2
  66. package/src/data-structures/heap/min-heap.ts +2 -2
  67. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  68. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  69. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  70. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  71. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  72. package/src/data-structures/queue/deque.ts +286 -183
  73. package/src/data-structures/queue/queue.ts +149 -63
  74. package/src/data-structures/stack/stack.ts +3 -18
  75. package/src/data-structures/trie/trie.ts +7 -3
  76. package/src/types/data-structures/base/base.ts +17 -8
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  78. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  79. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  80. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  81. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  82. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  83. package/src/types/data-structures/queue/deque.ts +2 -3
  84. package/src/types/data-structures/queue/queue.ts +2 -2
  85. package/src/utils/utils.ts +2 -2
@@ -92,13 +92,75 @@ exports.BinaryTreeNode = BinaryTreeNode;
92
92
  * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
93
93
  * 4. Subtrees: Each child of a node forms the root of a subtree.
94
94
  * 5. Leaf Nodes: Nodes without children are leaves.
95
+ * @example
96
+ * // determine loan approval using a decision tree
97
+ * // Decision tree structure
98
+ * const loanDecisionTree = new BinaryTree<string>(
99
+ * ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
100
+ * { isDuplicate: true }
101
+ * );
102
+ *
103
+ * function determineLoanApproval(
104
+ * node?: BinaryTreeNode<string> | null,
105
+ * conditions?: { [key: string]: boolean }
106
+ * ): string {
107
+ * if (!node) throw new Error('Invalid node');
108
+ *
109
+ * // If it's a leaf node, return the decision result
110
+ * if (!node.left && !node.right) return node.key;
111
+ *
112
+ * // Check if a valid condition exists for the current node's key
113
+ * return conditions?.[node.key]
114
+ * ? determineLoanApproval(node.left, conditions)
115
+ * : determineLoanApproval(node.right, conditions);
116
+ * }
117
+ *
118
+ * // Test case 1: Stable income and good credit score
119
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
120
+ *
121
+ * // Test case 2: Stable income but poor credit score
122
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
123
+ *
124
+ * // Test case 3: No stable income
125
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
126
+ *
127
+ * // Test case 4: No stable income and poor credit score
128
+ * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
129
+ * @example
130
+ * // evaluate the arithmetic expression represented by the binary tree
131
+ * const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
132
+ *
133
+ * function evaluate(node?: BinaryTreeNode<number | string> | null): number {
134
+ * if (!node) return 0;
135
+ *
136
+ * if (typeof node.key === 'number') return node.key;
137
+ *
138
+ * const leftValue = evaluate(node.left); // Evaluate the left subtree
139
+ * const rightValue = evaluate(node.right); // Evaluate the right subtree
140
+ *
141
+ * // Perform the operation based on the current node's operator
142
+ * switch (node.key) {
143
+ * case '+':
144
+ * return leftValue + rightValue;
145
+ * case '-':
146
+ * return leftValue - rightValue;
147
+ * case '*':
148
+ * return leftValue * rightValue;
149
+ * case '/':
150
+ * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
151
+ * default:
152
+ * throw new Error(`Unsupported operator: ${node.key}`);
153
+ * }
154
+ * }
155
+ *
156
+ * console.log(evaluate(expressionTree.root)); // -27
95
157
  */
96
158
  class BinaryTree extends base_1.IterableEntryBase {
97
159
  /**
98
160
  * This TypeScript constructor function initializes a binary tree with optional options and adds
99
161
  * elements based on the provided input.
100
162
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
101
- * iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
163
+ * iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
102
164
  * is used to initialize the binary tree with keys, nodes, entries, or raw data.
103
165
  * @param [options] - The `options` parameter in the constructor is an optional object that can
104
166
  * contain the following properties:
@@ -107,16 +169,19 @@ class BinaryTree extends base_1.IterableEntryBase {
107
169
  super();
108
170
  this.iterationType = 'ITERATIVE';
109
171
  this._isMapMode = true;
172
+ this._isDuplicate = false;
110
173
  this._store = new Map();
111
174
  this._size = 0;
112
175
  this._NIL = new BinaryTreeNode(NaN);
113
176
  this._DEFAULT_NODE_CALLBACK = (node) => (node ? node.key : undefined);
114
177
  if (options) {
115
- const { iterationType, toEntryFn, isMapMode } = options;
178
+ const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
116
179
  if (iterationType)
117
180
  this.iterationType = iterationType;
118
181
  if (isMapMode !== undefined)
119
182
  this._isMapMode = isMapMode;
183
+ if (isDuplicate !== undefined)
184
+ this._isDuplicate = isDuplicate;
120
185
  if (typeof toEntryFn === 'function')
121
186
  this._toEntryFn = toEntryFn;
122
187
  else if (toEntryFn)
@@ -128,6 +193,9 @@ class BinaryTree extends base_1.IterableEntryBase {
128
193
  get isMapMode() {
129
194
  return this._isMapMode;
130
195
  }
196
+ get isDuplicate() {
197
+ return this._isDuplicate;
198
+ }
131
199
  get store() {
132
200
  return this._store;
133
201
  }
@@ -178,8 +246,8 @@ class BinaryTree extends base_1.IterableEntryBase {
178
246
  *
179
247
  * The function `ensureNode` in TypeScript checks if a given input is a node, entry, key, or raw
180
248
  * value and returns the corresponding node or null.
181
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
182
- * parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It
249
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
250
+ * parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It
183
251
  * is used to determine whether the input is a key, node, entry, or raw data. The
184
252
  * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function
185
253
  * is used to specify the type of iteration to be performed. It has a default value of
@@ -211,7 +279,7 @@ class BinaryTree extends base_1.IterableEntryBase {
211
279
  * Space Complexity: O(1)
212
280
  *
213
281
  * The function isNode checks if the input is an instance of BinaryTreeNode.
214
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
282
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
215
283
  * `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is
216
284
  * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value
217
285
  * accordingly.
@@ -228,7 +296,7 @@ class BinaryTree extends base_1.IterableEntryBase {
228
296
  * Space Complexity: O(1)
229
297
  *
230
298
  * The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
231
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>>
299
+ * @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
232
300
  * @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
233
301
  * checking if it is an object. If the parameter is an object, the function will return `true`,
234
302
  * indicating that it is of type `R`.
@@ -241,8 +309,8 @@ class BinaryTree extends base_1.IterableEntryBase {
241
309
  * Space Complexity: O(1)
242
310
  *
243
311
  * The function `isRealNode` checks if a given input is a valid node in a binary tree.
244
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
245
- * parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
312
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
313
+ * parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
246
314
  * The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal
247
315
  * @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid
248
316
  * node by comparing it to `this._NIL`, `null`, and `undefined`. If the input is not one of these
@@ -259,7 +327,7 @@ class BinaryTree extends base_1.IterableEntryBase {
259
327
  * Space Complexity: O(1)
260
328
  *
261
329
  * The function checks if a given input is a valid node or null.
262
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
330
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
263
331
  * `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K,
264
332
  * V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or
265
333
  * @returns The function `isRealNodeOrNull` is returning a boolean value. It checks if the input
@@ -274,7 +342,7 @@ class BinaryTree extends base_1.IterableEntryBase {
274
342
  * Space Complexity: O(1)
275
343
  *
276
344
  * The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value.
277
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V,
345
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V,
278
346
  * BinaryTreeNode<K, V>>
279
347
  * @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL`
280
348
  * property of the current object and returning a boolean value based on that comparison.
@@ -287,9 +355,9 @@ class BinaryTree extends base_1.IterableEntryBase {
287
355
  * Space Complexity: O(1)
288
356
  *
289
357
  * The function `isRange` checks if the input parameter is an instance of the `Range` class.
290
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>}
291
- * keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
292
- * of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or
358
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate
359
+ * - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be
360
+ * of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or
293
361
  * `Range<K>`. The function checks if the `keyNodeEntry
294
362
  * @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an
295
363
  * instance of the `Range` class. If it is an instance of `Range`, the function will return `true`,
@@ -305,8 +373,8 @@ class BinaryTree extends base_1.IterableEntryBase {
305
373
  *
306
374
  * The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
307
375
  * tree.
308
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter
309
- * `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a
376
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
377
+ * `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a
310
378
  * key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the
311
379
  * provided
312
380
  * @returns The function `isLeaf` returns a boolean value indicating whether the input
@@ -326,8 +394,8 @@ class BinaryTree extends base_1.IterableEntryBase {
326
394
  *
327
395
  * The function `isEntry` checks if the input is a BTNEntry object by verifying if it is an array
328
396
  * with a length of 2.
329
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
330
- * parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`.
397
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
398
+ * parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`.
331
399
  * The function checks if the provided `keyNodeOrEntry` is of type `BTN
332
400
  * @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array
333
401
  * with a length of 2. If it is, then it returns `true`, indicating that the parameter is of type
@@ -358,7 +426,7 @@ class BinaryTree extends base_1.IterableEntryBase {
358
426
  *
359
427
  * The `add` function in TypeScript adds a new node to a binary tree while handling duplicate keys
360
428
  * and finding the correct insertion position.
361
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided
429
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided
362
430
  * seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry`
363
431
  * parameter in the method can accept different types of values:
364
432
  * @param {V} [value] - The `value` parameter in the `add` method represents the value associated
@@ -383,16 +451,18 @@ class BinaryTree extends base_1.IterableEntryBase {
383
451
  }
384
452
  const queue = new queue_1.Queue([this._root]);
385
453
  let potentialParent; // Record the parent node of the potential insertion location
386
- while (queue.size > 0) {
454
+ while (queue.length > 0) {
387
455
  const cur = queue.shift();
388
456
  if (!cur)
389
457
  continue;
390
- // Check for duplicate keys when newNode is not null
391
- if (newNode !== null && cur.key === newNode.key) {
392
- this._replaceNode(cur, newNode);
393
- if (this._isMapMode)
394
- this._setValue(cur.key, newValue);
395
- return true; // If duplicate keys are found, no insertion is performed
458
+ if (!this._isDuplicate) {
459
+ // Check for duplicate keys when newNode is not null
460
+ if (newNode !== null && cur.key === newNode.key) {
461
+ this._replaceNode(cur, newNode);
462
+ if (this._isMapMode)
463
+ this._setValue(cur.key, newValue);
464
+ return true; // If duplicate keys are found, no insertion is performed
465
+ }
396
466
  }
397
467
  // Record the first possible insertion location found
398
468
  if (potentialParent === undefined && (cur.left === undefined || cur.right === undefined)) {
@@ -432,7 +502,7 @@ class BinaryTree extends base_1.IterableEntryBase {
432
502
  * each insertion was successful.
433
503
  * @param keysNodesEntriesOrRaws - `keysNodesEntriesOrRaws` is an iterable that can contain a
434
504
  * mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type
435
- * `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`.
505
+ * `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`.
436
506
  * @param [values] - The `values` parameter in the `addMany` function is an optional parameter that
437
507
  * accepts an iterable of values. These values correspond to the keys or nodes being added in the
438
508
  * `keysNodesEntriesOrRaws` parameter. If provided, the function will iterate over the values and
@@ -480,7 +550,7 @@ class BinaryTree extends base_1.IterableEntryBase {
480
550
  * The `refill` function clears the existing data structure and then adds new key-value pairs based
481
551
  * on the provided input.
482
552
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill`
483
- * method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R`
553
+ * method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R`
484
554
  * objects.
485
555
  * @param [values] - The `values` parameter in the `refill` method is an optional parameter that
486
556
  * accepts an iterable of values of type `V` or `undefined`.
@@ -495,7 +565,7 @@ class BinaryTree extends base_1.IterableEntryBase {
495
565
  *
496
566
  * The function `delete` in TypeScript implements the deletion of a node in a binary tree and returns
497
567
  * the deleted node along with information for tree balancing.
498
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry
568
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry
499
569
  * - The `delete` method you provided is used to delete a node from a binary tree based on the key,
500
570
  * node, entry or raw data. The method returns an array of
501
571
  * `BinaryTreeDeleteResult` objects containing information about the deleted node and whether
@@ -557,15 +627,15 @@ class BinaryTree extends base_1.IterableEntryBase {
557
627
  *
558
628
  * The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
559
629
  * structure based on a given predicate or key, with options to return multiple results or just one.
560
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
630
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
561
631
  * `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values:
562
632
  * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
563
633
  * determines whether the search should stop after finding the first matching node. If `onlyOne` is
564
634
  * set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
565
635
  * @param {C} callback - The `callback` parameter in the `search` function is a callback function
566
636
  * that will be called on each node that matches the search criteria. It is of type `C`, which
567
- * extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
568
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is
637
+ * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
638
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is
569
639
  * used to specify the node from which the search operation should begin. It represents the starting
570
640
  * point in the binary tree where the search will be performed. If no specific `startNode` is
571
641
  * provided, the search operation will start from the root
@@ -626,12 +696,12 @@ class BinaryTree extends base_1.IterableEntryBase {
626
696
  *
627
697
  * The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
628
698
  * or predicate, with options for recursive or iterative traversal.
629
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
699
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
630
700
  * - The `getNodes` function you provided takes several parameters:
631
701
  * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
632
702
  * determines whether to return only the first node that matches the criteria specified by the
633
703
  * `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
634
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
704
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
635
705
  * `getNodes` function is used to specify the starting point for traversing the binary tree. It
636
706
  * represents the root node of the binary tree or the node from which the traversal should begin. If
637
707
  * not provided, the default value is set to `this._root
@@ -650,10 +720,10 @@ class BinaryTree extends base_1.IterableEntryBase {
650
720
  *
651
721
  * The `getNode` function retrieves a node based on the provided key, node, entry, raw data, or
652
722
  * predicate.
653
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
723
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
654
724
  * - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key,
655
725
  * node, entry, raw data, or a predicate function.
656
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
726
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
657
727
  * `getNode` function is used to specify the starting point for searching for a node in a binary
658
728
  * tree. If no specific starting point is provided, the default value is set to `this._root`, which
659
729
  * is typically the root node of the binary tree.
@@ -673,10 +743,10 @@ class BinaryTree extends base_1.IterableEntryBase {
673
743
  *
674
744
  * This function overrides the `get` method to retrieve the value associated with a specified key,
675
745
  * node, entry, raw data, or predicate in a data structure.
676
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
746
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
677
747
  * - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the
678
748
  * following types:
679
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get`
749
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get`
680
750
  * method is used to specify the starting point for searching for a key or node in the binary tree.
681
751
  * If no specific starting point is provided, the default starting point is the root of the binary
682
752
  * tree (`this._root`).
@@ -705,10 +775,10 @@ class BinaryTree extends base_1.IterableEntryBase {
705
775
  *
706
776
  * The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate
707
777
  * exists in the data structure.
708
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
778
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate
709
779
  * - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of
710
780
  * the following types:
711
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
781
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
712
782
  * `override` method is used to specify the starting point for the search operation within the data
713
783
  * structure. It defaults to `this._root` if not provided explicitly.
714
784
  * @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method
@@ -752,7 +822,7 @@ class BinaryTree extends base_1.IterableEntryBase {
752
822
  *
753
823
  * The function checks if a binary tree is perfectly balanced by comparing its minimum height with
754
824
  * its height.
755
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
825
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
756
826
  * point for checking if the binary tree is perfectly balanced. It represents the root node of the
757
827
  * binary tree or a specific node from which the balance check should begin.
758
828
  * @returns The method `isPerfectlyBalanced` is returning a boolean value, which indicates whether
@@ -770,7 +840,7 @@ class BinaryTree extends base_1.IterableEntryBase {
770
840
  *
771
841
  * The function `isBST` in TypeScript checks if a binary search tree is valid using either recursive
772
842
  * or iterative methods.
773
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST`
843
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST`
774
844
  * function represents the starting point for checking whether a binary search tree (BST) is valid.
775
845
  * It can be a node in the BST or a reference to the root of the BST. If no specific node is
776
846
  * provided, the function will default to
@@ -829,10 +899,10 @@ class BinaryTree extends base_1.IterableEntryBase {
829
899
  * Space Complexity: O(log n)
830
900
  *
831
901
  * The `getDepth` function calculates the depth between two nodes in a binary tree.
832
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth`
902
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth`
833
903
  * function represents the node or entry in a binary tree map, or a reference to a node in the tree.
834
904
  * It is the target node for which you want to calculate the depth from the `startNode` node.
835
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
905
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
836
906
  * `getDepth` function represents the starting point from which you want to calculate the depth of a
837
907
  * given node or entry in a binary tree. If no specific starting point is provided, the default value
838
908
  * for `startNode` is set to the root of the binary
@@ -859,7 +929,7 @@ class BinaryTree extends base_1.IterableEntryBase {
859
929
  *
860
930
  * The `getHeight` function calculates the maximum height of a binary tree using either a recursive
861
931
  * or iterative approach in TypeScript.
862
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting
932
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting
863
933
  * point from which the height of the binary tree will be calculated. It can be a node in the binary
864
934
  * tree or a reference to the root of the tree. If not provided, it defaults to the root of the
865
935
  * binary tree data structure.
@@ -904,7 +974,7 @@ class BinaryTree extends base_1.IterableEntryBase {
904
974
  *
905
975
  * The `getMinHeight` function calculates the minimum height of a binary tree using either a
906
976
  * recursive or iterative approach in TypeScript.
907
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
977
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
908
978
  * `getMinHeight` function represents the starting node from which the minimum height of the binary
909
979
  * tree will be calculated. It is either a node in the binary tree or a reference to the root of the
910
980
  * tree. If not provided, the default value is the root
@@ -970,7 +1040,7 @@ class BinaryTree extends base_1.IterableEntryBase {
970
1040
  * the path to the root. It is expected to be a function that takes a node as an argument and returns
971
1041
  * a value based on that node. The return type of the callback function is determined by the generic
972
1042
  * type `C
973
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the
1043
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the
974
1044
  * `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`.
975
1045
  * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines
976
1046
  * whether the resulting path from the given `beginNode` to the root should be in reverse order or
@@ -1002,7 +1072,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1002
1072
  * @param {C} callback - The `callback` parameter is a function that will be called with the leftmost
1003
1073
  * node of a binary tree or with `undefined` if the tree is empty. It is provided with a default
1004
1074
  * value of `_DEFAULT_NODE_CALLBACK` if not specified.
1005
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1075
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1006
1076
  * `getLeftMost` function represents the starting point for finding the leftmost node in a binary
1007
1077
  * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1008
1078
  * starting point is provided, the function will default
@@ -1048,7 +1118,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1048
1118
  * of finding the rightmost node in a binary tree. It is of type `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`,
1049
1119
  * which means it is a callback function that can accept either an optional binary tree node or null
1050
1120
  * as
1051
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1121
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1052
1122
  * `getRightMost` function represents the starting point for finding the rightmost node in a binary
1053
1123
  * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific
1054
1124
  * starting point is provided, the function will default
@@ -1142,33 +1212,35 @@ class BinaryTree extends base_1.IterableEntryBase {
1142
1212
  * Time complexity: O(n)
1143
1213
  * Space complexity: O(n)
1144
1214
  *
1145
- * The function `dfs` performs a depth-first search traversal on a binary tree structure based on the
1146
- * specified parameters.
1147
- * @param {C} callback - The `callback` parameter is a generic type `C` that extends the
1148
- * `NodeCallback` interface with a type parameter of `OptNodeOrNull<BinaryTreeNode<K, V>>`. It has a default value of
1149
- * `this._DEFAULT_NODE_CALLBACK as C`.
1150
- * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` method specifies the
1151
- * order in which the Depth-First Search (DFS) algorithm should traverse the nodes in the tree. The
1152
- * possible values for the `pattern` parameter are:
1153
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `dfs`
1154
- * method is used to specify the starting point for the Depth-First Search traversal. It can be
1155
- * either a `BTNRep` object representing a key, node, or entry in the binary tree map,
1156
- * or it can be a
1157
- * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` method specifies
1158
- * the type of iteration to be performed during the depth-first search traversal. It is used to
1159
- * determine the order in which nodes are visited during the traversal.
1160
- * @param [includeNull=false] - The `includeNull` parameter in the `dfs` method is a boolean flag
1161
- * that determines whether null values should be included in the traversal or not. If `includeNull`
1162
- * is set to `true`, then null values will be included in the traversal process. If it is set to
1163
- * `false`,
1164
- * @returns The `dfs` method is returning an array of the return type specified by the generic type
1165
- * parameter `C`. The return type is determined by the callback function provided to the method.
1215
+ * The function performs a depth-first search on a binary tree structure based on the specified
1216
+ * parameters.
1217
+ * @param {C} callback - The `callback` parameter is a function that will be called for each node
1218
+ * visited during the depth-first search. It should accept a `BinaryTreeNode` as an argument and
1219
+ * return an optional node or null. The default value for this parameter is `_DEFAULT_NODE_CALLBACK`.
1220
+ * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `dfs` function specifies
1221
+ * the order in which the nodes are visited during a depth-first search traversal. The possible
1222
+ * values for the `pattern` parameter are:
1223
+ * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `dfs` function is a boolean flag
1224
+ * that determines whether the depth-first search should stop after finding the first matching node
1225
+ * or continue searching for all matching nodes. If `onlyOne` is set to `true`, the search will stop
1226
+ * after finding the first matching node
1227
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
1228
+ * startNode - The `startNode` parameter in the `dfs` function can be one of the following types:
1229
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `dfs` function
1230
+ * specifies the type of iteration to be performed during the Depth-First Search traversal. It is
1231
+ * used to determine the order in which nodes are visited during the traversal. The possible values
1232
+ * for `iterationType` are typically defined as an enum or a
1233
+ * @param [includeNull=false] - The `includeNull` parameter in the `dfs` function determines whether
1234
+ * null nodes should be included in the depth-first search traversal. If `includeNull` is set to
1235
+ * `true`, null nodes will be included in the traversal process. If it is set to `false`, null nodes
1236
+ * will be skipped
1237
+ * @returns The `dfs` method is returning an array of the return type of the callback function `C`.
1166
1238
  */
1167
- dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1239
+ dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType, includeNull = false) {
1168
1240
  startNode = this.ensureNode(startNode);
1169
1241
  if (!startNode)
1170
1242
  return [];
1171
- return this._dfs(callback, pattern, startNode, iterationType, includeNull);
1243
+ return this._dfs(callback, pattern, onlyOne, startNode, iterationType, includeNull);
1172
1244
  }
1173
1245
  /**
1174
1246
  * Time complexity: O(n)
@@ -1179,7 +1251,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1179
1251
  * @param {C} callback - The `callback` parameter in the `bfs` function is a function that will be
1180
1252
  * called on each node visited during the breadth-first search traversal. It is a generic type `C`
1181
1253
  * that extends the `NodeCallback` type, which takes a parameter of type `BinaryTreeNode<K, V>` or `null`.
1182
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `bfs`
1254
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `bfs`
1183
1255
  * function represents the starting point for the breadth-first search traversal in a binary tree. It
1184
1256
  * can be specified as a key, node, or entry in the binary tree structure. If not provided, the
1185
1257
  * default value is the root node of the binary
@@ -1203,7 +1275,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1203
1275
  startNode
1204
1276
  ]);
1205
1277
  const dfs = (level) => {
1206
- if (queue.size === 0)
1278
+ if (queue.length === 0)
1207
1279
  return;
1208
1280
  const current = queue.shift();
1209
1281
  ans.push(callback(current));
@@ -1225,8 +1297,8 @@ class BinaryTree extends base_1.IterableEntryBase {
1225
1297
  }
1226
1298
  else {
1227
1299
  const queue = new queue_1.Queue([startNode]);
1228
- while (queue.size > 0) {
1229
- const levelSize = queue.size;
1300
+ while (queue.length > 0) {
1301
+ const levelSize = queue.length;
1230
1302
  for (let i = 0; i < levelSize; i++) {
1231
1303
  const current = queue.shift();
1232
1304
  ans.push(callback(current));
@@ -1255,7 +1327,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1255
1327
  * structure based on a specified callback and iteration type.
1256
1328
  * @param {C} callback - The `callback` parameter is a function that will be called on each leaf node
1257
1329
  * in the binary tree. It is optional and defaults to a default callback function if not provided.
1258
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves`
1330
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves`
1259
1331
  * method is used to specify the starting point for finding and processing the leaves of a binary
1260
1332
  * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not
1261
1333
  * explicitly provided, the default value
@@ -1286,7 +1358,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1286
1358
  }
1287
1359
  else {
1288
1360
  const queue = new queue_1.Queue([startNode]);
1289
- while (queue.size > 0) {
1361
+ while (queue.length > 0) {
1290
1362
  const cur = queue.shift();
1291
1363
  if (this.isRealNode(cur)) {
1292
1364
  if (this.isLeaf(cur)) {
@@ -1310,7 +1382,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1310
1382
  * @param {C} callback - The `callback` parameter is a function that will be applied to each node in
1311
1383
  * the binary tree during the traversal. It is used to process each node and determine what
1312
1384
  * information to include in the output for each level of the tree.
1313
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1385
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1314
1386
  * `listLevels` function represents the starting point for traversing the binary tree. It can be
1315
1387
  * either a key, a node, or an entry in the binary tree. If not provided, the default value is the
1316
1388
  * root of the binary tree.
@@ -1382,11 +1454,11 @@ class BinaryTree extends base_1.IterableEntryBase {
1382
1454
  * Morris Traversal algorithm with different order patterns.
1383
1455
  * @param {C} callback - The `callback` parameter in the `morris` function is a function that will be
1384
1456
  * called on each node in the binary tree during the traversal. It is of type `C`, which extends the
1385
- * `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT
1457
+ * `NodeCallback<BinaryTreeNode<K, V> | null>` type. The default value for `callback` is `this._DEFAULT
1386
1458
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies
1387
1459
  * the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible
1388
1460
  * values for the `pattern` parameter are:
1389
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris`
1461
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `morris`
1390
1462
  * function is the starting point for the Morris traversal algorithm. It represents the root node of
1391
1463
  * the binary tree or the node from which the traversal should begin. It can be provided as either a
1392
1464
  * key, a node, an entry, or a reference
@@ -1495,20 +1567,6 @@ class BinaryTree extends base_1.IterableEntryBase {
1495
1567
  this._clone(cloned);
1496
1568
  return cloned;
1497
1569
  }
1498
- _clone(cloned) {
1499
- this.bfs(node => {
1500
- if (node === null)
1501
- cloned.add(null);
1502
- else {
1503
- if (this._isMapMode)
1504
- cloned.add([node.key, this._store.get(node.key)]);
1505
- else
1506
- cloned.add([node.key, node.value]);
1507
- }
1508
- }, this._root, this.iterationType, true);
1509
- if (this._isMapMode)
1510
- cloned._store = this._store;
1511
- }
1512
1570
  /**
1513
1571
  * Time Complexity: O(n)
1514
1572
  * Space Complexity: O(n)
@@ -1568,7 +1626,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1568
1626
  *
1569
1627
  * The function `toVisual` in TypeScript overrides the visual representation of a binary tree with
1570
1628
  * customizable options for displaying undefined, null, and sentinel nodes.
1571
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1629
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1572
1630
  * `toVisual` method is used to specify the starting point for visualizing the binary tree structure.
1573
1631
  * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided,
1574
1632
  * the default is set to the root
@@ -1613,7 +1671,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1613
1671
  * printing options for the binary tree. It is an optional parameter that allows you to customize how
1614
1672
  * the binary tree is printed, such as choosing between different traversal orders or formatting
1615
1673
  * options.
1616
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the
1674
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the
1617
1675
  * `override print` method is used to specify the starting point for printing the binary tree. It can
1618
1676
  * be either a key, a node, an entry, or the root of the tree. If no specific starting point is
1619
1677
  * provided, the default value is set to
@@ -1621,21 +1679,35 @@ class BinaryTree extends base_1.IterableEntryBase {
1621
1679
  print(options, startNode = this._root) {
1622
1680
  console.log(this.toVisual(startNode, options));
1623
1681
  }
1682
+ _clone(cloned) {
1683
+ this.bfs(node => {
1684
+ if (node === null)
1685
+ cloned.add(null);
1686
+ else {
1687
+ if (this._isMapMode)
1688
+ cloned.add([node.key, this._store.get(node.key)]);
1689
+ else
1690
+ cloned.add([node.key, node.value]);
1691
+ }
1692
+ }, this._root, this.iterationType, true);
1693
+ if (this._isMapMode)
1694
+ cloned._store = this._store;
1695
+ }
1624
1696
  /**
1625
1697
  * Time Complexity: O(1)
1626
1698
  * Space Complexity: O(1)
1627
1699
  *
1628
1700
  * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
1629
1701
  * or returns null.
1630
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The
1702
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The
1631
1703
  * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which
1632
- * can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a
1704
+ * can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a
1633
1705
  * node, an entry
1634
1706
  * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
1635
1707
  * an optional parameter of type `V`. It represents the value associated with the key in the node
1636
1708
  * being created. If a `value` is provided, it will be used when creating the node. If
1637
1709
  * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
1638
- * (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the
1710
+ * (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the
1639
1711
  * input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null
1640
1712
  * value.
1641
1713
  */
@@ -1661,45 +1733,48 @@ class BinaryTree extends base_1.IterableEntryBase {
1661
1733
  * Time complexity: O(n)
1662
1734
  * Space complexity: O(n)
1663
1735
  *
1664
- * The `_dfs` function performs a depth-first search traversal on a binary tree structure based on
1665
- * the specified order pattern and callback function.
1736
+ * The `_dfs` function performs a depth-first search traversal on a binary tree, with customizable
1737
+ * options for traversal order and node processing.
1666
1738
  * @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be
1667
- * called on each node visited during the depth-first search traversal. It is of type `C`, which
1668
- * extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT
1739
+ * called on each node visited during the depth-first search traversal. It is a generic type `C` that
1740
+ * extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback`
1669
1741
  * @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the
1670
- * order in which the nodes are visited during the Depth-First Search traversal. It can have one of
1671
- * the following values:
1672
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs`
1673
- * method is used to specify the starting point for the depth-first search traversal in a binary
1674
- * tree. It can be provided as either a `BTNRep` object or a reference to the root node
1675
- * of the tree. If no specific
1742
+ * order in which the nodes are visited during a depth-first search traversal. It can have one of the
1743
+ * following values:
1744
+ * @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `_dfs` method is a boolean flag
1745
+ * that determines whether the traversal should stop after processing a single node. If `onlyOne` is
1746
+ * set to `true`, the traversal will return as soon as a single node is processed. If it is set to
1747
+ * `false
1748
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined}
1749
+ * startNode - The `startNode` parameter in the `_dfs` method is used to specify the starting node
1750
+ * for the depth-first search traversal. It can be provided in different forms:
1676
1751
  * @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method
1677
- * specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a
1678
- * binary tree. It can have two possible values:
1679
- * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag
1680
- * that determines whether null nodes should be included in the depth-first search traversal. If
1681
- * `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it
1682
- * is set to `false`,
1683
- * @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input
1684
- * and returns a boolean value. It is used to determine whether the left child of a node should be
1685
- * visited during the depth-first search traversal. By default, it checks if the node is truthy (not
1686
- * null or undefined
1687
- * @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an
1688
- * argument and returns a boolean value. It is used to determine whether the right child of a node
1689
- * should be visited during the depth-first search traversal. The default implementation checks if
1690
- * the node is truthy before visiting the right child
1691
- * @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an
1692
- * argument and returns a boolean value. It is used to determine whether the root node should be
1693
- * visited during the depth-first search traversal based on certain conditions. The default
1694
- * implementation checks if the node is a real node or null based
1695
- * @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an
1696
- * argument and returns a boolean value indicating whether the node should be processed during the
1697
- * depth-first search traversal. The default implementation checks if the node is a real node or null
1698
- * based on the `includeNull` flag. If `
1699
- * @returns The function `_dfs` returns an array of the return type of the callback function provided
1700
- * as input.
1752
+ * specifies whether the traversal should be done recursively or iteratively. It can have two
1753
+ * possible values:
1754
+ * @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method determines whether
1755
+ * null nodes should be included in the traversal process. If `includeNull` is set to `true`, the
1756
+ * method will consider null nodes as valid nodes to visit or process. If `includeNull` is set to
1757
+ * `false`,
1758
+ * @param shouldVisitLeft - The `shouldVisitLeft` parameter in the `_dfs` method is a function that
1759
+ * determines whether the left child of a node should be visited during the Depth-First Search
1760
+ * traversal. By default, it checks if the node is not null or undefined before visiting the left
1761
+ * child. You can customize this behavior
1762
+ * @param shouldVisitRight - The `shouldVisitRight` parameter in the `_dfs` method is a function that
1763
+ * determines whether to visit the right child node of the current node during a depth-first search
1764
+ * traversal. The default implementation of this function checks if the node is not null or undefined
1765
+ * before deciding to visit it.
1766
+ * @param shouldVisitRoot - The `shouldVisitRoot` parameter in the `_dfs` method is a function that
1767
+ * determines whether a given node should be visited during the depth-first search traversal. The
1768
+ * function takes a node as an argument and returns a boolean value indicating whether the node
1769
+ * should be visited.
1770
+ * @param shouldProcessRoot - The `shouldProcessRoot` parameter in the `_dfs` method is a function
1771
+ * that determines whether the root node should be processed during the Depth-First Search traversal.
1772
+ * It takes a node (BinaryTreeNode<K, V> | null | undefined) as input and returns a boolean value. If
1773
+ * the function
1774
+ * @returns The `_dfs` method returns an array of the return type of the provided callback function
1775
+ * `C`.
1701
1776
  */
1702
- _dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType, includeNull = false, shouldVisitLeft = node => !!node, shouldVisitRight = node => !!node, shouldVisitRoot = node => {
1777
+ _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 => {
1703
1778
  if (includeNull)
1704
1779
  return this.isRealNodeOrNull(node);
1705
1780
  return this.isRealNode(node);
@@ -1713,31 +1788,40 @@ class BinaryTree extends base_1.IterableEntryBase {
1713
1788
  if (!shouldVisitRoot(node))
1714
1789
  return;
1715
1790
  const visitLeft = () => {
1716
- if (shouldVisitLeft(node))
1791
+ if (shouldVisitLeft(node) && (node === null || node === void 0 ? void 0 : node.left) !== undefined)
1717
1792
  dfs(node === null || node === void 0 ? void 0 : node.left);
1718
1793
  };
1719
1794
  const visitRight = () => {
1720
- if (shouldVisitRight(node))
1795
+ if (shouldVisitRight(node) && (node === null || node === void 0 ? void 0 : node.right) !== undefined)
1721
1796
  dfs(node === null || node === void 0 ? void 0 : node.right);
1722
1797
  };
1723
1798
  switch (pattern) {
1724
1799
  case 'IN':
1725
1800
  visitLeft();
1726
- if (shouldProcessRoot(node))
1801
+ if (shouldProcessRoot(node)) {
1727
1802
  ans.push(callback(node));
1803
+ if (onlyOne)
1804
+ return;
1805
+ }
1728
1806
  visitRight();
1729
1807
  break;
1730
1808
  case 'PRE':
1731
- if (shouldProcessRoot(node))
1809
+ if (shouldProcessRoot(node)) {
1732
1810
  ans.push(callback(node));
1811
+ if (onlyOne)
1812
+ return;
1813
+ }
1733
1814
  visitLeft();
1734
1815
  visitRight();
1735
1816
  break;
1736
1817
  case 'POST':
1737
1818
  visitLeft();
1738
1819
  visitRight();
1739
- if (shouldProcessRoot(node))
1820
+ if (shouldProcessRoot(node)) {
1740
1821
  ans.push(callback(node));
1822
+ if (onlyOne)
1823
+ return;
1824
+ }
1741
1825
  break;
1742
1826
  }
1743
1827
  };
@@ -1766,8 +1850,11 @@ class BinaryTree extends base_1.IterableEntryBase {
1766
1850
  if (!shouldVisitRoot(cur.node))
1767
1851
  continue;
1768
1852
  if (cur.opt === common_1.DFSOperation.PROCESS) {
1769
- if (shouldProcessRoot(cur.node))
1853
+ if (shouldProcessRoot(cur.node) && cur.node !== undefined) {
1770
1854
  ans.push(callback(cur.node));
1855
+ if (onlyOne)
1856
+ return ans;
1857
+ }
1771
1858
  }
1772
1859
  else {
1773
1860
  switch (pattern) {
@@ -1913,12 +2000,12 @@ class BinaryTree extends base_1.IterableEntryBase {
1913
2000
  * Space Complexity: O(1)
1914
2001
  *
1915
2002
  * The _swapProperties function swaps key and value properties between two nodes in a binary tree.
1916
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the
2003
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the
1917
2004
  * `_swapProperties` method can be either a BTNRep object containing key and value
1918
2005
  * properties, or it can be of type R.
1919
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the
2006
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the
1920
2007
  * `_swapProperties` method represents the node or entry where the properties will be swapped with
1921
- * the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that
2008
+ * the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that
1922
2009
  * both `srcNode
1923
2010
  * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped
1924
2011
  * with the `srcNode`, or `undefined` if either `srcNode` or `destNode` is falsy.
@@ -1978,7 +2065,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1978
2065
  *
1979
2066
  * The function _setRoot sets the root node of a data structure while updating the parent reference
1980
2067
  * of the previous root node.
1981
- * @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means
2068
+ * @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means
1982
2069
  * it can either be an optional `BinaryTreeNode<K, V>` type or `null`.
1983
2070
  */
1984
2071
  _setRoot(v) {
@@ -1993,7 +2080,7 @@ class BinaryTree extends base_1.IterableEntryBase {
1993
2080
  *
1994
2081
  * The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid
1995
2082
  * predicate function for a binary tree node.
1996
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
2083
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The
1997
2084
  * `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input
1998
2085
  * parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be
1999
2086
  * used for filtering nodes in a binary tree.
@@ -2008,9 +2095,17 @@ class BinaryTree extends base_1.IterableEntryBase {
2008
2095
  return (node) => node === keyNodeEntryOrPredicate;
2009
2096
  if (this.isEntry(keyNodeEntryOrPredicate)) {
2010
2097
  const [key] = keyNodeEntryOrPredicate;
2011
- return (node) => node.key === key;
2098
+ return (node) => {
2099
+ if (!node)
2100
+ return false;
2101
+ return node.key === key;
2102
+ };
2012
2103
  }
2013
- return (node) => node.key === keyNodeEntryOrPredicate;
2104
+ return (node) => {
2105
+ if (!node)
2106
+ return false;
2107
+ return node.key === keyNodeEntryOrPredicate;
2108
+ };
2014
2109
  }
2015
2110
  /**
2016
2111
  * Time Complexity: O(1)
@@ -2033,8 +2128,8 @@ class BinaryTree extends base_1.IterableEntryBase {
2033
2128
  *
2034
2129
  * The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
2035
2130
  * entry, raw data, or null/undefined.
2036
- * @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a
2037
- * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`,
2131
+ * @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a
2132
+ * TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `,
2038
2133
  * where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and `
2039
2134
  * @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry`
2040
2135
  * parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on