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
package/dist/cjs/index.cjs
CHANGED
|
@@ -2850,27 +2850,6 @@ var BST = class extends BinaryTree {
|
|
|
2850
2850
|
get root() {
|
|
2851
2851
|
return this._root;
|
|
2852
2852
|
}
|
|
2853
|
-
/**
|
|
2854
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2855
|
-
* @remarks Time O(1) Space O(1)
|
|
2856
|
-
* @returns The default comparator function.
|
|
2857
|
-
*/
|
|
2858
|
-
_createDefaultComparator() {
|
|
2859
|
-
return (a, b) => {
|
|
2860
|
-
debugger;
|
|
2861
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2862
|
-
if (a > b) return 1;
|
|
2863
|
-
if (a < b) return -1;
|
|
2864
|
-
return 0;
|
|
2865
|
-
}
|
|
2866
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2867
|
-
throw TypeError(
|
|
2868
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2869
|
-
);
|
|
2870
|
-
}
|
|
2871
|
-
return 0;
|
|
2872
|
-
};
|
|
2873
|
-
}
|
|
2874
2853
|
/**
|
|
2875
2854
|
* The comparator function used to determine the order of keys in the tree.
|
|
2876
2855
|
|
|
@@ -3218,6 +3197,104 @@ var BST = class extends BinaryTree {
|
|
|
3218
3197
|
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3219
3198
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3220
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
|
+
}
|
|
3221
3298
|
/**
|
|
3222
3299
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3223
3300
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3388,13 +3465,7 @@ var BST = class extends BinaryTree {
|
|
|
3388
3465
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
3389
3466
|
*/
|
|
3390
3467
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3391
|
-
const toDelete = this.search(
|
|
3392
|
-
keyNodeEntryOrPredicate,
|
|
3393
|
-
onlyOne,
|
|
3394
|
-
(node) => node,
|
|
3395
|
-
startNode,
|
|
3396
|
-
iterationType
|
|
3397
|
-
);
|
|
3468
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
3398
3469
|
let results = [];
|
|
3399
3470
|
for (const node of toDelete) {
|
|
3400
3471
|
const deleteInfo = this.delete(node);
|
|
@@ -3402,6 +3473,191 @@ var BST = class extends BinaryTree {
|
|
|
3402
3473
|
}
|
|
3403
3474
|
return results;
|
|
3404
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.
|
|
3480
|
+
*/
|
|
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;
|
|
3488
|
+
}
|
|
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
|
+
);
|
|
3493
|
+
}
|
|
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;
|
|
3659
|
+
}
|
|
3660
|
+
}
|
|
3405
3661
|
/**
|
|
3406
3662
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3407
3663
|
* Unified logic for both lowerBound and upperBound.
|