data-structure-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.
@@ -288,12 +288,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
288
288
  * @returns The root node.
289
289
  */
290
290
  get root(): OptNode<BSTNode<K, V>>;
291
- /**
292
- * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
293
- * @remarks Time O(1) Space O(1)
294
- * @returns The default comparator function.
295
- */
296
- protected _createDefaultComparator(): Comparator<K>;
297
291
  /**
298
292
  * The comparator function used to determine the order of keys in the tree.
299
293
 
@@ -457,6 +451,54 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
457
451
  * @returns The first node with key > given key, or undefined if no such node exists.
458
452
  */
459
453
  upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
454
+ /**
455
+ * Returns the first node with a key greater than or equal to the given key.
456
+ * This is equivalent to Java TreeMap.ceilingEntry().
457
+ * Supports RECURSIVE and ITERATIVE implementations.
458
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
459
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
460
+ *
461
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
462
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
463
+ * @returns The first node with key >= given key, or undefined if no such node exists.
464
+ */
465
+ ceilingEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
466
+ /**
467
+ * Returns the first node with a key strictly greater than the given key.
468
+ * This is equivalent to Java TreeMap.higherEntry().
469
+ * Supports RECURSIVE and ITERATIVE implementations.
470
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
471
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
472
+ *
473
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
474
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
475
+ * @returns The first node with key > given key, or undefined if no such node exists.
476
+ */
477
+ higherEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
478
+ /**
479
+ * Returns the first node with a key less than or equal to the given key.
480
+ * This is equivalent to Java TreeMap.floorEntry().
481
+ * Supports RECURSIVE and ITERATIVE implementations.
482
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
483
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
484
+ *
485
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
486
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
487
+ * @returns The first node with key <= given key, or undefined if no such node exists.
488
+ */
489
+ floorEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
490
+ /**
491
+ * Returns the first node with a key strictly less than the given key.
492
+ * This is equivalent to Java TreeMap.lowerEntry().
493
+ * Supports RECURSIVE and ITERATIVE implementations.
494
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
495
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
496
+ *
497
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
498
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
499
+ * @returns The first node with key < given key, or undefined if no such node exists.
500
+ */
501
+ lowerEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
460
502
  /**
461
503
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
462
504
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -536,6 +578,58 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
536
578
  * - If no nodes match the search criteria, the returned map is empty.
537
579
  */
538
580
  deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
