doubly-linked-list-typed 1.52.6 → 1.52.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/constants/index.d.ts +4 -0
  2. package/dist/constants/index.js +8 -0
  3. package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
  4. package/dist/data-structures/base/iterable-element-base.js +10 -1
  5. package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
  6. package/dist/data-structures/base/iterable-entry-base.js +10 -10
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
  10. package/dist/data-structures/binary-tree/avl-tree.js +71 -64
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +591 -402
  13. package/dist/data-structures/binary-tree/binary-tree.js +690 -604
  14. package/dist/data-structures/binary-tree/bst.d.ts +72 -65
  15. package/dist/data-structures/binary-tree/bst.js +122 -125
  16. package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
  17. package/dist/data-structures/binary-tree/rb-tree.js +42 -39
  18. package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
  19. package/dist/data-structures/binary-tree/segment-tree.js +2 -2
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +27 -31
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +45 -43
  22. package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
  23. package/dist/data-structures/graph/abstract-graph.js +7 -4
  24. package/dist/data-structures/graph/directed-graph.d.ts +2 -2
  25. package/dist/data-structures/graph/directed-graph.js +4 -2
  26. package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
  27. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  28. package/dist/data-structures/hash/hash-map.js +1 -1
  29. package/dist/data-structures/heap/heap.js +3 -3
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  33. package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
  34. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  35. package/dist/data-structures/matrix/matrix.d.ts +2 -2
  36. package/dist/data-structures/matrix/navigator.d.ts +2 -2
  37. package/dist/data-structures/matrix/navigator.js +4 -2
  38. package/dist/data-structures/queue/deque.d.ts +3 -3
  39. package/dist/data-structures/queue/deque.js +29 -29
  40. package/dist/data-structures/queue/queue.d.ts +1 -1
  41. package/dist/data-structures/stack/stack.d.ts +2 -2
  42. package/dist/data-structures/trie/trie.d.ts +2 -2
  43. package/dist/data-structures/trie/trie.js +1 -1
  44. package/dist/index.d.ts +1 -0
  45. package/dist/index.js +1 -0
  46. package/dist/interfaces/binary-tree.d.ts +2 -2
  47. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
  48. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
  49. package/package.json +2 -2
  50. package/src/constants/index.ts +4 -0
  51. package/src/data-structures/base/iterable-element-base.ts +11 -1
  52. package/src/data-structures/base/iterable-entry-base.ts +11 -19
  53. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +46 -50
  54. package/src/data-structures/binary-tree/avl-tree.ts +68 -71
  55. package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
  56. package/src/data-structures/binary-tree/binary-tree.ts +716 -748
  57. package/src/data-structures/binary-tree/bst.ts +137 -146
  58. package/src/data-structures/binary-tree/rb-tree.ts +46 -46
  59. package/src/data-structures/binary-tree/segment-tree.ts +2 -2
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +49 -49
  61. package/src/data-structures/graph/abstract-graph.ts +6 -6
  62. package/src/data-structures/graph/directed-graph.ts +4 -4
  63. package/src/data-structures/graph/undirected-graph.ts +2 -2
  64. package/src/data-structures/hash/hash-map.ts +3 -3
  65. package/src/data-structures/heap/heap.ts +3 -3
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
  67. package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
  68. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  69. package/src/data-structures/matrix/matrix.ts +2 -2
  70. package/src/data-structures/matrix/navigator.ts +4 -4
  71. package/src/data-structures/queue/deque.ts +31 -31
  72. package/src/data-structures/queue/queue.ts +1 -1
  73. package/src/data-structures/stack/stack.ts +2 -2
  74. package/src/data-structures/trie/trie.ts +3 -3
  75. package/src/index.ts +2 -1
  76. package/src/interfaces/binary-tree.ts +3 -3
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
@@ -62,13 +62,13 @@ exports.BSTNode = BSTNode;
62
62
  class BST extends binary_tree_1.BinaryTree {
63
63
  /**
64
64
  * This is the constructor function for a Binary Search Tree class in TypeScript.
65
- * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
65
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
66
66
  * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
67
67
  * added to the binary search tree during the construction of the object.
68
68
  * @param [options] - An optional object that contains additional options for the Binary Search Tree.
69
69
  * It can include a comparator function that defines the order of the elements in the tree.
70
70
  */
71
- constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
71
+ constructor(keysOrNodesOrEntriesOrRaws = [], options) {
72
72
  super([], options);
73
73
  this._root = undefined;
74
74
  this._DEFAULT_COMPARATOR = (a, b) => {
@@ -87,8 +87,8 @@ class BST extends binary_tree_1.BinaryTree {
87
87
  if (comparator)
88
88
  this._comparator = comparator;
89
89
  }
90
- if (keysOrNodesOrEntriesOrRawElements)
91
- this.addMany(keysOrNodesOrEntriesOrRawElements);
90
+ if (keysOrNodesOrEntriesOrRaws)
91
+ this.addMany(keysOrNodesOrEntriesOrRaws);
92
92
  }
93
93
  /**
94
94
  * The function returns the root node of a tree structure.
@@ -116,20 +116,20 @@ class BST extends binary_tree_1.BinaryTree {
116
116
  * @returns a new instance of the BST class with the provided options.
117
117
  */
118
118
  createTree(options) {
119
- return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
119
+ return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
120
120
  }
121
121
  /**
122
122
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
123
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
123
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - A variable that can be of
124
124
  * type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
125
125
  * element.
126
126
  * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
127
127
  * value associated with a key in a key-value pair.
128
128
  * @returns either a NODE object or undefined.
129
129
  */
130
- keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) {
130
+ keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) {
131
131
  var _a;
132
- return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value)) !== null && _a !== void 0 ? _a : undefined;
132
+ return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value)) !== null && _a !== void 0 ? _a : undefined;
133
133
  }
