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.
@@ -996,6 +996,14 @@ var _IterableEntryBase = class _IterableEntryBase {
996
996
  }
997
997
  return accumulator;
998
998
  }
999
+ /**
1000
+ * Converts data structure to `[key, value]` pairs.
1001
+ * @returns Array of entries.
1002
+ * @remarks Time O(n), Space O(n)
1003
+ */
1004
+ toArray() {
1005
+ return [...this];
1006
+ }
999
1007
  /**
1000
1008
  * Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
1001
1009
  * @returns Array of entries (default) or a string.
@@ -1022,8 +1030,6 @@ var _Range = class _Range {
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
  // Determine whether a key is within the range
1029
1035
  isInRange(key, comparator) {
@@ -2850,27 +2856,6 @@ var _BST = class _BST 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
  * Gets the comparator function used by the tree.
2876
2861
  * @remarks Time O(1)
@@ -3188,31 +3173,141 @@ var _BST = class _BST extends BinaryTree {
3188
3173
  else _iterate();
3189
3174
  return inserted;
3190
3175
  }
3191
- /**
3192
- * Returns the first node with a key greater than or equal to the given key.
3193
- * This is equivalent to C++ std::lower_bound on a BST.
3194
- * Supports RECURSIVE and ITERATIVE implementations.
3195
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3196
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3197
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3198
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3199
- * @returns The first node with key >= given key, or undefined if no such node exists.
3200
- */
3201
- lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3202
- return this._bound(keyNodeEntryOrPredicate, true, iterationType);
3176
+ ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3177
+ let actualCallback = void 0;
3178
+ let actualIterationType = this.iterationType;
3179
+ if (typeof callback === "string") {
3180
+ actualIterationType = callback;
3181
+ } else if (callback) {
3182
+ actualCallback = callback;
3183
+ if (iterationType) {
3184
+ actualIterationType = iterationType;
3185
+ }
3186
+ }
3187
+ const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
3188
+ if (!actualCallback) {
3189
+ return node == null ? void 0 : node.key;
3190
+ }
3191
+ return node ? actualCallback(node) : void 0;
3192
+ }
3193
+ higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3194
+ let actualCallback = void 0;
3195
+ let actualIterationType = this.iterationType;
3196
+ if (typeof callback === "string") {
3197
+ actualIterationType = callback;
3198
+ } else if (callback) {
3199
+ actualCallback = callback;
3200
+ if (iterationType) {
3201
+ actualIterationType = iterationType;
3202
+ }
3203
+ }
3204
+ const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
3205
+ if (!actualCallback) {
3206
+ return node == null ? void 0 : node.key;
3207
+ }
3208
+ return node ? actualCallback(node) : void 0;
3203
3209
  }
