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