134
134
  /**
135
135
  * Time Complexity: O(log n)
@@ -137,8 +137,8 @@ class BST extends binary_tree_1.BinaryTree {
137
137
  *
138
138
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
139
139
  * it doesn't exist.
140
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
141
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
140
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
141
+ * `keyOrNodeOrEntryOrRaw` can accept a value of type `R`, which represents the key, node,
142
142
  * entry, or raw element that needs to be ensured in the tree.
143
143
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
144
144
  * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
@@ -146,20 +146,28 @@ class BST extends binary_tree_1.BinaryTree {
146
146
  * @returns The method is returning either the node that was ensured or `undefined` if the node could
147
147
  * not be ensured.
148
148
  */
149
- ensureNode(keyOrNodeOrEntryOrRawElement, iterationType = this.iterationType) {
149
+ ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
150
150
  var _a;
151
- return (_a = super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType)) !== null && _a !== void 0 ? _a : undefined;
151
+ return (_a = super.ensureNode(keyOrNodeOrEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined;
152
152
  }
153
153
  /**
154
154
  * The function checks if the input is an instance of the BSTNode class.
155
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
156
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
157
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
155
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
156
+ * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
157
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
158
158
  * an instance of the `BSTNode` class.
159
159
  */
160
- isNode(keyOrNodeOrEntryOrRawElement) {
161
- return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
160
+ isNode(keyOrNodeOrEntryOrRaw) {
161
+ return keyOrNodeOrEntryOrRaw instanceof BSTNode;
162
162
  }
163
+ /**
164
+ * The function "override isKey" checks if a key is comparable based on a given comparator.
165
+ * @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
166
+ * type `K`.
167
+ * @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
168
+ * the result of the `isComparable` function with the condition `this.comparator !==
169
+ * this._DEFAULT_COMPARATOR`.
170
+ */
163
171
  isKey(key) {
164
172
  return (0, utils_1.isComparable)(key, this.comparator !== this._DEFAULT_COMPARATOR);
165
173
  }
@@ -168,22 +176,22 @@ class BST extends binary_tree_1.BinaryTree {
168
176
  * Space Complexity: O(1)
169
177
  *
170
178
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
171
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
172
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
179
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
180
+ * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
173
181
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
174
182
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
175
183
  * @returns a boolean value.
176
184
  */
177
- add(keyOrNodeOrEntryOrRawElement, value) {
178
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
185
+ add(keyOrNodeOrEntryOrRaw, value) {
186
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
179
187
  if (newNode === undefined)
180
188
  return false;
181
- if (this.root === undefined) {
189
+ if (this._root === undefined) {
182
190
  this._setRoot(newNode);
183
191
  this._size++;
184
192
  return true;
185
193
  }
186
- let current = this.root;
194
+ let current = this._root;
187
195
  while (current !== undefined) {
188
196
  if (this.comparator(current.key, newNode.key) === 0) {
189
197
  this._replaceNode(current, newNode);
@@ -214,7 +222,7 @@ class BST extends binary_tree_1.BinaryTree {
214
222
  *
215
223
  * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
216
224
  * an array indicating whether each key or node was successfully inserted.
217
- * @param keysOrNodesOrEntriesOrRawElements - An iterable containing keys, nodes, entries, or raw
225
+ * @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
218
226
  * elements to be added to the data structure.
219
227
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
220
228
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
@@ -229,38 +237,34 @@ class BST extends binary_tree_1.BinaryTree {
229
237
  * @returns The function `addMany` returns an array of booleans indicating whether each element was
230
238
  * successfully inserted into the data structure.
231
239
  */
232
- addMany(keysOrNodesOrEntriesOrRawElements, values, isBalanceAdd = true, iterationType = this.iterationType) {
240
+ addMany(keysOrNodesOrEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
233
241
  const inserted = [];
234
242
  let valuesIterator;
235
243
  if (values) {
236
244
  valuesIterator = values[Symbol.iterator]();
237
245
  }
238
246
  if (!isBalanceAdd) {
239
- for (const kve of keysOrNodesOrEntriesOrRawElements) {
247
+ for (const kve of keysOrNodesOrEntriesOrRaws) {
240
248
  const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
241
- const nn = this.add(kve, value);
242
- inserted.push(nn);
249
+ inserted.push(this.add(kve, value));
243
250
  }
244
251
  return inserted;
245
252
  }
246
253
  const realBTNExemplars = [];
247
- const isRealBTNExemplar = (kve) => {
248
- if (kve === undefined || kve === null)
249
- return false;
250
- return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
251
- };
252
- for (const kve of keysOrNodesOrEntriesOrRawElements) {
253
- isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
254
+ let i = 0;
255
+ for (const kve of keysOrNodesOrEntriesOrRaws) {
256
+ realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i });
257
+ i++;
254
258
  }
255
259
  let sorted = [];
256
- sorted = realBTNExemplars.sort((a, b) => {
260
+ sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
257
261
  let keyA, keyB;
258
262
  if (this.isEntry(a))
259
263
  keyA = a[0];
260
264
  else if (this.isRealNode(a))
261
265
  keyA = a.key;
262
- else if (this.toEntryFn) {
263
- keyA = this.toEntryFn(a)[0];
266
+ else if (this._toEntryFn) {
267
+ keyA = this._toEntryFn(a)[0];
264
268
  }
265
269
  else {
266
270
  keyA = a;
@@ -269,8 +273,8 @@ class BST extends binary_tree_1.BinaryTree {
269
273
  keyB = b[0];
270
274
  else if (this.isRealNode(b))
271
275
  keyB = b.key;
272
- else if (this.toEntryFn) {
273
- keyB = this.toEntryFn(b)[0];
276
+ else if (this._toEntryFn) {
277
+ keyB = this._toEntryFn(b)[0];
274
278
  }
275
279
  else {
276
280
  keyB = b;
@@ -284,8 +288,8 @@ class BST extends binary_tree_1.BinaryTree {
284
288
  if (arr.length === 0)
285
289
  return;
286
290
  const mid = Math.floor((arr.length - 1) / 2);
287
- const newNode = this.add(arr[mid]);
288
- inserted.push(newNode);
291
+ const { key, value, orgIndex } = arr[mid];
292
+ inserted[orgIndex] = this.add(key, value);
289
293
  _dfs(arr.slice(0, mid));
290
294
  _dfs(arr.slice(mid + 1));
291
295
  };
@@ -298,8 +302,8 @@ class BST extends binary_tree_1.BinaryTree {
298
302
  const [l, r] = popped;
299
303
  if (l <= r) {
300
304
  const m = l + Math.floor((r - l) / 2);
301
- const newNode = this.add(sorted[m]);
302
- inserted.push(newNode);
305
+ const { key, value, orgIndex } = sorted[m];
306
+ inserted[orgIndex] = this.add(key, value);
303
307
  stack.push([m + 1, r]);
304
308
  stack.push([l, m - 1]);
305
309
  }
@@ -318,54 +322,55 @@ class BST extends binary_tree_1.BinaryTree {
318
322
  * Time Complexity: O(log n)
319
323
  * Space Complexity: O(k + log n)
320
324
  *
321
- * The `getNodes` function in TypeScript retrieves nodes from a binary tree based on a given
322
- * identifier and callback function.
323
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
324
- * want to search for in the binary tree. It can be of any type that is returned by the callback
325
- * function.
326
- * @param {C} callback - The `callback` parameter is a function that takes a node as input and
327
- * returns a value. This value is used to identify the nodes that match the given identifier. The
328
- * `callback` function is optional and defaults to `this._DEFAULT_CALLBACK`.
329
- * @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
330
- * or all matching nodes. If set to true, only the first matching node will be returned. If set to
331
- * false, all matching nodes will be returned. The default value is false.
332
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
333
- * point for the search in the binary tree. It can be either a node object, a key-value pair, or an
334
- * entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
335
- * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
336
- * iteration to be performed. It can have two possible values:
337
- * @returns The method `getNodes` returns an array of `NODE` objects.
325
+ * The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
326
+ * given predicate and iteration type.
327
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
328
+ * parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
329
+ * key, a node, an entry, or a custom predicate function that determines whether a node should be
330
+ * included in the result.
331
+ * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
332
+ * determines whether to return only the first node that matches the predicate (`true`) or all nodes
333
+ * that match the predicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
334
+ * and
335
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
336
+ * `getNodes` method is used to specify the starting point for traversing the tree when searching for
337
+ * nodes that match a given predicate. It represents the root node of the subtree where the search
338
+ * should begin. If not explicitly provided, the default value for `begin
339
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
340
+ * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
341
+ * have two possible values:
342
+ * @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
338
343
  */
339
- getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
340
- if (identifier === undefined)
344
+ getNodes(predicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
345
+ if (predicate === undefined)
341
346
  return [];
342
- if (identifier === null)
347
+ if (predicate === null)
343
348
  return [];
344
349
  beginRoot = this.ensureNode(beginRoot);
345
350
  if (!beginRoot)
346
351
  return [];
347
- callback = this._ensureCallback(identifier, callback);
352
+ const callback = this._ensurePredicate(predicate);
348
353
  const ans = [];
349
354
  if (iterationType === 'RECURSIVE') {
350
355
  const dfs = (cur) => {
351
- const callbackResult = callback(cur);
352
- if (callbackResult === identifier) {
356
+ if (callback(cur)) {
353
357
  ans.push(cur);
354
358
  if (onlyOne)
355
359
  return;
356
360
  }
357
361
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
358
362
  return;
359
- // TODO potential bug
360
- if (callback === this._DEFAULT_CALLBACK) {
361
- if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
363
+ if (this.isKey(predicate)) {
364
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
362
365
  dfs(cur.left);
363
- if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
366
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
364
367
  dfs(cur.right);
365
368
  }
366
369
  else {
367
- this.isRealNode(cur.left) && dfs(cur.left);
368
- this.isRealNode(cur.right) && dfs(cur.right);
370
+ if (this.isRealNode(cur.left))
371
+ dfs(cur.left);
372
+ if (this.isRealNode(cur.right))
373
+ dfs(cur.right);
369
374
  }
370
375
  };
371
376
  dfs(beginRoot);
@@ -374,28 +379,22 @@ class BST extends binary_tree_1.BinaryTree {
374
379
  const stack = [beginRoot];
375
380
  while (stack.length > 0) {
376
381
  const cur = stack.pop();
377
- const callbackResult = callback(cur);
378
- if (callbackResult === identifier) {
382
+ if (callback(cur)) {
379
383
  ans.push(cur);
380
384
  if (onlyOne)
381
385
  return ans;
382
386
  }
383
- // TODO potential bug
384
- if (callback === this._DEFAULT_CALLBACK) {
385
- if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
387
+ if (this.isKey(predicate)) {
388
+ if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
386
389
  stack.push(cur.right);
387
- if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
390
+ if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
388
391
  stack.push(cur.left);
389
- // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
390
- // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
391
- // // @ts-ignore
392
- // if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
393
- // // @ts-ignore
394
- // if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
395
392
  }
396
393
  else {
397
- this.isRealNode(cur.right) && stack.push(cur.right);
398
- this.isRealNode(cur.left) && stack.push(cur.left);
394
+ if (this.isRealNode(cur.right))
395
+ stack.push(cur.right);
396
+ if (this.isRealNode(cur.left))
397
+ stack.push(cur.left);
399
398
  }
400
399
  }
401
400
  }
@@ -405,26 +404,25 @@ class BST extends binary_tree_1.BinaryTree {
405
404
  * Time Complexity: O(log n)
406
405
  * Space Complexity: O(1)
407
406
  *
408
- * The function `getNode` returns the first node that matches the given identifier and callback
409
- * function in a binary search tree.
410
- * @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
411
- * want to search for in the binary search tree. It can be of any type that is compatible with the
412
- * type returned by the callback function.
413
- * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
414
- * node matches the desired criteria. It should be a function that takes a node as an argument and
415
- * returns a boolean value indicating whether the node matches the criteria or not. If no callback is
416
- * provided, the default callback will be
417
- * @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
418
- * search tree. It can be either a key or a node. If it is a key, the search will start from the node
419
- * with that key. If it is a node, the search will start from that node.
420
- * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
421
- * of iteration to be performed when searching for nodes in the binary search tree. It can have one
422
- * of the following values:
423
- * @returns The method is returning a NODE object or undefined.
407
+ * This function retrieves a node based on a given predicate within a binary search tree structure.
408
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
409
+ * parameter can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, `R`, or `BTNPredicate<NODE>`.
410
+ * @param {R | BSTNKeyOrNode<K, NODE>} beginRoot - The `beginRoot` parameter in the `getNode` method
411
+ * is used to specify the starting point for searching nodes in the binary search tree. If no
412
+ * specific starting point is provided, the default value is set to `this._root`, which is the root
413
+ * node of the binary search tree.
414
+ * @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is a
415
+ * parameter that specifies the type of iteration to be used. It has a default value of
416
+ * `this.iterationType`, which means it will use the iteration type defined in the class instance if
417
+ * no value is provided when calling the method.
418
+ * @returns The `getNode` method is returning an optional binary search tree node (`OptBSTN<NODE>`).
419
+ * It is using the `getNodes` method to find the node based on the provided predicate, beginning at
420
+ * the specified root node (`beginRoot`) and using the specified iteration type. The method then
421
+ * returns the first node found or `undefined` if no node is found.
424
422
  */
425
- getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
423
+ getNode(predicate, beginRoot = this._root, iterationType = this.iterationType) {
426
424
  var _a;
427
- return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
425
+ return (_a = this.getNodes(predicate, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
428
426
  }
429
427
  /**
430
428
  * Time Complexity: O(log n)
@@ -440,7 +438,7 @@ class BST extends binary_tree_1.BinaryTree {
440
438
  * @returns The method is returning a NODE object or undefined.
441
439
  */
442
440
  getNodeByKey(key, iterationType = this.iterationType) {
443
- return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
441
+ return this.getNode(key, this._root, iterationType);
444
442
  }
445
443
  /**
446
444
  * Time complexity: O(n)
@@ -450,11 +448,11 @@ class BST extends binary_tree_1.BinaryTree {
450
448
  * the callback function.
451
449
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
452
450
  * during the depth-first search traversal. It is an optional parameter and defaults to
453
- * `this._DEFAULT_CALLBACK`. The type `C` represents the type of the callback function.
451
+ * `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
454
452
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
455
453
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
456
454
  * take one of the following values:
457
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
455
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
458
456
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
459
457
  * node entry. If not specified, the default value is the root of the tree.
460
458
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -462,7 +460,7 @@ class BST extends binary_tree_1.BinaryTree {
462
460
  * following values:
463
461
  * @returns The method is returning an array of the return type of the callback function.
464
462
  */
465
- dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = this.iterationType) {
463
+ dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType) {
466
464
  return super.dfs(callback, pattern, beginRoot, iterationType);
467
465
  }
468
466
  /**
@@ -474,7 +472,7 @@ class BST extends binary_tree_1.BinaryTree {
474
472
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
475
473
  * visited during the breadth-first search. It should take a single argument, which is the current
476
474
  * node being visited, and it can return a value of any type.
477
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
475
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
478
476
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
479
477
  * object. If no value is provided, the default value is the root of the tree.
480
478
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -482,7 +480,7 @@ class BST extends binary_tree_1.BinaryTree {
482
480
  * the following values:
483
481
  * @returns an array of the return type of the callback function.
484
482
  */
485
- bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
483
+ bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
486
484
  return super.bfs(callback, beginRoot, iterationType, false);
487
485
  }
488
486
  /**
@@ -494,7 +492,7 @@ class BST extends binary_tree_1.BinaryTree {
494
492
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
495
493
  * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
496
494
  * tree during the iteration process.
497
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
495
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
498
496
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
499
497
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
500
498
  * value is provided, the root of
@@ -503,7 +501,7 @@ class BST extends binary_tree_1.BinaryTree {
503
501
  * @returns The method is returning a two-dimensional array of the return type of the callback
504
502
  * function.
505
503
  */
506
- listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
504
+ listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
507
505
  return super.listLevels(callback, beginRoot, iterationType, false);
508
506
  }
509
507
  /**
@@ -518,7 +516,7 @@ class BST extends binary_tree_1.BinaryTree {
518
516
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
519
517
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
520
518
  * 0, or 1, where:
521
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
519
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
522
520
  * the binary tree that you want to start traversing from. It can be specified either by providing
523
521
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
524
522
  * `targetNode` is provided,
@@ -527,12 +525,12 @@ class BST extends binary_tree_1.BinaryTree {
527
525
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
528
526
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
529
527
  */
530
- lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
528
+ lesserOrGreaterTraverse(callback = this._DEFAULT_BTN_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
531
529
  const targetNodeEnsured = this.ensureNode(targetNode);
532
530
  const ans = [];
533
- if (!targetNodeEnsured)
531
+ if (!this._root)
534
532
  return ans;
535
- if (!this.root)
533
+ if (!targetNodeEnsured)
536
534
  return ans;
537
535
  const targetKey = targetNodeEnsured.key;
538
536
  if (iterationType === 'RECURSIVE') {
@@ -545,11 +543,11 @@ class BST extends binary_tree_1.BinaryTree {
545
543
  if (this.isRealNode(cur.right))
546
544
  dfs(cur.right);
547
545
  };
548
- dfs(this.root);
546
+ dfs(this._root);
549
547
  return ans;
550
548
  }
551
549
  else {
552
- const queue = new queue_1.Queue([this.root]);
550
+ const queue = new queue_1.Queue([this._root]);
553
551
  while (queue.size > 0) {
554
552
  const cur = queue.shift();
555
553
  if (this.isRealNode(cur)) {
@@ -626,8 +624,7 @@ class BST extends binary_tree_1.BinaryTree {
626
624
  * @returns a boolean value.
627
625
  */
628
626
  isAVLBalanced(iterationType = this.iterationType) {
629
- var _a, _b;
630
- if (!this.root)
627
+ if (!this._root)
631
628
  return true;
632
629
  let balanced = true;
633
630
  if (iterationType === 'RECURSIVE') {
@@ -639,11 +636,11 @@ class BST extends binary_tree_1.BinaryTree {
639
636
  balanced = false;
640
637
  return Math.max(leftHeight, rightHeight) + 1;
641
638
  };
642
- _height(this.root);
639
+ _height(this._root);
643
640
  }
644
641
  else {
645
642
  const stack = [];
646
- let node = this.root, last = undefined;
643
+ let node = this._root, last = undefined;
647
644
  const depths = new Map();
648
645
  while (stack.length > 0 || node) {
649
646
  if (node) {
@@ -655,8 +652,8 @@ class BST extends binary_tree_1.BinaryTree {
655
652
  if (!node.right || last === node.right) {
656
653
  node = stack.pop();
657
654
  if (node) {
658
- const left = node.left ? ((_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1) : -1;
659
- const right = node.right ? ((_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1) : -1;
655
+ const left = node.left ? depths.get(node.left) : -1;
656
+ const right = node.right ? depths.get(node.right) : -1;
660
657
  if (Math.abs(left - right) > 1)
661
658
  return false;
662
659
  depths.set(node, 1 + Math.max(left, right));
@@ -1,5 +1,4 @@
1
- import type { BinaryTreeDeleteResult, BTNCallback, BTNKeyOrNodeOrEntry, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
2
- import { BTNEntry } from '../../types';
1
+ import type { BinaryTreeDeleteResult, BTNKeyOrNodeOrEntry, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested, BTNEntry } from '../../types';
3
2
  import { BST, BSTNode } from './bst';
4
3
  import { IBinaryTree } from '../../interfaces';
5
4
  export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
@@ -30,7 +29,7 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
30
29
  export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
31
30
  /**
32
31
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
33
- * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
32
+ * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
34
33
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
35
34
  * initialize the RBTree with the provided elements.
36
35
  * @param [options] - The `options` parameter is an optional object that can be passed to the
@@ -38,7 +37,7 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
38
37
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
39
38
  * depend on the implementation
40
39
  */
41
- constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
40
+ constructor(keysOrNodesOrEntriesOrRaws?: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
42
41
  protected _root: NODE | undefined;
43
42
  /**
44
43
  * The function returns the root node of a tree or undefined if there is no root.
@@ -72,12 +71,12 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
72
71
  * Space Complexity: O(1)
73
72
  *
74
73
  * The function checks if the input is an instance of the RedBlackTreeNode class.
75
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
76
- * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
77
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
74
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
75
+ * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
76
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
78
77
  * an instance of the `RedBlackTreeNode` class.
79
78
  */
80
- isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
79
+ isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE;
81
80
  /**
82
81
  * Time Complexity: O(1)
83
82
  * Space Complexity: O(1)
@@ -92,8 +91,8 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
92
91
  *
93
92
  * The function adds a new node to a binary search tree and returns true if the node was successfully
94
93
  * added.
95
- * @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
96
- * `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
94
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
95
+ * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
97
96
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
98
97
  * the key in the data structure. It represents the value that you want to add or update in the data
99
98
  * structure.
@@ -101,24 +100,22 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
101
100
  * the method returns true. If the node already exists and its value is updated, the method also
102
101
  * returns true. If the node cannot be added or updated, the method returns false.
103
102
  */
104
- add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
103
+ add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean;
105
104
  /**
106
105
  * Time Complexity: O(log n)
107
106
  * Space Complexity: O(1)
108
107
  *
109
- * The function overrides the delete method of a binary tree data structure, allowing for the
110
- * deletion of a node and maintaining the balance of the tree.
111
- * @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
112
- * that identifies the node to be deleted from the binary tree. It can be of any type that is
113
- * returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
114
- * delete.
115
- * @param {C} callback - The `callback` parameter is a function that is used to determine the
116
- * equality of nodes in the binary tree. It is optional and has a default value of
117
- * `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
118
- * that extends the `BTNCallback
119
- * @returns an array of BinaryTreeDeleteResult<NODE> objects.
120
- */
121
- delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<NODE>[];
108
+ * The function overrides the delete method in a binary tree data structure to remove a node based on
109
+ * a given predicate and maintain the binary search tree properties.
110
+ * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
111
+ * parameter in the `override delete` method is used to specify the condition or key based on which a
112
+ * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
113
+ * function that determines which node(s) should be deleted.
114
+ * @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
115
+ * objects. Each object in the array contains information about the deleted node and whether
116
+ * balancing is needed.
117
+ */
118
+ delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
122
119
  /**
123
120
  * Time Complexity: O(1)
124
121
  * Space Complexity: O(1)