avl-tree-typed 2.2.3 → 2.2.5
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.
- package/dist/cjs/index.cjs +341 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +343 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +341 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +343 -75
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +142 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/avl-tree-typed.js +343 -75
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +495 -85
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -2824,36 +2824,20 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2824
2824
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2825
2825
|
super([], options);
|
|
2826
2826
|
__publicField(this, "_root");
|
|
2827
|
-
__publicField(this, "_isReverse", false);
|
|
2828
2827
|
/**
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
if (a > b) return 1;
|
|
2835
|
-
if (a < b) return -1;
|
|
2836
|
-
return 0;
|
|
2837
|
-
}
|
|
2838
|
-
if (this._specifyComparable) {
|
|
2839
|
-
const va = this._specifyComparable(a);
|
|
2840
|
-
const vb = this._specifyComparable(b);
|
|
2841
|
-
if (va > vb) return 1;
|
|
2842
|
-
if (va < vb) return -1;
|
|
2843
|
-
return 0;
|
|
2844
|
-
}
|
|
2845
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2846
|
-
throw TypeError(
|
|
2847
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2848
|
-
);
|
|
2849
|
-
}
|
|
2850
|
-
return 0;
|
|
2851
|
-
}, "_comparator"));
|
|
2852
|
-
__publicField(this, "_specifyComparable");
|
|
2828
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2829
|
+
|
|
2830
|
+
* @remarks Time O(1) Space O(1)
|
|
2831
|
+
*/
|
|
2832
|
+
__publicField(this, "_comparator");
|
|
2853
2833
|
if (options) {
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2834
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2835
|
+
this._comparator = options.comparator;
|
|
2836
|
+
} else {
|
|
2837
|
+
this._comparator = this._createDefaultComparator();
|
|
2838
|
+
}
|
|
2839
|
+
} else {
|
|
2840
|
+
this._comparator = this._createDefaultComparator();
|
|
2857
2841
|
}
|
|
2858
2842
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2859
2843
|
}
|
|
@@ -2866,15 +2850,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2866
2850
|
get root() {
|
|
2867
2851
|
return this._root;
|
|
2868
2852
|
}
|
|
2869
|
-
/**
|
|
2870
|
-
* Gets whether the tree's comparison logic is reversed.
|
|
2871
|
-
* @remarks Time O(1)
|
|
2872
|
-
*
|
|
2873
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2874
|
-
*/
|
|
2875
|
-
get isReverse() {
|
|
2876
|
-
return this._isReverse;
|
|
2877
|
-
}
|
|
2878
2853
|
/**
|
|
2879
2854
|
* Gets the comparator function used by the tree.
|
|
2880
2855
|
* @remarks Time O(1)
|
|
@@ -2884,15 +2859,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2884
2859
|
get comparator() {
|
|
2885
2860
|
return this._comparator;
|
|
2886
2861
|
}
|
|
2887
|
-
/**
|
|
2888
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2889
|
-
* @remarks Time O(1)
|
|
2890
|
-
*
|
|
2891
|
-
* @returns The key-to-comparable conversion function.
|
|
2892
|
-
*/
|
|
2893
|
-
get specifyComparable() {
|
|
2894
|
-
return this._specifyComparable;
|
|
2895
|
-
}
|
|
2896
2862
|
/**
|
|
2897
2863
|
* (Protected) Creates a new BST node.
|
|
2898
2864
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2934,7 +2900,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2934
2900
|
* @returns True if the key is valid, false otherwise.
|
|
2935
2901
|
*/
|
|
2936
2902
|
isValidKey(key) {
|
|
2937
|
-
return isComparable(key
|
|
2903
|
+
return isComparable(key);
|
|
2938
2904
|
}
|
|
2939
2905
|
/**
|
|
2940
2906
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3024,8 +2990,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3024
2990
|
if (!this.isRealNode(cur.left)) return false;
|
|
3025
2991
|
if (isRange) {
|
|
3026
2992
|
const range = keyNodeEntryOrPredicate;
|
|
3027
|
-
const leftS =
|
|
3028
|
-
const leftI =
|
|
2993
|
+
const leftS = range.low;
|
|
2994
|
+
const leftI = range.includeLow;
|
|
3029
2995
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
3030
2996
|
}
|
|
3031
2997
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3039,8 +3005,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3039
3005
|
if (!this.isRealNode(cur.right)) return false;
|
|
3040
3006
|
if (isRange) {
|
|
3041
3007
|
const range = keyNodeEntryOrPredicate;
|
|
3042
|
-
const rightS =
|
|
3043
|
-
const rightI =
|
|
3008
|
+
const rightS = range.high;
|
|
3009
|
+
const rightI = range.includeHigh;
|
|
3044
3010
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
3045
3011
|
}
|
|
3046
3012
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3227,6 +3193,104 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3227
3193
|
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3228
3194
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3229
3195
|
}
|
|
3196
|
+
/**
|
|
3197
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3198
|
+
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
3199
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3200
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3201
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3202
|
+
*
|
|
3203
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3204
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3205
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3206
|
+
*/
|
|
3207
|
+
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3208
|
+
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
3209
|
+
}
|
|
3210
|
+
/**
|
|
3211
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3212
|
+
* This is equivalent to Java TreeMap.higherEntry().
|
|
3213
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3214
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3215
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3216
|
+
*
|
|
3217
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3218
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3219
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3220
|
+
*/
|
|
3221
|
+
higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3222
|
+
return this.upperBound(keyNodeEntryOrPredicate, iterationType);
|
|
3223
|
+
}
|
|
3224
|
+
/**
|
|
3225
|
+
* Returns the first node with a key less than or equal to the given key.
|
|
3226
|
+
* This is equivalent to Java TreeMap.floorEntry().
|
|
3227
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3228
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3229
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3230
|
+
*
|
|
3231
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3232
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3233
|
+
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
3234
|
+
*/
|
|
3235
|
+
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3236
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3237
|
+
return void 0;
|
|
3238
|
+
}
|
|
3239
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3240
|
+
return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3241
|
+
}
|
|
3242
|
+
let targetKey;
|
|
3243
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3244
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3245
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3246
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3247
|
+
if (key === null || key === void 0) {
|
|
3248
|
+
return void 0;
|
|
3249
|
+
}
|
|
3250
|
+
targetKey = key;
|
|
3251
|
+
} else {
|
|
3252
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3253
|
+
}
|
|
3254
|
+
if (targetKey !== void 0) {
|
|
3255
|
+
return this._floorByKey(targetKey, iterationType);
|
|
3256
|
+
}
|
|
3257
|
+
return void 0;
|
|
3258
|
+
}
|
|
3259
|
+
/**
|
|
3260
|
+
* Returns the first node with a key strictly less than the given key.
|
|
3261
|
+
* This is equivalent to Java TreeMap.lowerEntry().
|
|
3262
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3263
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3264
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3265
|
+
*
|
|
3266
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3267
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
3268
|
+
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
3269
|
+
*/
|
|
3270
|
+
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3271
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3272
|
+
return void 0;
|
|
3273
|
+
}
|
|
3274
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3275
|
+
return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3276
|
+
}
|
|
3277
|
+
let targetKey;
|
|
3278
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3279
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3280
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3281
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3282
|
+
if (key === null || key === void 0) {
|
|
3283
|
+
return void 0;
|
|
3284
|
+
}
|
|
3285
|
+
targetKey = key;
|
|
3286
|
+
} else {
|
|
3287
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3288
|
+
}
|
|
3289
|
+
if (targetKey !== void 0) {
|
|
3290
|
+
return this._lowerByKey(targetKey, iterationType);
|
|
3291
|
+
}
|
|
3292
|
+
return void 0;
|
|
3293
|
+
}
|
|
3230
3294
|
/**
|
|
3231
3295
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3232
3296
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3361,31 +3425,236 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3361
3425
|
return out;
|
|
3362
3426
|
}
|
|
3363
3427
|
/**
|
|
3364
|
-
* Deletes
|
|
3365
|
-
* @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.
|
|
3428
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
3366
3429
|
*
|
|
3367
|
-
* @
|
|
3368
|
-
*
|
|
3430
|
+
* @remarks
|
|
3431
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
3432
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
3433
|
+
*
|
|
3434
|
+
* @template K - The key type.
|
|
3435
|
+
* @template V - The value type.
|
|
3436
|
+
*
|
|
3437
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
3438
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
3439
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
3440
|
+
* - An entry tuple: searches for the key-value pair.
|
|
3441
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
3442
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
3443
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
3444
|
+
*
|
|
3445
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
3446
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
3447
|
+
*
|
|
3448
|
+
* @param startNode - The node to start the search from. Can be:
|
|
3449
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
3450
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
3451
|
+
* - Default value: this._root (the tree's root).
|
|
3452
|
+
*
|
|
3453
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
3454
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
3455
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
3456
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
3457
|
+
*
|
|
3458
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
3459
|
+
* - Key: the matched node's key.
|
|
3460
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
3461
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
3462
|
+
*/
|
|
3463
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3464
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
3465
|
+
let results = [];
|
|
3466
|
+
for (const node of toDelete) {
|
|
3467
|
+
const deleteInfo = this.delete(node);
|
|
3468
|
+
results = results.concat(deleteInfo);
|
|
3469
|
+
}
|
|
3470
|
+
return results;
|
|
3471
|
+
}
|
|
3472
|
+
/**
|
|
3473
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
3474
|
+
* @remarks Time O(1) Space O(1)
|
|
3475
|
+
* @returns The default comparator function.
|
|
3369
3476
|
*/
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3477
|
+
_createDefaultComparator() {
|
|
3478
|
+
return (a, b) => {
|
|
3479
|
+
debugger;
|
|
3480
|
+
if (isComparable(a) && isComparable(b)) {
|
|
3481
|
+
if (a > b) return 1;
|
|
3482
|
+
if (a < b) return -1;
|
|
3483
|
+
return 0;
|
|
3484
|
+
}
|
|
3485
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
3486
|
+
throw TypeError(
|
|
3487
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
3488
|
+
);
|
|
3489
|
+
}
|
|
3490
|
+
return 0;
|
|
3491
|
+
};
|
|
3492
|
+
}
|
|
3493
|
+
/**
|
|
3494
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
3495
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3496
|
+
* Finds first node where key <= target.
|
|
3497
|
+
* @remarks Time O(h) where h is tree height.
|
|
3498
|
+
*
|
|
3499
|
+
* @param key - The target key to search for.
|
|
3500
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3501
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
3502
|
+
*/
|
|
3503
|
+
_floorByKey(key, iterationType) {
|
|
3504
|
+
var _a, _b;
|
|
3505
|
+
if (iterationType === "RECURSIVE") {
|
|
3506
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3507
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3508
|
+
const cmp = this.comparator(cur.key, key);
|
|
3509
|
+
if (cmp <= 0) {
|
|
3510
|
+
const rightResult = dfs(cur.right);
|
|
3511
|
+
return rightResult != null ? rightResult : cur;
|
|
3512
|
+
} else {
|
|
3513
|
+
return dfs(cur.left);
|
|
3514
|
+
}
|
|
3515
|
+
}, "dfs");
|
|
3516
|
+
return dfs(this.root);
|
|
3517
|
+
} else {
|
|
3518
|
+
let current = this.root;
|
|
3519
|
+
let result = void 0;
|
|
3520
|
+
while (this.isRealNode(current)) {
|
|
3521
|
+
const cmp = this.comparator(current.key, key);
|
|
3522
|
+
if (cmp <= 0) {
|
|
3523
|
+
result = current;
|
|
3524
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
3525
|
+
} else {
|
|
3526
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
3527
|
+
}
|
|
3378
3528
|
}
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3529
|
+
return result;
|
|
3530
|
+
}
|
|
3531
|
+
}
|
|
3532
|
+
/**
|
|
3533
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
3534
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3535
|
+
* Returns the last node that satisfies the predicate function.
|
|
3536
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3537
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3538
|
+
*
|
|
3539
|
+
* @param predicate - The predicate function to test nodes.
|
|
3540
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3541
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
3542
|
+
*/
|
|
3543
|
+
_floorByPredicate(predicate, iterationType) {
|
|
3544
|
+
if (iterationType === "RECURSIVE") {
|
|
3545
|
+
let result = void 0;
|
|
3546
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3547
|
+
if (!this.isRealNode(cur)) return;
|
|
3548
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3549
|
+
if (predicate(cur)) {
|
|
3550
|
+
result = cur;
|
|
3551
|
+
}
|
|
3552
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3553
|
+
}, "dfs");
|
|
3554
|
+
dfs(this.root);
|
|
3555
|
+
return result;
|
|
3556
|
+
} else {
|
|
3557
|
+
const stack = [];
|
|
3558
|
+
let current = this.root;
|
|
3559
|
+
let result = void 0;
|
|
3560
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3561
|
+
if (this.isRealNode(current)) {
|
|
3562
|
+
stack.push(current);
|
|
3563
|
+
current = current.left;
|
|
3564
|
+
} else {
|
|
3565
|
+
const node = stack.pop();
|
|
3566
|
+
if (!this.isRealNode(node)) break;
|
|
3567
|
+
if (predicate(node)) {
|
|
3568
|
+
result = node;
|
|
3569
|
+
}
|
|
3570
|
+
current = node.right;
|
|
3571
|
+
}
|
|
3385
3572
|
}
|
|
3386
|
-
|
|
3573
|
+
return result;
|
|
3574
|
+
}
|
|
3575
|
+
}
|
|
3576
|
+
/**
|
|
3577
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
3578
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3579
|
+
* Finds first node where key < target.
|
|
3580
|
+
* @remarks Time O(h) where h is tree height.
|
|
3581
|
+
*
|
|
3582
|
+
* @param key - The target key to search for.
|
|
3583
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3584
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
3585
|
+
*/
|
|
3586
|
+
_lowerByKey(key, iterationType) {
|
|
3587
|
+
var _a, _b;
|
|
3588
|
+
if (iterationType === "RECURSIVE") {
|
|
3589
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3590
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3591
|
+
const cmp = this.comparator(cur.key, key);
|
|
3592
|
+
if (cmp < 0) {
|
|
3593
|
+
const rightResult = dfs(cur.right);
|
|
3594
|
+
return rightResult != null ? rightResult : cur;
|
|
3595
|
+
} else {
|
|
3596
|
+
return dfs(cur.left);
|
|
3597
|
+
}
|
|
3598
|
+
}, "dfs");
|
|
3599
|
+
return dfs(this.root);
|
|
3600
|
+
} else {
|
|
3601
|
+
let current = this.root;
|
|
3602
|
+
let result = void 0;
|
|
3603
|
+
while (this.isRealNode(current)) {
|
|
3604
|
+
const cmp = this.comparator(current.key, key);
|
|
3605
|
+
if (cmp < 0) {
|
|
3606
|
+
result = current;
|
|
3607
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
3608
|
+
} else {
|
|
3609
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
3610
|
+
}
|
|
3611
|
+
}
|
|
3612
|
+
return result;
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
/**
|
|
3616
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
3617
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3618
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
3619
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3620
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3621
|
+
*
|
|
3622
|
+
* @param predicate - The predicate function to test nodes.
|
|
3623
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3624
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
3625
|
+
*/
|
|
3626
|
+
_lowerByPredicate(predicate, iterationType) {
|
|
3627
|
+
if (iterationType === "RECURSIVE") {
|
|
3628
|
+
let result = void 0;
|
|
3629
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3630
|
+
if (!this.isRealNode(cur)) return;
|
|
3631
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3632
|
+
if (predicate(cur)) {
|
|
3633
|
+
result = cur;
|
|
3634
|
+
}
|
|
3635
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3636
|
+
}, "dfs");
|
|
3637
|
+
dfs(this.root);
|
|
3638
|
+
return result;
|
|
3639
|
+
} else {
|
|
3640
|
+
const stack = [];
|
|
3641
|
+
let current = this.root;
|
|
3642
|
+
let result = void 0;
|
|
3643
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3644
|
+
if (this.isRealNode(current)) {
|
|
3645
|
+
stack.push(current);
|
|
3646
|
+
current = current.left;
|
|
3647
|
+
} else {
|
|
3648
|
+
const node = stack.pop();
|
|
3649
|
+
if (!this.isRealNode(node)) break;
|
|
3650
|
+
if (predicate(node)) {
|
|
3651
|
+
result = node;
|
|
3652
|
+
}
|
|
3653
|
+
current = node.right;
|
|
3654
|
+
}
|
|
3655
|
+
}
|
|
3656
|
+
return result;
|
|
3387
3657
|
}
|
|
3388
|
-
return false;
|
|
3389
3658
|
}
|
|
3390
3659
|
/**
|
|
3391
3660
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -3538,8 +3807,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3538
3807
|
_snapshotOptions() {
|
|
3539
3808
|
return {
|
|
3540
3809
|
...super._snapshotOptions(),
|
|
3541
|
-
|
|
3542
|
-
isReverse: this.isReverse
|
|
3810
|
+
comparator: this._comparator
|
|
3543
3811
|
};
|
|
3544
3812
|
}
|
|
3545
3813
|
/**
|
|
@@ -3567,14 +3835,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3567
3835
|
}
|
|
3568
3836
|
/**
|
|
3569
3837
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3570
|
-
* @remarks Time O(1)
|
|
3838
|
+
* @remarks Time O(1) Space O(1)
|
|
3571
3839
|
*
|
|
3572
3840
|
* @param a - The first key.
|
|
3573
3841
|
* @param b - The second key.
|
|
3574
3842
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3575
3843
|
*/
|
|
3576
3844
|
_compare(a, b) {
|
|
3577
|
-
return this.
|
|
3845
|
+
return this._comparator(a, b);
|
|
3578
3846
|
}
|
|
3579
3847
|
/**
|
|
3580
3848
|
* (Private) Deletes a node by its key.
|