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
|
@@ -2852,27 +2852,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2852
2852
|
get root() {
|
|
2853
2853
|
return this._root;
|
|
2854
2854
|
}
|
|
2855
|
-
/**
|
|
2856
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2857
|
-
* @remarks Time O(1) Space O(1)
|
|
2858
|
-
* @returns The default comparator function.
|
|
2859
|
-
*/
|
|
2860
|
-
_createDefaultComparator() {
|
|
2861
|
-
return (a, b) => {
|
|
2862
|
-
debugger;
|
|
2863
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2864
|
-
if (a > b) return 1;
|
|
2865
|
-
if (a < b) return -1;
|
|
2866
|
-
return 0;
|
|
2867
|
-
}
|
|
2868
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2869
|
-
throw TypeError(
|
|
2870
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2871
|
-
);
|
|
2872
|
-
}
|
|
2873
|
-
return 0;
|
|
2874
|
-
};
|
|
2875
|
-
}
|
|
2876
2855
|
/**
|
|
2877
2856
|
* Gets the comparator function used by the tree.
|
|
2878
2857
|
* @remarks Time O(1)
|
|
@@ -3216,6 +3195,104 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3216
3195
|
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3217
3196
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3218
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
|
+
}
|
|
3219
3296
|
/**
|
|
3220
3297
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3221
3298
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3386,13 +3463,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3386
3463
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
3387
3464
|
*/
|
|
3388
3465
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3389
|
-
const toDelete = this.search(
|
|
3390
|
-
keyNodeEntryOrPredicate,
|
|
3391
|
-
onlyOne,
|
|
3392
|
-
(node) => node,
|
|
3393
|
-
startNode,
|
|
3394
|
-
iterationType
|
|
3395
|
-
);
|
|
3466
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
3396
3467
|
let results = [];
|
|
3397
3468
|
for (const node of toDelete) {
|
|
3398
3469
|
const deleteInfo = this.delete(node);
|
|
@@ -3400,6 +3471,193 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3400
3471
|
}
|
|
3401
3472
|
return results;
|
|
3402
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.
|
|
3478
|
+
*/
|
|
3479
|
+
_createDefaultComparator() {
|
|
3480
|
+
return (a, b) => {
|
|
3481
|
+
debugger;
|
|
3482
|
+
if (isComparable(a) && isComparable(b)) {
|
|
3483
|
+
if (a > b) return 1;
|
|
3484
|
+
if (a < b) return -1;
|
|
3485
|
+
return 0;
|
|
3486
|
+
}
|
|
3487
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
3488
|
+
throw TypeError(
|
|
3489
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
3490
|
+
);
|
|
3491
|
+
}
|
|
3492
|
+
return 0;
|
|
3493
|
+
};
|
|
3494
|
+
}
|
|
3495
|
+
/**
|
|
3496
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
3497
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3498
|
+
* Finds first node where key <= target.
|
|
3499
|
+
* @remarks Time O(h) where h is tree height.
|
|
3500
|
+
*
|
|
3501
|
+
* @param key - The target key to search for.
|
|
3502
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3503
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
3504
|
+
*/
|
|
3505
|
+
_floorByKey(key, iterationType) {
|
|
3506
|
+
var _a, _b;
|
|
3507
|
+
if (iterationType === "RECURSIVE") {
|
|
3508
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3509
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3510
|
+
const cmp = this.comparator(cur.key, key);
|
|
3511
|
+
if (cmp <= 0) {
|
|
3512
|
+
const rightResult = dfs(cur.right);
|
|
3513
|
+
return rightResult != null ? rightResult : cur;
|
|
3514
|
+
} else {
|
|
3515
|
+
return dfs(cur.left);
|
|
3516
|
+
}
|
|
3517
|
+
}, "dfs");
|
|
3518
|
+
return dfs(this.root);
|
|
3519
|
+
} else {
|
|
3520
|
+
let current = this.root;
|
|
3521
|
+
let result = void 0;
|
|
3522
|
+
while (this.isRealNode(current)) {
|
|
3523
|
+
const cmp = this.comparator(current.key, key);
|
|
3524
|
+
if (cmp <= 0) {
|
|
3525
|
+
result = current;
|
|
3526
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
3527
|
+
} else {
|
|
3528
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
return result;
|
|
3532
|
+
}
|
|
3533
|
+
}
|
|
3534
|
+
/**
|
|
3535
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
3536
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3537
|
+
* Returns the last node that satisfies the predicate function.
|
|
3538
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3539
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3540
|
+
*
|
|
3541
|
+
* @param predicate - The predicate function to test nodes.
|
|
3542
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3543
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
3544
|
+
*/
|
|
3545
|
+
_floorByPredicate(predicate, iterationType) {
|
|
3546
|
+
if (iterationType === "RECURSIVE") {
|
|
3547
|
+
let result = void 0;
|
|
3548
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3549
|
+
if (!this.isRealNode(cur)) return;
|
|
3550
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3551
|
+
if (predicate(cur)) {
|
|
3552
|
+
result = cur;
|
|
3553
|
+
}
|
|
3554
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3555
|
+
}, "dfs");
|
|
3556
|
+
dfs(this.root);
|
|
3557
|
+
return result;
|
|
3558
|
+
} else {
|
|
3559
|
+
const stack = [];
|
|
3560
|
+
let current = this.root;
|
|
3561
|
+
let result = void 0;
|
|
3562
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3563
|
+
if (this.isRealNode(current)) {
|
|
3564
|
+
stack.push(current);
|
|
3565
|
+
current = current.left;
|
|
3566
|
+
} else {
|
|
3567
|
+
const node = stack.pop();
|
|
3568
|
+
if (!this.isRealNode(node)) break;
|
|
3569
|
+
if (predicate(node)) {
|
|
3570
|
+
result = node;
|
|
3571
|
+
}
|
|
3572
|
+
current = node.right;
|
|
3573
|
+
}
|
|
3574
|
+
}
|
|
3575
|
+
return result;
|
|
3576
|
+
}
|
|
3577
|
+
}
|
|
3578
|
+
/**
|
|
3579
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
3580
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3581
|
+
* Finds first node where key < target.
|
|
3582
|
+
* @remarks Time O(h) where h is tree height.
|
|
3583
|
+
*
|
|
3584
|
+
* @param key - The target key to search for.
|
|
3585
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3586
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
3587
|
+
*/
|
|
3588
|
+
_lowerByKey(key, iterationType) {
|
|
3589
|
+
var _a, _b;
|
|
3590
|
+
if (iterationType === "RECURSIVE") {
|
|
3591
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3592
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3593
|
+
const cmp = this.comparator(cur.key, key);
|
|
3594
|
+
if (cmp < 0) {
|
|
3595
|
+
const rightResult = dfs(cur.right);
|
|
3596
|
+
return rightResult != null ? rightResult : cur;
|
|
3597
|
+
} else {
|
|
3598
|
+
return dfs(cur.left);
|
|
3599
|
+
}
|
|
3600
|
+
}, "dfs");
|
|
3601
|
+
return dfs(this.root);
|
|
3602
|
+
} else {
|
|
3603
|
+
let current = this.root;
|
|
3604
|
+
let result = void 0;
|
|
3605
|
+
while (this.isRealNode(current)) {
|
|
3606
|
+
const cmp = this.comparator(current.key, key);
|
|
3607
|
+
if (cmp < 0) {
|
|
3608
|
+
result = current;
|
|
3609
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
3610
|
+
} else {
|
|
3611
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
3612
|
+
}
|
|
3613
|
+
}
|
|
3614
|
+
return result;
|
|
3615
|
+
}
|
|
3616
|
+
}
|
|
3617
|
+
/**
|
|
3618
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
3619
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3620
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
3621
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3622
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3623
|
+
*
|
|
3624
|
+
* @param predicate - The predicate function to test nodes.
|
|
3625
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3626
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
3627
|
+
*/
|
|
3628
|
+
_lowerByPredicate(predicate, iterationType) {
|
|
3629
|
+
if (iterationType === "RECURSIVE") {
|
|
3630
|
+
let result = void 0;
|
|
3631
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3632
|
+
if (!this.isRealNode(cur)) return;
|
|
3633
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3634
|
+
if (predicate(cur)) {
|
|
3635
|
+
result = cur;
|
|
3636
|
+
}
|
|
3637
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3638
|
+
}, "dfs");
|
|
3639
|
+
dfs(this.root);
|
|
3640
|
+
return result;
|
|
3641
|
+
} else {
|
|
3642
|
+
const stack = [];
|
|
3643
|
+
let current = this.root;
|
|
3644
|
+
let result = void 0;
|
|
3645
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3646
|
+
if (this.isRealNode(current)) {
|
|
3647
|
+
stack.push(current);
|
|
3648
|
+
current = current.left;
|
|
3649
|
+
} else {
|
|
3650
|
+
const node = stack.pop();
|
|
3651
|
+
if (!this.isRealNode(node)) break;
|
|
3652
|
+
if (predicate(node)) {
|
|
3653
|
+
result = node;
|
|
3654
|
+
}
|
|
3655
|
+
current = node.right;
|
|
3656
|
+
}
|
|
3657
|
+
}
|
|
3658
|
+
return result;
|
|
3659
|
+
}
|
|
3660
|
+
}
|
|
3403
3661
|
/**
|
|
3404
3662
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3405
3663
|
* Unified logic for both lowerBound and upperBound.
|