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