581
+ /**
582
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
583
+ * @remarks Time O(1) Space O(1)
584
+ * @returns The default comparator function.
585
+ */
586
+ protected _createDefaultComparator(): Comparator<K>;
587
+ /**
588
+ * (Protected) Binary search for floor by key with pruning optimization.
589
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
590
+ * Finds first node where key <= target.
591
+ * @remarks Time O(h) where h is tree height.
592
+ *
593
+ * @param key - The target key to search for.
594
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
595
+ * @returns The first node with key <= target, or undefined if none exists.
596
+ */
597
+ protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
598
+ /**
599
+ * (Protected) In-order traversal search for floor by predicate.
600
+ * Falls back to linear in-order traversal when predicate-based search is required.
601
+ * Returns the last node that satisfies the predicate function.
602
+ * @remarks Time Complexity: O(n) since it may visit every node.
603
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
604
+ *
605
+ * @param predicate - The predicate function to test nodes.
606
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
607
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
608
+ */
609
+ protected _floorByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
610
+ /**
611
+ * (Protected) Binary search for lower by key with pruning optimization.
612
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
613
+ * Finds first node where key < target.
614
+ * @remarks Time O(h) where h is tree height.
615
+ *
616
+ * @param key - The target key to search for.
617
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
618
+ * @returns The first node with key < target, or undefined if none exists.
619
+ */
620
+ protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
621
+ /**
622
+ * (Protected) In-order traversal search for lower by predicate.
623
+ * Falls back to linear in-order traversal when predicate-based search is required.
624
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
625
+ * @remarks Time Complexity: O(n) since it may visit every node.
626
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
627
+ *
628
+ * @param predicate - The predicate function to test nodes.
629
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
630
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
631
+ */
632
+ protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
539
633
  /**
540
634
  * (Protected) Core bound search implementation supporting all parameter types.
541
635
  * Unified logic for both lowerBound and upperBound.
@@ -8654,27 +8654,6 @@ var dataStructureTyped = (() => {
8654
8654
  get root() {
8655
8655
  return this._root;
8656
8656
  }
8657
- /**
8658
- * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
8659
- * @remarks Time O(1) Space O(1)
8660
- * @returns The default comparator function.
8661
- */
8662
- _createDefaultComparator() {
8663
- return (a, b) => {
8664
- debugger;
8665
- if (isComparable(a) && isComparable(b)) {
8666
- if (a > b) return 1;
8667
- if (a < b) return -1;
8668
- return 0;
8669
- }
8670
- if (typeof a === "object" || typeof b === "object") {
8671
- throw TypeError(
8672
- `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
8673
- );
8674
- }
8675
- return 0;
8676
- };
8677
- }
8678
8657
  /**
8679
8658
  * Gets the comparator function used by the tree.
8680
8659
  * @remarks Time O(1)
@@ -9018,6 +8997,104 @@ var dataStructureTyped = (() => {
9018
8997
  upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9019
8998
  return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9020
8999
  }
9000
+ /**
9001
+ * Returns the first node with a key greater than or equal to the given key.
9002
+ * This is equivalent to Java TreeMap.ceilingEntry().
9003
+ * Supports RECURSIVE and ITERATIVE implementations.
9004
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9005
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9006
+ *
9007
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9008
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9009
+ * @returns The first node with key >= given key, or undefined if no such node exists.
9010
+ */
9011
+ ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9012
+ return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
9013
+ }
9014
+ /**
9015
+ * Returns the first node with a key strictly greater than the given key.
9016
+ * This is equivalent to Java TreeMap.higherEntry().
9017
+ * Supports RECURSIVE and ITERATIVE implementations.
9018
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9019
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9020
+ *
9021
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9022
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9023
+ * @returns The first node with key > given key, or undefined if no such node exists.
9024
+ */
9025
+ higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9026
+ return this.upperBound(keyNodeEntryOrPredicate, iterationType);
9027
+ }
9028
+ /**
9029
+ * Returns the first node with a key less than or equal to the given key.
9030
+ * This is equivalent to Java TreeMap.floorEntry().
9031
+ * Supports RECURSIVE and ITERATIVE implementations.
9032
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9033
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9034
+ *
9035
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9036
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9037
+ * @returns The first node with key <= given key, or undefined if no such node exists.
9038
+ */
9039
+ floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9040
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9041
+ return void 0;
9042
+ }
9043
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9044
+ return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
9045
+ }
9046
+ let targetKey;
9047
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9048
+ targetKey = keyNodeEntryOrPredicate.key;
9049
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9050
+ const key = keyNodeEntryOrPredicate[0];
9051
+ if (key === null || key === void 0) {
9052
+ return void 0;
9053
+ }
9054
+ targetKey = key;
9055
+ } else {
9056
+ targetKey = keyNodeEntryOrPredicate;
9057
+ }
9058
+ if (targetKey !== void 0) {
9059
+ return this._floorByKey(targetKey, iterationType);
9060
+ }
9061
+ return void 0;
9062
+ }
9063
+ /**
9064
+ * Returns the first node with a key strictly less than the given key.
9065
+ * This is equivalent to Java TreeMap.lowerEntry().
9066
+ * Supports RECURSIVE and ITERATIVE implementations.
9067
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9068
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9069
+ *
9070
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9071
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9072
+ * @returns The first node with key < given key, or undefined if no such node exists.
9073
+ */
9074
+ lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9075
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9076
+ return void 0;
9077
+ }
9078
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9079
+ return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
9080
+ }
9081
+ let targetKey;
9082
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9083
+ targetKey = keyNodeEntryOrPredicate.key;
9084
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9085
+ const key = keyNodeEntryOrPredicate[0];
9086
+ if (key === null || key === void 0) {
9087
+ return void 0;
9088
+ }
9089
+ targetKey = key;
9090
+ } else {
9091
+ targetKey = keyNodeEntryOrPredicate;
9092
+ }
9093
+ if (targetKey !== void 0) {
9094
+ return this._lowerByKey(targetKey, iterationType);
9095
+ }
9096
+ return void 0;
9097
+ }
9021
9098
  /**
9022
9099
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
9023
9100
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9188,13 +9265,7 @@ var dataStructureTyped = (() => {
9188
9265
  * - If no nodes match the search criteria, the returned map is empty.
9189
9266
  */
9190
9267
  deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9191
- const toDelete = this.search(
9192
- keyNodeEntryOrPredicate,
9193
- onlyOne,
9194
- (node) => node,
9195
- startNode,
9196
- iterationType
9197
- );
9268
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
9198
9269
  let results = [];
9199
9270
  for (const node of toDelete) {
9200
9271
  const deleteInfo = this.delete(node);
@@ -9202,6 +9273,193 @@ var dataStructureTyped = (() => {
9202
9273
  }
9203
9274
  return results;
9204
9275
  }
9276
+ /**
9277
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
9278
+ * @remarks Time O(1) Space O(1)
9279
+ * @returns The default comparator function.
9280
+ */
9281
+ _createDefaultComparator() {
9282
+ return (a, b) => {
9283
+ debugger;
9284
+ if (isComparable(a) && isComparable(b)) {
9285
+ if (a > b) return 1;
9286
+ if (a < b) return -1;
9287
+ return 0;
9288
+ }
9289
+ if (typeof a === "object" || typeof b === "object") {
9290
+ throw TypeError(
9291
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
9292
+ );
9293
+ }
9294
+ return 0;
9295
+ };
9296
+ }
9297
+ /**
9298
+ * (Protected) Binary search for floor by key with pruning optimization.
9299
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9300
+ * Finds first node where key <= target.
9301
+ * @remarks Time O(h) where h is tree height.
9302
+ *
9303
+ * @param key - The target key to search for.
9304
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9305
+ * @returns The first node with key <= target, or undefined if none exists.
9306
+ */
9307
+ _floorByKey(key, iterationType) {
9308
+ var _a, _b;
9309
+ if (iterationType === "RECURSIVE") {
9310
+ const dfs = (cur) => {
9311
+ if (!this.isRealNode(cur)) return void 0;
9312
+ const cmp = this.comparator(cur.key, key);
9313
+ if (cmp <= 0) {
9314
+ const rightResult = dfs(cur.right);
9315
+ return rightResult != null ? rightResult : cur;
9316
+ } else {
9317
+ return dfs(cur.left);
9318
+ }
9319
+ };
9320
+ return dfs(this.root);
9321
+ } else {
9322
+ let current = this.root;
9323
+ let result = void 0;
9324
+ while (this.isRealNode(current)) {
9325
+ const cmp = this.comparator(current.key, key);
9326
+ if (cmp <= 0) {
9327
+ result = current;
9328
+ current = (_a = current.right) != null ? _a : void 0;
9329
+ } else {
9330
+ current = (_b = current.left) != null ? _b : void 0;
9331
+ }
9332
+ }
9333
+ return result;
9334
+ }
9335
+ }
9336
+ /**
9337
+ * (Protected) In-order traversal search for floor by predicate.
9338
+ * Falls back to linear in-order traversal when predicate-based search is required.
9339
+ * Returns the last node that satisfies the predicate function.
9340
+ * @remarks Time Complexity: O(n) since it may visit every node.
9341
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9342
+ *
9343
+ * @param predicate - The predicate function to test nodes.
9344
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9345
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
9346
+ */
9347
+ _floorByPredicate(predicate, iterationType) {
9348
+ if (iterationType === "RECURSIVE") {
9349
+ let result = void 0;
9350
+ const dfs = (cur) => {
9351
+ if (!this.isRealNode(cur)) return;
9352
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9353
+ if (predicate(cur)) {
9354
+ result = cur;
9355
+ }
9356
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9357
+ };
9358
+ dfs(this.root);
9359
+ return result;
9360
+ } else {
9361
+ const stack = [];
9362
+ let current = this.root;
9363
+ let result = void 0;
9364
+ while (stack.length > 0 || this.isRealNode(current)) {
9365
+ if (this.isRealNode(current)) {
9366
+ stack.push(current);
9367
+ current = current.left;
9368
+ } else {
9369
+ const node = stack.pop();
9370
+ if (!this.isRealNode(node)) break;
9371
+ if (predicate(node)) {
9372
+ result = node;
9373
+ }
9374
+ current = node.right;
9375
+ }
9376
+ }
9377
+ return result;
9378
+ }
9379
+ }
9380
+ /**
9381
+ * (Protected) Binary search for lower by key with pruning optimization.
9382
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9383
+ * Finds first node where key < target.
9384
+ * @remarks Time O(h) where h is tree height.
9385
+ *
9386
+ * @param key - The target key to search for.
9387
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9388
+ * @returns The first node with key < target, or undefined if none exists.
9389
+ */
9390
+ _lowerByKey(key, iterationType) {
9391
+ var _a, _b;
9392
+ if (iterationType === "RECURSIVE") {
9393
+ const dfs = (cur) => {
9394
+ if (!this.isRealNode(cur)) return void 0;
9395
+ const cmp = this.comparator(cur.key, key);
9396
+ if (cmp < 0) {
9397
+ const rightResult = dfs(cur.right);
9398
+ return rightResult != null ? rightResult : cur;
9399
+ } else {
9400
+ return dfs(cur.left);
9401
+ }
9402
+ };
9403
+ return dfs(this.root);
9404
+ } else {
9405
+ let current = this.root;
9406
+ let result = void 0;
9407
+ while (this.isRealNode(current)) {
9408
+ const cmp = this.comparator(current.key, key);
9409
+ if (cmp < 0) {
9410
+ result = current;
9411
+ current = (_a = current.right) != null ? _a : void 0;
9412
+ } else {
9413
+ current = (_b = current.left) != null ? _b : void 0;
9414
+ }
9415
+ }
9416
+ return result;
9417
+ }
9418
+ }
9419
+ /**
9420
+ * (Protected) In-order traversal search for lower by predicate.
9421
+ * Falls back to linear in-order traversal when predicate-based search is required.
9422
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
9423
+ * @remarks Time Complexity: O(n) since it may visit every node.
9424
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9425
+ *
9426
+ * @param predicate - The predicate function to test nodes.
9427
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9428
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
9429
+ */
9430
+ _lowerByPredicate(predicate, iterationType) {
9431
+ if (iterationType === "RECURSIVE") {
9432
+ let result = void 0;
9433
+ const dfs = (cur) => {
9434
+ if (!this.isRealNode(cur)) return;
9435
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9436
+ if (predicate(cur)) {
9437
+ result = cur;
9438
+ }
9439
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9440
+ };
9441
+ dfs(this.root);
9442
+ return result;
9443
+ } else {
9444
+ const stack = [];
9445
+ let current = this.root;
9446
+ let result = void 0;
9447
+ while (stack.length > 0 || this.isRealNode(current)) {
9448
+ if (this.isRealNode(current)) {
9449
+ stack.push(current);
9450
+ current = current.left;
9451
+ } else {
9452
+ const node = stack.pop();
9453
+ if (!this.isRealNode(node)) break;
9454
+ if (predicate(node)) {
9455
+ result = node;
9456
+ }
9457
+ current = node.right;
9458
+ }
9459
+ }
9460
+ return result;
9461
+ }
9462
+ }
9205
9463
  /**
9206
9464
  * (Protected) Core bound search implementation supporting all parameter types.
9207
9465
  * Unified logic for both lowerBound and upperBound.