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