tree-multimap-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 +385 -101
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +387 -101
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +385 -101
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +387 -101
  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/tree-multimap-typed.js +387 -101
  20. package/dist/umd/tree-multimap-typed.js.map +1 -1
  21. package/dist/umd/tree-multimap-typed.min.js +3 -3
  22. package/dist/umd/tree-multimap-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
@@ -1000,6 +1000,14 @@ var IterableEntryBase = class {
1000
1000
  }
1001
1001
  return accumulator;
1002
1002
  }
1003
+ /**
1004
+ * Converts data structure to `[key, value]` pairs.
1005
+ * @returns Array of entries.
1006
+ * @remarks Time O(n), Space O(n)
1007
+ */
1008
+ toArray() {
1009
+ return [...this];
1010
+ }
1003
1011
  /**
1004
1012
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
1005
1013
  * @returns Array of entries (default) or a string.
@@ -1029,8 +1037,6 @@ var Range = class {
1029
1037
  this.high = high;
1030
1038
  this.includeLow = includeLow;
1031
1039
  this.includeHigh = includeHigh;
1032
- if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
1033
- if (low > high) throw new RangeError("low must be less than or equal to high");
1034
1040
  }
1035
1041
  static {
1036
1042
  __name(this, "Range");
@@ -2835,9 +2841,13 @@ var BST = class extends BinaryTree {
2835
2841
  constructor(keysNodesEntriesOrRaws = [], options) {
2836
2842
  super([], options);
2837
2843
  if (options) {
2838
- const { specifyComparable, isReverse } = options;
2839
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2840
- if (isReverse !== void 0) this._isReverse = isReverse;
2844
+ if ("comparator" in options && options.comparator !== void 0) {
2845
+ this._comparator = options.comparator;
2846
+ } else {
2847
+ this._comparator = this._createDefaultComparator();
2848
+ }
2849
+ } else {
2850
+ this._comparator = this._createDefaultComparator();
2841
2851
  }
2842
2852
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2843
2853
  }
@@ -2851,40 +2861,12 @@ var BST = class extends BinaryTree {
2851
2861
  get root() {
2852
2862
  return this._root;
2853
2863
  }
2854
- _isReverse = false;
2855
2864
  /**
2856
- * Gets whether the tree's comparison logic is reversed.
2857
- * @remarks Time O(1)
2858
- *
2859
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2860
- */
2861
- get isReverse() {
2862
- return this._isReverse;
2863
- }
2864
- /**
2865
- * The default comparator function.
2866
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2867
- */
2868
- _comparator = /* @__PURE__ */ __name((a, b) => {
2869
- if (isComparable(a) && isComparable(b)) {
2870
- if (a > b) return 1;
2871
- if (a < b) return -1;
2872
- return 0;
2873
- }
2874
- if (this._specifyComparable) {
2875
- const va = this._specifyComparable(a);
2876
- const vb = this._specifyComparable(b);
2877
- if (va > vb) return 1;
2878
- if (va < vb) return -1;
2879
- return 0;
2880
- }
2881
- if (typeof a === "object" || typeof b === "object") {
2882
- throw TypeError(
2883
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2884
- );
2885
- }
2886
- return 0;
2887
- }, "_comparator");
2865
+ * The comparator function used to determine the order of keys in the tree.
2866
+
2867
+ * @remarks Time O(1) Space O(1)
2868
+ */
2869
+ _comparator;
2888
2870
  /**
2889
2871
  * Gets the comparator function used by the tree.
2890
2872
  * @remarks Time O(1)
@@ -2894,16 +2876,6 @@ var BST = class extends BinaryTree {
2894
2876
  get comparator() {
2895
2877
  return this._comparator;
2896
2878
  }
2897
- _specifyComparable;
2898
- /**
2899
- * Gets the function used to extract a comparable value from a complex key.
2900
- * @remarks Time O(1)
2901
- *
2902
- * @returns The key-to-comparable conversion function.
2903
- */
2904
- get specifyComparable() {
2905
- return this._specifyComparable;
2906
- }
2907
2879
  /**
2908
2880
  * (Protected) Creates a new BST node.
2909
2881
  * @remarks Time O(1), Space O(1)
@@ -2944,7 +2916,7 @@ var BST = class extends BinaryTree {
2944
2916
  * @returns True if the key is valid, false otherwise.
2945
2917
  */
