data-structure-typed 1.51.6 → 1.51.7

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 (39) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +37 -1
  4. package/benchmark/report.json +405 -3
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -2
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +24 -46
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.d.ts +19 -19
  13. package/dist/cjs/data-structures/binary-tree/bst.js +59 -89
  14. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +10 -1
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js +19 -0
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  19. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  20. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
  21. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -2
  22. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
  23. package/dist/mjs/data-structures/binary-tree/binary-tree.js +23 -45
  24. package/dist/mjs/data-structures/binary-tree/bst.d.ts +19 -19
  25. package/dist/mjs/data-structures/binary-tree/bst.js +59 -89
  26. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +10 -1
  27. package/dist/mjs/data-structures/binary-tree/rb-tree.js +19 -0
  28. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +1 -0
  29. package/dist/umd/data-structure-typed.js +98 -118
  30. package/dist/umd/data-structure-typed.min.js +2 -2
  31. package/dist/umd/data-structure-typed.min.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -0
  34. package/src/data-structures/binary-tree/avl-tree.ts +0 -1
  35. package/src/data-structures/binary-tree/binary-tree.ts +27 -38
  36. package/src/data-structures/binary-tree/bst.ts +59 -81
  37. package/src/data-structures/binary-tree/rb-tree.ts +19 -2
  38. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -0
  39. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +7 -7
@@ -161,25 +161,6 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
161
161
  * @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
162
162
  */
163
163
  addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
164
- /**
165
- * Time Complexity: O(log n)
166
- * Space Complexity: O(1)
167
- */
168
- /**
169
- * Time Complexity: O(log n)
170
- * Space Complexity: O(1)
171
- *
172
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
173
- * either recursive or iterative methods.
174
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
175
- * It is used to identify the node that we want to retrieve.
176
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
177
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
178
- * values:
179
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
180
- * found in the binary tree. If no node is found, it returns `undefined`.
181
- */
182
- getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
183
164
  /**
184
165
  * Time Complexity: O(log n)
185
166
  * Space Complexity: O(k + log n)
@@ -235,6 +216,25 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
235
216
  * @returns The method is returning a value of type `NODE | null | undefined`.
236
217
  */
237
218
  getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | undefined;
219
+ /**
220
+ * Time Complexity: O(log n)
221
+ * Space Complexity: O(1)
222
+ */
223
+ /**
224
+ * Time Complexity: O(log n)
225
+ * Space Complexity: O(1)
226
+ *
227
+ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
228
+ * either recursive or iterative methods.
229
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
230
+ * It is used to identify the node that we want to retrieve.
231
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
232
+ * type of iteration to use when searching for a node in the binary tree. It can have two possible
233
+ * values:
234
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
235
+ * found in the binary tree. If no node is found, it returns `undefined`.
236
+ */
237
+ getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
238
238
  /**
239
239
  * Time complexity: O(n)
240
240
  * Space complexity: O(n)
@@ -169,19 +169,21 @@ export class BST extends BinaryTree {
169
169
  * @returns either a node object (NODE) or undefined.
170
170
  */
171
171
  ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
172
+ if (keyOrNodeOrEntry === this.NIL)
173
+ return;
172
174
  if (this.isRealNode(keyOrNodeOrEntry)) {
173
175
  return keyOrNodeOrEntry;
174
176
  }
175
- else if (this.isEntry(keyOrNodeOrEntry)) {
176
- if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined)
177
- return;
178
- return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
179
- }
180
- else {
181
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined)
177
+ if (this.isEntry(keyOrNodeOrEntry)) {
178
+ const key = keyOrNodeOrEntry[0];
179
+ if (key === null || key === undefined)
182
180
  return;
183
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
181
+ return this.getNodeByKey(key, iterationType);
184
182
  }
183
+ const key = keyOrNodeOrEntry;
184
+ if (key === null || key === undefined)
185
+ return;
186
+ return this.getNodeByKey(key, iterationType);
185
187
  }
