avl-tree-typed 2.2.4 → 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 +284 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +286 -28
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +284 -28
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +286 -28
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +100 -6
- package/dist/umd/avl-tree-typed.js +286 -28
- 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-multi-map.ts +3 -1
- package/src/data-structures/binary-tree/bst.ts +429 -48
|
@@ -288,12 +288,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
288
288
|
* @returns The root node.
|
|
289
289
|
*/
|
|
290
290
|
get root(): OptNode<BSTNode<K, V>>;
|
|
291
|
-
/**
|
|
292
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
293
|
-
* @remarks Time O(1) Space O(1)
|
|
294
|
-
* @returns The default comparator function.
|
|
295
|
-
*/
|
|
296
|
-
protected _createDefaultComparator(): Comparator<K>;
|
|
297
291
|
/**
|
|
298
292
|
* The comparator function used to determine the order of keys in the tree.
|
|
299
293
|
|
|
@@ -457,6 +451,54 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
457
451
|
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
458
452
|
*/
|
|
459
453
|
upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
454
|
+
/**
|
|
455
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
456
|
+
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
457
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
458
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
459
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
460
|
+
*
|
|
461
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
462
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
463
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
464
|
+
*/
|
|
465
|
+
ceilingEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
466
|
+
/**
|
|
467
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
468
|
+
* This is equivalent to Java TreeMap.higherEntry().
|
|
469
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
470
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
471
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
472
|
+
*
|
|
473
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
474
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
475
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
476
|
+
*/
|
|
477
|
+
higherEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
478
|
+
/**
|
|
479
|
+
* Returns the first node with a key less than or equal to the given key.
|
|
480
|
+
* This is equivalent to Java TreeMap.floorEntry().
|
|
481
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
482
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
483
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
484
|
+
*
|
|
485
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
486
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
487
|
+
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
488
|
+
*/
|
|
489
|
+
floorEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
490
|
+
/**
|
|
491
|
+
* Returns the first node with a key strictly less than the given key.
|
|
492
|
+
* This is equivalent to Java TreeMap.lowerEntry().
|
|
493
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
494
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
495
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
496
|
+
*
|
|
497
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
498
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
499
|
+
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
500
|
+
*/
|
|
501
|
+
lowerEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
460
502
|
/**
|
|
461
503
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
462
504
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -536,6 +578,58 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
536
578
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
537
579
|
*/
|
|
538
580
|
deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
|
|
581
|
+
/**
|
|
582
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
583
|
+
* @remarks Time O(1) Space O(1)
|
|
584
|
+
* @returns The default comparator function.
|
|
585
|
+
*/
|
|
586
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
587
|
+
/**
|
|
588
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
589
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
590
|
+
* Finds first node where key <= target.
|
|
591
|
+
* @remarks Time O(h) where h is tree height.
|
|
592
|
+
*
|
|
593
|
+
* @param key - The target key to search for.
|
|
594
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
595
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
596
|
+
*/
|
|
597
|
+
protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
598
|
+
/**
|
|
599
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
600
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
601
|
+
* Returns the last node that satisfies the predicate function.
|
|
602
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
603
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
604
|
+
*
|
|
605
|
+
* @param predicate - The predicate function to test nodes.
|
|
606
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
607
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
608
|
+
*/
|
|
609
|
+
protected _floorByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
610
|
+
/**
|
|
611
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
612
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
613
|
+
* Finds first node where key < target.
|
|
614
|
+
* @remarks Time O(h) where h is tree height.
|
|
615
|
+
*
|
|
616
|
+
* @param key - The target key to search for.
|
|
617
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
618
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
619
|
+
*/
|
|
620
|
+
protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
621
|
+
/**
|
|
622
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
623
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
624
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
625
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
626
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
627
|
+
*
|
|
628
|
+
* @param predicate - The predicate function to test nodes.
|
|
629
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
630
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
631
|
+
*/
|
|
632
|
+
protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
539
633
|
/**
|
|
540
634
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
541
635
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -2857,27 +2857,6 @@ var avlTreeTyped = (() => {
|
|
|
2857
2857
|
get root() {
|
|
2858
2858
|
return this._root;
|
|
2859
2859
|
}
|
|
2860
|
-
/**
|
|
2861
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2862
|
-
* @remarks Time O(1) Space O(1)
|
|
2863
|
-
* @returns The default comparator function.
|
|
2864
|
-
*/
|
|
2865
|
-
_createDefaultComparator() {
|
|
2866
|
-
return (a, b) => {
|
|
2867
|
-
debugger;
|
|
2868
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2869
|
-
if (a > b) return 1;
|
|
2870
|
-
if (a < b) return -1;
|
|
2871
|
-
return 0;
|
|
2872
|
-
}
|
|
2873
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2874
|
-
throw TypeError(
|
|
2875
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2876
|
-
);
|
|
2877
|
-
}
|
|
2878
|
-
return 0;
|
|
2879
|
-
};
|
|
2880
|
-
}
|
|
2881
2860
|
/**
|
|
2882
2861
|
* Gets the comparator function used by the tree.
|
|
2883
2862
|
* @remarks Time O(1)
|
|
@@ -3221,6 +3200,104 @@ var avlTreeTyped = (() => {
|
|
|
3221
3200
|
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3222
3201
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3223
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
|
+
}
|
|
3224
3301
|
/**
|
|
3225
3302
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3226
3303
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3391,13 +3468,7 @@ var avlTreeTyped = (() => {
|
|
|
3391
3468
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
3392
3469
|
*/
|
|
3393
3470
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3394
|
-
const toDelete = this.search(
|
|
3395
|
-
keyNodeEntryOrPredicate,
|
|
3396
|
-
onlyOne,
|
|
3397
|
-
(node) => node,
|
|
3398
|
-
startNode,
|
|
3399
|
-
iterationType
|
|
3400
|
-
);
|
|
3471
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
3401
3472
|
let results = [];
|
|
3402
3473
|
for (const node of toDelete) {
|
|
3403
3474
|
const deleteInfo = this.delete(node);
|
|
@@ -3405,6 +3476,193 @@ var avlTreeTyped = (() => {
|
|
|
3405
3476
|
}
|
|
3406
3477
|
return results;
|
|
3407
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.
|
|
3483
|
+
*/
|
|
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;
|
|
3491
|
+
}
|
|
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
|
+
);
|
|
3496
|
+
}
|
|
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;
|
|
3664
|
+
}
|
|
3665
|
+
}
|
|
3408
3666
|
/**
|
|
3409
3667
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3410
3668
|
* Unified logic for both lowerBound and upperBound.
|