tree-multimap-typed 2.2.3 → 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 +385 -101
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +387 -101
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +385 -101
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +387 -101
- 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/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +151 -117
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/tree-multimap-typed.js +387 -101
- 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-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +724 -108
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
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");
|
|
@@ -2833,9 +2839,13 @@ var BST = class extends BinaryTree {
|
|
|
2833
2839
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2834
2840
|
super([], options);
|
|
2835
2841
|
if (options) {
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2842
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2843
|
+
this._comparator = options.comparator;
|
|
2844
|
+
} else {
|
|
2845
|
+
this._comparator = this._createDefaultComparator();
|
|
2846
|
+
}
|
|
2847
|
+
} else {
|
|
2848
|
+
this._comparator = this._createDefaultComparator();
|
|
2839
2849
|
}
|
|
2840
2850
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2841
2851
|
}
|
|
@@ -2849,40 +2859,12 @@ var BST = class extends BinaryTree {
|
|
|
2849
2859
|
get root() {
|
|
2850
2860
|
return this._root;
|
|
2851
2861
|
}
|
|
2852
|
-
_isReverse = false;
|
|
2853
2862
|
/**
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
get isReverse() {
|
|
2860
|
-
return this._isReverse;
|
|
2861
|
-
}
|
|
2862
|
-
/**
|
|
2863
|
-
* The default comparator function.
|
|
2864
|
-
* @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
|
|
2865
|
-
*/
|
|
2866
|
-
_comparator = /* @__PURE__ */ __name((a, b) => {
|
|
2867
|
-
if (isComparable(a) && isComparable(b)) {
|
|
2868
|
-
if (a > b) return 1;
|
|
2869
|
-
if (a < b) return -1;
|
|
2870
|
-
return 0;
|
|
2871
|
-
}
|
|
2872
|
-
if (this._specifyComparable) {
|
|
2873
|
-
const va = this._specifyComparable(a);
|
|
2874
|
-
const vb = this._specifyComparable(b);
|
|
2875
|
-
if (va > vb) return 1;
|
|
2876
|
-
if (va < vb) return -1;
|
|
2877
|
-
return 0;
|
|
2878
|
-
}
|
|
2879
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2880
|
-
throw TypeError(
|
|
2881
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2882
|
-
);
|
|
2883
|
-
}
|
|
2884
|
-
return 0;
|
|
2885
|
-
}, "_comparator");
|
|
2863
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2864
|
+
|
|
2865
|
+
* @remarks Time O(1) Space O(1)
|
|
2866
|
+
*/
|
|
2867
|
+
_comparator;
|
|
2886
2868
|
/**
|
|
2887
2869
|
* Gets the comparator function used by the tree.
|
|
2888
2870
|
* @remarks Time O(1)
|
|
@@ -2892,16 +2874,6 @@ var BST = class extends BinaryTree {
|
|
|
2892
2874
|
get comparator() {
|
|
2893
2875
|
return this._comparator;
|
|
2894
2876
|
}
|
|
2895
|
-
_specifyComparable;
|
|
2896
|
-
/**
|
|
2897
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2898
|
-
* @remarks Time O(1)
|
|
2899
|
-
*
|
|
2900
|
-
* @returns The key-to-comparable conversion function.
|
|
2901
|
-
*/
|
|
2902
|
-
get specifyComparable() {
|
|
2903
|
-
return this._specifyComparable;
|
|
2904
|
-
}
|
|
2905
2877
|
/**
|
|
2906
2878
|
* (Protected) Creates a new BST node.
|
|
2907
2879
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2942,7 +2914,7 @@ var BST = class extends BinaryTree {
|
|
|
2942
2914
|
* @returns True if the key is valid, false otherwise.
|
|
2943
2915
|
*/
|
|
2944
2916
|
isValidKey(key) {
|
|
2945
|
-
return isComparable(key
|
|
2917
|
+
return isComparable(key);
|
|
2946
2918
|
}
|
|
2947
2919
|
/**
|
|
2948
2920
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3031,8 +3003,8 @@ var BST = class extends BinaryTree {
|
|
|
3031
3003
|
if (!this.isRealNode(cur.left)) return false;
|
|
3032
3004
|
if (isRange) {
|
|
3033
3005
|
const range = keyNodeEntryOrPredicate;
|
|
3034
|
-
const leftS =
|
|
3035
|
-
const leftI =
|
|
3006
|
+
const leftS = range.low;
|
|
3007
|
+
const leftI = range.includeLow;
|
|
3036
3008
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
3037
3009
|
}
|
|
3038
3010
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3046,8 +3018,8 @@ var BST = class extends BinaryTree {
|
|
|
3046
3018
|
if (!this.isRealNode(cur.right)) return false;
|
|
3047
3019
|
if (isRange) {
|
|
3048
3020
|
const range = keyNodeEntryOrPredicate;
|
|
3049
|
-
const rightS =
|
|
3050
|
-
const rightI =
|
|
3021
|
+
const rightS = range.high;
|
|
3022
|
+
const rightI = range.includeHigh;
|
|
3051
3023
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
3052
3024
|
}
|
|
3053
3025
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3208,31 +3180,141 @@ var BST = class extends BinaryTree {
|
|
|
3208
3180
|
else _iterate();
|
|
3209
3181
|
return inserted;
|
|
3210
3182
|
}
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
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;
|
|
3223
3216
|
}
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
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;
|
|
3236
3318
|
}
|
|
3237
3319
|
/**
|
|
3238
3320
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
@@ -3368,31 +3450,234 @@ var BST = class extends BinaryTree {
|
|
|
3368
3450
|
return out;
|
|
3369
3451
|
}
|
|
3370
3452
|
/**
|
|
3371
|
-
* Deletes
|
|
3372
|
-
* @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
|
|
3453
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
3373
3454
|
*
|
|
3374
|
-
* @
|
|
3375
|
-
*
|
|
3455
|
+
* @remarks
|
|
3456
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
3457
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
3458
|
+
*
|
|
3459
|
+
* @template K - The key type.
|
|
3460
|
+
* @template V - The value type.
|
|
3461
|
+
*
|
|
3462
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
3463
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
3464
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
3465
|
+
* - An entry tuple: searches for the key-value pair.
|
|
3466
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
3467
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
3468
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
3469
|
+
*
|
|
3470
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
3471
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
3472
|
+
*
|
|
3473
|
+
* @param startNode - The node to start the search from. Can be:
|
|
3474
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
3475
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
3476
|
+
* - Default value: this._root (the tree's root).
|
|
3477
|
+
*
|
|
3478
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
3479
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
3480
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
3481
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
3482
|
+
*
|
|
3483
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
3484
|
+
* - Key: the matched node's key.
|
|
3485
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
3486
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
3487
|
+
*/
|
|
3488
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3489
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
3490
|
+
let results = [];
|
|
3491
|
+
for (const node of toDelete) {
|
|
3492
|
+
const deleteInfo = this.delete(node);
|
|
3493
|
+
results = results.concat(deleteInfo);
|
|
3494
|
+
}
|
|
3495
|
+
return results;
|
|
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.
|
|
3376
3501
|
*/
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
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
|
+
}
|
|
3385
3552
|
}
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
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
|
+
}
|
|
3392
3596
|
}
|
|
3393
|
-
|
|
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;
|
|
3394
3680
|
}
|
|
3395
|
-
return false;
|
|
3396
3681
|
}
|
|
3397
3682
|
/**
|
|
3398
3683
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -3544,8 +3829,7 @@ var BST = class extends BinaryTree {
|
|
|
3544
3829
|
_snapshotOptions() {
|
|
3545
3830
|
return {
|
|
3546
3831
|
...super._snapshotOptions(),
|
|
3547
|
-
|
|
3548
|
-
isReverse: this.isReverse
|
|
3832
|
+
comparator: this._comparator
|
|
3549
3833
|
};
|
|
3550
3834
|
}
|
|
3551
3835
|
/**
|
|
@@ -3573,14 +3857,14 @@ var BST = class extends BinaryTree {
|
|
|
3573
3857
|
}
|
|
3574
3858
|
/**
|
|
3575
3859
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3576
|
-
* @remarks Time O(1)
|
|
3860
|
+
* @remarks Time O(1) Space O(1)
|
|
3577
3861
|
*
|
|
3578
3862
|
* @param a - The first key.
|
|
3579
3863
|
* @param b - The second key.
|
|
3580
3864
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3581
3865
|
*/
|
|
3582
3866
|
_compare(a, b) {
|
|
3583
|
-
return this.
|
|
3867
|
+
return this._comparator(a, b);
|
|
3584
3868
|
}
|
|
3585
3869
|
/**
|
|
3586
3870
|
* (Private) Deletes a node by its key.
|