min-priority-queue-typed 2.2.3 → 2.2.6

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 (34) hide show
  1. package/dist/cjs/index.cjs +0 -36
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +0 -36
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +0 -36
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +0 -36
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
  10. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  11. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  12. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +151 -117
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  18. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  19. package/dist/umd/max-priority-queue-typed.js +0 -33
  20. package/dist/umd/max-priority-queue-typed.js.map +1 -1
  21. package/dist/umd/max-priority-queue-typed.min.js +1 -1
  22. package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
  23. package/package.json +2 -2
  24. package/src/common/index.ts +2 -4
  25. package/src/data-structures/base/iterable-entry-base.ts +9 -0
  26. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  27. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -8
  28. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  29. package/src/data-structures/binary-tree/binary-tree.ts +67 -0
  30. package/src/data-structures/binary-tree/bst.ts +724 -108
  31. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  32. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  33. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  34. package/src/types/data-structures/binary-tree/bst.ts +5 -5
@@ -7,11 +7,10 @@
7
7
  */
8
8
 
9
9
  import type {
10
- BinaryTreeOptions,
10
+ BinaryTreeDeleteResult,
11
11
  BSTNOptKeyOrNode,
12
12
  BSTOptions,
13
13
  BTNRep,
14
- Comparable,
15
14
  Comparator,
16
15
  CP,
17
16
  DFSOrderPattern,
@@ -332,7 +331,7 @@ export class BSTNode<K = any, V = any> {
332
331
  * return findFirstCommon(path1, path2);
333
332
  * };
334
333
  *
335
- * function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
334
+ * function findFirstCommon(arr1: (number | undefined)[], arr2: (number | undefined)[]): number | undefined {
336
335
  * for (const num of arr1) {
337
336
  * if (arr2.indexOf(num) !== -1) {
338
337
  * return num;
@@ -355,18 +354,22 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
355
354
  * @param [options] - Configuration options for the BST, including comparator.
356
355
  */
357
356
  constructor(
358
- keysNodesEntriesOrRaws: Iterable<
359
- K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
360
- > = [],
357
+ keysNodesEntriesOrRaws: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R> = [],
361
358
  options?: BSTOptions<K, V, R>
362
359
  ) {
363
360
  super([], options);
364
361
 
365
362
  if (options) {
366
- const { specifyComparable, isReverse } = options;
367
- if (typeof specifyComparable === 'function') this._specifyComparable = specifyComparable;
368
- if (isReverse !== undefined) this._isReverse = isReverse;
363
+ // Use the 'in' operator to check if the field is present
364
+ if ('comparator' in options && options.comparator !== undefined) {
365
+ this._comparator = options.comparator;
366
+ } else {
367
+ this._comparator = this._createDefaultComparator();
368
+ }
369
+ } else {
370
+ this._comparator = this._createDefaultComparator();
369
371
  }
372
+
370
373
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
371
374
  }
372
375
 
@@ -382,42 +385,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
382
385
  return this._root;
383
386
  }
384
387
 
385
- protected _isReverse: boolean = false;
386
-
387
388
  /**
388
- * Gets whether the tree's comparison logic is reversed.
389
- * @remarks Time O(1)
390
- *
391
- * @returns True if the tree is reversed (e.g., a max-heap logic).
392
- */
393
- get isReverse(): boolean {
394
- return this._isReverse;
395
- }
389
+ * The comparator function used to determine the order of keys in the tree.
396
390
 
397
- /**
398
- * The default comparator function.
399
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
391
+ * @remarks Time O(1) Space O(1)
400
392
  */
401
- protected _comparator: Comparator<K> = (a: K, b: K): number => {
402
- if (isComparable(a) && isComparable(b)) {
403
- if (a > b) return 1;
404
- if (a < b) return -1;
405
- return 0;
406
- }
407
- if (this._specifyComparable) {
408
- const va = this._specifyComparable(a);
409
- const vb = this._specifyComparable(b);
410
- if (va > vb) return 1;
411
- if (va < vb) return -1;
412
- return 0;
413
- }
414
- if (typeof a === 'object' || typeof b === 'object') {
415
- throw TypeError(
416
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
417
- );
418
- }
419
- return 0;
420
- };
393
+ protected _comparator: Comparator<K>;
421
394
 
422
395
  /**
423
396
  * Gets the comparator function used by the tree.
@@ -429,18 +402,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
429
402
  return this._comparator;
430
403
  }
431
404
 
432
- protected _specifyComparable?: (key: K) => Comparable;
433
-
434
- /**
435
- * Gets the function used to extract a comparable value from a complex key.
436
- * @remarks Time O(1)
437
- *
438
- * @returns The key-to-comparable conversion function.
439
- */
440
- get specifyComparable(): ((key: K) => Comparable) | undefined {
441
- return this._specifyComparable;
442
- }
443
-
444
405
  /**
445
406
  * (Protected) Creates a new BST node.
446
407
  * @remarks Time O(1), Space O(1)
@@ -489,9 +450,19 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
489
450
  * @returns True if the key is valid, false otherwise.
490
451
  */
491
452
  override isValidKey(key: any): key is K {
492
- return isComparable(key, this._specifyComparable !== undefined);
453
+ return isComparable(key);
493
454
  }
494
455
 
456
+ override dfs(): (K | undefined)[];
457
+
458
+ override dfs<C extends NodeCallback<BSTNode<K, V>>>(
459
+ callback: C,
460
+ pattern?: DFSOrderPattern,
461
+ onlyOne?: boolean,
462
+ startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
463
+ iterationType?: IterationType
464
+ ): ReturnType<C>[];
465
+
495
466
  /**
496
467
  * Performs a Depth-First Search (DFS) traversal.
497
468
  * @remarks Time O(N), visits every node. Space O(log N) for the call/explicit stack. O(N) worst-case.
@@ -514,6 +485,13 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
514
485
  return super.dfs(callback, pattern, onlyOne, startNode, iterationType);
515
486
  }
516
487
 
488
+ override bfs(): (K | undefined)[];
489
+ override bfs<C extends NodeCallback<BSTNode<K, V>>>(
490
+ callback: C,
491
+ startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
492
+ iterationType?: IterationType
493
+ ): ReturnType<C>[];
494
+
517
495
  /**
518
496
  * Performs a Breadth-First Search (BFS) or Level-Order traversal.
519
497
  * @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
@@ -532,6 +510,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
532
510
  return super.bfs(callback, startNode, iterationType, false);
533
511
  }
534
512
 
513
+ override listLevels(): (K | undefined)[][];
514
+
515
+ override listLevels<C extends NodeCallback<BSTNode<K, V>>>(
516
+ callback: C,
517
+ startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
518
+ iterationType?: IterationType
519
+ ): ReturnType<C>[][];
520
+
535
521
  /**
536
522
  * Returns a 2D array of nodes, grouped by level.
537
523
  * @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
@@ -573,6 +559,33 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
573
559
  return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? undefined;
574
560
  }
575
561
 
562
+ override search(
563
+ keyNodeEntryOrPredicate:
564
+ | K
565
+ | BSTNode<K, V>
566
+ | [K | null | undefined, V | undefined]
567
+ | null
568
+ | undefined
569
+ | NodePredicate<BSTNode<K, V>>
570
+ | Range<K>,
571
+ onlyOne?: boolean
572
+ ): (K | undefined)[];
573
+
574
+ override search<C extends NodeCallback<BSTNode<K, V>>>(
575
+ keyNodeEntryOrPredicate:
576
+ | K
577
+ | BSTNode<K, V>
578
+ | [K | null | undefined, V | undefined]
579
+ | null
580
+ | undefined
581
+ | NodePredicate<BSTNode<K, V>>
582
+ | Range<K>,
583
+ onlyOne: boolean,
584
+ callback: C,
585
+ startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
586
+ iterationType?: IterationType
587
+ ): ReturnType<C>[];
588
+
576
589
  /**
577
590
  * Searches the tree for nodes matching a predicate, key, or range.
578
591
  * @remarks This is an optimized search for a BST. If searching by key or range, it prunes branches.
@@ -625,8 +638,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
625
638
  if (isRange) {
626
639
  // Range search: Only go left if the current key is >= the lower bound
627
640
  const range = keyNodeEntryOrPredicate as Range<K>;
628
- const leftS = this.isReverse ? range.high : range.low;
629
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
641
+ const leftS = range.low;
642
+ const leftI = range.includeLow;
630
643
  return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
631
644
  }
632
645
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -643,8 +656,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
643
656
  if (isRange) {
644
657
  // Range search: Only go right if current key <= upper bound
645
658
  const range = keyNodeEntryOrPredicate as Range<K>;
646
- const rightS = this.isReverse ? range.low : range.high;
647
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
659
+ const rightS = range.high;
660
+ const rightI = range.includeHigh;
648
661
  return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
649
662
  }
650
663
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -669,6 +682,15 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
669
682
  );
670
683
  }
671
684
 
685
+ rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
686
+
687
+ rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(
688
+ range: Range<K> | [K, K],
689
+ callback: C,
690
+ startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
691
+ iterationType?: IterationType
692
+ ): ReturnType<C>[];
693
+
672
694
  /**
673
695
  * Performs an optimized search for nodes within a given key range.
674
696
  * @remarks Time O(H + M), where H is tree height and M is the number of matches.
@@ -846,16 +868,27 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
846
868
  }
847
869
 
848
870
  /**
849
- * Returns the first node with a key greater than or equal to the given key.
850
- * This is equivalent to C++ std::lower_bound on a BST.
851
- * Supports RECURSIVE and ITERATIVE implementations.
852
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
871
+ * Returns the first key with a value >= target.
872
+ * Equivalent to Java TreeMap.ceiling.
873
+ * Time Complexity: O(log n) average, O(h) worst case.
874
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
875
+ */
876
+ ceiling(
877
+ keyNodeEntryOrPredicate:
878
+ | K
879
+ | BSTNode<K, V>
880
+ | [K | null | undefined, V | undefined]
881
+ | null
882
+ | undefined
883
+ | NodePredicate<BSTNode<K, V>>
884
+ ): K | undefined;
885
+
886
+ /**
887
+ * Returns the first node with a key >= target and applies callback.
888
+ * Time Complexity: O(log n) average, O(h) worst case.
853
889
  * Space Complexity: O(h) for recursion, O(1) for iteration.
854
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
855
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
856
- * @returns The first node with key >= given key, or undefined if no such node exists.
857
890
  */
858
- lowerBound(
891
+ ceiling<C extends NodeCallback<BSTNode<K, V>>>(
859
892
  keyNodeEntryOrPredicate:
860
893
  | K
861
894
  | BSTNode<K, V>
@@ -863,22 +896,64 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
863
896
  | null
864
897
  | undefined
865
898
  | NodePredicate<BSTNode<K, V>>,
866
- iterationType: IterationType = this.iterationType
867
- ): BSTNode<K, V> | undefined {
868
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
899
+ callback: C,
900
+ iterationType?: IterationType
901
+ ): ReturnType<C>;
902
+
903
+ ceiling<C extends NodeCallback<BSTNode<K, V>>>(
904
+ keyNodeEntryOrPredicate:
905
+ | K
906
+ | BSTNode<K, V>
907
+ | [K | null | undefined, V | undefined]
908
+ | null
909
+ | undefined
910
+ | NodePredicate<BSTNode<K, V>>,
911
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
912
+ iterationType?: IterationType
913
+ ): K | undefined | ReturnType<C> {
914
+ let actualCallback: C | undefined = undefined;
915
+ let actualIterationType: IterationType = this.iterationType;
916
+
917
+ if (typeof callback === 'string') {
918
+ actualIterationType = callback;
919
+ } else if (callback) {
920
+ actualCallback = callback;
921
+ if (iterationType) {
922
+ actualIterationType = iterationType;
923
+ }
924
+ }
925
+
926
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
927
+
928
+ if (!actualCallback) {
929
+ return node?.key;
930
+ }
931
+
932
+ return node ? actualCallback(node) : undefined;
869
933
  }
870
934
 
871
935
  /**
872
- * Returns the first node with a key strictly greater than the given key.
873
- * This is equivalent to C++ std::upper_bound on a BST.
874
- * Supports RECURSIVE and ITERATIVE implementations.
875
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
936
+ * Returns the first key with a value > target.
937
+ * Equivalent to Java TreeMap.higher.
938
+ * Time Complexity: O(log n) average, O(h) worst case.
876
939
  * Space Complexity: O(h) for recursion, O(1) for iteration.
877
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
878
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
879
- * @returns The first node with key > given key, or undefined if no such node exists.
880
940
  */
881
- upperBound(
941
+ higher(
942
+ keyNodeEntryOrPredicate:
943
+ | K
944
+ | BSTNode<K, V>
945
+ | [K | null | undefined, V | undefined]
946
+ | null
947
+ | undefined
948
+ | NodePredicate<BSTNode<K, V>>
949
+ ): K | undefined;
950
+
951
+ /**
952
+ * Returns the first node with a key > target and applies callback.
953
+ * Time Complexity: O(log n) average, O(h) worst case.
954
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
955
+ */
956
+ higher<C extends NodeCallback<BSTNode<K, V>>>(
882
957
  keyNodeEntryOrPredicate:
883
958
  | K
884
959
  | BSTNode<K, V>
@@ -886,11 +961,261 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
886
961
  | null
887
962
  | undefined
888
963
  | NodePredicate<BSTNode<K, V>>,
889
- iterationType: IterationType = this.iterationType
890
- ): BSTNode<K, V> | undefined {
891
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
964
+ callback: C,
965
+ iterationType?: IterationType
966
+ ): ReturnType<C>;
967
+
968
+ higher<C extends NodeCallback<BSTNode<K, V>>>(
969
+ keyNodeEntryOrPredicate:
970
+ | K
971
+ | BSTNode<K, V>
972
+ | [K | null | undefined, V | undefined]
973
+ | null
974
+ | undefined
975
+ | NodePredicate<BSTNode<K, V>>,
976
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
977
+ iterationType?: IterationType
978
+ ): K | undefined | ReturnType<C> {
979
+ let actualCallback: C | undefined = undefined;
980
+ let actualIterationType: IterationType = this.iterationType;
981
+
982
+ if (typeof callback === 'string') {
983
+ actualIterationType = callback;
984
+ } else if (callback) {
985
+ actualCallback = callback;
986
+ if (iterationType) {
987
+ actualIterationType = iterationType;
988
+ }
989
+ }
990
+
991
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
992
+
993
+ if (!actualCallback) {
994
+ return node?.key;
995
+ }
996
+
997
+ return node ? actualCallback(node) : undefined;
998
+ }
999
+
1000
+ /**
1001
+ * Returns the first key with a value <= target.
1002
+ * Equivalent to Java TreeMap.floor.
1003
+ * Time Complexity: O(log n) average, O(h) worst case.
1004
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
1005
+ */
1006
+ floor(
1007
+ keyNodeEntryOrPredicate:
1008
+ | K
1009
+ | BSTNode<K, V>
1010
+ | [K | null | undefined, V | undefined]
1011
+ | null
1012
+ | undefined
1013
+ | NodePredicate<BSTNode<K, V>>
1014
+ ): K | undefined;
1015
+
1016
+ /**
1017
+ * Returns the first node with a key <= target and applies callback.
1018
+ * Time Complexity: O(log n) average, O(h) worst case.
1019
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
1020
+ */
1021
+ floor<C extends NodeCallback<BSTNode<K, V>>>(
1022
+ keyNodeEntryOrPredicate:
1023
+ | K
1024
+ | BSTNode<K, V>
1025
+ | [K | null | undefined, V | undefined]
1026
+ | null
1027
+ | undefined
1028
+ | NodePredicate<BSTNode<K, V>>,
1029
+ callback: C,
1030
+ iterationType?: IterationType
1031
+ ): ReturnType<C>;
1032
+
1033
+ floor<C extends NodeCallback<BSTNode<K, V>>>(
1034
+ keyNodeEntryOrPredicate:
1035
+ | K
1036
+ | BSTNode<K, V>
1037
+ | [K | null | undefined, V | undefined]
1038
+ | null
1039
+ | undefined
1040
+ | NodePredicate<BSTNode<K, V>>,
1041
+ callback: C = this._DEFAULT_NODE_CALLBACK as C,
1042
+ iterationType?: IterationType
1043
+ ): K | undefined | ReturnType<C> {
1044
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
1045
+ if (typeof callback === 'string' || !callback) {
1046
+ return undefined;
1047
+ }
1048
+ return undefined;
1049
+ }
1050
+
1051
+ let actualCallback: C | undefined = undefined;
1052
+ let actualIterationType: IterationType = this.iterationType;
1053
+
1054
+ if (typeof callback === 'string') {
1055
+ actualIterationType = callback;
1056
+ } else if (callback) {
1057
+ actualCallback = callback;
1058
+ if (iterationType) {
1059
+ actualIterationType = iterationType;
1060
+ }
1061
+ }
1062
+
1063
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
1064
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
1065
+
1066
+ if (!actualCallback) {
1067
+ return node?.key;
1068
+ }
1069
+
1070
+ return node ? actualCallback(node) : undefined;
1071
+ }
1072
+
1073
+ let targetKey: K | undefined;
1074
+ if (this.isNode(keyNodeEntryOrPredicate)) {
1075
+ targetKey = keyNodeEntryOrPredicate.key;
1076
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
1077
+ const key = keyNodeEntryOrPredicate[0];
1078
+ if (key === null || key === undefined) {
1079
+ if (typeof callback === 'string' || !callback) {
1080
+ return undefined;
1081
+ }
1082
+ return undefined;
1083
+ }
1084
+ targetKey = key;
1085
+ } else {
1086
+ targetKey = keyNodeEntryOrPredicate;
1087
+ }
1088
+
1089
+ if (targetKey !== undefined) {
1090
+ const node = this._floorByKey(targetKey, actualIterationType);
1091
+
1092
+ if (!actualCallback) {
1093
+ return node?.key;
1094
+ }
1095
+
1096
+ return node ? actualCallback(node) : undefined;
1097
+ }
1098
+
1099
+ if (typeof callback === 'string' || !callback) {
1100
+ return undefined;
1101
+ }
1102
+ return undefined;
1103
+ }
1104
+
1105
+ /**
1106
+ * Returns the first key with a value < target.
1107
+ * Equivalent to Java TreeMap.lower.
1108
+ * Time Complexity: O(log n) average, O(h) worst case.
1109
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
1110
+ */
1111
+ lower(
1112
+ keyNodeEntryOrPredicate:
1113
+ | K
1114
+ | BSTNode<K, V>
1115
+ | [K | null | undefined, V | undefined]
1116
+ | null
1117
+ | undefined
1118
+ | NodePredicate<BSTNode<K, V>>
1119
+ ): K | undefined;
1120
+
1121
+ /**
1122
+ * Returns the first node with a key < target and applies callback.
1123
+ * Time Complexity: O(log n) average, O(h) worst case.
1124
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
1125
+ */
1126
+ lower<C extends NodeCallback<BSTNode<K, V>>>(
1127
+ keyNodeEntryOrPredicate:
1128
+ | K
1129
+ | BSTNode<K, V>
1130
+ | [K | null | undefined, V | undefined]
1131
+ | null
1132
+ | undefined
1133
+ | NodePredicate<BSTNode<K, V>>,
1134
+ callback: C,
1135
+ iterationType?: IterationType
1136
+ ): ReturnType<C>;
1137
+
1138
+ lower<C extends NodeCallback<BSTNode<K, V>>>(
1139
+ keyNodeEntryOrPredicate:
1140
+ | K
1141
+ | BSTNode<K, V>
1142
+ | [K | null | undefined, V | undefined]
1143
+ | null
1144
+ | undefined
1145
+ | NodePredicate<BSTNode<K, V>>,
1146
+ callback?: C | IterationType,
1147
+ iterationType?: IterationType
1148
+ ): K | undefined | ReturnType<C> {
1149
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === undefined) {
1150
+ if (typeof callback === 'string' || !callback) {
1151
+ return undefined;
1152
+ }
1153
+ return undefined;
1154
+ }
1155
+
1156
+ let actualCallback: C | undefined = undefined;
1157
+ let actualIterationType: IterationType = this.iterationType;
1158
+
1159
+ if (typeof callback === 'string') {
1160
+ actualIterationType = callback;
1161
+ } else if (callback) {
1162
+ actualCallback = callback;
1163
+ if (iterationType) {
1164
+ actualIterationType = iterationType;
1165
+ }
1166
+ }
1167
+
1168
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
1169
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
1170
+
1171
+ if (!actualCallback) {
1172
+ return node?.key;
1173
+ }
1174
+
1175
+ return node ? actualCallback(node) : undefined;
1176
+ }
1177
+
1178
+ let targetKey: K | undefined;
1179
+ if (this.isNode(keyNodeEntryOrPredicate)) {
1180
+ targetKey = keyNodeEntryOrPredicate.key;
1181
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
1182
+ const key = keyNodeEntryOrPredicate[0];
1183
+ if (key === null || key === undefined) {
1184
+ if (typeof callback === 'string' || !callback) {
1185
+ return undefined;
1186
+ }
1187
+ return undefined;
1188
+ }
1189
+ targetKey = key;
1190
+ } else {
1191
+ targetKey = keyNodeEntryOrPredicate;
1192
+ }
1193
+
1194
+ if (targetKey !== undefined) {
1195
+ const node = this._lowerByKey(targetKey, actualIterationType);
1196
+
1197
+ if (!actualCallback) {
1198
+ return node?.key;
1199
+ }
1200
+
1201
+ return node ? actualCallback(node) : undefined;
1202
+ }
1203
+
1204
+ if (typeof callback === 'string' || !callback) {
1205
+ return undefined;
1206
+ }
1207
+ return undefined;
892
1208
  }
893
1209
 
1210
+ lesserOrGreaterTraverse(): (K | undefined)[];
1211
+
1212
+ lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(
1213
+ callback: C,
1214
+ lesserOrGreater?: number,
1215
+ targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
1216
+ iterationType?: IterationType
1217
+ ): ReturnType<C>[];
1218
+
894
1219
  /**
895
1220
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
896
1221
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -1037,7 +1362,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1037
1362
  */
1038
1363
  override map<MK = K, MV = V, MR = any>(
1039
1364
  callback: EntryCallback<K, V | undefined, [MK, MV]>,
1040
- options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
1365
+ options?: Partial<BSTOptions<MK, MV, MR>>,
1041
1366
  thisArg?: unknown
1042
1367
  ): BST<MK, MV, MR> {
1043
1368
  const out = this._createLike<MK, MV, MR>([], options);
@@ -1050,34 +1375,326 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1050
1375
  }
1051
1376
 
1052
1377
  /**
1053
- * Deletes the first node found that satisfies the predicate.
1054
- * @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
1378
+ * Deletes nodes that match a key, node, entry, predicate, or range.
1379
+ *
1380
+ * @remarks
1381
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
1382
+ * Space Complexity: O(M) for storing matched nodes and result map.
1383
+ *
1384
+ * @template K - The key type.
1385
+ * @template V - The value type.
1386
+ *
1387
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
1388
+ * - A key (type K): searches for exact key match using the comparator.
1389
+ * - A BSTNode: searches for the matching node in the tree.
1390
+ * - An entry tuple: searches for the key-value pair.
1391
+ * - A NodePredicate function: tests each node and returns true for matches.
1392
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
1393
+ * - null or undefined: treated as no match, returns empty results.
1055
1394
  *
1056
- * @param predicate - A function to test each [key, value] pair.
1057
- * @returns True if a node was deleted, false otherwise.
1395
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
1396
+ * If false (default), searches for and deletes all matching nodes.
1397
+ *
1398
+ * @param startNode - The node to start the search from. Can be:
1399
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
1400
+ * - null or undefined: defaults to the root, searching the entire tree.
1401
+ * - Default value: this._root (the tree's root).
1402
+ *
1403
+ * @param iterationType - Controls the internal traversal implementation:
1404
+ * - 'RECURSIVE': uses recursive function calls for traversal.
1405
+ * - 'ITERATIVE': uses explicit stack-based iteration.
1406
+ * - Default: this.iterationType (the tree's default iteration mode).
1407
+ *
1408
+ * @returns A Map<K, boolean> containing the deletion results:
1409
+ * - Key: the matched node's key.
1410
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
1411
+ * - If no nodes match the search criteria, the returned map is empty.
1058
1412
  */
1059
- deleteWhere(predicate: (key: K, value: V | undefined, index: number, tree: this) => boolean): boolean {
1060
- const stack: Array<BSTNode<K, V> | null | undefined> = [];
1061
- let cur = this._root as BSTNode<K, V> | null | undefined;
1062
- let index = 0;
1413
+ deleteWhere(
1414
+ keyNodeEntryOrPredicate:
1415
+ | K
1416
+ | BSTNode<K, V>
1417
+ | [K | null | undefined, V | undefined]
1418
+ | null
1419
+ | undefined
1420
+ | NodePredicate<BSTNode<K, V>>
1421
+ | Range<K>,
1422
+ onlyOne = false,
1423
+ startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
1424
+ iterationType: IterationType = this.iterationType
1425
+ ): BinaryTreeDeleteResult<BSTNode<K, V>>[] {
1426
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
1063
1427
 
1064
- // In-order traversal to find the node
1065
- while (stack.length > 0 || cur !== undefined) {
1066
- while (cur !== undefined && cur !== null) {
1067
- stack.push(cur);
1068
- cur = cur.left as BSTNode<K, V> | null | undefined;
1428
+ let results: BinaryTreeDeleteResult<BSTNode<K, V>>[] = [];
1429
+ for (const node of toDelete) {
1430
+ const deleteInfo = this.delete(node);
1431
+ results = results.concat(deleteInfo);
1432
+ }
1433
+
1434
+ return results;
1435
+ }
1436
+
1437
+ /**
1438
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
1439
+ * @remarks Time O(1) Space O(1)
1440
+ * @returns The default comparator function.
1441
+ */
1442
+ protected _createDefaultComparator(): Comparator<K> {
1443
+ return (a: K, b: K): number => {
1444
+ debugger;
1445
+ // If both keys are comparable (primitive types), use direct comparison
1446
+ if (isComparable(a) && isComparable(b)) {
1447
+ if (a > b) return 1;
1448
+ if (a < b) return -1;
1449
+ return 0;
1069
1450
  }
1070
- const node = stack.pop() as BSTNode<K, V> | undefined;
1071
- if (!node) break;
1072
1451
 
1073
- const key = node.key as K;
1074
- const val = node.value as V | undefined;
1075
- if (predicate(key, val, index++, this)) {
1076
- return this._deleteByKey(key); // Found, now delete
1452
+ // If keys are objects and no comparator is provided, throw an error
1453
+ if (typeof a === 'object' || typeof b === 'object') {
1454
+ throw TypeError(
1455
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
1456
+ );
1457
+ }
1458
+
1459
+ // Default: keys are equal (fallback case)
1460
+ return 0;
1461
+ };
1462
+ }
1463
+
1464
+ /**
1465
+ * (Protected) Binary search for floor by key with pruning optimization.
1466
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
1467
+ * Finds first node where key <= target.
1468
+ * @remarks Time O(h) where h is tree height.
1469
+ *
1470
+ * @param key - The target key to search for.
1471
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
1472
+ * @returns The first node with key <= target, or undefined if none exists.
1473
+ */
1474
+ protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined {
1475
+ if (iterationType === 'RECURSIVE') {
1476
+ // Recursive binary search implementation
1477
+ const dfs = (cur: BSTNode<K, V> | null | undefined): BSTNode<K, V> | undefined => {
1478
+ if (!this.isRealNode(cur)) return undefined;
1479
+
1480
+ const cmp = this.comparator(cur.key!, key);
1481
+
1482
+ if (cmp <= 0) {
1483
+ // Current node satisfies the floor condition (cur.key <= target).
1484
+ // Try to find a larger candidate in the right subtree.
1485
+ const rightResult = dfs(cur.right);
1486
+ return rightResult ?? cur;
1487
+ } else {
1488
+ // Current node is too large, move left to find smaller keys.
1489
+ return dfs(cur.left);
1490
+ }
1491
+ };
1492
+
1493
+ return dfs(this.root);
1494
+ } else {
1495
+ // Iterative binary search implementation
1496
+ let current: BSTNode<K, V> | undefined = this.root;
1497
+ let result: BSTNode<K, V> | undefined = undefined;
1498
+
1499
+ while (this.isRealNode(current)) {
1500
+ const cmp = this.comparator(current.key!, key);
1501
+
1502
+ if (cmp <= 0) {
1503
+ // Current node is a candidate. Save it and try right subtree for a larger key.
1504
+ result = current;
1505
+ current = current.right ?? undefined;
1506
+ } else {
1507
+ // Current node is too large, move left.
1508
+ current = current.left ?? undefined;
1509
+ }
1077
1510
  }
1078
- cur = node.right as BSTNode<K, V> | null | undefined;
1511
+
1512
+ return result;
1513
+ }
1514
+ }
1515
+
1516
+ /**
1517
+ * (Protected) In-order traversal search for floor by predicate.
1518
+ * Falls back to linear in-order traversal when predicate-based search is required.
1519
+ * Returns the last node that satisfies the predicate function.
1520
+ * @remarks Time Complexity: O(n) since it may visit every node.
1521
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
1522
+ *
1523
+ * @param predicate - The predicate function to test nodes.
1524
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
1525
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
1526
+ */
1527
+ protected _floorByPredicate(
1528
+ predicate: NodePredicate<BSTNode<K, V>>,
1529
+ iterationType: IterationType
1530
+ ): BSTNode<K, V> | undefined {
1531
+ if (iterationType === 'RECURSIVE') {
1532
+ // Recursive in-order traversal
1533
+ let result: BSTNode<K, V> | undefined = undefined;
1534
+
1535
+ const dfs = (cur: BSTNode<K, V> | null | undefined): void => {
1536
+ if (!this.isRealNode(cur)) return;
1537
+
1538
+ // In-order: process left subtree first
1539
+ if (this.isRealNode(cur.left)) dfs(cur.left);
1540
+
1541
+ // Check current node
1542
+ if (predicate(cur)) {
1543
+ result = cur;
1544
+ }
1545
+
1546
+ // Process right subtree
1547
+ if (this.isRealNode(cur.right)) dfs(cur.right);
1548
+ };
1549
+
1550
+ dfs(this.root);
1551
+ return result;
1552
+ } else {
1553
+ // Iterative in-order traversal using explicit stack
1554
+ const stack: (BSTNode<K, V> | null | undefined)[] = [];
1555
+ let current: BSTNode<K, V> | null | undefined = this.root;
1556
+ let result: BSTNode<K, V> | undefined = undefined;
1557
+
1558
+ while (stack.length > 0 || this.isRealNode(current)) {
1559
+ if (this.isRealNode(current)) {
1560
+ // Go to the leftmost node
1561
+ stack.push(current);
1562
+ current = current.left;
1563
+ } else {
1564
+ // Pop from stack and process
1565
+ const node = stack.pop();
1566
+ if (!this.isRealNode(node)) break;
1567
+
1568
+ // Check if current node satisfies predicate
1569
+ if (predicate(node)) {
1570
+ result = node;
1571
+ }
1572
+
1573
+ // Visit right subtree
1574
+ current = node.right;
1575
+ }
1576
+ }
1577
+
1578
+ return result;
1579
+ }
1580
+ }
1581
+
1582
+ /**
1583
+ * (Protected) Binary search for lower by key with pruning optimization.
1584
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
1585
+ * Finds first node where key < target.
1586
+ * @remarks Time O(h) where h is tree height.
1587
+ *
1588
+ * @param key - The target key to search for.
1589
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
1590
+ * @returns The first node with key < target, or undefined if none exists.
1591
+ */
1592
+ protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined {
1593
+ if (iterationType === 'RECURSIVE') {
1594
+ // Recursive binary search implementation
1595
+ const dfs = (cur: BSTNode<K, V> | null | undefined): BSTNode<K, V> | undefined => {
1596
+ if (!this.isRealNode(cur)) return undefined;
1597
+
1598
+ const cmp = this.comparator(cur.key!, key);
1599
+
1600
+ if (cmp < 0) {
1601
+ // Current node satisfies the lower condition (cur.key < target).
1602
+ // Try to find a larger candidate in the right subtree.
1603
+ const rightResult = dfs(cur.right);
1604
+ return rightResult ?? cur;
1605
+ } else {
1606
+ // Current node is too large or equal, move left to find smaller keys.
1607
+ return dfs(cur.left);
1608
+ }
1609
+ };
1610
+
1611
+ return dfs(this.root);
1612
+ } else {
1613
+ // Iterative binary search implementation
1614
+ let current: BSTNode<K, V> | undefined = this.root;
1615
+ let result: BSTNode<K, V> | undefined = undefined;
1616
+
1617
+ while (this.isRealNode(current)) {
1618
+ const cmp = this.comparator(current.key!, key);
1619
+
1620
+ if (cmp < 0) {
1621
+ // Current node is a candidate. Save it and try right subtree for a larger key.
1622
+ result = current;
1623
+ current = current.right ?? undefined;
1624
+ } else {
1625
+ // Current node is too large or equal, move left.
1626
+ current = current.left ?? undefined;
1627
+ }
1628
+ }
1629
+
1630
+ return result;
1631
+ }
1632
+ }
1633
+
1634
+ /**
1635
+ * (Protected) In-order traversal search for lower by predicate.
1636
+ * Falls back to linear in-order traversal when predicate-based search is required.
1637
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
1638
+ * @remarks Time Complexity: O(n) since it may visit every node.
1639
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
1640
+ *
1641
+ * @param predicate - The predicate function to test nodes.
1642
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
1643
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
1644
+ */
1645
+ protected _lowerByPredicate(
1646
+ predicate: NodePredicate<BSTNode<K, V>>,
1647
+ iterationType: IterationType
1648
+ ): BSTNode<K, V> | undefined {
1649
+ if (iterationType === 'RECURSIVE') {
1650
+ // Recursive in-order traversal
1651
+ let result: BSTNode<K, V> | undefined = undefined;
1652
+
1653
+ const dfs = (cur: BSTNode<K, V> | null | undefined): void => {
1654
+ if (!this.isRealNode(cur)) return;
1655
+
1656
+ // In-order: process left subtree first
1657
+ if (this.isRealNode(cur.left)) dfs(cur.left);
1658
+
1659
+ // Check current node
1660
+ if (predicate(cur)) {
1661
+ result = cur;
1662
+ }
1663
+
1664
+ // Process right subtree
1665
+ if (this.isRealNode(cur.right)) dfs(cur.right);
1666
+ };
1667
+
1668
+ dfs(this.root);
1669
+ return result;
1670
+ } else {
1671
+ // Iterative in-order traversal using explicit stack
1672
+ const stack: (BSTNode<K, V> | null | undefined)[] = [];
1673
+ let current: BSTNode<K, V> | null | undefined = this.root;
1674
+ let result: BSTNode<K, V> | undefined = undefined;
1675
+
1676
+ while (stack.length > 0 || this.isRealNode(current)) {
1677
+ if (this.isRealNode(current)) {
1678
+ // Go to the leftmost node
1679
+ stack.push(current);
1680
+ current = current.left;
1681
+ } else {
1682
+ // Pop from stack and process
1683
+ const node = stack.pop();
1684
+ if (!this.isRealNode(node)) break;
1685
+
1686
+ // Check if current node satisfies predicate
1687
+ if (predicate(node)) {
1688
+ result = node;
1689
+ }
1690
+
1691
+ // Visit right subtree
1692
+ current = node.right;
1693
+ }
1694
+ }
1695
+
1696
+ return result;
1079
1697
  }
1080
- return false;
1081
1698
  }
1082
1699
 
1083
1700
  /**
@@ -1300,8 +1917,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1300
1917
  protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
1301
1918
  return {
1302
1919
  ...super._snapshotOptions<TK, TV, TR>(),
1303
- specifyComparable: this.specifyComparable as BSTOptions<TK, TV, TR>['specifyComparable'],
1304
- isReverse: this.isReverse as BSTOptions<TK, TV, TR>['isReverse']
1920
+ comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator']
1305
1921
  };
1306
1922
  }
1307
1923
 
@@ -1335,14 +1951,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1335
1951
 
1336
1952
  /**
1337
1953
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
1338
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
1954
+ * @remarks Time O(1) Space O(1)
1339
1955
  *
1340
1956
  * @param a - The first key.
1341
1957
  * @param b - The second key.
1342
1958
  * @returns A number (1, -1, or 0) representing the comparison.
1343
1959
  */
1344
1960
  protected _compare(a: K, b: K) {
1345
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
1961
+ return this._comparator(a, b);
1346
1962
  }
1347
1963
 
1348
1964
  /**
@@ -1352,7 +1968,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1352
1968
  * @param key - The key of the node to delete.
1353
1969
  * @returns True if the node was found and deleted, false otherwise.
1354
1970
  */
1355
- private _deleteByKey(key: K): boolean {
1971
+ protected _deleteByKey(key: K): boolean {
1356
1972
  let node = this._root as BSTNode<K, V> | undefined;
1357
1973
 
1358
1974
  // 1. Find the node