min-heap-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
@@ -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
 
@@ -145,7 +141,7 @@ export class BST<
145
141
  * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
146
142
  */
147
143
  override createNode(key: K, value?: V): NODE {
148
- return new BSTNode<K, V, NODE>(key, value) as NODE;
144
+ return new BSTNode<K, V, NODE>(key, this._isMapMode ? undefined : value) as NODE;
149
145
  }
150
146
 
151
147
  /**
@@ -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, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
178
+ if (node === null) return [undefined, undefined];
179
+ return [node, value ?? entryValue];
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
  }
@@ -250,10 +250,12 @@ export class BST<
250
250
  while (current !== undefined) {
251
251
  if (this.comparator(current.key, newNode.key) === 0) {
252
252
  this._replaceNode(current, newNode);
253
+ if (this._isMapMode) this._setValue(current.key, newValue);
253
254
  return true;
254
255
  } else if (this.comparator(current.key, newNode.key) > 0) {
255
256
  if (current.left === undefined) {
256
257
  current.left = newNode;
258
+ if (this._isMapMode) this._setValue(newNode?.key, newValue);
257
259
  this._size++;
258
260
  return true;
259
261
  }
@@ -261,6 +263,7 @@ export class BST<
261
263
  } else {
262
264
  if (current.right === undefined) {
263
265
  current.right = newNode;
266
+ if (this._isMapMode) this._setValue(newNode?.key, newValue);
264
267
  this._size++;
265
268
  return true;
266
269
  }
@@ -277,7 +280,7 @@ export class BST<
277
280
  *
278
281
  * The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
279
282
  * an array indicating whether each key or node was successfully inserted.
280
- * @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
283
+ * @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
281
284
  * elements to be added to the data structure.
282
285
  * @param [values] - An optional iterable of values to be associated with the keys or nodes being
283
286
  * added. If provided, the values will be assigned to the corresponding keys or nodes in the same
@@ -293,7 +296,7 @@ export class BST<
293
296
  * successfully inserted into the data structure.
294
297
  */
295
298
  override addMany(
296
- keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
299
+ keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>,
297
300
  values?: Iterable<V | undefined>,
298
301
  isBalanceAdd = true,
299
302
  iterationType: IterationType = this.iterationType
@@ -307,7 +310,7 @@ export class BST<
307
310
  }
308
311
 
309
312
  if (!isBalanceAdd) {
310
- for (const kve of keysOrNodesOrEntriesOrRaws) {
313
+ for (const kve of keysNodesEntriesOrRaws) {
311
314
  const value = valuesIterator?.next().value;
312
315
  inserted.push(this.add(kve, value));
313
316
  }
@@ -315,18 +318,18 @@ export class BST<
315
318
  }
316
319
 
317
320
  const realBTNExemplars: {
318
- key: R | BTNKeyOrNodeOrEntry<K, V, NODE>;
321
+ key: R | BTNRep<K, V, NODE>;
319
322
  value: V | undefined;
320
323
  orgIndex: number;
321
324
  }[] = [];
322
325
 
323
326
  let i = 0;
324
- for (const kve of keysOrNodesOrEntriesOrRaws) {
327
+ for (const kve of keysNodesEntriesOrRaws) {
325
328
  realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
326
329
  i++;
327
330
  }
328
331
 
329
- let sorted: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
332
+ let sorted: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
330
333
 
331
334
  sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
332
335
  let keyA: K | undefined | null, keyB: K | undefined | null;
@@ -352,7 +355,7 @@ export class BST<
352
355
  return 0;
353
356
  });
354
357
 