2946
2918
  isValidKey(key) {
2947
- return isComparable(key, this._specifyComparable !== void 0);
2919
+ return isComparable(key);
2948
2920
  }
2949
2921
  /**
2950
2922
  * Performs a Depth-First Search (DFS) traversal.
@@ -3033,8 +3005,8 @@ var BST = class extends BinaryTree {
3033
3005
  if (!this.isRealNode(cur.left)) return false;
3034
3006
  if (isRange) {
3035
3007
  const range = keyNodeEntryOrPredicate;
3036
- const leftS = this.isReverse ? range.high : range.low;
3037
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3008
+ const leftS = range.low;
3009
+ const leftI = range.includeLow;
3038
3010
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3039
3011
  }
3040
3012
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3048,8 +3020,8 @@ var BST = class extends BinaryTree {
3048
3020
  if (!this.isRealNode(cur.right)) return false;
3049
3021
  if (isRange) {
3050
3022
  const range = keyNodeEntryOrPredicate;
3051
- const rightS = this.isReverse ? range.low : range.high;
3052
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3023
+ const rightS = range.high;
3024
+ const rightI = range.includeHigh;
3053
3025
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3054
3026
  }
3055
3027
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3210,31 +3182,141 @@ var BST = class extends BinaryTree {
3210
3182
  else _iterate();
3211
3183
  return inserted;
3212
3184
  }
3213
- /**
3214
- * Returns the first node with a key greater than or equal to the given key.
3215
- * This is equivalent to C++ std::lower_bound on a BST.
3216
- * Supports RECURSIVE and ITERATIVE implementations.
3217
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3218
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3219
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3220
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3221
- * @returns The first node with key >= given key, or undefined if no such node exists.
3222
- */
3223
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3224
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3185
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3186
+ let actualCallback = void 0;
3187
+ let actualIterationType = this.iterationType;
3188
+ if (typeof callback === "string") {
3189
+ actualIterationType = callback;
3190
+ } else if (callback) {
3191
+ actualCallback = callback;
3192
+ if (iterationType) {
3193
+ actualIterationType = iterationType;
3194
+ }
3195
+ }
3196
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
3197
+ if (!actualCallback) {
3198
+ return node?.key;
3199
+ }
3200
+ return node ? actualCallback(node) : void 0;
3201
+ }
3202
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3203
+ let actualCallback = void 0;
3204
+ let actualIterationType = this.iterationType;
3205
+ if (typeof callback === "string") {
3206
+ actualIterationType = callback;
3207
+ } else if (callback) {
3208
+ actualCallback = callback;
3209
+ if (iterationType) {
3210
+ actualIterationType = iterationType;
3211
+ }
3212
+ }
3213
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
3214
+ if (!actualCallback) {
3215
+ return node?.key;
3216
+ }
3217
+ return node ? actualCallback(node) : void 0;
3225
3218
  }
