doubly-linked-list-typed 1.52.9 → 1.53.1

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 (27) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +21 -21
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
  4. package/dist/data-structures/binary-tree/avl-tree.js +29 -27
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
  6. package/dist/data-structures/binary-tree/binary-tree.js +376 -265
  7. package/dist/data-structures/binary-tree/bst.d.ts +56 -56
  8. package/dist/data-structures/binary-tree/bst.js +108 -78
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/rb-tree.js +42 -36
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +59 -49
  13. package/dist/data-structures/trie/trie.js +3 -3
  14. package/dist/interfaces/binary-tree.d.ts +5 -5
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
  17. package/package.json +2 -2
  18. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +60 -54
  19. package/src/data-structures/binary-tree/avl-tree.ts +32 -35
  20. package/src/data-structures/binary-tree/binary-tree.ts +440 -360
  21. package/src/data-structures/binary-tree/bst.ts +144 -113
  22. package/src/data-structures/binary-tree/rb-tree.ts +44 -43
  23. package/src/data-structures/binary-tree/tree-multi-map.ts +57 -61
  24. package/src/data-structures/trie/trie.ts +3 -3
  25. package/src/interfaces/binary-tree.ts +6 -6
  26. package/src/types/data-structures/binary-tree/binary-tree.ts +13 -14
  27. package/src/types/data-structures/binary-tree/bst.ts +3 -3
@@ -20,7 +20,7 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
20
20
  }
21
21
  /**
22
22
  * The function sets the left child of a node and updates the parent reference of the child.
23
- * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
23
+ * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
24
24
  * instance of the `NODE` class or `undefined`.
25
25
  */
26
26
  set left(v) {
@@ -39,7 +39,7 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
39
39
  }
40
40
  /**
41
41
  * The function sets the right child of a node and updates the parent reference of the child.
42
- * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
42
+ * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
43
43
  * `NODE` object or `undefined`.
44
44
  */
