avl-tree-typed 2.2.4 → 2.2.5

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.
@@ -2850,27 +2850,6 @@ var _BST = class _BST extends BinaryTree {
2850
2850
  get root() {
2851
2851
  return this._root;
2852
2852
  }
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
2853
  /**
2875
2854
  * Gets the comparator function used by the tree.
2876
2855
  * @remarks Time O(1)
@@ -3214,6 +3193,104 @@ var _BST = class _BST extends BinaryTree {
3214
3193
  upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3215
3194
  return this._bound(keyNodeEntryOrPredicate, false, iterationType);
3216
3195
  }
3196
+ /**
3197
+ * Returns the first node with a key greater than or equal to the given key.
3198
+ * This is equivalent to Java TreeMap.ceilingEntry().
3199
+ * Supports RECURSIVE and ITERATIVE implementations.
3200
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3201
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3202
+ *
3203
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3204
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3205
+ * @returns The first node with key >= given key, or undefined if no such node exists.
3206
+ */
3207
+ ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3208
+ return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
3209
+ }
3210
+ /**
3211
+ * Returns the first node with a key strictly greater than the given key.
3212
+ * This is equivalent to Java TreeMap.higherEntry().
3213
+ * Supports RECURSIVE and ITERATIVE implementations.
3214
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3215
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3216
+ *
3217
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3218
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3219
+ * @returns The first node with key > given key, or undefined if no such node exists.
3220
+ */
3221
+ higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3222
+ return this.upperBound(keyNodeEntryOrPredicate, iterationType);
3223
+ }
3224
+ /**
3225
+ * Returns the first node with a key less than or equal to the given key.
3226
+ * This is equivalent to Java TreeMap.floorEntry().
3227
+ * Supports RECURSIVE and ITERATIVE implementations.
3228
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3229
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3230
+ *
3231
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3232
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3233
+ * @returns The first node with key <= given key, or undefined if no such node exists.
3234
+ */
3235
+ floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3236
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3237
+ return void 0;
3238
+ }
3239
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3240
+ return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
3241
+ }
3242
+ let targetKey;
3243
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3244
+ targetKey = keyNodeEntryOrPredicate.key;
3245
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3246
+ const key = keyNodeEntryOrPredicate[0];
3247
+ if (key === null || key === void 0) {
3248
+ return void 0;
3249
+ }
3250
+ targetKey = key;
3251
+ } else {
3252
+ targetKey = keyNodeEntryOrPredicate;
3253
+ }
3254
+ if (targetKey !== void 0) {
3255
+ return this._floorByKey(targetKey, iterationType);
3256
+ }
3257
+ return void 0;
3258
+ }
3259
+ /**
3260
+ * Returns the first node with a key strictly less than the given key.
3261
+ * This is equivalent to Java TreeMap.lowerEntry().
3262
+ * Supports RECURSIVE and ITERATIVE implementations.
3263
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
3264
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
3265
+ *
3266
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
3267
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
3268
+ * @returns The first node with key < given key, or undefined if no such node exists.
3269
+ */
3270
+ lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
3271
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
3272
+ return void 0;
3273
+ }
3274
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
3275
+ return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
3276
+ }
3277
+ let targetKey;
3278
+ if (this.isNode(keyNodeEntryOrPredicate)) {
3279
+ targetKey = keyNodeEntryOrPredicate.key;
3280
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
3281
+ const key = keyNodeEntryOrPredicate[0];
3282
+ if (key === null || key === void 0) {
3283
+ return void 0;
3284
+ }
3285
+ targetKey = key;
3286
+ } else {
3287
+ targetKey = keyNodeEntryOrPredicate;
3288
+ }
3289
+ if (targetKey !== void 0) {
3290
+ return this._lowerByKey(targetKey, iterationType);
3291
+ }
3292
+ return void 0;
3293
+ }
3217
3294
  /**
3218
3295
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
3219
3296
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -3384,13 +3461,7 @@ var _BST = class _BST extends BinaryTree {
3384
3461
  * - If no nodes match the search criteria, the returned map is empty.
3385
3462
  */
3386
3463
  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
- );
3464
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
3394
3465
  let results = [];
3395
3466
  for (const node of toDelete) {
3396
3467
  const deleteInfo = this.delete(node);
@@ -3398,6 +3469,193 @@ var _BST = class _BST extends BinaryTree {
3398
3469
  }
3399
3470
  return results;
3400
3471
  }
