avl-tree-typed 2.2.4 → 2.2.6
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 +328 -54
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +330 -54
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +328 -54
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +330 -54
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +110 -96
- package/dist/umd/avl-tree-typed.js +330 -54
- 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/common/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +3 -1
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +658 -71
package/dist/esm/index.mjs
CHANGED
|
@@ -998,6 +998,14 @@ var IterableEntryBase = class {
|
|
|
998
998
|
}
|
|
999
999
|
return accumulator;
|
|
1000
1000
|
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Converts data structure to `[key, value]` pairs.
|
|
1003
|
+
* @returns Array of entries.
|
|
1004
|
+
* @remarks Time O(n), Space O(n)
|
|
1005
|
+
*/
|
|
1006
|
+
toArray() {
|
|
1007
|
+
return [...this];
|
|
1008
|
+
}
|
|
1001
1009
|
/**
|
|
1002
1010
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
1003
1011
|
* @returns Array of entries (default) or a string.
|
|
@@ -1022,8 +1030,6 @@ var Range = class {
|
|
|
1022
1030
|
this.high = high;
|
|
1023
1031
|
this.includeLow = includeLow;
|
|
1024
1032
|
this.includeHigh = includeHigh;
|
|
1025
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
1026
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
1027
1033
|
}
|
|
1028
1034
|
static {
|
|
1029
1035
|
__name(this, "Range");
|
|
@@ -2848,27 +2854,6 @@ var BST = class extends BinaryTree {
|
|
|
2848
2854
|
get root() {
|
|
2849
2855
|
return this._root;
|
|
2850
2856
|
}
|
|
2851
|
-
/**
|
|
2852
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2853
|
-
* @remarks Time O(1) Space O(1)
|
|
2854
|
-
* @returns The default comparator function.
|
|
2855
|
-
*/
|
|
2856
|
-
_createDefaultComparator() {
|
|
2857
|
-
return (a, b) => {
|
|
2858
|
-
debugger;
|
|
2859
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2860
|
-
if (a > b) return 1;
|
|
2861
|
-
if (a < b) return -1;
|
|
2862
|
-
return 0;
|
|
2863
|
-
}
|
|
2864
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2865
|
-
throw TypeError(
|
|
2866
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2867
|
-
);
|
|
2868
|
-
}
|
|
2869
|
-
return 0;
|
|
2870
|
-
};
|
|
2871
|
-
}
|
|
2872
2857
|
/**
|
|
2873
2858
|
* The comparator function used to determine the order of keys in the tree.
|
|
2874
2859
|
|
|
@@ -3190,31 +3175,141 @@ var BST = class extends BinaryTree {
|
|
|
3190
3175
|
else _iterate();
|
|
3191
3176
|
return inserted;
|
|
3192
3177
|
}
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3178
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
3179
|
+
let actualCallback = void 0;
|
|
3180
|
+
let actualIterationType = this.iterationType;
|
|
3181
|
+
if (typeof callback === "string") {
|
|
3182
|
+
actualIterationType = callback;
|
|
3183
|
+
} else if (callback) {
|
|
3184
|
+
actualCallback = callback;
|
|
3185
|
+
if (iterationType) {
|
|
3186
|
+
actualIterationType = iterationType;
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
3190
|
+
if (!actualCallback) {
|
|
3191
|
+
return node?.key;
|
|
3192
|
+
}
|
|
3193
|
+
return node ? actualCallback(node) : void 0;
|
|
3194
|
+
}
|
|
3195
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
3196
|
+
let actualCallback = void 0;
|
|
3197
|
+
let actualIterationType = this.iterationType;
|
|
3198
|
+
if (typeof callback === "string") {
|
|
3199
|
+
actualIterationType = callback;
|
|
3200
|
+
} else if (callback) {
|
|
3201
|
+
actualCallback = callback;
|
|
3202
|
+
if (iterationType) {
|
|
3203
|
+
actualIterationType = iterationType;
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
3207
|
+
if (!actualCallback) {
|
|
3208
|
+
return node?.key;
|
|
3209
|
+
}
|
|
3210
|
+
return node ? actualCallback(node) : void 0;
|
|
3205
3211
|
}
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3212
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
3213
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3214
|
+
if (typeof callback === "string" || !callback) {
|
|
3215
|
+
return void 0;
|
|
3216
|
+
}
|
|
3217
|
+
return void 0;
|
|
3218
|
+
}
|
|
3219
|
+
let actualCallback = void 0;
|
|
3220
|
+
let actualIterationType = this.iterationType;
|
|
3221
|
+
if (typeof callback === "string") {
|
|
3222
|
+
actualIterationType = callback;
|
|
3223
|
+
} else if (callback) {
|
|
3224
|
+
actualCallback = callback;
|
|
3225
|
+
if (iterationType) {
|
|
3226
|
+
actualIterationType = iterationType;
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3230
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
3231
|
+
if (!actualCallback) {
|
|
3232
|
+
return node?.key;
|
|
3233
|
+
}
|
|
3234
|
+
return node ? actualCallback(node) : void 0;
|
|
3235
|
+
}
|
|
3236
|
+
let targetKey;
|
|
3237
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3238
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3239
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3240
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3241
|
+
if (key === null || key === void 0) {
|
|
3242
|
+
if (typeof callback === "string" || !callback) {
|
|
3243
|
+
return void 0;
|
|
3244
|
+
}
|
|
3245
|
+
return void 0;
|
|
3246
|
+
}
|
|
3247
|
+
targetKey = key;
|
|
3248
|
+
} else {
|
|
3249
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3250
|
+
}
|
|
3251
|
+
if (targetKey !== void 0) {
|
|
3252
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
3253
|
+
if (!actualCallback) {
|
|
3254
|
+
return node?.key;
|
|
3255
|
+
}
|
|
3256
|
+
return node ? actualCallback(node) : void 0;
|
|
3257
|
+
}
|
|
3258
|
+
if (typeof callback === "string" || !callback) {
|
|
3259
|
+
return void 0;
|
|
3260
|
+
}
|
|
3261
|
+
return void 0;
|
|
3262
|
+
}
|
|
3263
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
3264
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3265
|
+
if (typeof callback === "string" || !callback) {
|
|
3266
|
+
return void 0;
|
|
3267
|
+
}
|
|
3268
|
+
return void 0;
|
|
3269
|
+
}
|
|
3270
|
+
let actualCallback = void 0;
|
|
3271
|
+
let actualIterationType = this.iterationType;
|
|
3272
|
+
if (typeof callback === "string") {
|
|
3273
|
+
actualIterationType = callback;
|
|
3274
|
+
} else if (callback) {
|
|
3275
|
+
actualCallback = callback;
|
|
3276
|
+
if (iterationType) {
|
|
3277
|
+
actualIterationType = iterationType;
|
|
3278
|
+
}
|
|
3279
|
+
}
|
|
3280
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3281
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
3282
|
+
if (!actualCallback) {
|
|
3283
|
+
return node?.key;
|
|
3284
|
+
}
|
|
3285
|
+
return node ? actualCallback(node) : void 0;
|
|
3286
|
+
}
|
|
3287
|
+
let targetKey;
|
|
3288
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3289
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3290
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3291
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3292
|
+
if (key === null || key === void 0) {
|
|
3293
|
+
if (typeof callback === "string" || !callback) {
|
|
3294
|
+
return void 0;
|
|
3295
|
+
}
|
|
3296
|
+
return void 0;
|
|
3297
|
+
}
|
|
3298
|
+
targetKey = key;
|
|
3299
|
+
} else {
|
|
3300
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3301
|
+
}
|
|
3302
|
+
if (targetKey !== void 0) {
|
|
3303
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
3304
|
+
if (!actualCallback) {
|
|
3305
|
+
return node?.key;
|
|
3306
|
+
}
|
|
3307
|
+
return node ? actualCallback(node) : void 0;
|
|
3308
|
+
}
|
|
3309
|
+
if (typeof callback === "string" || !callback) {
|
|
3310
|
+
return void 0;
|
|
3311
|
+
}
|
|
3312
|
+
return void 0;
|
|
3218
3313
|
}
|
|
3219
3314
|
/**
|
|
3220
3315
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
@@ -3386,13 +3481,7 @@ var BST = class extends BinaryTree {
|
|
|
3386
3481
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
3387
3482
|
*/
|
|
3388
3483
|
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
|
-
);
|
|
3484
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
3396
3485
|
let results = [];
|
|
3397
3486
|
for (const node of toDelete) {
|
|
3398
3487
|
const deleteInfo = this.delete(node);
|
|
@@ -3400,6 +3489,191 @@ var BST = class extends BinaryTree {
|
|
|
3400
3489
|
}
|
|
3401
3490
|
return results;
|
|
3402
3491
|
}
|
|
3492
|
+
/**
|
|
3493
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
3494
|
+
* @remarks Time O(1) Space O(1)
|
|
3495
|
+
* @returns The default comparator function.
|
|
3496
|
+
*/
|
|
3497
|
+
_createDefaultComparator() {
|
|
3498
|
+
return (a, b) => {
|
|
3499
|
+
debugger;
|
|
3500
|
+
if (isComparable(a) && isComparable(b)) {
|
|
3501
|
+
if (a > b) return 1;
|
|
3502
|
+
if (a < b) return -1;
|
|
3503
|
+
return 0;
|
|
3504
|
+
}
|
|
3505
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
3506
|
+
throw TypeError(
|
|
3507
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
3508
|
+
);
|
|
3509
|
+
}
|
|
3510
|
+
return 0;
|
|
3511
|
+
};
|
|
3512
|
+
}
|
|
3513
|
+
/**
|
|
3514
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
3515
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3516
|
+
* Finds first node where key <= target.
|
|
3517
|
+
* @remarks Time O(h) where h is tree height.
|
|
3518
|
+
*
|
|
3519
|
+
* @param key - The target key to search for.
|
|
3520
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3521
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
3522
|
+
*/
|
|
3523
|
+
_floorByKey(key, iterationType) {
|
|
3524
|
+
if (iterationType === "RECURSIVE") {
|
|
3525
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3526
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3527
|
+
const cmp = this.comparator(cur.key, key);
|
|
3528
|
+
if (cmp <= 0) {
|
|
3529
|
+
const rightResult = dfs(cur.right);
|
|
3530
|
+
return rightResult ?? cur;
|
|
3531
|
+
} else {
|
|
3532
|
+
return dfs(cur.left);
|
|
3533
|
+
}
|
|
3534
|
+
}, "dfs");
|
|
3535
|
+
return dfs(this.root);
|
|
3536
|
+
} else {
|
|
3537
|
+
let current = this.root;
|
|
3538
|
+
let result = void 0;
|
|
3539
|
+
while (this.isRealNode(current)) {
|
|
3540
|
+
const cmp = this.comparator(current.key, key);
|
|
3541
|
+
if (cmp <= 0) {
|
|
3542
|
+
result = current;
|
|
3543
|
+
current = current.right ?? void 0;
|
|
3544
|
+
} else {
|
|
3545
|
+
current = current.left ?? void 0;
|
|
3546
|
+
}
|
|
3547
|
+
}
|
|
3548
|
+
return result;
|
|
3549
|
+
}
|
|
3550
|
+
}
|
|
3551
|
+
/**
|
|
3552
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
3553
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3554
|
+
* Returns the last node that satisfies the predicate function.
|
|
3555
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3556
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3557
|
+
*
|
|
3558
|
+
* @param predicate - The predicate function to test nodes.
|
|
3559
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3560
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
3561
|
+
*/
|
|
3562
|
+
_floorByPredicate(predicate, iterationType) {
|
|
3563
|
+
if (iterationType === "RECURSIVE") {
|
|
3564
|
+
let result = void 0;
|
|
3565
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3566
|
+
if (!this.isRealNode(cur)) return;
|
|
3567
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3568
|
+
if (predicate(cur)) {
|
|
3569
|
+
result = cur;
|
|
3570
|
+
}
|
|
3571
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3572
|
+
}, "dfs");
|
|
3573
|
+
dfs(this.root);
|
|
3574
|
+
return result;
|
|
3575
|
+
} else {
|
|
3576
|
+
const stack = [];
|
|
3577
|
+
let current = this.root;
|
|
3578
|
+
let result = void 0;
|
|
3579
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3580
|
+
if (this.isRealNode(current)) {
|
|
3581
|
+
stack.push(current);
|
|
3582
|
+
current = current.left;
|
|
3583
|
+
} else {
|
|
3584
|
+
const node = stack.pop();
|
|
3585
|
+
if (!this.isRealNode(node)) break;
|
|
3586
|
+
if (predicate(node)) {
|
|
3587
|
+
result = node;
|
|
3588
|
+
}
|
|
3589
|
+
current = node.right;
|
|
3590
|
+
}
|
|
3591
|
+
}
|
|
3592
|
+
return result;
|
|
3593
|
+
}
|
|
3594
|
+
}
|
|
3595
|
+
/**
|
|
3596
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
3597
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3598
|
+
* Finds first node where key < target.
|
|
3599
|
+
* @remarks Time O(h) where h is tree height.
|
|
3600
|
+
*
|
|
3601
|
+
* @param key - The target key to search for.
|
|
3602
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3603
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
3604
|
+
*/
|
|
3605
|
+
_lowerByKey(key, iterationType) {
|
|
3606
|
+
if (iterationType === "RECURSIVE") {
|
|
3607
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3608
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3609
|
+
const cmp = this.comparator(cur.key, key);
|
|
3610
|
+
if (cmp < 0) {
|
|
3611
|
+
const rightResult = dfs(cur.right);
|
|
3612
|
+
return rightResult ?? cur;
|
|
3613
|
+
} else {
|
|
3614
|
+
return dfs(cur.left);
|
|
3615
|
+
}
|
|
3616
|
+
}, "dfs");
|
|
3617
|
+
return dfs(this.root);
|
|
3618
|
+
} else {
|
|
3619
|
+
let current = this.root;
|
|
3620
|
+
let result = void 0;
|
|
3621
|
+
while (this.isRealNode(current)) {
|
|
3622
|
+
const cmp = this.comparator(current.key, key);
|
|
3623
|
+
if (cmp < 0) {
|
|
3624
|
+
result = current;
|
|
3625
|
+
current = current.right ?? void 0;
|
|
3626
|
+
} else {
|
|
3627
|
+
current = current.left ?? void 0;
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
return result;
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
/**
|
|
3634
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
3635
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3636
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
3637
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
3638
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
3639
|
+
*
|
|
3640
|
+
* @param predicate - The predicate function to test nodes.
|
|
3641
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3642
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
3643
|
+
*/
|
|
3644
|
+
_lowerByPredicate(predicate, iterationType) {
|
|
3645
|
+
if (iterationType === "RECURSIVE") {
|
|
3646
|
+
let result = void 0;
|
|
3647
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3648
|
+
if (!this.isRealNode(cur)) return;
|
|
3649
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3650
|
+
if (predicate(cur)) {
|
|
3651
|
+
result = cur;
|
|
3652
|
+
}
|
|
3653
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
3654
|
+
}, "dfs");
|
|
3655
|
+
dfs(this.root);
|
|
3656
|
+
return result;
|
|
3657
|
+
} else {
|
|
3658
|
+
const stack = [];
|
|
3659
|
+
let current = this.root;
|
|
3660
|
+
let result = void 0;
|
|
3661
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3662
|
+
if (this.isRealNode(current)) {
|
|
3663
|
+
stack.push(current);
|
|
3664
|
+
current = current.left;
|
|
3665
|
+
} else {
|
|
3666
|
+
const node = stack.pop();
|
|
3667
|
+
if (!this.isRealNode(node)) break;
|
|
3668
|
+
if (predicate(node)) {
|
|
3669
|
+
result = node;
|
|
3670
|
+
}
|
|
3671
|
+
current = node.right;
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
return result;
|
|
3675
|
+
}
|
|
3676
|
+
}
|
|
3403
3677
|
/**
|
|
3404
3678
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3405
3679
|
* Unified logic for both lowerBound and upperBound.
|