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/esm/index.mjs
CHANGED
|
@@ -2828,9 +2828,13 @@ var BST = class extends BinaryTree {
|
|
|
2828
2828
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2829
2829
|
super([], options);
|
|
2830
2830
|
if (options) {
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2831
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2832
|
+
this._comparator = options.comparator;
|
|
2833
|
+
} else {
|
|
2834
|
+
this._comparator = this._createDefaultComparator();
|
|
2835
|
+
}
|
|
2836
|
+
} else {
|
|
2837
|
+
this._comparator = this._createDefaultComparator();
|
|
2834
2838
|
}
|
|
2835
2839
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2836
2840
|
}
|
|
@@ -2844,40 +2848,12 @@ var BST = class extends BinaryTree {
|
|
|
2844
2848
|
get root() {
|
|
2845
2849
|
return this._root;
|
|
2846
2850
|
}
|
|
2847
|
-
_isReverse = false;
|
|
2848
|
-
/**
|
|
2849
|
-
* Gets whether the tree's comparison logic is reversed.
|
|
2850
|
-
* @remarks Time O(1)
|
|
2851
|
-
*
|
|
2852
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2853
|
-
*/
|
|
2854
|
-
get isReverse() {
|
|
2855
|
-
return this._isReverse;
|
|
2856
|
-
}
|
|
2857
2851
|
/**
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
if (a > b) return 1;
|
|
2864
|
-
if (a < b) return -1;
|
|
2865
|
-
return 0;
|
|
2866
|
-
}
|
|
2867
|
-
if (this._specifyComparable) {
|
|
2868
|
-
const va = this._specifyComparable(a);
|
|
2869
|
-
const vb = this._specifyComparable(b);
|
|
2870
|
-
if (va > vb) return 1;
|
|
2871
|
-
if (va < vb) return -1;
|
|
2872
|
-
return 0;
|
|
2873
|
-
}
|
|
2874
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2875
|
-
throw TypeError(
|
|
2876
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2877
|
-
);
|
|
2878
|
-
}
|
|
2879
|
-
return 0;
|
|
2880
|
-
}, "_comparator");
|
|
2852
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2853
|
+
|
|
2854
|
+
* @remarks Time O(1) Space O(1)
|
|
2855
|
+
*/
|
|
2856
|
+
_comparator;
|
|
2881
2857
|
/**
|
|
2882
2858
|
* Gets the comparator function used by the tree.
|
|
2883
2859
|
* @remarks Time O(1)
|
|
@@ -2887,16 +2863,6 @@ var BST = class extends BinaryTree {
|
|
|
2887
2863
|
get comparator() {
|
|
2888
2864
|
return this._comparator;
|
|
2889
2865
|
}
|
|
2890
|
-
_specifyComparable;
|
|
2891
|
-
/**
|
|
2892
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2893
|
-
* @remarks Time O(1)
|
|
2894
|
-
*
|
|
2895
|
-
* @returns The key-to-comparable conversion function.
|
|
2896
|
-
*/
|
|
2897
|
-
get specifyComparable() {
|
|
2898
|
-
return this._specifyComparable;
|
|
2899
|
-
}
|
|
2900
2866
|
/**
|
|
2901
2867
|
* (Protected) Creates a new BST node.
|
|
2902
2868
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2937,7 +2903,7 @@ var BST = class extends BinaryTree {
|
|
|
2937
2903
|
* @returns True if the key is valid, false otherwise.
|
|
2938
2904
|
*/
|
|
2939
2905
|
isValidKey(key) {
|
|
2940
|
-
return isComparable(key
|
|
2906
|
+
return isComparable(key);
|
|
2941
2907
|
}
|
|
2942
2908
|
/**
|
|
2943
2909
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3026,8 +2992,8 @@ var BST = class 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 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 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,234 @@ var BST = class 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
|
-
cur = cur.left;
|
|
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;
|
|
3380
3486
|
}
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
if (predicate(key, val, index++, this)) {
|
|
3386
|
-
return this._deleteByKey(key);
|
|
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
|
+
);
|
|
3387
3491
|
}
|
|
3388
|
-
|
|
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
|
+
if (iterationType === "RECURSIVE") {
|
|
3507
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3508
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3509
|
+
const cmp = this.comparator(cur.key, key);
|
|
3510
|
+
if (cmp <= 0) {
|
|
3511
|
+
const rightResult = dfs(cur.right);
|
|
3512
|
+
return rightResult ?? cur;
|
|
3513
|
+
} else {
|
|
3514
|
+
return dfs(cur.left);
|
|
3515
|
+
}
|
|
3516
|
+
}, "dfs");
|
|
3517
|
+
return dfs(this.root);
|
|
3518
|
+
} else {
|
|
3519
|
+
let current = this.root;
|
|
3520
|
+
let result = void 0;
|
|
3521
|
+
while (this.isRealNode(current)) {
|
|
3522
|
+
const cmp = this.comparator(current.key, key);
|
|
3523
|
+
if (cmp <= 0) {
|
|
3524
|
+
result = current;
|
|
3525
|
+
current = current.right ?? void 0;
|
|
3526
|
+
} else {
|
|
3527
|
+
current = current.left ?? void 0;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
return result;
|
|
3531
|
+
}
|
|
3532
|
+
}
|
|
3533
|
+
/**
|
|
3534
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
3535
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3536
|
+
* Returns the last node that satisfies the predicate function.
|
|
3537
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3538
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3539
|
+
*
|
|
3540
|
+
* @param predicate - The predicate function to test nodes.
|
|
3541
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3542
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
3543
|
+
*/
|
|
3544
|
+
_floorByPredicate(predicate, iterationType) {
|
|
3545
|
+
if (iterationType === "RECURSIVE") {
|
|
3546
|
+
let result = void 0;
|
|
3547
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3548
|
+
if (!this.isRealNode(cur)) return;
|
|
3549
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3550
|
+
if (predicate(cur)) {
|
|
3551
|
+
result = cur;
|
|
3552
|
+
}
|
|
3553
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3554
|
+
}, "dfs");
|
|
3555
|
+
dfs(this.root);
|
|
3556
|
+
return result;
|
|
3557
|
+
} else {
|
|
3558
|
+
const stack = [];
|
|
3559
|
+
let current = this.root;
|
|
3560
|
+
let result = void 0;
|
|
3561
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3562
|
+
if (this.isRealNode(current)) {
|
|
3563
|
+
stack.push(current);
|
|
3564
|
+
current = current.left;
|
|
3565
|
+
} else {
|
|
3566
|
+
const node = stack.pop();
|
|
3567
|
+
if (!this.isRealNode(node)) break;
|
|
3568
|
+
if (predicate(node)) {
|
|
3569
|
+
result = node;
|
|
3570
|
+
}
|
|
3571
|
+
current = node.right;
|
|
3572
|
+
}
|
|
3573
|
+
}
|
|
3574
|
+
return result;
|
|
3575
|
+
}
|
|
3576
|
+
}
|
|
3577
|
+
/**
|
|
3578
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
3579
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3580
|
+
* Finds first node where key < target.
|
|
3581
|
+
* @remarks Time O(h) where h is tree height.
|
|
3582
|
+
*
|
|
3583
|
+
* @param key - The target key to search for.
|
|
3584
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3585
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
3586
|
+
*/
|
|
3587
|
+
_lowerByKey(key, iterationType) {
|
|
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 ?? 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 = current.right ?? void 0;
|
|
3608
|
+
} else {
|
|
3609
|
+
current = current.left ?? 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;
|
|
3389
3657
|
}
|
|
3390
|
-
return false;
|
|
3391
3658
|
}
|
|
3392
3659
|
/**
|
|
3393
3660
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -3539,8 +3806,7 @@ var BST = class extends BinaryTree {
|
|
|
3539
3806
|
_snapshotOptions() {
|
|
3540
3807
|
return {
|
|
3541
3808
|
...super._snapshotOptions(),
|
|
3542
|
-
|
|
3543
|
-
isReverse: this.isReverse
|
|
3809
|
+
comparator: this._comparator
|
|
3544
3810
|
};
|
|
3545
3811
|
}
|
|
3546
3812
|
/**
|
|
@@ -3568,14 +3834,14 @@ var BST = class extends BinaryTree {
|
|
|
3568
3834
|
}
|
|
3569
3835
|
/**
|
|
3570
3836
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3571
|
-
* @remarks Time O(1)
|
|
3837
|
+
* @remarks Time O(1) Space O(1)
|
|
3572
3838
|
*
|
|
3573
3839
|
* @param a - The first key.
|
|
3574
3840
|
* @param b - The second key.
|
|
3575
3841
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3576
3842
|
*/
|
|
3577
3843
|
_compare(a, b) {
|
|
3578
|
-
return this.
|
|
3844
|
+
return this._comparator(a, b);
|
|
3579
3845
|
}
|
|
3580
3846
|
/**
|
|
3581
3847
|
* (Private) Deletes a node by its key.
|