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