3472
+ /**
3473
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
3474
+ * @remarks Time O(1) Space O(1)
3475
+ * @returns The default comparator function.
3476
+ */
3477
+ _createDefaultComparator() {
3478
+ return (a, b) => {
3479
+ debugger;
3480
+ if (isComparable(a) && isComparable(b)) {
3481
+ if (a > b) return 1;
3482
+ if (a < b) return -1;
3483
+ return 0;
3484
+ }
3485
+ if (typeof a === "object" || typeof b === "object") {
3486
+ throw TypeError(
3487
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
3488
+ );
3489
+ }
3490
+ return 0;
3491
+ };
3492
+ }
3493
+ /**
3494
+ * (Protected) Binary search for floor by key with pruning optimization.
3495
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3496
+ * Finds first node where key <= target.
3497
+ * @remarks Time O(h) where h is tree height.
3498
+ *
3499
+ * @param key - The target key to search for.
3500
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3501
+ * @returns The first node with key <= target, or undefined if none exists.
3502
+ */
3503
+ _floorByKey(key, iterationType) {
3504
+ var _a, _b;
3505
+ if (iterationType === "RECURSIVE") {
3506
+ const dfs = /* @__PURE__ */ __name((cur) => {
3507
+ if (!this.isRealNode(cur)) return void 0;
3508
+ const cmp = this.comparator(cur.key, key);
3509
+ if (cmp <= 0) {
3510
+ const rightResult = dfs(cur.right);
3511
+ return rightResult != null ? rightResult : cur;
3512
+ } else {
3513
+ return dfs(cur.left);
3514
+ }
3515
+ }, "dfs");
3516
+ return dfs(this.root);
3517
+ } else {
3518
+ let current = this.root;
3519
+ let result = void 0;
3520
+ while (this.isRealNode(current)) {
3521
+ const cmp = this.comparator(current.key, key);
3522
+ if (cmp <= 0) {
3523
+ result = current;
3524
+ current = (_a = current.right) != null ? _a : void 0;
3525
+ } else {
3526
+ current = (_b = current.left) != null ? _b : void 0;
3527
+ }
3528
+ }
3529
+ return result;
3530
+ }
3531
+ }
3532
+ /**
3533
+ * (Protected) In-order traversal search for floor by predicate.
3534
+ * Falls back to linear in-order traversal when predicate-based search is required.
3535
+ * Returns the last node that satisfies the predicate function.
3536
+ * @remarks Time Complexity: O(n) since it may visit every node.
3537
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3538
+ *
3539
+ * @param predicate - The predicate function to test nodes.
3540
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3541
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
3542
+ */
3543
+ _floorByPredicate(predicate, iterationType) {
3544
+ if (iterationType === "RECURSIVE") {
3545
+ let result = void 0;
3546
+ const dfs = /* @__PURE__ */ __name((cur) => {
3547
+ if (!this.isRealNode(cur)) return;
3548
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3549
+ if (predicate(cur)) {
3550
+ result = cur;
3551
+ }
3552
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3553
+ }, "dfs");
3554
+ dfs(this.root);
3555
+ return result;
3556
+ } else {
3557
+ const stack = [];
3558
+ let current = this.root;
3559
+ let result = void 0;
3560
+ while (stack.length > 0 || this.isRealNode(current)) {
3561
+ if (this.isRealNode(current)) {
3562
+ stack.push(current);
3563
+ current = current.left;
3564
+ } else {
3565
+ const node = stack.pop();
3566
+ if (!this.isRealNode(node)) break;
3567
+ if (predicate(node)) {
3568
+ result = node;
3569
+ }
3570
+ current = node.right;
3571
+ }
3572
+ }
3573
+ return result;
3574
+ }
3575
+ }
3576
+ /**
3577
+ * (Protected) Binary search for lower by key with pruning optimization.
3578
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
3579
+ * Finds first node where key < target.
3580
+ * @remarks Time O(h) where h is tree height.
3581
+ *
3582
+ * @param key - The target key to search for.
3583
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3584
+ * @returns The first node with key < target, or undefined if none exists.
3585
+ */
3586
+ _lowerByKey(key, iterationType) {
3587
+ var _a, _b;
3588
+ if (iterationType === "RECURSIVE") {
3589
+ const dfs = /* @__PURE__ */ __name((cur) => {
3590
+ if (!this.isRealNode(cur)) return void 0;
3591
+ const cmp = this.comparator(cur.key, key);
3592
+ if (cmp < 0) {
3593
+ const rightResult = dfs(cur.right);
3594
+ return rightResult != null ? rightResult : cur;
3595
+ } else {
3596
+ return dfs(cur.left);
3597
+ }
3598
+ }, "dfs");
3599
+ return dfs(this.root);
3600
+ } else {
3601
+ let current = this.root;
3602
+ let result = void 0;
3603
+ while (this.isRealNode(current)) {
3604
+ const cmp = this.comparator(current.key, key);
3605
+ if (cmp < 0) {
3606
+ result = current;
3607
+ current = (_a = current.right) != null ? _a : void 0;
3608
+ } else {
3609
+ current = (_b = current.left) != null ? _b : void 0;
3610
+ }
3611
+ }
3612
+ return result;
3613
+ }
3614
+ }
3615
+ /**
3616
+ * (Protected) In-order traversal search for lower by predicate.
3617
+ * Falls back to linear in-order traversal when predicate-based search is required.
3618
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
3619
+ * @remarks Time Complexity: O(n) since it may visit every node.
3620
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
3621
+ *
3622
+ * @param predicate - The predicate function to test nodes.
3623
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
3624
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
3625
+ */
3626
+ _lowerByPredicate(predicate, iterationType) {
3627
+ if (iterationType === "RECURSIVE") {
3628
+ let result = void 0;
3629
+ const dfs = /* @__PURE__ */ __name((cur) => {
3630
+ if (!this.isRealNode(cur)) return;
3631
+ if (this.isRealNode(cur.left)) dfs(cur.left);
3632
+ if (predicate(cur)) {
3633
+ result = cur;
3634
+ }
3635
+ if (this.isRealNode(cur.right)) dfs(cur.right);
3636
+ }, "dfs");
3637
+ dfs(this.root);
3638
+ return result;
3639
+ } else {
3640
+ const stack = [];
3641
+ let current = this.root;
3642
+ let result = void 0;
3643
+ while (stack.length > 0 || this.isRealNode(current)) {
3644
+ if (this.isRealNode(current)) {
3645
+ stack.push(current);
3646
+ current = current.left;
3647
+ } else {
3648
+ const node = stack.pop();
3649
+ if (!this.isRealNode(node)) break;
3650
+ if (predicate(node)) {
3651
+ result = node;
3652
+ }
3653
+ current = node.right;
3654
+ }
3655
+ }
3656
+ return result;
3657
+ }
3658
+ }
3401
3659
  /**
3402
3660
  * (Protected) Core bound search implementation supporting all parameter types.
3403
3661
  * Unified logic for both lowerBound and upperBound.