186
188
  /**
187
189
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
@@ -346,56 +348,6 @@ export class BST extends BinaryTree {
346
348
  }
347
349
  return inserted;
348
350
  }
349
- /**
350
- * Time Complexity: O(log n)
351
- * Space Complexity: O(1)
352
- */
353
- /**
354
- * Time Complexity: O(log n)
355
- * Space Complexity: O(1)
356
- *
357
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
358
- * either recursive or iterative methods.
359
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
360
- * It is used to identify the node that we want to retrieve.
361
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
362
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
363
- * values:
364
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
365
- * found in the binary tree. If no node is found, it returns `undefined`.
366
- */
367
- getNodeByKey(key, iterationType = 'ITERATIVE') {
368
- // return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
369
- if (!this.isRealNode(this.root))
370
- return;
371
- if (iterationType === 'RECURSIVE') {
372
- const dfs = (cur) => {
373
- if (cur.key === key)
374
- return cur;
375
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
376
- return;
377
- if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
378
- return dfs(cur.left);
379
- if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
380
- return dfs(cur.right);
381
- };
382
- return dfs(this.root);
383
- }
384
- else {
385
- const stack = [this.root];
386
- while (stack.length > 0) {
387
- const cur = stack.pop();
388
- if (this.isRealNode(cur)) {
389
- if (this._compare(cur.key, key) === 'EQ')
390
- return cur;
391
- if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
392
- stack.push(cur.left);
393
- if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
394
- stack.push(cur.right);
395
- }
396
- }
397
- }
398
- }
399
351
  /**
400
352
  * Time Complexity: O(log n)
401
353
  * Space Complexity: O(k + log n)
@@ -428,6 +380,7 @@ export class BST extends BinaryTree {
428
380
  beginRoot = this.ensureNode(beginRoot);
429
381
  if (!beginRoot)
430
382
  return [];
383
+ callback = this._ensureCallback(identifier, callback);
431
384
  const ans = [];
432
385
  if (iterationType === 'RECURSIVE') {
433
386
  const dfs = (cur) => {
@@ -457,30 +410,28 @@ export class BST extends BinaryTree {
457
410
  const stack = [beginRoot];
458
411
  while (stack.length > 0) {
459
412
  const cur = stack.pop();
460
- if (this.isRealNode(cur)) {
461
- const callbackResult = callback(cur);
462
- if (callbackResult === identifier) {
463
- ans.push(cur);
464
- if (onlyOne)
465
- return ans;
466
- }
467
- // TODO potential bug
468
- if (callback === this._DEFAULT_CALLBACK) {
469
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
470
- stack.push(cur.right);
471
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
472
- stack.push(cur.left);
473
- // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
474
- // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
475
- // // @ts-ignore
476
- // if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
477
- // // @ts-ignore
478
- // if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
479
- }
480
- else {
481
- this.isRealNode(cur.right) && stack.push(cur.right);
482
- this.isRealNode(cur.left) && stack.push(cur.left);
483
- }
413
+ const callbackResult = callback(cur);
414
+ if (callbackResult === identifier) {
415
+ ans.push(cur);
416
+ if (onlyOne)
417
+ return ans;
418
+ }
419
+ // TODO potential bug
420
+ if (callback === this._DEFAULT_CALLBACK) {
421
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
422
+ stack.push(cur.right);
423
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
424
+ stack.push(cur.left);
425
+ // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
426
+ // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
427
+ // // @ts-ignore
428
+ // if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
429
+ // // @ts-ignore
430
+ // if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
431
+ }
432
+ else {
433
+ this.isRealNode(cur.right) && stack.push(cur.right);
434
+ this.isRealNode(cur.left) && stack.push(cur.left);
484
435
  }
485
436
  }
486
437
  }
@@ -514,6 +465,27 @@ export class BST extends BinaryTree {
514
465
  getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
515
466
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
516
467
  }
468
+ /**
469
+ * Time Complexity: O(log n)
470
+ * Space Complexity: O(1)
471
+ */
472
+ /**
473
+ * Time Complexity: O(log n)
474
+ * Space Complexity: O(1)
475
+ *
476
+ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
477
+ * either recursive or iterative methods.
478
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
479
+ * It is used to identify the node that we want to retrieve.
480
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
481
+ * type of iteration to use when searching for a node in the binary tree. It can have two possible
482
+ * values:
483
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
484
+ * found in the binary tree. If no node is found, it returns `undefined`.
485
+ */
486
+ getNodeByKey(key, iterationType = 'ITERATIVE') {
487
+ return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
488
+ }
517
489
  /**
518
490
  * Time complexity: O(n)
519
491
  * Space complexity: O(n)
@@ -828,7 +800,11 @@ export class BST extends BinaryTree {
828
800
  const extractedA = this.extractor(a);
829
801
  const extractedB = this.extractor(b);
830
802
  const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
831
- return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
803
+ if (compared > 0)
804
+ return 'GT';
805
+ if (compared < 0)
806
+ return 'LT';
807
+ return 'EQ';
832
808
  }
833
809
  /**
834
810
  * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
@@ -842,10 +818,7 @@ export class BST extends BinaryTree {
842
818
  _lt(a, b) {
843
819
  const extractedA = this.extractor(a);
844
820
  const extractedB = this.extractor(b);
845
- // return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
846
821
  return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
847
- // return extractedA < extractedB;
848
- // return a < b;
849
822
  }
850
823
  /**
851
824
  * The function compares two values using a custom extractor function and returns true if the first
@@ -858,9 +831,6 @@ export class BST extends BinaryTree {
858
831
  _gt(a, b) {
859
832
  const extractedA = this.extractor(a);
860
833
  const extractedB = this.extractor(b);
861
- // return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
862
834
  return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
863
- // return extractedA > extractedB;
864
- // return a > b;
865
835
  }
866
836
  }
@@ -1,5 +1,5 @@
1
1
  import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
2
- import { CRUD, RBTNColor } from '../../types';
2
+ import { CP, CRUD, RBTNColor } from '../../types';
3
3
  import { BST, BSTNode } from './bst';
4
4
  import { IBinaryTree } from '../../interfaces';
5
5
  export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
@@ -255,4 +255,13 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
255
255
  * @returns void, which means it does not return any value.
256
256
  */