3226
- /**
3227
- * Returns the first node with a key strictly greater than the given key.
3228
- * This is equivalent to C++ std::upper_bound on a BST.
3229
- * Supports RECURSIVE and ITERATIVE implementations.
3230
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3231
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3232
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3233
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3234
- * @returns The first node with key > given key, or undefined if no such node exists.
3235
- */
3236
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3237
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3219
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3220
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3221
+ if (typeof callback === "string" || !callback) {
3222
+ return void 0;
3223
+ }
3224
+ return void 0;
3225
+ }
3226
+ let actualCallback = void 0;
3227
+ let actualIterationType = this.iterationType;
3228
+ if (typeof callback === "string") {
3229
+ actualIterationType = callback;
3230
+ } else if (callback) {
3231
+ actualCallback = callback;
3232
+ if (iterationType) {
3233
+ actualIterationType = iterationType;
3234
+ }
3235
+ }
3236
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3237
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3238
+ if (!actualCallback) {
3239
+ return node?.key;
3240
+ }
3241
+ return node ? actualCallback(node) : void 0;
3242
+ }
3243
+ let targetKey;
3244
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3245
+ targetKey = keyNodeEntryOrPredicate.key;
3246
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3247
+ const key = keyNodeEntryOrPredicate[0];
3248
+ if (key === null || key === void 0) {
3249
+ if (typeof callback === "string" || !callback) {
3250
+ return void 0;
3251
+ }
3252
+ return void 0;
3253
+ }
3254
+ targetKey = key;
3255
+ } else {
3256
+ targetKey = keyNodeEntryOrPredicate;
3257
+ }
3258
+ if (targetKey !== void 0) {
3259
+ const node = this._floorByKey(targetKey, actualIterationType);
3260
+ if (!actualCallback) {
3261
+ return node?.key;
3262
+ }
3263
+ return node ? actualCallback(node) : void 0;
3264
+ }
3265
+ if (typeof callback === "string" || !callback) {
3266
+ return void 0;
3267
+ }
3268
+ return void 0;
3269
+ }
3270
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
3271
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3272
+ if (typeof callback === "string" || !callback) {
3273
+ return void 0;
3274
+ }
3275
+ return void 0;
3276
+ }
3277
+ let actualCallback = void 0;
3278
+ let actualIterationType = this.iterationType;
3279
+ if (typeof callback === "string") {
3280
+ actualIterationType = callback;
3281
+ } else if (callback) {
3282
+ actualCallback = callback;
3283
+ if (iterationType) {
3284
+ actualIterationType = iterationType;
3285
+ }
3286
+ }
3287
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3288
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3289
+ if (!actualCallback) {
3290
+ return node?.key;
3291
+ }
3292
+ return node ? actualCallback(node) : void 0;
3293
+ }
3294
+ let targetKey;
3295
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3296
+ targetKey = keyNodeEntryOrPredicate.key;
3297
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3298
+ const key = keyNodeEntryOrPredicate[0];
3299
+ if (key === null || key === void 0) {
3300
+ if (typeof callback === "string" || !callback) {
3301
+ return void 0;
3302
+ }
3303
+ return void 0;
3304
+ }
3305
+ targetKey = key;
3306
+ } else {
3307
+ targetKey = keyNodeEntryOrPredicate;
3308
+ }
3309
+ if (targetKey !== void 0) {
3310
+ const node = this._lowerByKey(targetKey, actualIterationType);
3311
+ if (!actualCallback) {
3312
+ return node?.key;
3313
+ }
3314
+ return node ? actualCallback(node) : void 0;
3315
+ }
3316
+ if (typeof callback === "string" || !callback) {
3317
+ return void 0;
3318
+ }
3319
+ return void 0;
3238
3320
  }
3239
3321
  /**
3240
3322
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
@@ -3370,31 +3452,234 @@ var BST = class extends BinaryTree {
3370
3452
  return out;
3371
3453
  }
3372
3454
  /**
3373
- * Deletes the first node found that satisfies the predicate.
3374
- * @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.
3455
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3375
3456
  *
3376
- * @param predicate - A function to test each [key, value] pair.
3377
- * @returns True if a node was deleted, false otherwise.
3457
+ * @remarks
3458
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3459
+ * Space Complexity: O(M) for storing matched nodes and result map.
3460
+ *
3461
+ * @template K - The key type.
3462
+ * @template V - The value type.
3463
+ *
3464
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3465
+ * - A key (type K): searches for exact key match using the comparator.
3466
+ * - A BSTNode: searches for the matching node in the tree.
3467
+ * - An entry tuple: searches for the key-value pair.
3468
+ * - A NodePredicate function: tests each node and returns true for matches.
3469
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3470
+ * - null or undefined: treated as no match, returns empty results.
3471
+ *
3472
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3473
+ * If false (default), searches for and deletes all matching nodes.
3474
+ *
3475
+ * @param startNode - The node to start the search from. Can be:
3476
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3477
+ * - null or undefined: defaults to the root, searching the entire tree.
3478
+ * - Default value: this._root (the tree's root).
3479
+ *
3480
+ * @param iterationType - Controls the internal traversal implementation:
3481
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3482
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3483
+ * - Default: this.iterationType (the tree's default iteration mode).
3484
+ *
3485
+ * @returns A Map<K, boolean> containing the deletion results:
3486
+ * - Key: the matched node's key.
3487
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3488
+ * - If no nodes match the search criteria, the returned map is empty.
3489
+ */
3490
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3491
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
3492
+ let results = [];
3493
+ for (const node of toDelete) {
3494
+ const deleteInfo = this.delete(node);
3495
+ results = results.concat(deleteInfo);
3496
+ }
3497
+ return results;
3498
+ }
3499
+ /**
3500
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
3501
+ * @remarks Time O(1) Space O(1)
3502
+ * @returns The default comparator function.
3378
3503
  */