3204
- /**
3205
- * Returns the first node with a key strictly greater than the given key.
3206
- * This is equivalent to C++ std::upper_bound on a BST.
3207
- * Supports RECURSIVE and ITERATIVE implementations.
3208
- * Time Complexity: O(log n) on average, O(h) where h is tree height.
3209
- * Space Complexity: O(h) for recursion, O(1) for iteration.
3210
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3211
- * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
3212
- * @returns The first node with key > given key, or undefined if no such node exists.
3213
- */
3214
- upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3215
- return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3210
+ floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
3211
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3212
+ if (typeof callback === "string" || !callback) {
3213
+ return void 0;
3214
+ }
3215
+ return void 0;
3216
+ }
3217
+ let actualCallback = void 0;
3218
+ let actualIterationType = this.iterationType;
3219
+ if (typeof callback === "string") {
3220
+ actualIterationType = callback;
3221
+ } else if (callback) {
3222
+ actualCallback = callback;
3223
+ if (iterationType) {
3224
+ actualIterationType = iterationType;
3225
+ }
3226
+ }
3227
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3228
+ const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3229
+ if (!actualCallback) {
3230
+ return node == null ? void 0 : node.key;
3231
+ }
3232
+ return node ? actualCallback(node) : void 0;
3233
+ }
3234
+ let targetKey;
3235
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3236
+ targetKey = keyNodeEntryOrPredicate.key;
3237
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3238
+ const key = keyNodeEntryOrPredicate[0];
3239
+ if (key === null || key === void 0) {
3240
+ if (typeof callback === "string" || !callback) {
3241
+ return void 0;
3242
+ }
3243
+ return void 0;
3244
+ }
3245
+ targetKey = key;
3246
+ } else {
3247
+ targetKey = keyNodeEntryOrPredicate;
3248
+ }
3249
+ if (targetKey !== void 0) {
3250
+ const node = this._floorByKey(targetKey, actualIterationType);
3251
+ if (!actualCallback) {
3252
+ return node == null ? void 0 : node.key;
3253
+ }
3254
+ return node ? actualCallback(node) : void 0;
3255
+ }
3256
+ if (typeof callback === "string" || !callback) {
3257
+ return void 0;
3258
+ }
3259
+ return void 0;
3260
+ }
3261
+ lower(keyNodeEntryOrPredicate, callback, iterationType) {
3262
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3263
+ if (typeof callback === "string" || !callback) {
3264
+ return void 0;
3265
+ }
3266
+ return void 0;
3267
+ }
3268
+ let actualCallback = void 0;
3269
+ let actualIterationType = this.iterationType;
3270
+ if (typeof callback === "string") {
3271
+ actualIterationType = callback;
3272
+ } else if (callback) {
3273
+ actualCallback = callback;
3274
+ if (iterationType) {
3275
+ actualIterationType = iterationType;
3276
+ }
3277
+ }
3278
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3279
+ const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
3280
+ if (!actualCallback) {
3281
+ return node == null ? void 0 : node.key;
3282
+ }
3283
+ return node ? actualCallback(node) : void 0;
3284
+ }
3285
+ let targetKey;
3286
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3287
+ targetKey = keyNodeEntryOrPredicate.key;
3288
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3289
+ const key = keyNodeEntryOrPredicate[0];
3290
+ if (key === null || key === void 0) {
3291
+ if (typeof callback === "string" || !callback) {
3292
+ return void 0;
3293
+ }
3294
+ return void 0;
3295
+ }
3296
+ targetKey = key;
3297
+ } else {
3298
+ targetKey = keyNodeEntryOrPredicate;
3299
+ }
3300
+ if (targetKey !== void 0) {
3301
+ const node = this._lowerByKey(targetKey, actualIterationType);
3302
+ if (!actualCallback) {
3303
+ return node == null ? void 0 : node.key;
3304
+ }
3305
+ return node ? actualCallback(node) : void 0;
3306
+ }
3307
+ if (typeof callback === "string" || !callback) {
3308
+ return void 0;
3309
+ }
3310
+ return void 0;
3216
3311
  }
3217
3312
  /**
3218
3313
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
@@ -3384,13 +3479,7 @@ var _BST = class _BST extends BinaryTree {
3384
3479
  * - If no nodes match the search criteria, the returned map is empty.
3385
3480
  */
3386
3481
  deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3387
- const toDelete = this.search(
3388
- keyNodeEntryOrPredicate,
3389
- onlyOne,
3390
- (node) => node,
3391
- startNode,
3392
- iterationType
3393
- );
3482
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
3394
3483
  let results = [];
3395
3484
  for (const node of toDelete) {
3396
3485
  const deleteInfo = this.delete(node);
@@ -3398,6 +3487,193 @@ var _BST = class _BST extends BinaryTree {
3398
3487
  }
3399
3488
  return results;
3400
3489
  }