355
- const _dfs = (arr: { key: R | BTNKeyOrNodeOrEntry<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
358
+ const _dfs = (arr: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
356
359
  if (arr.length === 0) return;
357
360
 
358
361
  const mid = Math.floor((arr.length - 1) / 2);
@@ -394,35 +397,35 @@ export class BST<
394
397
  * Space Complexity: O(k + log n)
395
398
  *
396
399
  * 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`
400
+ * given keyNodeEntryRawOrPredicate and iteration type.
401
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
399
402
  * 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
403
+ * key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
401
404
  * included in the result.
402
405
  * @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
406
+ * determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
407
+ * that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
405
408
  * and
406
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
409
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
407
410
  * `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
411
+ * nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
409
412
  * should begin. If not explicitly provided, the default value for `begin
410
413
  * @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
411
414
  * specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
412
415
  * have two possible values:
413
- * @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
416
+ * @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
414
417
  */
415
418
  override getNodes(
416
- predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
419
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
417
420
  onlyOne = false,
418
- beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
421
+ startNode: BTNRep<K, V, NODE> | R = this._root,
419
422
  iterationType: IterationType = this.iterationType
420
423
  ): 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);
424
+ if (keyNodeEntryRawOrPredicate === undefined) return [];
425
+ if (keyNodeEntryRawOrPredicate === null) return [];
426
+ startNode = this.ensureNode(startNode);
427
+ if (!startNode) return [];
428
+ const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
426
429
  const ans: NODE[] = [];
427
430
 
428
431
  if (iterationType === 'RECURSIVE') {
@@ -433,27 +436,53 @@ export class BST<
433
436
  }
434
437
 
435
438
  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);
439
+ if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
440
+ const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
441
+ if (
442
+ this.isRealNode(cur.left) &&
443
+ benchmarkKey !== null &&
444
+ benchmarkKey !== undefined &&
445
+ this.comparator(cur.key, benchmarkKey) > 0
446
+ )
447
+ dfs(cur.left);
448
+ if (
449
+ this.isRealNode(cur.right) &&
450
+ benchmarkKey !== null &&
451
+ benchmarkKey !== undefined &&
452
+ this.comparator(cur.key, benchmarkKey) < 0
453
+ )
454
+ dfs(cur.right);
439
455
  } else {
440
456
  if (this.isRealNode(cur.left)) dfs(cur.left);
441
457
  if (this.isRealNode(cur.right)) dfs(cur.right);
442
458
  }
443
459
  };
444
460
 
445
- dfs(beginRoot);
461
+ dfs(startNode);
446
462
  } else {
447
- const stack = [beginRoot];
463
+ const stack = [startNode];
448
464
  while (stack.length > 0) {
449
465
  const cur = stack.pop()!;
450
466
  if (callback(cur)) {
451
467
  ans.push(cur);
452
468
  if (onlyOne) return ans;
453
469
  }
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);
470
+ if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
471
+ const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
472
+ if (
473
+ this.isRealNode(cur.right) &&
474
+ benchmarkKey !== null &&
475
+ benchmarkKey !== undefined &&
476
+ this.comparator(cur.key, benchmarkKey) < 0
477
+ )
478
+ stack.push(cur.right);
479
+ if (
480
+ this.isRealNode(cur.left) &&
481
+ benchmarkKey !== null &&
482
+ benchmarkKey !== undefined &&
483
+ this.comparator(cur.key, benchmarkKey) > 0
484
+ )
485
+ stack.push(cur.left);
457
486
  } else {
458
487
  if (this.isRealNode(cur.right)) stack.push(cur.right);
459
488
  if (this.isRealNode(cur.left)) stack.push(cur.left);
@@ -468,10 +497,10 @@ export class BST<
468
497
  * Time Complexity: O(log n)
469
498
  * Space Complexity: O(1)
470
499
  *
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
500
+ * This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
501
+ * @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
502
+ * parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
503
+ * @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
475
504
  * is used to specify the starting point for searching nodes in the binary search tree. If no
476
505
  * specific starting point is provided, the default value is set to `this._root`, which is the root
477
506
  * node of the binary search tree.
@@ -479,17 +508,17 @@ export class BST<
479
508
  * parameter that specifies the type of iteration to be used. It has a default value of
480
509
  * `this.iterationType`, which means it will use the iteration type defined in the class instance if
481
510
  * 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
511
+ * @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
512
+ * It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
513
+ * the specified root node (`startNode`) and using the specified iteration type. The method then
485
514
  * returns the first node found or `undefined` if no node is found.
486
515
  */
487
516
  override getNode(
488
- predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
489
- beginRoot: R | BSTNKeyOrNode<K, NODE> = this._root,
517
+ keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
518
+ startNode: R | BSTNOptKeyOrNode<K, NODE> = this._root,
490
519
  iterationType: IterationType = this.iterationType
491
- ): OptBSTN<NODE> {
492
- return this.getNodes(predicate, true, beginRoot, iterationType)[0] ?? undefined;
520
+ ): OptNode<NODE> {
521
+ return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined;
493
522
  }
494
523
 
495
524
  /**
@@ -505,7 +534,7 @@ export class BST<
505
534
  * It has a default value of `'ITERATIVE'`.
506
535
  * @returns The method is returning a NODE object or undefined.
507
536
  */
508
- override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBSTN<NODE> {
537
+ override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptNode<NODE> {
509
538
  return this.getNode(key, this._root, iterationType);
510
539
  }
511
540
 
@@ -517,11 +546,11 @@ export class BST<
517
546
  * the callback function.
518
547
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
519
548
  * 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.
549
+ * `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
521
550
  * @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
522
551
  * order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
523
552
  * take one of the following values:
524
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
553
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
525
554
  * point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
526
555
  * node entry. If not specified, the default value is the root of the tree.
527
556
  * @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@@ -529,13 +558,13 @@ export class BST<
529
558
  * following values:
530
559
  * @returns The method is returning an array of the return type of the callback function.
531
560
  */
532
- override dfs<C extends BTNCallback<NODE>>(
533
- callback: C = this._DEFAULT_BTN_CALLBACK as C,
561
+ override dfs<C extends NodeCallback<NODE>>(
562
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
534
563
  pattern: DFSOrderPattern = 'IN',
535
- beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
564
+ startNode: BTNRep<K, V, NODE> | R = this._root,
536
565
  iterationType: IterationType = this.iterationType
537
566
  ): ReturnType<C>[] {
538
- return super.dfs(callback, pattern, beginRoot, iterationType);
567
+ return super.dfs(callback, pattern, startNode, iterationType);
539
568
  }
540
569
 
541
570
  /**
@@ -547,7 +576,7 @@ export class BST<
547
576
  * @param {C} callback - The `callback` parameter is a function that will be called for each node
548
577
  * visited during the breadth-first search. It should take a single argument, which is the current
549
578
  * 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
579
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
551
580
  * point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
552
581
  * object. If no value is provided, the default value is the root of the tree.
553
582
  * @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@@ -555,12 +584,12 @@ export class BST<
555
584
  * the following values:
556
585
  * @returns an array of the return type of the callback function.
557
586
  */
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,
587
+ override bfs<C extends NodeCallback<NODE>>(
588
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
589
+ startNode: BTNRep<K, V, NODE> | R = this._root,
561
590
  iterationType: IterationType = this.iterationType
562
591
  ): ReturnType<C>[] {
563
- return super.bfs(callback, beginRoot, iterationType, false);
592
+ return super.bfs(callback, startNode, iterationType, false);
564
593
  }
565
594
 
566
595
  /**
@@ -570,9 +599,9 @@ export class BST<
570
599
  * The function overrides the listLevels method from the superclass and returns an array of arrays
571
600
  * containing the results of the callback function applied to each level of the tree.
572
601
  * @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
602
+ * `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
574
603
  * tree during the iteration process.
575
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
604
+ * @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
576
605
  * point for listing the levels of the binary tree. It can be either a root node of the tree, a
577
606
  * key-value pair representing a node in the tree, or a key representing a node in the tree. If no
578
607
  * value is provided, the root of
@@ -581,12 +610,12 @@ export class BST<
581
610
  * @returns The method is returning a two-dimensional array of the return type of the callback
582
611
  * function.
583
612
  */
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,
613
+ override listLevels<C extends NodeCallback<NODE>>(
614
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
615
+ startNode: BTNRep<K, V, NODE> | R = this._root,
587
616
  iterationType: IterationType = this.iterationType
588
617
  ): ReturnType<C>[][] {
589
- return super.listLevels(callback, beginRoot, iterationType, false);
618
+ return super.listLevels(callback, startNode, iterationType, false);
590
619
  }
591
620
 
592
621
  /**
@@ -601,7 +630,7 @@ export class BST<
601
630
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
602
631
  * traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
603
632
  * 0, or 1, where:
604
- * @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
633
+ * @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
605
634
  * the binary tree that you want to start traversing from. It can be specified either by providing
606
635
  * the key of the node, the node itself, or an entry containing the key and value of the node. If no
607
636
  * `targetNode` is provided,
@@ -610,14 +639,14 @@ export class BST<
610
639
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
611
640
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
612
641
  */
613
- lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
614
- callback: C = this._DEFAULT_BTN_CALLBACK as C,
642
+ lesserOrGreaterTraverse<C extends NodeCallback<NODE>>(
643
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
615
644
  lesserOrGreater: CP = -1,
616
- targetNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
645
+ targetNode: BTNRep<K, V, NODE> | R = this._root,
617
646
  iterationType: IterationType = this.iterationType
618
647
  ): ReturnType<C>[] {
619
648
  const targetNodeEnsured = this.ensureNode(targetNode);
620
- const ans: ReturnType<BTNCallback<NODE>>[] = [];
649
+ const ans: ReturnType<NodeCallback<NODE>>[] = [];
621
650
  if (!this._root) return ans;
622
651
  if (!targetNodeEnsured) return ans;
623
652
 
@@ -665,7 +694,7 @@ export class BST<
665
694
  perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
666
695
  const sorted = this.dfs(node => node, 'IN'),
667
696
  n = sorted.length;
668
- this.clear();
697
+ this._clearNodes();
669
698
 
670
699
  if (sorted.length < 1) return false;
671
700
  if (iterationType === 'RECURSIVE') {
@@ -673,7 +702,8 @@ export class BST<
673
702
  if (l > r) return;
674
703
  const m = l + Math.floor((r - l) / 2);
675
704
  const midNode = sorted[m];
676
- this.add([midNode.key, midNode.value]);
705
+ if (this._isMapMode) this.add(midNode.key);
706
+ else this.add([midNode.key, midNode.value]);
677
707
  buildBalanceBST(l, m - 1);
678
708
  buildBalanceBST(m + 1, r);
679
709
  };
@@ -689,7 +719,8 @@ export class BST<
689
719
  if (l <= r) {
690
720
  const m = l + Math.floor((r - l) / 2);
691
721
  const midNode = sorted[m];
692
- this.add([midNode.key, midNode.value]);
722
+ if (this._isMapMode) this.add(midNode.key);
723
+ else this.add([midNode.key, midNode.value]);
693
724
  stack.push([m + 1, r]);
694
725
  stack.push([l, m - 1]);
695
726
  }
@@ -717,7 +748,7 @@ export class BST<
717
748
  let balanced = true;
718
749
 
719
750
  if (iterationType === 'RECURSIVE') {
720
- const _height = (cur: OptBSTN<NODE>): number => {
751
+ const _height = (cur: OptNode<NODE>): number => {
721
752
  if (!cur) return 0;
722
753
  const leftHeight = _height(cur.left),
723
754
  rightHeight = _height(cur.right);
@@ -727,8 +758,8 @@ export class BST<
727
758
  _height(this._root);
728
759
  } else {
729
760
  const stack: NODE[] = [];
730
- let node: OptBSTN<NODE> = this._root,
731
- last: OptBSTN<NODE> = undefined;
761
+ let node: OptNode<NODE> = this._root,
762
+ last: OptNode<NODE> = undefined;
732
763
  const depths: Map<NODE, number> = new Map();
733
764
 
734
765
  while (stack.length > 0 || node) {
@@ -779,9 +810,9 @@ export class BST<
779
810
  /**
780
811
  * The function sets the root of a tree-like structure and updates the parent property of the new
781
812
  * root.
782
- * @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
813
+ * @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
783
814
  */
784
- protected override _setRoot(v: OptBSTN<NODE>) {
815
+ protected override _setRoot(v: OptNode<NODE>) {
785
816
  if (v) {
786
817
  v.parent = undefined;
787
818
  }