linked-list-typed 1.52.9 → 1.53.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 (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 +63 -46
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
  4. package/dist/data-structures/binary-tree/avl-tree.js +28 -26
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
  6. package/dist/data-structures/binary-tree/binary-tree.js +375 -264
  7. package/dist/data-structures/binary-tree/bst.d.ts +56 -56
  8. package/dist/data-structures/binary-tree/bst.js +105 -77
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/rb-tree.js +35 -33
  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 +58 -48
  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 +59 -53
  19. package/src/data-structures/binary-tree/avl-tree.ts +31 -34
  20. package/src/data-structures/binary-tree/binary-tree.ts +439 -359
  21. package/src/data-structures/binary-tree/bst.ts +142 -112
  22. package/src/data-structures/binary-tree/rb-tree.ts +37 -41
  23. package/src/data-structures/binary-tree/tree-multi-map.ts +56 -60
  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 +14 -15
  27. package/src/types/data-structures/binary-tree/bst.ts +4 -4
@@ -7,18 +7,17 @@
7
7
  */
8
8
  import type {
9
9
  BSTNested,
10
- BSTNKeyOrNode,
11
10
  BSTNodeNested,
11
+ BSTNOptKeyOrNode,
12
12
  BSTOptions,
13
- BTNCallback,
14
- BTNEntry,
15
- BTNKeyOrNodeOrEntry,
16
- BTNPredicate,
13
+ BTNRep,
17
14
  Comparator,
18
15
  CP,
19
16
  DFSOrderPattern,
20
17
  IterationType,
21
- OptBSTN
18
+ NodeCallback,
19
+ NodePredicate,
20
+ OptNode
22
21
  } from '../../types';
23
22
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
24
23
  import { IBinaryTree } from '../../interfaces';
@@ -45,16 +44,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
45
44
  * The function returns the value of the `_left` property.
46
45
  * @returns The `_left` property of the current object is being returned.
47
46
  */
48
- override get left(): OptBSTN<NODE> {
47
+ override get left(): OptNode<NODE> {
49
48
  return this._left;
50
49
  }
51
50
 
52
51
  /**
53
52
  * The function sets the left child of a node and updates the parent reference of the child.
54
- * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
53
+ * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
55
54
  * instance of the `NODE` class or `undefined`.
56
55
  */
57
- override set left(v: OptBSTN<NODE>) {
56
+ override set left(v: OptNode<NODE>) {
58
57
  if (v) {
59
58
  v.parent = this as unknown as NODE;
60
59
  }
@@ -68,16 +67,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
68
67
  * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
69
68
  * `undefined`.
70
69
  */
71
- override get right(): OptBSTN<NODE> {
70
+ override get right(): OptNode<NODE> {
72
71
  return this._right;
73
72
  }
74
73
 
75
74
  /**
76
75
  * The function sets the right child of a node and updates the parent reference of the child.
77
- * @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
76
+ * @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
78
77
  * `NODE` object or `undefined`.
79
78
  */
80
- override set right(v: OptBSTN<NODE>) {
79
+ override set right(v: OptNode<NODE>) {
81
80
  if (v) {
82
81
  v.parent = this as unknown as NODE;
83
82
  }
@@ -97,7 +96,7 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
97
96
  export class BST<
98
97
  K = any,
99
98
  V = any,
100
- R = BTNEntry<K, V>,
99
+ R = object,
101
100
  NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
102
101
  TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
103
102
  >
@@ -106,16 +105,13 @@ export class BST<
106
105
  {
107
106
  /**
108
107
  * This is the constructor function for a Binary Search Tree class in TypeScript.
109
- * @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
108
+ * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
110
109
  * iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
111
110
  * added to the binary search tree during the construction of the object.
112
111
  * @param [options] - An optional object that contains additional options for the Binary Search Tree.
113
112
  * It can include a comparator function that defines the order of the elements in the tree.
114
113
  */
115
- constructor(
116
- keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
117
- options?: BSTOptions<K, V, R>
118
- ) {
114
+ constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: BSTOptions<K, V, R>) {
119
115
  super([], options);
120
116
 
121
117
  if (options) {
@@ -123,7 +119,7 @@ export class BST<
123
119
  if (comparator) this._comparator = comparator;
124
120
  }
125
121
 
126
- if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
122
+ if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
127
123
  }
128
124
 
129
125
  protected override _root?: NODE = undefined;
@@ -132,7 +128,7 @@ export class BST<
132
128
  * The function returns the root node of a tree structure.
133
129
  * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
134
130
  */
135
- override get root(): OptBSTN<NODE> {
131
+ override get root(): OptNode<NODE> {
136
132
  return this._root;
137
133
  }
138
134
 
@@ -158,6 +154,7 @@ export class BST<
158
154
  override createTree(options?: BSTOptions<K, V, R>): TREE {
159
155
  return new BST<K, V, R, NODE, TREE>([], {
160
156
  iterationType: this.iterationType,
157
+ isMapMode: this._isMapMode,
161
158
  comparator: this._comparator,
162
159
  toEntryFn: this._toEntryFn,
163
160
  ...options
@@ -166,18 +163,20 @@ export class BST<
166
163
 
167
164
  /**
168
165
  * The function overrides a method and converts a key, value pair or entry or raw element to a node.
169
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - A variable that can be of
170
- * type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
166
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
167
+ * type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
171
168
  * element.
172
169
  * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
173
170
  * value associated with a key in a key-value pair.
174
171
  * @returns either a NODE object or undefined.
175
172
  */
176
- override keyValueOrEntryOrRawElementToNode(
177
- keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
173
+ override keyValueNodeEntryRawToNodeAndValue(
174
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
178
175
  value?: V
179
- ): OptBSTN<NODE> {
180
- return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) ?? undefined;
176
+ ): [OptNode<NODE>, V | undefined] {
177
+ const [node, tValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
178
+ if (node === null) return [undefined, undefined];
179
+ return [node, tValue ?? value];
181
180
  }
182
181
 
183
182
  /**
@@ -186,8 +185,8 @@ export class BST<
186
185
  *
187
186
  * The function ensures the existence of a node in a data structure and returns it, or undefined if
188
187
  * it doesn't exist.
189
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
190
- * `keyOrNodeOrEntryOrRaw` can accept a value of type `R`, which represents the key, node,
188
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
189
+ * `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
191
190
  * entry, or raw element that needs to be ensured in the tree.
192
191
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
193
192
  * parameter that specifies the type of iteration to be used when ensuring a node. It has a default
@@ -196,21 +195,21 @@ export class BST<
196
195
  * not be ensured.
197
196
  */
198
197
  override ensureNode(
199
- keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
198
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
200
199
  iterationType: IterationType = this.iterationType
201
- ): OptBSTN<NODE> {
202
- return super.ensureNode(keyOrNodeOrEntryOrRaw, iterationType) ?? undefined;
200
+ ): OptNode<NODE> {
201
+ return super.ensureNode(keyNodeEntryOrRaw, iterationType) ?? undefined;
203
202
  }
204
203
 
205
204
  /**
206
205
  * The function checks if the input is an instance of the BSTNode class.
207
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
208
- * `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
209
- * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
206
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
207
+ * `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
208
+ * @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
210
209
  * an instance of the `BSTNode` class.
211
210
  */
212
- override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
213
- return keyOrNodeOrEntryOrRaw instanceof BSTNode;
211
+ override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
212
+ return keyNodeEntryOrRaw instanceof BSTNode;
214
213
  }
215
214
 
216
215
  /**
@@ -230,18 +229,19 @@ export class BST<
230
229
  * Space Complexity: O(1)
231
230
  *
232
231
  * The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
233
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
234
- * `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
232
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
233
+ * `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
235
234
  * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
236
235
  * key in the binary search tree. If provided, it will be stored in the node along with the key.
237
236
  * @returns a boolean value.
238
237
  */
239
- override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
240
- const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
238
+ override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
239
+ const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
241
240
  if (newNode === undefined) return false;
242
241
 
243
242
  if (this._root === undefined) {
244
243
  this._setRoot(newNode);
244
+ if (this._isMapMode) this._setValue(newNode?.key, newValue);
245
245
  this._size++;
246
246
  return true;
247
247
  }
@@ -254,6 +254,7 @@ export class BST<
254
254
  } else if (this.comparator(current.key, newNode.key) > 0) {
255
255
  if (current.left === undefined) {
256
256
  current.left = newNode;
257
+ if (this._isMapMode) this._setValue(newNode?.key, newValue);
257
258
  this._size++;
258
259
  return true;
259
260
  }
@@ -261,6 +262,7 @@ export class BST<
261
262
  } else {
262
263
  if (current.right === undefined) {
263
264
  current.right = newNode;
265
+ if (this._isMapMode) this._setValue(newNode?.key, newValue);
264
266
  this._size++;
265
267
  return true;
266
268
  }
@@ -277,7 +279,7 @@ export class BST<
277
279
  *
278
280
  * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
279
281
  * an array indicating whether each key or node was successfully inserted.
280
- * @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
282
+ * @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
281
283
  * elements to be added to the data structure.
282
284
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
283
285
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
@@ -293,7 +295,7 @@ export class BST<
293
295
  * successfully inserted into the data structure.
294
296
  */
295
297
  override addMany(
296
- keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
298
+ keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>,
297
299
  values?: Iterable<V | undefined>,
298
300
  isBalanceAdd = true,
299
301
  iterationType: IterationType = this.iterationType
@@ -307,7 +309,7 @@ export class BST<
307
309
  }
308
310
 
309
311
  if (!isBalanceAdd) {
310
- for (const kve of keysOrNodesOrEntriesOrRaws) {
312
+ for (const kve of keysNodesEntriesOrRaws) {
311
313
  const value = valuesIterator?.next().value;
312
314
  inserted.push(this.add(kve, value));
313
315
  }
@@ -315,18 +317,18 @@ export class BST<
315
317
  }
316
318
 
317
319
  const realBTNExemplars: {
318
- key: R | BTNKeyOrNodeOrEntry<K, V, NODE>;
320
+ key: R | BTNRep<K, V, NODE>;
319
321
  value: V | undefined;
320
322
  orgIndex: number;
321
323
  }[] = [];
322
324
 
323
325
  let i = 0;
324
- for (const kve of keysOrNodesOrEntriesOrRaws) {
326
+ for (const kve of keysNodesEntriesOrRaws) {
325
327
  realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
326
328
  i++;
327
329
  }
328
330
 
329
- let sorted: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
331
+ let sorted: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
330
332
 
331
333
  sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
332
334
  let keyA: K | undefined | null, keyB: K | undefined | null;
@@ -352,7 +354,7 @@ export class BST<
352
354
  return 0;
353
355
  });
354
356
 
355
- const _dfs = (arr: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
357
+ const _dfs = (arr: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
356
358
  if (arr.length === 0) return;
357
359
 
358
360
  const mid = Math.floor((arr.length - 1) / 2);
@@ -394,35 +396,35 @@ export class BST<
394
396
  * Space Complexity: O(k + log n)
395
397
  *
396
398
  * The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
397
- * given predicate and iteration type.
398
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
399
+ * given keyNodeEntryRawOrPredicate and iteration type.
400
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
399
401
  * parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
400
- * key, a node, an entry, or a custom predicate function that determines whether a node should be
402
+ * key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
401
403
  * included in the result.
402
404
  * @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
403
- * determines whether to return only the first node that matches the predicate (`true`) or all nodes
404
- * that match the predicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
405
+ * determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
406
+ * that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
405
407
  * and
406
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
408
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
407
409
  * `getNodes` method is used to specify the starting point for traversing the tree when searching for
408
- * nodes that match a given predicate. It represents the root node of the subtree where the search
410
+ * nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
409
411
  * should begin. If not explicitly provided, the default value for `begin
410
412
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
411
413
  * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
412
414
  * have two possible values:
413
- * @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
415
+ * @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
414
416
  */
415
417
  override getNodes(
416
- predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
418
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
417
419
  onlyOne = false,
418
- beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
420
+ startNode: BTNRep<K, V, NODE> | R = this._root,
419
421
  iterationType: IterationType = this.iterationType
420
422
  ): NODE[] {
421
- if (predicate === undefined) return [];
422
- if (predicate === null) return [];
423
- beginRoot = this.ensureNode(beginRoot);
424
- if (!beginRoot) return [];
425
- const callback = this._ensurePredicate(predicate);
423
+ if (keyNodeEntryRawOrPredicate === undefined) return [];
424
+ if (keyNodeEntryRawOrPredicate === null) return [];
425
+ startNode = this.ensureNode(startNode);
426
+ if (!startNode) return [];
427
+ const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
426
428
  const ans: NODE[] = [];
427
429
 
428
430
  if (iterationType === 'RECURSIVE') {
@@ -433,27 +435,53 @@ export class BST<
433
435
  }
434
436
 
435
437
  if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
436
- if (this.isKey(predicate)) {
437
- if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0) dfs(cur.left);
438
- if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0) dfs(cur.right);
438
+ if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
439
+ const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
440
+ if (
441
+ this.isRealNode(cur.left) &&
442
+ benchmarkKey !== null &&
443
+ benchmarkKey !== undefined &&
444
+ this.comparator(cur.key, benchmarkKey) > 0
445
+ )
446
+ dfs(cur.left);
447
+ if (
448
+ this.isRealNode(cur.right) &&
449
+ benchmarkKey !== null &&
450
+ benchmarkKey !== undefined &&
451
+ this.comparator(cur.key, benchmarkKey) < 0
452
+ )
453
+ dfs(cur.right);
439
454
  } else {
440
455
  if (this.isRealNode(cur.left)) dfs(cur.left);
441
456
  if (this.isRealNode(cur.right)) dfs(cur.right);
442
457
  }
443
458
  };
444
459
 
445
- dfs(beginRoot);
460
+ dfs(startNode);
446
461
  } else {
447
- const stack = [beginRoot];
462
+ const stack = [startNode];
448
463
  while (stack.length > 0) {
449
464
  const cur = stack.pop()!;
450
465
  if (callback(cur)) {
451
466
  ans.push(cur);
452
467
  if (onlyOne) return ans;
453
468
  }
454
- if (this.isKey(predicate)) {
455
- if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0) stack.push(cur.right);
456
- if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0) stack.push(cur.left);
469
+ if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
470
+ const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
471
+ if (
472
+ this.isRealNode(cur.right) &&
473
+ benchmarkKey !== null &&
474
+ benchmarkKey !== undefined &&
475
+ this.comparator(cur.key, benchmarkKey) < 0
476
+ )
477
+ stack.push(cur.right);
478
+ if (
479
+ this.isRealNode(cur.left) &&
480
+ benchmarkKey !== null &&
481
+ benchmarkKey !== undefined &&
482
+ this.comparator(cur.key, benchmarkKey) > 0
483
+ )
484
+ stack.push(cur.left);
457
485
  } else {
458
486
  if (this.isRealNode(cur.right)) stack.push(cur.right);
459
487
  if (this.isRealNode(cur.left)) stack.push(cur.left);
@@ -468,10 +496,10 @@ export class BST<
468
496
  * Time Complexity: O(log n)
469
497
  * Space Complexity: O(1)
470
498
  *
471
- * This function retrieves a node based on a given predicate within a binary search tree structure.
472
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
473
- * parameter can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, `R`, or `BTNPredicate<NODE>`.
474
- * @param {R | BSTNKeyOrNode<K, NODE>} beginRoot - The `beginRoot` parameter in the `getNode` method
499
+ * This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
500
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
501
+ * parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
502
+ * @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
475
503
  * is used to specify the starting point for searching nodes in the binary search tree. If no
476
504
  * specific starting point is provided, the default value is set to `this._root`, which is the root
477
505
  * node of the binary search tree.
@@ -479,17 +507,17 @@ export class BST<
479
507
  * parameter that specifies the type of iteration to be used. It has a default value of
480
508
  * `this.iterationType`, which means it will use the iteration type defined in the class instance if
481
509
  * no value is provided when calling the method.
482
- * @returns The `getNode` method is returning an optional binary search tree node (`OptBSTN<NODE>`).
483
- * It is using the `getNodes` method to find the node based on the provided predicate, beginning at
484
- * the specified root node (`beginRoot`) and using the specified iteration type. The method then
510
+ * @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
511
+ * It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
512
+ * the specified root node (`startNode`) and using the specified iteration type. The method then
485
513
  * returns the first node found or `undefined` if no node is found.
486
514
  */
487
515
  override getNode(
488
- predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
489
- beginRoot: R | BSTNKeyOrNode<K, NODE> = this._root,
516
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
517
+ startNode: R | BSTNOptKeyOrNode<K, NODE> = this._root,
490
518
  iterationType: IterationType = this.iterationType
491
- ): OptBSTN<NODE> {
492
- return this.getNodes(predicate, true, beginRoot, iterationType)[0] ?? undefined;
519
+ ): OptNode<NODE> {
520
+ return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined;
493
521
  }
494
522
 
495
523
  /**
@@ -505,7 +533,7 @@ export class BST<
505
533
  * It has a default value of `'ITERATIVE'`.
506
534
  * @returns The method is returning a NODE object or undefined.
507
535
  */
508
- override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBSTN<NODE> {
536
+ override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptNode<NODE> {
509
537
  return this.getNode(key, this._root, iterationType);
510
538
  }
511
539
 
@@ -517,11 +545,11 @@ export class BST<
517
545
  * the callback function.
518
546
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
519
547
  * during the depth-first search traversal. It is an optional parameter and defaults to
520
- * `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
548
+ * `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
521
549
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
522
550
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
523
551
  * take one of the following values:
524
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
552
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
525
553
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
526
554
  * node entry. If not specified, the default value is the root of the tree.
527
555
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -529,13 +557,13 @@ export class BST<
529
557
  * following values:
530
558
  * @returns The method is returning an array of the return type of the callback function.
531
559
  */
532
- override dfs<C extends BTNCallback<NODE>>(
533
- callback: C = this._DEFAULT_BTN_CALLBACK as C,
560
+ override dfs<C extends NodeCallback<NODE>>(
561
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
534
562
  pattern: DFSOrderPattern = 'IN',
535
- beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
563
+ startNode: BTNRep<K, V, NODE> | R = this._root,
536
564
  iterationType: IterationType = this.iterationType
537
565
  ): ReturnType<C>[] {
538
- return super.dfs(callback, pattern, beginRoot, iterationType);
566
+ return super.dfs(callback, pattern, startNode, iterationType);
539
567
  }
540
568
 
541
569
  /**
@@ -547,7 +575,7 @@ export class BST<
547
575
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
548
576
  * visited during the breadth-first search. It should take a single argument, which is the current
549
577
  * node being visited, and it can return a value of any type.
550
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
578
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
551
579
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
552
580
  * object. If no value is provided, the default value is the root of the tree.
553
581
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -555,12 +583,12 @@ export class BST<
555
583
  * the following values:
556
584
  * @returns an array of the return type of the callback function.
557
585
  */
558
- override bfs<C extends BTNCallback<NODE>>(
559
- callback: C = this._DEFAULT_BTN_CALLBACK as C,
560
- beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
586
+ override bfs<C extends NodeCallback<NODE>>(
587
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
588
+ startNode: BTNRep<K, V, NODE> | R = this._root,
561
589
  iterationType: IterationType = this.iterationType
562
590
  ): ReturnType<C>[] {
563
- return super.bfs(callback, beginRoot, iterationType, false);
591
+ return super.bfs(callback, startNode, iterationType, false);
564
592
  }
565
593
 
566
594
  /**
@@ -570,9 +598,9 @@ export class BST<
570
598
  * The function overrides the listLevels method from the superclass and returns an array of arrays
571
599
  * containing the results of the callback function applied to each level of the tree.
572
600
  * @param {C} callback - The `callback` parameter is a generic type `C` that extends
573
- * `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
601
+ * `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
574
602
  * tree during the iteration process.
575
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
603
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
576
604
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
577
605
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
578
606
  * value is provided, the root of
@@ -581,12 +609,12 @@ export class BST<
581
609
  * @returns The method is returning a two-dimensional array of the return type of the callback
582
610
  * function.
583
611
  */
584
- override listLevels<C extends BTNCallback<NODE>>(
585
- callback: C = this._DEFAULT_BTN_CALLBACK as C,
586
- beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
612
+ override listLevels<C extends NodeCallback<NODE>>(
613
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
614
+ startNode: BTNRep<K, V, NODE> | R = this._root,
587
615
  iterationType: IterationType = this.iterationType
588
616
  ): ReturnType<C>[][] {
589
- return super.listLevels(callback, beginRoot, iterationType, false);
617
+ return super.listLevels(callback, startNode, iterationType, false);
590
618
  }
591
619
 
592
620
  /**
@@ -601,7 +629,7 @@ export class BST<
601
629
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
602
630
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
603
631
  * 0, or 1, where:
604
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
632
+ * @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
605
633
  * the binary tree that you want to start traversing from. It can be specified either by providing
606
634
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
607
635
  * `targetNode` is provided,
@@ -610,14 +638,14 @@ export class BST<
610
638
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
611
639
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
612
640
  */
613
- lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
614
- callback: C = this._DEFAULT_BTN_CALLBACK as C,
641
+ lesserOrGreaterTraverse<C extends NodeCallback<NODE>>(
642
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
615
643
  lesserOrGreater: CP = -1,
616
- targetNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
644
+ targetNode: BTNRep<K, V, NODE> | R = this._root,
617
645
  iterationType: IterationType = this.iterationType
618
646
  ): ReturnType<C>[] {
619
647
  const targetNodeEnsured = this.ensureNode(targetNode);
620
- const ans: ReturnType<BTNCallback<NODE>>[] = [];
648
+ const ans: ReturnType<NodeCallback<NODE>>[] = [];
621
649
  if (!this._root) return ans;
622
650
  if (!targetNodeEnsured) return ans;
623
651
 
@@ -665,7 +693,7 @@ export class BST<
665
693
  perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
666
694
  const sorted = this.dfs(node => node, 'IN'),
667
695
  n = sorted.length;
668
- this.clear();
696
+ this._clearNodes();
669
697
 
670
698
  if (sorted.length < 1) return false;
671
699
  if (iterationType === 'RECURSIVE') {
@@ -673,7 +701,8 @@ export class BST<
673
701
  if (l > r) return;
674
702
  const m = l + Math.floor((r - l) / 2);
675
703
  const midNode = sorted[m];
676
- this.add([midNode.key, midNode.value]);
704
+ if (this._isMapMode) this.add(midNode.key);
705
+ else this.add([midNode.key, midNode.value]);
677
706
  buildBalanceBST(l, m - 1);
678
707
  buildBalanceBST(m + 1, r);
679
708
  };
@@ -689,7 +718,8 @@ export class BST<
689
718
  if (l <= r) {
690
719
  const m = l + Math.floor((r - l) / 2);
691
720
  const midNode = sorted[m];
692
- this.add([midNode.key, midNode.value]);
721
+ if (this._isMapMode) this.add(midNode.key);
722
+ else this.add([midNode.key, midNode.value]);
693
723
  stack.push([m + 1, r]);
694
724
  stack.push([l, m - 1]);
695
725
  }
@@ -717,7 +747,7 @@ export class BST<
717
747
  let balanced = true;
718
748
 
719
749
  if (iterationType === 'RECURSIVE') {
720
- const _height = (cur: OptBSTN<NODE>): number => {
750
+ const _height = (cur: OptNode<NODE>): number => {
721
751
  if (!cur) return 0;
722
752
  const leftHeight = _height(cur.left),
723
753
  rightHeight = _height(cur.right);
@@ -727,8 +757,8 @@ export class BST<
727
757
  _height(this._root);
728
758
  } else {
729
759
  const stack: NODE[] = [];
730
- let node: OptBSTN<NODE> = this._root,
731
- last: OptBSTN<NODE> = undefined;
760
+ let node: OptNode<NODE> = this._root,
761
+ last: OptNode<NODE> = undefined;
732
762
  const depths: Map<NODE, number> = new Map();
733
763
 
734
764
  while (stack.length > 0 || node) {
@@ -779,9 +809,9 @@ export class BST<
779
809
  /**
780
810
  * The function sets the root of a tree-like structure and updates the parent property of the new
781
811
  * root.
782
- * @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
812
+ * @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
783
813
  */
784
- protected override _setRoot(v: OptBSTN<NODE>) {
814
+ protected override _setRoot(v: OptNode<NODE>) {
785
815
  if (v) {
786
816
  v.parent = undefined;
787
817
  }