3490
+ /**
3491
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
3492
+ * @remarks Time O(1) Space O(1)
3493
+ * @returns The default comparator function.
3494
+ */
3495
+ _createDefaultComparator() {
3496
+ return (a, b) => {
3497
+ debugger;
3498
+ if (isComparable(a) && isComparable(b)) {
3499
+ if (a > b) return 1;
3500
+ if (a < b) return -1;
3501
+ return 0;
3502
+ }
3503
+ if (typeof a === "object" || typeof b === "object") {
3504
+ throw TypeError(
3505
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3506
+ );
3507
+ }
3508
+ return 0;
3509
+ };
3510
+ }
3511
+ /**
3512
+ * (Protected) Binary search for floor by key with pruning optimization.
3513
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3514
+ * Finds first node where key <= target.
3515
+ * @remarks Time O(h) where h is tree height.
3516
+ *
3517
+ * @param key - The target key to search for.
3518
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3519
+ * @returns The first node with key <= target, or undefined if none exists.
3520
+ */
3521
+ _floorByKey(key, iterationType) {
3522
+ var _a, _b;
3523
+ if (iterationType === "RECURSIVE") {
3524
+ const dfs = /* @__PURE__ */ __name((cur) => {
3525
+ if (!this.isRealNode(cur)) return void 0;
3526
+ const cmp = this.comparator(cur.key, key);
3527
+ if (cmp <= 0) {
3528
+ const rightResult = dfs(cur.right);
3529
+ return rightResult != null ? rightResult : cur;
3530
+ } else {
3531
+ return dfs(cur.left);
3532
+ }
3533
+ }, "dfs");
3534
+ return dfs(this.root);
3535
+ } else {
3536
+ let current = this.root;
3537
+ let result = void 0;
3538
+ while (this.isRealNode(current)) {
3539
+ const cmp = this.comparator(current.key, key);
3540
+ if (cmp <= 0) {
3541
+ result = current;
3542
+ current = (_a = current.right) != null ? _a : void 0;
3543
+ } else {
3544
+ current = (_b = current.left) != null ? _b : void 0;
3545
+ }
3546
+ }
3547
+ return result;
3548
+ }
3549
+ }
3550
+ /**
3551
+ * (Protected) In-order traversal search for floor by predicate.
3552
+ * Falls back to linear in-order traversal when predicate-based search is required.
3553
+ * Returns the last node that satisfies the predicate function.
3554
+ * @remarks Time Complexity: O(n) since it may visit every node.
3555
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3556
+ *
3557
+ * @param predicate - The predicate function to test nodes.
3558
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3559
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
3560
+ */
3561
+ _floorByPredicate(predicate, iterationType) {
3562
+ if (iterationType === "RECURSIVE") {
3563
+ let result = void 0;
3564
+ const dfs = /* @__PURE__ */ __name((cur) => {
3565
+ if (!this.isRealNode(cur)) return;
3566
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3567
+ if (predicate(cur)) {
3568
+ result = cur;
3569
+ }
3570
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3571
+ }, "dfs");
3572
+ dfs(this.root);
3573
+ return result;
3574
+ } else {
3575
+ const stack = [];
3576
+ let current = this.root;
3577
+ let result = void 0;
3578
+ while (stack.length > 0 || this.isRealNode(current)) {
3579
+ if (this.isRealNode(current)) {
3580
+ stack.push(current);
3581
+ current = current.left;
3582
+ } else {
3583
+ const node = stack.pop();
3584
+ if (!this.isRealNode(node)) break;
3585
+ if (predicate(node)) {
3586
+ result = node;
3587
+ }
3588
+ current = node.right;
3589
+ }
3590
+ }
3591
+ return result;
3592
+ }
3593
+ }
3594
+ /**
3595
+ * (Protected) Binary search for lower by key with pruning optimization.
3596
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3597
+ * Finds first node where key < target.
3598
+ * @remarks Time O(h) where h is tree height.
3599
+ *
3600
+ * @param key - The target key to search for.
3601
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3602
+ * @returns The first node with key < target, or undefined if none exists.
3603
+ */
3604
+ _lowerByKey(key, iterationType) {
3605
+ var _a, _b;
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 != null ? 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 = (_a = current.right) != null ? _a : void 0;
3626
+ } else {
3627
+ current = (_b = current.left) != null ? _b : 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
+ }
3401
3677
  /**
3402
3678
  * (Protected) Core bound search implementation supporting all parameter types.
3403
3679
  * Unified logic for both lowerBound and upperBound.