257
257
  protected _rightRotate(y: NODE | undefined): void;
258
+ /**
259
+ * The function compares two values using a comparator function and returns whether the first value
260
+ * is greater than, less than, or equal to the second value.
261
+ * @param {K} a - The parameter "a" is of type K.
262
+ * @param {K} b - The parameter "b" in the above code represents a K.
263
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
264
+ * than), 'LT' (less than), or 'EQ' (equal).
265
+ */
266
+ protected _compare(a: K, b: K): CP;
258
267
  }
@@ -215,6 +215,7 @@ export class RedBlackTree extends BST {
215
215
  if (identifier === null)
216
216
  return [];
217
217
  const results = [];
218
+ callback = this._ensureCallback(identifier, callback);
218
219
  const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
219
220
  if (!nodeToDelete) {
220
221
  return results;
@@ -596,4 +597,22 @@ export class RedBlackTree extends BST {
596
597
  x.right = y;
597
598
  y.parent = x;
598
599
  }
600
+ /**
601
+ * The function compares two values using a comparator function and returns whether the first value
602
+ * is greater than, less than, or equal to the second value.
603
+ * @param {K} a - The parameter "a" is of type K.
604
+ * @param {K} b - The parameter "b" in the above code represents a K.
605
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
606
+ * than), 'LT' (less than), or 'EQ' (equal).
607
+ */
608
+ _compare(a, b) {
609
+ const extractedA = this.extractor(a);
610
+ const extractedB = this.extractor(b);
611
+ const compared = extractedA - extractedB;
612
+ if (compared > 0)
613
+ return 'GT';
614
+ if (compared < 0)
615
+ return 'LT';
616
+ return 'EQ';
617
+ }
599
618
  }
@@ -209,6 +209,7 @@ export class TreeMultiMap extends RedBlackTree {
209
209
  if (identifier === null)
210
210
  return [];
211
211
  const results = [];
212
+ callback = this._ensureCallback(identifier, callback);
212
213
  const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
213
214
  if (!nodeToDelete) {
214
215
  return results;
@@ -7860,21 +7860,24 @@ var dataStructureTyped = (() => {
7860
7860
  * itself if it is not a valid node key.
7861
7861
  */
7862
7862
  ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE") {
7863
+ if (keyOrNodeOrEntry === this.NIL)
7864
+ return;
7863
7865
  if (this.isRealNode(keyOrNodeOrEntry)) {
7864
7866
  return keyOrNodeOrEntry;
7865
- } else if (this.isEntry(keyOrNodeOrEntry)) {
7866
- if (keyOrNodeOrEntry[0] === null)
7867
- return null;
7868
- if (keyOrNodeOrEntry[0] === void 0)
7869
- return;
7870
- return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
7871
- } else {
7872
- if (keyOrNodeOrEntry === null)
7867
+ }
7868
+ if (this.isEntry(keyOrNodeOrEntry)) {
7869
+ const key = keyOrNodeOrEntry[0];
7870
+ if (key === null)
7873
7871
  return null;
7874
- if (keyOrNodeOrEntry === void 0)
7872
+ if (key === void 0)
7875
7873
  return;
7876
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
7874
+ return this.getNodeByKey(key, iterationType);
7877
7875
  }
7876
+ if (keyOrNodeOrEntry === null)
7877
+ return null;
7878
+ if (keyOrNodeOrEntry === void 0)
7879
+ return;
7880
+ return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
7878
7881
  }
7879
7882
  /**
7880
7883
  * The function checks if a given node is a real node or null.
@@ -8052,8 +8055,7 @@ var dataStructureTyped = (() => {
8052
8055
  const deletedResult = [];
8053
8056
  if (!this.root)
8054
8057
  return deletedResult;
8055
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8056
- callback = (node) => node;
8058
+ callback = this._ensureCallback(identifier, callback);
8057
8059
  const curr = this.getNode(identifier, callback);
8058
8060
  if (!curr)
8059
8061
  return deletedResult;
@@ -8121,11 +8123,10 @@ var dataStructureTyped = (() => {
8121
8123
  * @returns an array of nodes of type `NODE`.
8122
8124
  */
8123
8125
  getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
8124
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8125
- callback = (node) => node;
8126
8126
  beginRoot = this.ensureNode(beginRoot);
8127
8127
  if (!beginRoot)
8128
8128
  return [];
8129
+ callback = this._ensureCallback(identifier, callback);
8129
8130
  const ans = [];
8130
8131
  if (iterationType === "RECURSIVE") {
8131
8132
  const dfs = (cur) => {
@@ -8205,32 +8206,7 @@ var dataStructureTyped = (() => {
8205
8206
  * found in the binary tree. If no node is found, it returns `undefined`.
8206
8207
  */
8207
8208
  getNodeByKey(key, iterationType = "ITERATIVE") {
8208
- if (!this.root)
8209
- return void 0;
8210
- if (iterationType === "RECURSIVE") {
8211
- const dfs = (cur) => {
8212
- if (cur.key === key)
8213
- return cur;
8214
- if (!cur.left && !cur.right)
8215
- return;
8216
- if (cur.left)
8217
- return dfs(cur.left);
8218
- if (cur.right)
8219
- return dfs(cur.right);
8220
- };
8221
- return dfs(this.root);
8222
- } else {
8223
- const stack = [this.root];
8224
- while (stack.length > 0) {
8225
- const cur = stack.pop();
8226
- if (cur) {
8227
- if (cur.key === key)
8228
- return cur;
8229
- cur.left && stack.push(cur.left);
8230
- cur.right && stack.push(cur.right);
8231
- }
8232
- }
8233
- }
8209
+ return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
8234
8210
  }
8235
8211
  /**
8236
8212
  * Time Complexity: O(n)
@@ -8259,8 +8235,8 @@ var dataStructureTyped = (() => {
8259
8235
  * found, `undefined` is returned.
8260
8236
  */
8261
8237
  get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
8262
- var _a, _b;
8263
- return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
8238
+ var _a;
8239
+ return (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value;
8264
8240
  }
8265
8241
  /**
8266
8242
  * Time Complexity: O(n)
@@ -8288,8 +8264,7 @@ var dataStructureTyped = (() => {
8288
8264
  * @returns a boolean value.
8289
8265
  */
8290
8266
  has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
8291
- if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
8292
- callback = (node) => node;
8267
+ callback = this._ensureCallback(identifier, callback);
8293
8268
  return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
8294
8269
  }
8295
8270
  /**
@@ -9344,6 +9319,12 @@ var dataStructureTyped = (() => {
9344
9319
  }
9345
9320
  this._root = v;
9346
9321
  }
9322
+ _ensureCallback(identifier, callback = this._DEFAULT_CALLBACK) {
9323
+ if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
9324
+ callback = (node) => node;
9325
+ }
9326
+ return callback;
9327
+ }
9347
9328
  };
9348
9329
 
9349
9330
  // src/data-structures/binary-tree/bst.ts
@@ -9501,17 +9482,21 @@ var dataStructureTyped = (() => {
9501
9482
  * @returns either a node object (NODE) or undefined.
9502
9483
  */
9503
9484
  ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE") {
9485
+ if (keyOrNodeOrEntry === this.NIL)
9486
+ return;
9504
9487
  if (this.isRealNode(keyOrNodeOrEntry)) {
9505
9488
  return keyOrNodeOrEntry;
9506
- } else if (this.isEntry(keyOrNodeOrEntry)) {
9507
- if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === void 0)
9508
- return;
9509
- return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
9510
- } else {
9511
- if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0)
9489
+ }
9490
+ if (this.isEntry(keyOrNodeOrEntry)) {
9491
+ const key2 = keyOrNodeOrEntry[0];
9492
+ if (key2 === null || key2 === void 0)
9512
9493
  return;
9513
- return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
9494
+ return this.getNodeByKey(key2, iterationType);
9514
9495
  }
9496
+ const key = keyOrNodeOrEntry;
9497
+ if (key === null || key === void 0)
9498
+ return;
9499
+ return this.getNodeByKey(key, iterationType);
9515
9500
  }
9516
9501
  /**
9517
9502
  * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
@@ -9666,54 +9651,6 @@ var dataStructureTyped = (() => {
9666
9651
  }
9667
9652
  return inserted;
9668
9653
  }
9669
- /**
9670
- * Time Complexity: O(log n)
9671
- * Space Complexity: O(1)
9672
- */
9673
- /**
9674
- * Time Complexity: O(log n)
9675
- * Space Complexity: O(1)
9676
- *
9677
- * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
9678
- * either recursive or iterative methods.
9679
- * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
9680
- * It is used to identify the node that we want to retrieve.
9681
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9682
- * type of iteration to use when searching for a node in the binary tree. It can have two possible
9683
- * values:
9684
- * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
9685
- * found in the binary tree. If no node is found, it returns `undefined`.
9686
- */
9687
- getNodeByKey(key, iterationType = "ITERATIVE") {
9688
- if (!this.isRealNode(this.root))
9689
- return;
9690
- if (iterationType === "RECURSIVE") {
9691
- const dfs = (cur) => {
9692
- if (cur.key === key)
9693
- return cur;
9694
- if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
9695
- return;
9696
- if (this.isRealNode(cur.left) && this._compare(cur.key, key) === "GT")
9697
- return dfs(cur.left);
9698
- if (this.isRealNode(cur.right) && this._compare(cur.key, key) === "LT")
9699
- return dfs(cur.right);
9700
- };
9701
- return dfs(this.root);
9702
- } else {
9703
- const stack = [this.root];
9704
- while (stack.length > 0) {
9705
- const cur = stack.pop();
9706
- if (this.isRealNode(cur)) {
9707
- if (this._compare(cur.key, key) === "EQ")
9708
- return cur;
9709
- if (this.isRealNode(cur.left) && this._compare(cur.key, key) === "GT")
9710
- stack.push(cur.left);
9711
- if (this.isRealNode(cur.right) && this._compare(cur.key, key) === "LT")
9712
- stack.push(cur.right);
9713
- }
9714
- }
9715
- }
9716
- }
9717
9654
  /**
9718
9655
  * Time Complexity: O(log n)
9719
9656
  * Space Complexity: O(k + log n)
@@ -9746,6 +9683,7 @@ var dataStructureTyped = (() => {
9746
9683
  beginRoot = this.ensureNode(beginRoot);
9747
9684
  if (!beginRoot)
9748
9685
  return [];
9686
+ callback = this._ensureCallback(identifier, callback);
9749
9687
  const ans = [];
9750
9688
  if (iterationType === "RECURSIVE") {
9751
9689
  const dfs = (cur) => {
@@ -9772,22 +9710,20 @@ var dataStructureTyped = (() => {
9772
9710
  const stack = [beginRoot];
9773
9711
  while (stack.length > 0) {
9774
9712
  const cur = stack.pop();
9775
- if (this.isRealNode(cur)) {
9776
- const callbackResult = callback(cur);
9777
- if (callbackResult === identifier) {
9778
- ans.push(cur);
9779
- if (onlyOne)
9780
- return ans;
9781
- }
9782
- if (callback === this._DEFAULT_CALLBACK) {
9783
- if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === "LT")
9784
- stack.push(cur.right);
9785
- if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === "GT")
9786
- stack.push(cur.left);
9787
- } else {
9788
- this.isRealNode(cur.right) && stack.push(cur.right);
9789
- this.isRealNode(cur.left) && stack.push(cur.left);
9790
- }
9713
+ const callbackResult = callback(cur);
9714
+ if (callbackResult === identifier) {
9715
+ ans.push(cur);
9716
+ if (onlyOne)
9717
+ return ans;
9718
+ }
9719
+ if (callback === this._DEFAULT_CALLBACK) {
9720
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === "LT")
9721
+ stack.push(cur.right);
9722
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === "GT")
9723
+ stack.push(cur.left);
9724
+ } else {
9725
+ this.isRealNode(cur.right) && stack.push(cur.right);
9726
+ this.isRealNode(cur.left) && stack.push(cur.left);
9791
9727
  }
9792
9728
  }
9793
9729
  }
@@ -9822,6 +9758,27 @@ var dataStructureTyped = (() => {
9822
9758
  var _a;
9823
9759
  return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
9824
9760
  }
9761
+ /**
9762
+ * Time Complexity: O(log n)
9763
+ * Space Complexity: O(1)
9764
+ */
9765
+ /**
9766
+ * Time Complexity: O(log n)
9767
+ * Space Complexity: O(1)
9768
+ *
9769
+ * The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
9770
+ * either recursive or iterative methods.
9771
+ * @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
9772
+ * It is used to identify the node that we want to retrieve.
9773
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
9774
+ * type of iteration to use when searching for a node in the binary tree. It can have two possible
9775
+ * values:
9776
+ * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
9777
+ * found in the binary tree. If no node is found, it returns `undefined`.
9778
+ */
9779
+ getNodeByKey(key, iterationType = "ITERATIVE") {
9780
+ return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
9781
+ }
9825
9782
  /**
9826
9783
  * Time complexity: O(n)
9827
9784
  * Space complexity: O(n)
@@ -10129,7 +10086,11 @@ var dataStructureTyped = (() => {
10129
10086
  const extractedA = this.extractor(a);
10130
10087
  const extractedB = this.extractor(b);
10131
10088
  const compared = this.variant === "STANDARD" ? extractedA - extractedB : extractedB - extractedA;
10132
- return compared > 0 ? "GT" : compared < 0 ? "LT" : "EQ";
10089
+ if (compared > 0)
10090
+ return "GT";
10091
+ if (compared < 0)
10092
+ return "LT";
10093
+ return "EQ";
10133
10094
  }
10134
10095
  /**
10135
10096
  * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
@@ -10849,8 +10810,6 @@ var dataStructureTyped = (() => {
10849
10810
  * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
10850
10811
  */
10851
10812
  delete(identifier, callback = this._DEFAULT_CALLBACK) {
10852
- if (identifier instanceof AVLTreeNode)
10853
- callback = (node) => node;
10854
10813
  const deletedResults = super.delete(identifier, callback);
10855
10814
  for (const { needBalanced } of deletedResults) {
10856
10815
  if (needBalanced) {
@@ -11383,6 +11342,7 @@ var dataStructureTyped = (() => {
11383
11342
  if (identifier === null)
11384
11343
  return [];
11385
11344
  const results = [];
11345
+ callback = this._ensureCallback(identifier, callback);
11386
11346
  const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
11387
11347
  if (!nodeToDelete) {
11388
11348
  return results;
@@ -11726,6 +11686,24 @@ var dataStructureTyped = (() => {
11726
11686
  x.right = y;
11727
11687
  y.parent = x;
11728
11688
  }
11689
+ /**
11690
+ * The function compares two values using a comparator function and returns whether the first value
11691
+ * is greater than, less than, or equal to the second value.
11692
+ * @param {K} a - The parameter "a" is of type K.
11693
+ * @param {K} b - The parameter "b" in the above code represents a K.
11694
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
11695
+ * than), 'LT' (less than), or 'EQ' (equal).
11696
+ */
11697
+ _compare(a, b) {
11698
+ const extractedA = this.extractor(a);
11699
+ const extractedB = this.extractor(b);
11700
+ const compared = extractedA - extractedB;
11701
+ if (compared > 0)
11702
+ return "GT";
11703
+ if (compared < 0)
11704
+ return "LT";
11705
+ return "EQ";
11706
+ }
11729
11707
  };
11730
11708
 
11731
11709
  // src/data-structures/binary-tree/avl-tree-multi-map.ts
@@ -11920,6 +11898,7 @@ var dataStructureTyped = (() => {
11920
11898
  const deletedResult = [];
11921
11899
  if (!this.root)
11922
11900
  return deletedResult;
11901
+ callback = this._ensureCallback(identifier, callback);
11923
11902
  const curr = (_a = this.getNode(identifier, callback)) != null ? _a : void 0;
11924
11903
  if (!curr)
11925
11904
  return deletedResult;
@@ -12295,6 +12274,7 @@ var dataStructureTyped = (() => {
12295
12274
  if (identifier === null)
12296
12275
  return [];
12297
12276
  const results = [];
12277
+ callback = this._ensureCallback(identifier, callback);
12298
12278
  const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
12299
12279
  if (!nodeToDelete) {
12300
12280
  return results;