min-priority-queue-typed 2.2.4 → 2.2.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.
@@ -331,7 +331,7 @@ export class BSTNode<K = any, V = any> {
331
331
  * return findFirstCommon(path1, path2);
332
332
  * };
333
333
  *
334
- * function findFirstCommon(arr1: number[], arr2: number[]): number | undefined {
334
+ * function findFirstCommon(arr1: (number | undefined)[], arr2: (number | undefined)[]): number | undefined {
335
335
  * for (const num of arr1) {
336
336
  * if (arr2.indexOf(num) !== -1) {
337
337
  * return num;
@@ -354,15 +354,12 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
354
354
  * @param [options] - Configuration options for the BST, including comparator.
355
355
  */
356
356
  constructor(
357
- keysNodesEntriesOrRaws: Iterable<
358
- K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R
359
- > = [],
357
+ keysNodesEntriesOrRaws: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R> = [],
360
358
  options?: BSTOptions<K, V, R>
361
359
  ) {
362
360
  super([], options);
363
361
 
364
362
  if (options) {
365
-
366
363
  // Use the 'in' operator to check if the field is present
367
364
  if ('comparator' in options && options.comparator !== undefined) {
368
365
  this._comparator = options.comparator;
@@ -376,7 +373,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
376
373
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
377
374
  }
378
375
 
379
-
380
376
  protected override _root?: BSTNode<K, V> = undefined;
381
377
 
382
378
  /**
@@ -389,33 +385,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
389
385
  return this._root;
390
386
  }
391
387
 
392
- /**
393
- * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
394
- * @remarks Time O(1) Space O(1)
395
- * @returns The default comparator function.
396
- */
397
- protected _createDefaultComparator(): Comparator<K> {
398
- return (a: K, b: K): number => {
399
- debugger
400
- // If both keys are comparable (primitive types), use direct comparison
401
- if (isComparable(a) && isComparable(b)) {
402
- if (a > b) return 1;
403
- if (a < b) return -1;
404
- return 0;
405
- }
406
-
407
- // If keys are objects and no comparator is provided, throw an error
408
- if (typeof a === 'object' || typeof b === 'object') {
409
- throw TypeError(
410
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
411
- );
412
- }
413
-
414
- // Default: keys are equal (fallback case)
415
- return 0;
416
- };
417
- }
418
-
419
388
  /**
420
389
  * The comparator function used to determine the order of keys in the tree.
421
390
 
@@ -423,7 +392,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
423
392
  */
424
393
  protected _comparator: Comparator<K>;
425
394
 
426
-
427
395
  /**
428
396
  * Gets the comparator function used by the tree.
429
397
  * @remarks Time O(1)
@@ -485,6 +453,16 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
485
453
  return isComparable(key);
486
454
  }
487
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
+
488
466
  /**
489
467
  * Performs a Depth-First Search (DFS) traversal.
490
468
  * @remarks Time O(N), visits every node. Space O(log N) for the call/explicit stack. O(N) worst-case.
@@ -507,6 +485,13 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
507
485
  return super.dfs(callback, pattern, onlyOne, startNode, iterationType);
508
486
  }
509
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
+
510
495
  /**
511
496
  * Performs a Breadth-First Search (BFS) or Level-Order traversal.
512
497
  * @remarks Time O(N), visits every node. Space O(N) in the worst case for the queue.
@@ -525,6 +510,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
525
510
  return super.bfs(callback, startNode, iterationType, false);
526
511
  }
527
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
+
528
521
  /**
529
522
  * Returns a 2D array of nodes, grouped by level.
530
523
  * @remarks Time O(N), visits every node. Space O(N) for the result array and the queue/stack.
@@ -566,6 +559,33 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
566
559
  return this.getNodes(keyNodeEntryOrPredicate, true, startNode, iterationType)[0] ?? undefined;
567
560
  }
568
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
+
569
589
  /**
570
590
  * Searches the tree for nodes matching a predicate, key, or range.
571
591
  * @remarks This is an optimized search for a BST. If searching by key or range, it prunes branches.
@@ -618,8 +638,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
618
638
  if (isRange) {
619
639
  // Range search: Only go left if the current key is >= the lower bound
620
640
  const range = keyNodeEntryOrPredicate as Range<K>;
621
- const leftS = range.low;
622
- const leftI = range.includeLow;
641
+ const leftS = range.low;
642
+ const leftI = range.includeLow;
623
643
  return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
624
644
  }
625
645
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -636,8 +656,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
636
656
  if (isRange) {
637
657
  // Range search: Only go right if current key <= upper bound
638
658
  const range = keyNodeEntryOrPredicate as Range<K>;
639
- const rightS = range.high;
640
- const rightI = range.includeHigh;
659
+ const rightS = range.high;
660
+ const rightI = range.includeHigh;
641
661
  return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
642
662
  }
643
663
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -662,6 +682,15 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
662
682
  );
663
683
  }
664
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
+
665
694
  /**
666
695
  * Performs an optimized search for nodes within a given key range.
667
696
  * @remarks Time O(H + M), where H is tree height and M is the number of matches.
@@ -839,16 +868,27 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
839
868
  }
840
869
 
841
870
  /**
842
- * Returns the first node with a key greater than or equal to the given key.
843
- * This is equivalent to C++ std::lower_bound on a BST.
844
- * Supports RECURSIVE and ITERATIVE implementations.
845
- * 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.
846
874
  * Space Complexity: O(h) for recursion, O(1) for iteration.
847
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
848
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
849
- * @returns The first node with key >= given key, or undefined if no such node exists.
850
875
  */
851
- lowerBound(
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.
889
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
890
+ */
891
+ ceiling<C extends NodeCallback<BSTNode<K, V>>>(
852
892
  keyNodeEntryOrPredicate:
853
893
  | K
854
894
  | BSTNode<K, V>
@@ -856,22 +896,64 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
856
896
  | null
857
897
  | undefined
858
898
  | NodePredicate<BSTNode<K, V>>,
859
- iterationType: IterationType = this.iterationType
860
- ): BSTNode<K, V> | undefined {
861
- 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;
862
933
  }
863
934
 
864
935
  /**
865
- * Returns the first node with a key strictly greater than the given key.
866
- * This is equivalent to C++ std::upper_bound on a BST.
867
- * Supports RECURSIVE and ITERATIVE implementations.
868
- * 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.
869
939
  * Space Complexity: O(h) for recursion, O(1) for iteration.
870
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
871
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
872
- * @returns The first node with key > given key, or undefined if no such node exists.
873
940
  */
874
- 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>>>(
875
957
  keyNodeEntryOrPredicate:
876
958
  | K
877
959
  | BSTNode<K, V>
@@ -879,11 +961,261 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
879
961
  | null
880
962
  | undefined
881
963
  | NodePredicate<BSTNode<K, V>>,
882
- iterationType: IterationType = this.iterationType
883
- ): BSTNode<K, V> | undefined {
884
- 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;
885
1103
  }
886
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;
1208
+ }
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
+
887
1219
  /**
888
1220
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
889
1221
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -1090,17 +1422,10 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1090
1422
  onlyOne = false,
1091
1423
  startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
1092
1424
  iterationType: IterationType = this.iterationType
1093
- ): BinaryTreeDeleteResult<BSTNode<K, V>>[] {
1425
+ ): BinaryTreeDeleteResult<BSTNode<K, V>>[] {
1426
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, node => node, startNode, iterationType);
1094
1427
 
1095
- const toDelete = this.search (
1096
- keyNodeEntryOrPredicate,
1097
- onlyOne,
1098
- (node) => node,
1099
- startNode,
1100
- iterationType
1101
- );
1102
-
1103
- let results : BinaryTreeDeleteResult<BSTNode<K, V>>[] = [];
1428
+ let results: BinaryTreeDeleteResult<BSTNode<K, V>>[] = [];
1104
1429
  for (const node of toDelete) {
1105
1430
  const deleteInfo = this.delete(node);
1106
1431
  results = results.concat(deleteInfo);
@@ -1109,6 +1434,268 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1109
1434
  return results;
1110
1435
  }
1111
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;
1450
+ }
1451
+
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
+ }
1510
+ }
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;
1697
+ }
1698
+ }
1112
1699
 
1113
1700
  /**
1114
1701
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -1330,7 +1917,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
1330
1917
  protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
1331
1918
  return {
1332
1919
  ...super._snapshotOptions<TK, TV, TR>(),
1333
- comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator'],
1920
+ comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator']
1334
1921
  };
1335
1922
  }
1336
1923