data-structure-typed 2.2.3 → 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.
Files changed (36) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +70 -11
  3. package/dist/cjs/index.cjs +341 -75
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs-legacy/index.cjs +343 -75
  6. package/dist/cjs-legacy/index.cjs.map +1 -1
  7. package/dist/esm/index.mjs +341 -75
  8. package/dist/esm/index.mjs.map +1 -1
  9. package/dist/esm-legacy/index.mjs +343 -75
  10. package/dist/esm-legacy/index.mjs.map +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  12. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +142 -28
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  16. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  18. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  19. package/dist/umd/data-structure-typed.js +343 -75
  20. package/dist/umd/data-structure-typed.js.map +1 -1
  21. package/dist/umd/data-structure-typed.min.js +3 -3
  22. package/dist/umd/data-structure-typed.min.js.map +1 -1
  23. package/package.json +2 -2
  24. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  25. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -8
  26. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  27. package/src/data-structures/binary-tree/bst.ts +495 -85
  28. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  29. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  30. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  31. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  32. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
  33. package/test/unit/data-structures/binary-tree/bst.test.ts +459 -8
  34. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  35. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
  36. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
package/CHANGELOG.md CHANGED
@@ -8,7 +8,9 @@ All notable changes to this project will be documented in this file.
8
8
  - [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
9
9
  - [`auto-changelog`](https://github.com/CookPete/auto-changelog)
10
10
 
11
- ## [v2.2.3](https://github.com/zrwusa/data-structure-typed/compare/v2.2.2...main) (upcoming)
11
+ ## [v2.2.5](https://github.com/zrwusa/data-structure-typed/compare/v2.2.3...main) (upcoming)
12
+
13
+ ## [v2.2.3](https://github.com/zrwusa/data-structure-typed/compare/v2.2.2...v2.2.3) (6 January 2026)
12
14
 
13
15
  ## [v2.2.2](https://github.com/zrwusa/data-structure-typed/compare/v2.0.4...v2.2.2) (5 January 2026)
14
16
 
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A comprehensive TypeScript data structures library with production-ready implementations.
4
4
 
5
- **📚 [Quick Start](#-quick-start-30-seconds) • [Full Docs](./docs/CONCEPTS.md) • [API Reference](./docs/REFERENCE.md) • [Examples](./docs/GUIDES.md)**
5
+ **📚 [Quick Start](#-quick-start-30-seconds) • [Installation](#-installation) • [Full Docs](./docs/CONCEPTS.md) • [API Reference](./docs/REFERENCE.md) • [Playground](#-playground) • [Installation](#-installation) • [Examples](./docs/GUIDES.md)**
6
6
 
7
7
  ---
8
8
 
@@ -56,10 +56,11 @@ for (let i = 0; i < 100000; i++) {
56
56
  queue.shift(); // O(n) - Reindexes EVERY element!
57
57
  }
58
58
  // Time: 2829ms ❌
59
+ ```
59
60
 
61
+ ```javascript
60
62
  // ✅ WITH data-structure-typed (Deque)
61
- const deque = new Deque([1, 2, 3, ..., 100000
62
- ])
63
+ const deque = new Deque([1, 2, 3, ..., 100000])
63
64
  ;
64
65
  for (let i = 0; i < 100000; i++) {
65
66
  deque.shift(); // O(1) - Just moves a pointer
@@ -95,7 +96,8 @@ Don't learn new APIs. Just use `push`, `pop`, `map`, `filter`, and `reduce` ever
95
96
  // All linear structures use THE SAME 4 methods
96
97
  const deque = new Deque([1, 2, 3]);
97
98
  const queue = new Queue([1, 2, 3]);
98
- const stack = new Stack([1, 2, 3]);
99
+ const doublyLinkeList = new DoublyLinkedList([1, 2, 3]);
100
+ const singlyLinkedList = new SinglyLinkedList([1, 2, 3]);
99
101
 
100
102
  // They ALL support:
101
103
  structure.push(item); // Add to end
@@ -211,7 +213,7 @@ const topPlayers = [...leaderboard.values()].reverse().slice(0, 3);
211
213
  ```typescript
212
214
  import { MaxPriorityQueue } from 'data-structure-typed';
213
215
 
214
- const taskQueue = new MaxPriorityQueue([], {
216
+ const taskQueue = new MaxPriorityQueue<{priority: number; task: string}>([], {
215
217
  comparator: (a, b) => b.priority - a.priority
216
218
  });
217
219
 
@@ -329,20 +331,67 @@ class LRUCache<K, V> {
329
331
  ### Leaderboard
330
332
 
331
333
  ```typescript
332
- class Leaderboard {
333
- private scores = new RedBlackTree<number, Player>(
334
- (a, b) => b - a // Descending
335
- );
336
334
 
337
- getTopN(n: number): Player[] {
338
- return [...this.scores.values()].slice(0, n);
335
+ type Player = {
336
+ id: string;
337
+ name: string;
338
+ score: number;
339
+ };
340
+
341
+ const seedPlayers: Player[] = [
342
+ { id: 'player_01HZX4E8Q2K8Y3J9M7T1A6B3C4', name: 'Pablo', score: 65 },
343
+ { id: 'player_01HZX4E9R6V2D8K1P0N5S4T7U8', name: 'Bunny', score: 10 },
344
+ { id: 'player_01HZX4EA3M9Q7W1E2R8T6Y5U0I', name: 'Jeff', score: 99 },
345
+ ];
346
+
347
+ class ScoreLeaderboard {
348
+ private readonly byScore: RedBlackTree<number, Player, Player>;
349
+
350
+ constructor(initialPlayers: Player[]) {
351
+ this.byScore = new RedBlackTree<number, Player, Player>(initialPlayers, {
352
+ isMapMode: false,// Use "node value" storage rather than Map-style.
353
+ toEntryFn: (player) => [player.score, player], // Convert a player object into the tree entry: key = score, value = player.
354
+ });
355
+ }
356
+
357
+ /**
358
+ * Returns players whose scores fall within the given range.
359
+ * Supports either a tuple [min, max] or a Range object for inclusive/exclusive bounds.
360
+ */
361
+ public findPlayersByScoreRange(range: [number, number] | Range<number>): (Player | undefined)[] {
362
+ return this.byScore.rangeSearch(range, (node) => node.value);
363
+ }
364
+
365
+ public upsertPlayer(player: Player) {
366
+ return this.byScore.set(player.score, player);
339
367
  }
340
368
  }
369
+
370
+ const leaderboard = new ScoreLeaderboard(seedPlayers);
371
+
372
+ console.log(leaderboard.findPlayersByScoreRange([65, 100]));
373
+
374
+ leaderboard.upsertPlayer({
375
+ id: 'player_01HZX4EB7C4N2M9Q8R1T3Y6U5I',
376
+ name: 'Alex',
377
+ score: 80,
378
+ });
379
+
380
+ console.log(leaderboard.findPlayersByScoreRange(new Range(65, 100, true, true)));
341
381
  ```
342
382
 
343
383
  ### Message Queue
344
384
 
345
385
  ```typescript
386
+ type Message = {
387
+ id: string;
388
+ type: string;
389
+ payload: unknown;
390
+ priority: 'urgent' | 'normal';
391
+ createdAt: number;
392
+ retryCount?: number;
393
+ };
394
+
346
395
  class MessageQueue {
347
396
  private urgent = new Deque<Message>();
348
397
  private normal = new Deque<Message>();
@@ -435,6 +484,16 @@ const tree = new RedBlackTree([5, 2, 8]);
435
484
  console.log([...tree]); // [2, 5, 8] - Automatically sorted!
436
485
  ```
437
486
 
487
+ ## 🏃🏻‍♀️ Playground
488
+
489
+ Try it instantly:
490
+
491
+ - [Node.js TypeScript](https://stackblitz.com/edit/stackblitz-starters-e1vdy3zw?file=src%2Findex.ts)
492
+ - [Node.js JavaScript](https://stackblitz.com/edit/stackblitz-starters-dgvchziu?file=src%2Findex.js)
493
+ - [React TypeScript](https://stackblitz.com/edit/vitejs-vite-6xvhtdua?file=src%2FApp.tsx)
494
+ - [NestJS](https://stackblitz.com/edit/nestjs-typescript-starter-3cyp7pel?file=src%2Franking%2Franking.service.ts)
495
+
496
+
438
497
  ### Step 4: Learn More
439
498
 
440
499
  👉 Check [CONCEPTS.md](./docs/CONCEPTS.md) for core concepts
@@ -8636,9 +8636,13 @@ var BST = class extends BinaryTree {
8636
8636
  constructor(keysNodesEntriesOrRaws = [], options) {
8637
8637
  super([], options);
8638
8638
  if (options) {
8639
- const { specifyComparable, isReverse } = options;
8640
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8641
- if (isReverse !== void 0) this._isReverse = isReverse;
8639
+ if ("comparator" in options && options.comparator !== void 0) {
8640
+ this._comparator = options.comparator;
8641
+ } else {
8642
+ this._comparator = this._createDefaultComparator();
8643
+ }
8644
+ } else {
8645
+ this._comparator = this._createDefaultComparator();
8642
8646
  }
8643
8647
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8644
8648
  }
@@ -8652,40 +8656,12 @@ var BST = class extends BinaryTree {
8652
8656
  get root() {
8653
8657
  return this._root;
8654
8658
  }
8655
- _isReverse = false;
8656
- /**
8657
- * Gets whether the tree's comparison logic is reversed.
8658
- * @remarks Time O(1)
8659
- *
8660
- * @returns True if the tree is reversed (e.g., a max-heap logic).
8661
- */
8662
- get isReverse() {
8663
- return this._isReverse;
8664
- }
8665
8659
  /**
8666
- * The default comparator function.
8667
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8668
- */
8669
- _comparator = /* @__PURE__ */ __name((a, b) => {
8670
- if (isComparable(a) && isComparable(b)) {
8671
- if (a > b) return 1;
8672
- if (a < b) return -1;
8673
- return 0;
8674
- }
8675
- if (this._specifyComparable) {
8676
- const va = this._specifyComparable(a);
8677
- const vb = this._specifyComparable(b);
8678
- if (va > vb) return 1;
8679
- if (va < vb) return -1;
8680
- return 0;
8681
- }
8682
- if (typeof a === "object" || typeof b === "object") {
8683
- throw TypeError(
8684
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8685
- );
8686
- }
8687
- return 0;
8688
- }, "_comparator");
8660
+ * The comparator function used to determine the order of keys in the tree.
8661
+
8662
+ * @remarks Time O(1) Space O(1)
8663
+ */
8664
+ _comparator;
8689
8665
  /**
8690
8666
  * Gets the comparator function used by the tree.
8691
8667
  * @remarks Time O(1)
@@ -8695,16 +8671,6 @@ var BST = class extends BinaryTree {
8695
8671
  get comparator() {
8696
8672
  return this._comparator;
8697
8673
  }
8698
- _specifyComparable;
8699
- /**
8700
- * Gets the function used to extract a comparable value from a complex key.
8701
- * @remarks Time O(1)
8702
- *
8703
- * @returns The key-to-comparable conversion function.
8704
- */
8705
- get specifyComparable() {
8706
- return this._specifyComparable;
8707
- }
8708
8674
  /**
8709
8675
  * (Protected) Creates a new BST node.
8710
8676
  * @remarks Time O(1), Space O(1)
@@ -8745,7 +8711,7 @@ var BST = class extends BinaryTree {
8745
8711
  * @returns True if the key is valid, false otherwise.
8746
8712
  */
8747
8713
  isValidKey(key) {
8748
- return isComparable(key, this._specifyComparable !== void 0);
8714
+ return isComparable(key);
8749
8715
  }
8750
8716
  /**
8751
8717
  * Performs a Depth-First Search (DFS) traversal.
@@ -8834,8 +8800,8 @@ var BST = class extends BinaryTree {
8834
8800
  if (!this.isRealNode(cur.left)) return false;
8835
8801
  if (isRange) {
8836
8802
  const range = keyNodeEntryOrPredicate;
8837
- const leftS = this.isReverse ? range.high : range.low;
8838
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8803
+ const leftS = range.low;
8804
+ const leftI = range.includeLow;
8839
8805
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8840
8806
  }
8841
8807
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8849,8 +8815,8 @@ var BST = class extends BinaryTree {
8849
8815
  if (!this.isRealNode(cur.right)) return false;
8850
8816
  if (isRange) {
8851
8817
  const range = keyNodeEntryOrPredicate;
8852
- const rightS = this.isReverse ? range.low : range.high;
8853
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8818
+ const rightS = range.high;
8819
+ const rightI = range.includeHigh;
8854
8820
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8855
8821
  }
8856
8822
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -9037,6 +9003,104 @@ var BST = class extends BinaryTree {
9037
9003
  upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9038
9004
  return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9039
9005
  }
9006
+ /**
9007
+ * Returns the first node with a key greater than or equal to the given key.
9008
+ * This is equivalent to Java TreeMap.ceilingEntry().
9009
+ * Supports RECURSIVE and ITERATIVE implementations.
9010
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9011
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9012
+ *
9013
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9014
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9015
+ * @returns The first node with key >= given key, or undefined if no such node exists.
9016
+ */
9017
+ ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9018
+ return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
9019
+ }
9020
+ /**
9021
+ * Returns the first node with a key strictly greater than the given key.
9022
+ * This is equivalent to Java TreeMap.higherEntry().
9023
+ * Supports RECURSIVE and ITERATIVE implementations.
9024
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9025
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9026
+ *
9027
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9028
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9029
+ * @returns The first node with key > given key, or undefined if no such node exists.
9030
+ */
9031
+ higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9032
+ return this.upperBound(keyNodeEntryOrPredicate, iterationType);
9033
+ }
9034
+ /**
9035
+ * Returns the first node with a key less than or equal to the given key.
9036
+ * This is equivalent to Java TreeMap.floorEntry().
9037
+ * Supports RECURSIVE and ITERATIVE implementations.
9038
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9039
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9040
+ *
9041
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9042
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9043
+ * @returns The first node with key <= given key, or undefined if no such node exists.
9044
+ */
9045
+ floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9046
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9047
+ return void 0;
9048
+ }
9049
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9050
+ return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
9051
+ }
9052
+ let targetKey;
9053
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9054
+ targetKey = keyNodeEntryOrPredicate.key;
9055
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9056
+ const key = keyNodeEntryOrPredicate[0];
9057
+ if (key === null || key === void 0) {
9058
+ return void 0;
9059
+ }
9060
+ targetKey = key;
9061
+ } else {
9062
+ targetKey = keyNodeEntryOrPredicate;
9063
+ }
9064
+ if (targetKey !== void 0) {
9065
+ return this._floorByKey(targetKey, iterationType);
9066
+ }
9067
+ return void 0;
9068
+ }
9069
+ /**
9070
+ * Returns the first node with a key strictly less than the given key.
9071
+ * This is equivalent to Java TreeMap.lowerEntry().
9072
+ * Supports RECURSIVE and ITERATIVE implementations.
9073
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9074
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9075
+ *
9076
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9077
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9078
+ * @returns The first node with key < given key, or undefined if no such node exists.
9079
+ */
9080
+ lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9081
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9082
+ return void 0;
9083
+ }
9084
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9085
+ return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
9086
+ }
9087
+ let targetKey;
9088
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9089
+ targetKey = keyNodeEntryOrPredicate.key;
9090
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9091
+ const key = keyNodeEntryOrPredicate[0];
9092
+ if (key === null || key === void 0) {
9093
+ return void 0;
9094
+ }
9095
+ targetKey = key;
9096
+ } else {
9097
+ targetKey = keyNodeEntryOrPredicate;
9098
+ }
9099
+ if (targetKey !== void 0) {
9100
+ return this._lowerByKey(targetKey, iterationType);
9101
+ }
9102
+ return void 0;
9103
+ }
9040
9104
  /**
9041
9105
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
9042
9106
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9171,31 +9235,234 @@ var BST = class extends BinaryTree {
9171
9235
  return out;
9172
9236
  }
9173
9237
  /**
9174
- * Deletes the first node found that satisfies the predicate.
9175
- * @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
9238
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9176
9239
  *
9177
- * @param predicate - A function to test each [key, value] pair.
9178
- * @returns True if a node was deleted, false otherwise.
9240
+ * @remarks
9241
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9242
+ * Space Complexity: O(M) for storing matched nodes and result map.
9243
+ *
9244
+ * @template K - The key type.
9245
+ * @template V - The value type.
9246
+ *
9247
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9248
+ * - A key (type K): searches for exact key match using the comparator.
9249
+ * - A BSTNode: searches for the matching node in the tree.
9250
+ * - An entry tuple: searches for the key-value pair.
9251
+ * - A NodePredicate function: tests each node and returns true for matches.
9252
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9253
+ * - null or undefined: treated as no match, returns empty results.
9254
+ *
9255
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9256
+ * If false (default), searches for and deletes all matching nodes.
9257
+ *
9258
+ * @param startNode - The node to start the search from. Can be:
9259
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9260
+ * - null or undefined: defaults to the root, searching the entire tree.
9261
+ * - Default value: this._root (the tree's root).
9262
+ *
9263
+ * @param iterationType - Controls the internal traversal implementation:
9264
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9265
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9266
+ * - Default: this.iterationType (the tree's default iteration mode).
9267
+ *
9268
+ * @returns A Map<K, boolean> containing the deletion results:
9269
+ * - Key: the matched node's key.
9270
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9271
+ * - If no nodes match the search criteria, the returned map is empty.
9272
+ */
9273
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9274
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
9275
+ let results = [];
9276
+ for (const node of toDelete) {
9277
+ const deleteInfo = this.delete(node);
9278
+ results = results.concat(deleteInfo);
9279
+ }
9280
+ return results;
9281
+ }
9282
+ /**
9283
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
9284
+ * @remarks Time O(1) Space O(1)
9285
+ * @returns The default comparator function.
9179
9286
  */
9180
- deleteWhere(predicate) {
9181
- const stack = [];
9182
- let cur = this._root;
9183
- let index = 0;
9184
- while (stack.length > 0 || cur !== void 0) {
9185
- while (cur !== void 0 && cur !== null) {
9186
- stack.push(cur);
9187
- cur = cur.left;
9287
+ _createDefaultComparator() {
9288
+ return (a, b) => {
9289
+ debugger;
9290
+ if (isComparable(a) && isComparable(b)) {
9291
+ if (a > b) return 1;
9292
+ if (a < b) return -1;
9293
+ return 0;
9294
+ }
9295
+ if (typeof a === "object" || typeof b === "object") {
9296
+ throw TypeError(
9297
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
9298
+ );
9299
+ }
9300
+ return 0;
9301
+ };
9302
+ }
9303
+ /**
9304
+ * (Protected) Binary search for floor by key with pruning optimization.
9305
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9306
+ * Finds first node where key <= target.
9307
+ * @remarks Time O(h) where h is tree height.
9308
+ *
9309
+ * @param key - The target key to search for.
9310
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9311
+ * @returns The first node with key <= target, or undefined if none exists.
9312
+ */
9313
+ _floorByKey(key, iterationType) {
9314
+ if (iterationType === "RECURSIVE") {
9315
+ const dfs = /* @__PURE__ */ __name((cur) => {
9316
+ if (!this.isRealNode(cur)) return void 0;
9317
+ const cmp = this.comparator(cur.key, key);
9318
+ if (cmp <= 0) {
9319
+ const rightResult = dfs(cur.right);
9320
+ return rightResult ?? cur;
9321
+ } else {
9322
+ return dfs(cur.left);
9323
+ }
9324
+ }, "dfs");
9325
+ return dfs(this.root);
9326
+ } else {
9327
+ let current = this.root;
9328
+ let result = void 0;
9329
+ while (this.isRealNode(current)) {
9330
+ const cmp = this.comparator(current.key, key);
9331
+ if (cmp <= 0) {
9332
+ result = current;
9333
+ current = current.right ?? void 0;
9334
+ } else {
9335
+ current = current.left ?? void 0;
9336
+ }
9188
9337
  }
9189
- const node = stack.pop();
9190
- if (!node) break;
9191
- const key = node.key;
9192
- const val = node.value;
9193
- if (predicate(key, val, index++, this)) {
9194
- return this._deleteByKey(key);
9338
+ return result;
9339
+ }
9340
+ }
9341
+ /**
9342
+ * (Protected) In-order traversal search for floor by predicate.
9343
+ * Falls back to linear in-order traversal when predicate-based search is required.
9344
+ * Returns the last node that satisfies the predicate function.
9345
+ * @remarks Time Complexity: O(n) since it may visit every node.
9346
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9347
+ *
9348
+ * @param predicate - The predicate function to test nodes.
9349
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9350
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
9351
+ */
9352
+ _floorByPredicate(predicate, iterationType) {
9353
+ if (iterationType === "RECURSIVE") {
9354
+ let result = void 0;
9355
+ const dfs = /* @__PURE__ */ __name((cur) => {
9356
+ if (!this.isRealNode(cur)) return;
9357
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9358
+ if (predicate(cur)) {
9359
+ result = cur;
9360
+ }
9361
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9362
+ }, "dfs");
9363
+ dfs(this.root);
9364
+ return result;
9365
+ } else {
9366
+ const stack = [];
9367
+ let current = this.root;
9368
+ let result = void 0;
9369
+ while (stack.length > 0 || this.isRealNode(current)) {
9370
+ if (this.isRealNode(current)) {
9371
+ stack.push(current);
9372
+ current = current.left;
9373
+ } else {
9374
+ const node = stack.pop();
9375
+ if (!this.isRealNode(node)) break;
9376
+ if (predicate(node)) {
9377
+ result = node;
9378
+ }
9379
+ current = node.right;
9380
+ }
9195
9381
  }
9196
- cur = node.right;
9382
+ return result;
9383
+ }
9384
+ }
9385
+ /**
9386
+ * (Protected) Binary search for lower by key with pruning optimization.
9387
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9388
+ * Finds first node where key < target.
9389
+ * @remarks Time O(h) where h is tree height.
9390
+ *
9391
+ * @param key - The target key to search for.
9392
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9393
+ * @returns The first node with key < target, or undefined if none exists.
9394
+ */
9395
+ _lowerByKey(key, iterationType) {
9396
+ if (iterationType === "RECURSIVE") {
9397
+ const dfs = /* @__PURE__ */ __name((cur) => {
9398
+ if (!this.isRealNode(cur)) return void 0;
9399
+ const cmp = this.comparator(cur.key, key);
9400
+ if (cmp < 0) {
9401
+ const rightResult = dfs(cur.right);
9402
+ return rightResult ?? cur;
9403
+ } else {
9404
+ return dfs(cur.left);
9405
+ }
9406
+ }, "dfs");
9407
+ return dfs(this.root);
9408
+ } else {
9409
+ let current = this.root;
9410
+ let result = void 0;
9411
+ while (this.isRealNode(current)) {
9412
+ const cmp = this.comparator(current.key, key);
9413
+ if (cmp < 0) {
9414
+ result = current;
9415
+ current = current.right ?? void 0;
9416
+ } else {
9417
+ current = current.left ?? void 0;
9418
+ }
9419
+ }
9420
+ return result;
9421
+ }
9422
+ }
9423
+ /**
9424
+ * (Protected) In-order traversal search for lower by predicate.
9425
+ * Falls back to linear in-order traversal when predicate-based search is required.
9426
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
9427
+ * @remarks Time Complexity: O(n) since it may visit every node.
9428
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9429
+ *
9430
+ * @param predicate - The predicate function to test nodes.
9431
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9432
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
9433
+ */
9434
+ _lowerByPredicate(predicate, iterationType) {
9435
+ if (iterationType === "RECURSIVE") {
9436
+ let result = void 0;
9437
+ const dfs = /* @__PURE__ */ __name((cur) => {
9438
+ if (!this.isRealNode(cur)) return;
9439
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9440
+ if (predicate(cur)) {
9441
+ result = cur;
9442
+ }
9443
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9444
+ }, "dfs");
9445
+ dfs(this.root);
9446
+ return result;
9447
+ } else {
9448
+ const stack = [];
9449
+ let current = this.root;
9450
+ let result = void 0;
9451
+ while (stack.length > 0 || this.isRealNode(current)) {
9452
+ if (this.isRealNode(current)) {
9453
+ stack.push(current);
9454
+ current = current.left;
9455
+ } else {
9456
+ const node = stack.pop();
9457
+ if (!this.isRealNode(node)) break;
9458
+ if (predicate(node)) {
9459
+ result = node;
9460
+ }
9461
+ current = node.right;
9462
+ }
9463
+ }
9464
+ return result;
9197
9465
  }
9198
- return false;
9199
9466
  }
9200
9467
  /**
9201
9468
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -9347,8 +9614,7 @@ var BST = class extends BinaryTree {
9347
9614
  _snapshotOptions() {
9348
9615
  return {
9349
9616
  ...super._snapshotOptions(),
9350
- specifyComparable: this.specifyComparable,
9351
- isReverse: this.isReverse
9617
+ comparator: this._comparator
9352
9618
  };
9353
9619
  }
9354
9620
  /**
@@ -9376,14 +9642,14 @@ var BST = class extends BinaryTree {
9376
9642
  }
9377
9643
  /**
9378
9644
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9379
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9645
+ * @remarks Time O(1) Space O(1)
9380
9646
  *
9381
9647
  * @param a - The first key.
9382
9648
  * @param b - The second key.
9383
9649
  * @returns A number (1, -1, or 0) representing the comparison.
9384
9650
  */
9385
9651
  _compare(a, b) {
9386
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9652
+ return this._comparator(a, b);
9387
9653
  }
9388
9654
  /**
9389
9655
  * (Private) Deletes a node by its key.