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