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