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.
@@ -998,6 +998,14 @@ var _IterableEntryBase = class _IterableEntryBase {
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.
@@ -1024,8 +1032,6 @@ var _Range = class _Range {
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
  // Determine whether a key is within the range
1031
1037
  isInRange(key, comparator) {
@@ -2852,27 +2858,6 @@ var _BST = class _BST extends BinaryTree {
2852
2858
  get root() {
2853
2859
  return this._root;
2854
2860
  }
2855
- /**
2856
- * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2857
- * @remarks Time O(1) Space O(1)
2858
- * @returns The default comparator function.
2859
- */
2860
- _createDefaultComparator() {
2861
- return (a, b) => {
2862
- debugger;
2863
- if (isComparable(a) && isComparable(b)) {
2864
- if (a > b) return 1;
2865
- if (a < b) return -1;
2866
- return 0;
2867
- }
2868
- if (typeof a === "object" || typeof b === "object") {
2869
- throw TypeError(
2870
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2871
- );
2872
- }
2873
- return 0;
2874
- };
2875
- }
2876
2861
  /**
2877
2862
  * Gets the comparator function used by the tree.
2878
2863
  * @remarks Time O(1)
@@ -3190,31 +3175,141 @@ var _BST = class _BST extends BinaryTree {
3190
3175
  else _iterate();
3191
3176
  return inserted;
3192
3177
  }
3193
- /**
3194
- * Returns the first node with a key greater than or equal to the given key.
3195
- * This is equivalent to C++ std::lower_bound on a BST.
3196
- * Supports RECURSIVE and ITERATIVE implementations.
3197
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3198
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3199
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3200
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3201
- * @returns The first node with key >= given key, or undefined if no such node exists.
3202
- */
3203
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3204
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
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 == null ? void 0 : 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 == null ? void 0 : node.key;
3209
+ }
3210
+ return node ? actualCallback(node) : void 0;
3205
3211
  }
3206
- /**
3207
- * Returns the first node with a key strictly greater than the given key.
3208
- * This is equivalent to C++ std::upper_bound on a BST.
3209
- * Supports RECURSIVE and ITERATIVE implementations.
3210
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3211
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3212
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3213
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3214
- * @returns The first node with key > given key, or undefined if no such node exists.
3215
- */
3216
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3217
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
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 == null ? void 0 : 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 == null ? void 0 : 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 == null ? void 0 : 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 == null ? void 0 : 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 _BST 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,193 @@ var _BST = class _BST 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
+ var _a, _b;
3525
+ if (iterationType === "RECURSIVE") {
3526
+ const dfs = /* @__PURE__ */ __name((cur) => {
3527
+ if (!this.isRealNode(cur)) return void 0;
3528
+ const cmp = this.comparator(cur.key, key);
3529
+ if (cmp <= 0) {
3530
+ const rightResult = dfs(cur.right);
3531
+ return rightResult != null ? rightResult : cur;
3532
+ } else {
3533
+ return dfs(cur.left);
3534
+ }
3535
+ }, "dfs");
3536
+ return dfs(this.root);
3537
+ } else {
3538
+ let current = this.root;
3539
+ let result = void 0;
3540
+ while (this.isRealNode(current)) {
3541
+ const cmp = this.comparator(current.key, key);
3542
+ if (cmp <= 0) {
3543
+ result = current;
3544
+ current = (_a = current.right) != null ? _a : void 0;
3545
+ } else {
3546
+ current = (_b = current.left) != null ? _b : void 0;
3547
+ }
3548
+ }
3549
+ return result;
3550
+ }
3551
+ }
3552
+ /**
3553
+ * (Protected) In-order traversal search for floor by predicate.
3554
+ * Falls back to linear in-order traversal when predicate-based search is required.
3555
+ * Returns the last node that satisfies the predicate function.
3556
+ * @remarks Time Complexity: O(n) since it may visit every node.
3557
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3558
+ *
3559
+ * @param predicate - The predicate function to test nodes.
3560
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3561
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
3562
+ */
3563
+ _floorByPredicate(predicate, iterationType) {
3564
+ if (iterationType === "RECURSIVE") {
3565
+ let result = void 0;
3566
+ const dfs = /* @__PURE__ */ __name((cur) => {
3567
+ if (!this.isRealNode(cur)) return;
3568
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3569
+ if (predicate(cur)) {
3570
+ result = cur;
3571
+ }
3572
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3573
+ }, "dfs");
3574
+ dfs(this.root);
3575
+ return result;
3576
+ } else {
3577
+ const stack = [];
3578
+ let current = this.root;
3579
+ let result = void 0;
3580
+ while (stack.length > 0 || this.isRealNode(current)) {
3581
+ if (this.isRealNode(current)) {
3582
+ stack.push(current);
3583
+ current = current.left;
3584
+ } else {
3585
+ const node = stack.pop();
3586
+ if (!this.isRealNode(node)) break;
3587
+ if (predicate(node)) {
3588
+ result = node;
3589
+ }
3590
+ current = node.right;
3591
+ }
3592
+ }
3593
+ return result;
3594
+ }
3595
+ }
3596
+ /**
3597
+ * (Protected) Binary search for lower by key with pruning optimization.
3598
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3599
+ * Finds first node where key < target.
3600
+ * @remarks Time O(h) where h is tree height.
3601
+ *
3602
+ * @param key - The target key to search for.
3603
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3604
+ * @returns The first node with key < target, or undefined if none exists.
3605
+ */
3606
+ _lowerByKey(key, iterationType) {
3607
+ var _a, _b;
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 != null ? 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 = (_a = current.right) != null ? _a : void 0;
3628
+ } else {
3629
+ current = (_b = current.left) != null ? _b : 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
+ }
3403
3679
  /**
3404
3680
  * (Protected) Core bound search implementation supporting all parameter types.
3405
3681
  * Unified logic for both lowerBound and upperBound.