3379
- deleteWhere(predicate) {
3380
- const stack = [];
3381
- let cur = this._root;
3382
- let index = 0;
3383
- while (stack.length > 0 || cur !== void 0) {
3384
- while (cur !== void 0 && cur !== null) {
3385
- stack.push(cur);
3386
- cur = cur.left;
3504
+ _createDefaultComparator() {
3505
+ return (a, b) => {
3506
+ debugger;
3507
+ if (isComparable(a) && isComparable(b)) {
3508
+ if (a > b) return 1;
3509
+ if (a < b) return -1;
3510
+ return 0;
3511
+ }
3512
+ if (typeof a === "object" || typeof b === "object") {
3513
+ throw TypeError(
3514
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3515
+ );
3516
+ }
3517
+ return 0;
3518
+ };
3519
+ }
3520
+ /**
3521
+ * (Protected) Binary search for floor by key with pruning optimization.
3522
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3523
+ * Finds first node where key <= target.
3524
+ * @remarks Time O(h) where h is tree height.
3525
+ *
3526
+ * @param key - The target key to search for.
3527
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3528
+ * @returns The first node with key <= target, or undefined if none exists.
3529
+ */
3530
+ _floorByKey(key, iterationType) {
3531
+ if (iterationType === "RECURSIVE") {
3532
+ const dfs = /* @__PURE__ */ __name((cur) => {
3533
+ if (!this.isRealNode(cur)) return void 0;
3534
+ const cmp = this.comparator(cur.key, key);
3535
+ if (cmp <= 0) {
3536
+ const rightResult = dfs(cur.right);
3537
+ return rightResult ?? cur;
3538
+ } else {
3539
+ return dfs(cur.left);
3540
+ }
3541
+ }, "dfs");
3542
+ return dfs(this.root);
3543
+ } else {
3544
+ let current = this.root;
3545
+ let result = void 0;
3546
+ while (this.isRealNode(current)) {
3547
+ const cmp = this.comparator(current.key, key);
3548
+ if (cmp <= 0) {
3549
+ result = current;
3550
+ current = current.right ?? void 0;
3551
+ } else {
3552
+ current = current.left ?? void 0;
3553
+ }
3387
3554
  }
3388
- const node = stack.pop();
3389
- if (!node) break;
3390
- const key = node.key;
3391
- const val = node.value;
3392
- if (predicate(key, val, index++, this)) {
3393
- return this._deleteByKey(key);
3555
+ return result;
3556
+ }
3557
+ }
3558
+ /**
3559
+ * (Protected) In-order traversal search for floor by predicate.
3560
+ * Falls back to linear in-order traversal when predicate-based search is required.
3561
+ * Returns the last node that satisfies the predicate function.
3562
+ * @remarks Time Complexity: O(n) since it may visit every node.
3563
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3564
+ *
3565
+ * @param predicate - The predicate function to test nodes.
3566
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3567
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
3568
+ */
3569
+ _floorByPredicate(predicate, iterationType) {
3570
+ if (iterationType === "RECURSIVE") {
3571
+ let result = void 0;
3572
+ const dfs = /* @__PURE__ */ __name((cur) => {
3573
+ if (!this.isRealNode(cur)) return;
3574
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3575
+ if (predicate(cur)) {
3576
+ result = cur;
3577
+ }
3578
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3579
+ }, "dfs");
3580
+ dfs(this.root);
3581
+ return result;
3582
+ } else {
3583
+ const stack = [];
3584
+ let current = this.root;
3585
+ let result = void 0;
3586
+ while (stack.length > 0 || this.isRealNode(current)) {
3587
+ if (this.isRealNode(current)) {
3588
+ stack.push(current);
3589
+ current = current.left;
3590
+ } else {
3591
+ const node = stack.pop();
3592
+ if (!this.isRealNode(node)) break;
3593
+ if (predicate(node)) {
3594
+ result = node;
3595
+ }
3596
+ current = node.right;
3597
+ }
3394
3598
  }
3395
- cur = node.right;
3599
+ return result;
3600
+ }
3601
+ }
3602
+ /**
3603
+ * (Protected) Binary search for lower by key with pruning optimization.
3604
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3605
+ * Finds first node where key < target.
3606
+ * @remarks Time O(h) where h is tree height.
3607
+ *
3608
+ * @param key - The target key to search for.
3609
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3610
+ * @returns The first node with key < target, or undefined if none exists.
3611
+ */
3612
+ _lowerByKey(key, iterationType) {
3613
+ if (iterationType === "RECURSIVE") {
3614
+ const dfs = /* @__PURE__ */ __name((cur) => {
3615
+ if (!this.isRealNode(cur)) return void 0;
3616
+ const cmp = this.comparator(cur.key, key);
3617
+ if (cmp < 0) {
3618
+ const rightResult = dfs(cur.right);
3619
+ return rightResult ?? cur;
3620
+ } else {
3621
+ return dfs(cur.left);
3622
+ }
3623
+ }, "dfs");
3624
+ return dfs(this.root);
3625
+ } else {
3626
+ let current = this.root;
3627
+ let result = void 0;
3628
+ while (this.isRealNode(current)) {
3629
+ const cmp = this.comparator(current.key, key);
3630
+ if (cmp < 0) {
3631
+ result = current;
3632
+ current = current.right ?? void 0;
3633
+ } else {
3634
+ current = current.left ?? void 0;
3635
+ }
3636
+ }
3637
+ return result;
3638
+ }
3639
+ }
3640
+ /**
3641
+ * (Protected) In-order traversal search for lower by predicate.
3642
+ * Falls back to linear in-order traversal when predicate-based search is required.
3643
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
3644
+ * @remarks Time Complexity: O(n) since it may visit every node.
3645
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3646
+ *
3647
+ * @param predicate - The predicate function to test nodes.
3648
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3649
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
3650
+ */
3651
+ _lowerByPredicate(predicate, iterationType) {
3652
+ if (iterationType === "RECURSIVE") {
3653
+ let result = void 0;
3654
+ const dfs = /* @__PURE__ */ __name((cur) => {
3655
+ if (!this.isRealNode(cur)) return;
3656
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3657
+ if (predicate(cur)) {
3658
+ result = cur;
3659
+ }
3660
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3661
+ }, "dfs");
3662
+ dfs(this.root);
3663
+ return result;
3664
+ } else {
3665
+ const stack = [];
3666
+ let current = this.root;
3667
+ let result = void 0;
3668
+ while (stack.length > 0 || this.isRealNode(current)) {
3669
+ if (this.isRealNode(current)) {
3670
+ stack.push(current);
3671
+ current = current.left;
3672
+ } else {
3673
+ const node = stack.pop();
3674
+ if (!this.isRealNode(node)) break;
3675
+ if (predicate(node)) {
3676
+ result = node;
3677
+ }
3678
+ current = node.right;
3679
+ }
3680
+ }
3681
+ return result;
3396
3682
  }
3397
- return false;
3398
3683
  }
3399
3684
  /**
3400
3685
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -3546,8 +3831,7 @@ var BST = class extends BinaryTree {
3546
3831
  _snapshotOptions() {
3547
3832
  return {
3548
3833
  ...super._snapshotOptions(),
3549
- specifyComparable: this.specifyComparable,
3550
- isReverse: this.isReverse
3834
+ comparator: this._comparator
3551
3835
  };
3552
3836
  }
3553
3837
  /**
@@ -3575,14 +3859,14 @@ var BST = class extends BinaryTree {
3575
3859
  }
3576
3860
  /**
3577
3861
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3578
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3862
+ * @remarks Time O(1) Space O(1)
3579
3863
  *
3580
3864
  * @param a - The first key.
3581
3865
  * @param b - The second key.
3582
3866
  * @returns A number (1, -1, or 0) representing the comparison.
3583
3867
  */
3584
3868
  _compare(a, b) {
3585
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3869
+ return this._comparator(a, b);
3586
3870
  }
3587
3871
  /**
3588
3872
  * (Private) Deletes a node by its key.