45
45
  set right(v) {
@@ -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 keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
65
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` 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(keysOrNodesOrEntriesOrRaws = [], options) {
71
+ constructor(keysNodesEntriesOrRaws = [], 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 (keysOrNodesOrEntriesOrRaws)
91
- this.addMany(keysOrNodesOrEntriesOrRaws);
90
+ if (keysNodesEntriesOrRaws)
91
+ this.addMany(keysNodesEntriesOrRaws);
92
92
  }
93
93
  /**
94
94
  * The function returns the root node of a tree structure.
@@ -106,7 +106,7 @@ class BST extends binary_tree_1.BinaryTree {
106
106
  * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
107
107
  */
108
108
  createNode(key, value) {
109
- return new BSTNode(key, value);
109
+ return new BSTNode(key, this._isMapMode ? undefined : value);
110
110
  }
111
111
  /**
112
112
  * The function creates a new binary search tree with the specified options.
@@ -116,20 +116,22 @@ 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, toEntryFn: this._toEntryFn }, options));
119
+ return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, 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 {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - A variable that can be of
124
- * type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
123
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
124
+ * type R or BTNRep<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(keyOrNodeOrEntryOrRaw, value) {
131
- var _a;
132
- return (_a = super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value)) !== null && _a !== void 0 ? _a : undefined;
130
+ keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
131
+ const [node, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
132
+ if (node === null)
133
+ return [undefined, undefined];
134
+ return [node, value !== null && value !== void 0 ? value : entryValue];
133
135
  }
134
136
  /**
135
137
  * Time Complexity: O(log n)
@@ -137,8 +139,8 @@ class BST extends binary_tree_1.BinaryTree {
137
139
  *
138
140
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
139
141
  * it doesn't exist.
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
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
143
+ * `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
142
144
  * entry, or raw element that needs to be ensured in the tree.
143
145
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
144
146
  * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
@@ -146,19 +148,19 @@ class BST extends binary_tree_1.BinaryTree {
146
148
  * @returns The method is returning either the node that was ensured or `undefined` if the node could
147
149
  * not be ensured.
148
150
  */
149
- ensureNode(keyOrNodeOrEntryOrRaw, iterationType = this.iterationType) {
151
+ ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
150
152
  var _a;
151
- return (_a = super.ensureNode(keyOrNodeOrEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined;
153
+ return (_a = super.ensureNode(keyNodeEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined;
152
154
  }
153
155
  /**
154
156
  * The function checks if the input is an instance of the BSTNode class.
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
157
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
158
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
159
+ * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
158
160
  * an instance of the `BSTNode` class.
159
161
  */
160
- isNode(keyOrNodeOrEntryOrRaw) {
161
- return keyOrNodeOrEntryOrRaw instanceof BSTNode;
162
+ isNode(keyNodeEntryOrRaw) {
163
+ return keyNodeEntryOrRaw instanceof BSTNode;
162
164
  }
163
165
  /**
164
166
  * The function "override isKey" checks if a key is comparable based on a given comparator.
@@ -176,18 +178,20 @@ class BST extends binary_tree_1.BinaryTree {
176
178
  * Space Complexity: O(1)
177
179
  *
178
180
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
179
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
180
- * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
181
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
182
+ * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
181
183
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
182
184
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
183
185
  * @returns a boolean value.
184
186
  */
185
- add(keyOrNodeOrEntryOrRaw, value) {
186
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
187
+ add(keyNodeEntryOrRaw, value) {
188
+ const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
187
189
  if (newNode === undefined)
188
190
  return false;
189
191
  if (this._root === undefined) {
190
192
  this._setRoot(newNode);
193
+ if (this._isMapMode)
194
+ this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
191
195
  this._size++;
192
196
  return true;
193
197
  }
@@ -195,11 +199,15 @@ class BST extends binary_tree_1.BinaryTree {
195
199
  while (current !== undefined) {
196
200
  if (this.comparator(current.key, newNode.key) === 0) {
197
201
  this._replaceNode(current, newNode);
202
+ if (this._isMapMode)
203
+ this._setValue(current.key, newValue);
198
204
  return true;
199
205
  }
200
206
  else if (this.comparator(current.key, newNode.key) > 0) {
201
207
  if (current.left === undefined) {
202
208
  current.left = newNode;
209
+ if (this._isMapMode)
210
+ this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
203
211
  this._size++;
204
212
  return true;
205
213
  }
@@ -208,6 +216,8 @@ class BST extends binary_tree_1.BinaryTree {
208
216
  else {
209
217
  if (current.right === undefined) {
210
218
  current.right = newNode;
219
+ if (this._isMapMode)
220
+ this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
211
221
  this._size++;
212
222
  return true;
213
223
  }
@@ -222,7 +232,7 @@ class BST extends binary_tree_1.BinaryTree {
222
232
  *
223
233
  * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
224
234
  * an array indicating whether each key or node was successfully inserted.
225
- * @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
235
+ * @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
226
236
  * elements to be added to the data structure.
227
237
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
228
238
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
@@ -237,14 +247,14 @@ class BST extends binary_tree_1.BinaryTree {
237
247
  * @returns The function `addMany` returns an array of booleans indicating whether each element was
238
248
  * successfully inserted into the data structure.
239
249
  */
240
- addMany(keysOrNodesOrEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
250
+ addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
241
251
  const inserted = [];
242
252
  let valuesIterator;
243
253
  if (values) {
244
254
  valuesIterator = values[Symbol.iterator]();
245
255
  }
246
256
  if (!isBalanceAdd) {
247
- for (const kve of keysOrNodesOrEntriesOrRaws) {
257
+ for (const kve of keysNodesEntriesOrRaws) {
248
258
  const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
249
259
  inserted.push(this.add(kve, value));
250
260
  }
@@ -252,7 +262,7 @@ class BST extends binary_tree_1.BinaryTree {
252
262
  }
253
263
  const realBTNExemplars = [];
254
264
  let i = 0;
255
- for (const kve of keysOrNodesOrEntriesOrRaws) {
265
+ for (const kve of keysNodesEntriesOrRaws) {
256
266
  realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i });
257
267
  i++;
258
268
  }
@@ -323,33 +333,33 @@ class BST extends binary_tree_1.BinaryTree {
323
333
  * Space Complexity: O(k + log n)
324
334
  *
325
335
  * 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`
336
+ * given keyNodeEntryRawOrPredicate and iteration type.
337
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
328
338
  * 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
339
+ * key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
330
340
  * included in the result.
331
341
  * @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
342
+ * determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
343
+ * that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
334
344
  * and
335
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
345
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
336
346
  * `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
347
+ * nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
338
348
  * should begin. If not explicitly provided, the default value for `begin
339
349
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
340
350
  * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
341
351
  * have two possible values:
342
- * @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
352
+ * @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
343
353
  */
344
- getNodes(predicate, onlyOne = false, beginRoot = this._root, iterationType = this.iterationType) {
345
- if (predicate === undefined)
354
+ getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
355
+ if (keyNodeEntryRawOrPredicate === undefined)
346
356
  return [];
347
- if (predicate === null)
357
+ if (keyNodeEntryRawOrPredicate === null)
348
358
  return [];
349
- beginRoot = this.ensureNode(beginRoot);
350
- if (!beginRoot)
359
+ startNode = this.ensureNode(startNode);
360
+ if (!startNode)
351
361
  return [];
352
- const callback = this._ensurePredicate(predicate);
362
+ const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
353
363
  const ans = [];
354
364
  if (iterationType === 'RECURSIVE') {
355
365
  const dfs = (cur) => {
@@ -360,10 +370,17 @@ class BST extends binary_tree_1.BinaryTree {
360
370
  }
361
371
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
362
372
  return;
363
- if (this.isKey(predicate)) {
364
- if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
373
+ if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
374
+ const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
375
+ if (this.isRealNode(cur.left) &&
376
+ benchmarkKey !== null &&
377
+ benchmarkKey !== undefined &&
378
+ this.comparator(cur.key, benchmarkKey) > 0)
365
379
  dfs(cur.left);
366
- if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
380
+ if (this.isRealNode(cur.right) &&
381
+ benchmarkKey !== null &&
382
+ benchmarkKey !== undefined &&
383
+ this.comparator(cur.key, benchmarkKey) < 0)
367
384
  dfs(cur.right);
368
385
  }
369
386
  else {
@@ -373,10 +390,10 @@ class BST extends binary_tree_1.BinaryTree {
373
390
  dfs(cur.right);
374
391
  }
375
392
  };
376
- dfs(beginRoot);
393
+ dfs(startNode);
377
394
  }
378
395
  else {
379
- const stack = [beginRoot];
396
+ const stack = [startNode];
380
397
  while (stack.length > 0) {
381
398
  const cur = stack.pop();
382
399
  if (callback(cur)) {
@@ -384,10 +401,17 @@ class BST extends binary_tree_1.BinaryTree {
384
401
  if (onlyOne)
385
402
  return ans;
386
403
  }
387
- if (this.isKey(predicate)) {
388
- if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0)
404
+ if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
405
+ const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
406
+ if (this.isRealNode(cur.right) &&
407
+ benchmarkKey !== null &&
408
+ benchmarkKey !== undefined &&
409
+ this.comparator(cur.key, benchmarkKey) < 0)
389
410
  stack.push(cur.right);
390
- if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0)
411
+ if (this.isRealNode(cur.left) &&
412
+ benchmarkKey !== null &&
413
+ benchmarkKey !== undefined &&
414
+ this.comparator(cur.key, benchmarkKey) > 0)
391
415
  stack.push(cur.left);
392
416
  }
393
417
  else {
@@ -404,10 +428,10 @@ class BST extends binary_tree_1.BinaryTree {
404
428
  * Time Complexity: O(log n)
405
429
  * Space Complexity: O(1)
406
430
  *
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
431
+ * This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
432
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
433
+ * parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
434
+ * @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
411
435
  * is used to specify the starting point for searching nodes in the binary search tree. If no
412
436
  * specific starting point is provided, the default value is set to `this._root`, which is the root
413
437
  * node of the binary search tree.
@@ -415,14 +439,14 @@ class BST extends binary_tree_1.BinaryTree {
415
439
  * parameter that specifies the type of iteration to be used. It has a default value of
416
440
  * `this.iterationType`, which means it will use the iteration type defined in the class instance if
417
441
  * 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
442
+ * @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
443
+ * It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
444
+ * the specified root node (`startNode`) and using the specified iteration type. The method then
421
445
  * returns the first node found or `undefined` if no node is found.
422
446
  */
423
- getNode(predicate, beginRoot = this._root, iterationType = this.iterationType) {
447
+ getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
424
448
  var _a;
425
- return (_a = this.getNodes(predicate, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
449
+ return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
426
450
  }
427
451
  /**
428
452
  * Time Complexity: O(log n)
@@ -448,11 +472,11 @@ class BST extends binary_tree_1.BinaryTree {
448
472
  * the callback function.
449
473
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
450
474
  * during the depth-first search traversal. It is an optional parameter and defaults to
451
- * `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
475
+ * `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
452
476
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
453
477
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
454
478
  * take one of the following values:
455
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
479
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
456
480
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
457
481
  * node entry. If not specified, the default value is the root of the tree.
458
482
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -460,8 +484,8 @@ class BST extends binary_tree_1.BinaryTree {
460
484
  * following values:
461
485
  * @returns The method is returning an array of the return type of the callback function.
462
486
  */
463
- dfs(callback = this._DEFAULT_BTN_CALLBACK, pattern = 'IN', beginRoot = this._root, iterationType = this.iterationType) {
464
- return super.dfs(callback, pattern, beginRoot, iterationType);
487
+ dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) {
488
+ return super.dfs(callback, pattern, startNode, iterationType);
465
489
  }
466
490
  /**
467
491
  * Time complexity: O(n)
@@ -472,7 +496,7 @@ class BST extends binary_tree_1.BinaryTree {
472
496
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
473
497
  * visited during the breadth-first search. It should take a single argument, which is the current
474
498
  * node being visited, and it can return a value of any type.
475
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
499
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
476
500
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
477
501
  * object. If no value is provided, the default value is the root of the tree.
478
502
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -480,8 +504,8 @@ class BST extends binary_tree_1.BinaryTree {
480
504
  * the following values:
481
505
  * @returns an array of the return type of the callback function.
482
506
  */
483
- bfs(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
484
- return super.bfs(callback, beginRoot, iterationType, false);
507
+ bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
508
+ return super.bfs(callback, startNode, iterationType, false);
485
509
  }
486
510
  /**
487
511
  * Time complexity: O(n)
@@ -490,9 +514,9 @@ class BST extends binary_tree_1.BinaryTree {
490
514
  * The function overrides the listLevels method from the superclass and returns an array of arrays
491
515
  * containing the results of the callback function applied to each level of the tree.
492
516
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
493
- * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
517
+ * `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
494
518
  * tree during the iteration process.
495
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
519
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
496
520
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
497
521
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
498
522
  * value is provided, the root of
@@ -501,8 +525,8 @@ class BST extends binary_tree_1.BinaryTree {
501
525
  * @returns The method is returning a two-dimensional array of the return type of the callback
502
526
  * function.
503
527
  */
504
- listLevels(callback = this._DEFAULT_BTN_CALLBACK, beginRoot = this._root, iterationType = this.iterationType) {
505
- return super.listLevels(callback, beginRoot, iterationType, false);
528
+ listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
529
+ return super.listLevels(callback, startNode, iterationType, false);
506
530
  }
507
531
  /**
508
532
  * Time complexity: O(n)
@@ -516,7 +540,7 @@ class BST extends binary_tree_1.BinaryTree {
516
540
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
517
541
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
518
542
  * 0, or 1, where:
519
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
543
+ * @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
520
544
  * the binary tree that you want to start traversing from. It can be specified either by providing
521
545
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
522
546
  * `targetNode` is provided,
@@ -525,7 +549,7 @@ class BST extends binary_tree_1.BinaryTree {
525
549
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
526
550
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
527
551
  */
528
- lesserOrGreaterTraverse(callback = this._DEFAULT_BTN_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
552
+ lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
529
553
  const targetNodeEnsured = this.ensureNode(targetNode);
530
554
  const ans = [];
531
555
  if (!this._root)
@@ -577,7 +601,7 @@ class BST extends binary_tree_1.BinaryTree {
577
601
  */
578
602
  perfectlyBalance(iterationType = this.iterationType) {
579
603
  const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
580
- this.clear();
604
+ this._clearNodes();
581
605
  if (sorted.length < 1)
582
606
  return false;
583
607
  if (iterationType === 'RECURSIVE') {
@@ -586,7 +610,10 @@ class BST extends binary_tree_1.BinaryTree {
586
610
  return;
587
611
  const m = l + Math.floor((r - l) / 2);
588
612
  const midNode = sorted[m];
589
- this.add([midNode.key, midNode.value]);
613
+ if (this._isMapMode)
614
+ this.add(midNode.key);
615
+ else
616
+ this.add([midNode.key, midNode.value]);
590
617
  buildBalanceBST(l, m - 1);
591
618
  buildBalanceBST(m + 1, r);
592
619
  };
@@ -602,7 +629,10 @@ class BST extends binary_tree_1.BinaryTree {
602
629
  if (l <= r) {
603
630
  const m = l + Math.floor((r - l) / 2);
604
631
  const midNode = sorted[m];
605
- this.add([midNode.key, midNode.value]);
632
+ if (this._isMapMode)
633
+ this.add(midNode.key);
634
+ else
635
+ this.add([midNode.key, midNode.value]);
606
636
  stack.push([m + 1, r]);
607
637
  stack.push([l, m - 1]);
608
638
  }
@@ -678,7 +708,7 @@ class BST extends binary_tree_1.BinaryTree {
678
708
  /**
679
709
  * The function sets the root of a tree-like structure and updates the parent property of the new
680
710
  * root.
681
- * @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
711
+ * @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
682
712
  */
683
713
  _setRoot(v) {
684
714
  if (v) {
@@ -1,4 +1,4 @@
1
- import type { BinaryTreeDeleteResult, BTNKeyOrNodeOrEntry, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested, BTNEntry } from '../../types';
1
+ import type { BinaryTreeDeleteResult, BTNRep, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
2
2
  import { BST, BSTNode } from './bst';
3
3
  import { IBinaryTree } from '../../interfaces';
4
4
  export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
@@ -26,10 +26,10 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
26
26
  */
27
27
  set color(value: RBTNColor);
28
28
  }
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> {
29
+ export declare class RedBlackTree<K = any, V = any, R = object, 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> {
30
30
  /**
31
31
  * This is the constructor function for a Red-Black Tree data structure in TypeScript.
32
- * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
32
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
33
33
  * iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
34
34
  * initialize the RBTree with the provided elements.
35
35
  * @param [options] - The `options` parameter is an optional object that can be passed to the
@@ -37,7 +37,7 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
37
37
  * configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
38
38
  * depend on the implementation
39
39
  */
40
- constructor(keysOrNodesOrEntriesOrRaws?: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
40
+ constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
41
41
  protected _root: NODE | undefined;
42
42
  /**
43
43
  * The function returns the root node of a tree or undefined if there is no root.
@@ -71,12 +71,12 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
71
71
  * Space Complexity: O(1)
72
72
  *
73
73
  * The function checks if the input is an instance of the RedBlackTreeNode class.
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
74
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
75
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
76
+ * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
77
77
  * an instance of the `RedBlackTreeNode` class.
78
78
  */
79
- isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE;
79
+ isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
80
80
  /**
81
81
  * Time Complexity: O(1)
82
82
  * Space Complexity: O(1)
@@ -91,8 +91,8 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
91
91
  *
92
92
  * The function adds a new node to a binary search tree and returns true if the node was successfully
93
93
  * added.
94
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
95
- * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
94
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
95
+ * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
96
96
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
97
97
  * the key in the data structure. It represents the value that you want to add or update in the data
98
98
  * structure.
@@ -100,14 +100,14 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
100
100
  * the method returns true. If the node already exists and its value is updated, the method also
101
101
  * returns true. If the node cannot be added or updated, the method returns false.
102
102
  */
103
- add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean;
103
+ add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean;
104
104
  /**
105
105
  * Time Complexity: O(log n)
106
106
  * Space Complexity: O(1)
107
107
  *
108
108
  * The function overrides the delete method in a binary tree data structure to remove a node based on
109
109
  * a given predicate and maintain the binary search tree properties.
110
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
110
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
111
111
  * parameter in the `override delete` method is used to specify the condition or key based on which a
112
112
  * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
113
113
  * function that determines which node(s) should be deleted.
@@ -115,7 +115,7 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
115
115
  * objects. Each object in the array contains information about the deleted node and whether
116
116
  * balancing is needed.
117
117
  */
118
- delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
118
+ delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
119
119
  /**
120
120
  * Time Complexity: O(1)
121
121
  * Space Complexity: O(1)