data-structure-typed 2.2.2 → 2.2.4

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 (94) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/README.md +355 -1672
  3. package/README_CN.md +509 -0
  4. package/SECURITY.md +962 -11
  5. package/SECURITY.zh-CN.md +966 -0
  6. package/SPECIFICATION.md +689 -30
  7. package/SPECIFICATION.zh-CN.md +715 -0
  8. package/SPONSOR.zh-CN.md +62 -0
  9. package/SPONSOR_POLISHED.md +62 -0
  10. package/benchmark/report.html +1 -1
  11. package/benchmark/report.json +215 -172
  12. package/dist/cjs/index.cjs +245 -72
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs-legacy/index.cjs +246 -72
  15. package/dist/cjs-legacy/index.cjs.map +1 -1
  16. package/dist/esm/index.mjs +245 -72
  17. package/dist/esm/index.mjs.map +1 -1
  18. package/dist/esm-legacy/index.mjs +246 -72
  19. package/dist/esm-legacy/index.mjs.map +1 -1
  20. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  21. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  22. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
  23. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  24. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  25. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  26. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  27. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  28. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  29. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  30. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  31. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  32. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  33. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  34. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  35. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  36. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  37. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  38. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  40. package/dist/umd/data-structure-typed.js +246 -72
  41. package/dist/umd/data-structure-typed.js.map +1 -1
  42. package/dist/umd/data-structure-typed.min.js +3 -3
  43. package/dist/umd/data-structure-typed.min.js.map +1 -1
  44. package/package.json +3 -2
  45. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  46. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  47. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  48. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  49. package/src/data-structures/binary-tree/bst.ts +431 -93
  50. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  51. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  53. package/src/data-structures/graph/directed-graph.ts +126 -1
  54. package/src/data-structures/graph/undirected-graph.ts +160 -1
  55. package/src/data-structures/hash/hash-map.ts +110 -27
  56. package/src/data-structures/heap/heap.ts +107 -58
  57. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  58. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  59. package/src/data-structures/queue/deque.ts +95 -67
  60. package/src/data-structures/queue/queue.ts +90 -34
  61. package/src/data-structures/stack/stack.ts +58 -40
  62. package/src/data-structures/trie/trie.ts +109 -47
  63. package/src/interfaces/binary-tree.ts +2 -0
  64. package/src/types/data-structures/binary-tree/bst.ts +5 -5
  65. package/test/performance/benchmark-runner.ts +14 -11
  66. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
  67. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
  68. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
  69. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
  70. package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
  71. package/test/performance/reportor.ts +2 -1
  72. package/test/performance/single-suite-runner.ts +7 -4
  73. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
  74. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
  75. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
  76. package/test/unit/data-structures/binary-tree/bst.test.ts +771 -16
  77. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  78. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +90 -38
  79. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
  80. package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
  81. package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
  82. package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
  83. package/test/unit/data-structures/heap/heap.test.ts +182 -47
  84. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
  85. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
  86. package/test/unit/data-structures/queue/deque.test.ts +98 -67
  87. package/test/unit/data-structures/queue/queue.test.ts +85 -51
  88. package/test/unit/data-structures/stack/stack.test.ts +142 -33
  89. package/test/unit/data-structures/trie/trie.test.ts +135 -39
  90. package/tsup.leetcode.config.js +99 -0
  91. package/typedoc.json +2 -1
  92. package/POSTS_zh-CN.md +0 -54
  93. package/README_zh-CN.md +0 -1208
  94. package/SPECIFICATION_zh-CN.md +0 -81
@@ -7249,6 +7249,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
7249
7249
  }
7250
7250
  return false;
7251
7251
  }
7252
+ /**
7253
+ * Adds or updates a new node to the tree.
7254
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
7255
+ *
7256
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
7257
+ * @param [value] - The value, if providing just a key.
7258
+ * @returns True if the addition was successful, false otherwise.
7259
+ */
7260
+ set(keyNodeOrEntry, value) {
7261
+ return this.add(keyNodeOrEntry, value);
7262
+ }
7252
7263
  /**
7253
7264
  * Adds multiple items to the tree.
7254
7265
  * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
@@ -7276,6 +7287,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
7276
7287
  }
7277
7288
  return inserted;
7278
7289
  }
7290
+ /**
7291
+ * Adds or updates multiple items to the tree.
7292
+ * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
7293
+ *
7294
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
7295
+ * @param [values] - An optional parallel iterable of values.
7296
+ * @returns An array of booleans indicating the success of each individual `add` operation.
7297
+ */
7298
+ setMany(keysNodesEntriesOrRaws, values) {
7299
+ return this.addMany(keysNodesEntriesOrRaws, values);
7300
+ }
7279
7301
  /**
7280
7302
  * Merges another tree into this one by adding all its nodes.
7281
7303
  * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
@@ -8592,36 +8614,20 @@ var _BST = class _BST extends BinaryTree {
8592
8614
  constructor(keysNodesEntriesOrRaws = [], options) {
8593
8615
  super([], options);
8594
8616
  __publicField(this, "_root");
8595
- __publicField(this, "_isReverse", false);
8596
8617
  /**
8597
- * The default comparator function.
8598
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8599
- */
8600
- __publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
8601
- if (isComparable(a) && isComparable(b)) {
8602
- if (a > b) return 1;
8603
- if (a < b) return -1;
8604
- return 0;
8605
- }
8606
- if (this._specifyComparable) {
8607
- const va = this._specifyComparable(a);
8608
- const vb = this._specifyComparable(b);
8609
- if (va > vb) return 1;
8610
- if (va < vb) return -1;
8611
- return 0;
8612
- }
8613
- if (typeof a === "object" || typeof b === "object") {
8614
- throw TypeError(
8615
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8616
- );
8617
- }
8618
- return 0;
8619
- }, "_comparator"));
8620
- __publicField(this, "_specifyComparable");
8618
+ * The comparator function used to determine the order of keys in the tree.
8619
+
8620
+ * @remarks Time O(1) Space O(1)
8621
+ */
8622
+ __publicField(this, "_comparator");
8621
8623
  if (options) {
8622
- const { specifyComparable, isReverse } = options;
8623
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8624
- if (isReverse !== void 0) this._isReverse = isReverse;
8624
+ if ("comparator" in options && options.comparator !== void 0) {
8625
+ this._comparator = options.comparator;
8626
+ } else {
8627
+ this._comparator = this._createDefaultComparator();
8628
+ }
8629
+ } else {
8630
+ this._comparator = this._createDefaultComparator();
8625
8631
  }
8626
8632
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8627
8633
  }
@@ -8635,13 +8641,25 @@ var _BST = class _BST extends BinaryTree {
8635
8641
  return this._root;
8636
8642
  }
8637
8643
  /**
8638
- * Gets whether the tree's comparison logic is reversed.
8639
- * @remarks Time O(1)
8640
- *
8641
- * @returns True if the tree is reversed (e.g., a max-heap logic).
8644
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
8645
+ * @remarks Time O(1) Space O(1)
8646
+ * @returns The default comparator function.
8642
8647
  */
8643
- get isReverse() {
8644
- return this._isReverse;
8648
+ _createDefaultComparator() {
8649
+ return (a, b) => {
8650
+ debugger;
8651
+ if (isComparable(a) && isComparable(b)) {
8652
+ if (a > b) return 1;
8653
+ if (a < b) return -1;
8654
+ return 0;
8655
+ }
8656
+ if (typeof a === "object" || typeof b === "object") {
8657
+ throw TypeError(
8658
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
8659
+ );
8660
+ }
8661
+ return 0;
8662
+ };
8645
8663
  }
8646
8664
  /**
8647
8665
  * Gets the comparator function used by the tree.
@@ -8652,15 +8670,6 @@ var _BST = class _BST extends BinaryTree {
8652
8670
  get comparator() {
8653
8671
  return this._comparator;
8654
8672
  }
8655
- /**
8656
- * Gets the function used to extract a comparable value from a complex key.
8657
- * @remarks Time O(1)
8658
- *
8659
- * @returns The key-to-comparable conversion function.
8660
- */
8661
- get specifyComparable() {
8662
- return this._specifyComparable;
8663
- }
8664
8673
  /**
8665
8674
  * (Protected) Creates a new BST node.
8666
8675
  * @remarks Time O(1), Space O(1)
@@ -8702,7 +8711,7 @@ var _BST = class _BST extends BinaryTree {
8702
8711
  * @returns True if the key is valid, false otherwise.
8703
8712
  */
8704
8713
  isValidKey(key) {
8705
- return isComparable(key, this._specifyComparable !== void 0);
8714
+ return isComparable(key);
8706
8715
  }
8707
8716
  /**
8708
8717
  * Performs a Depth-First Search (DFS) traversal.
@@ -8792,8 +8801,8 @@ var _BST = class _BST extends BinaryTree {
8792
8801
  if (!this.isRealNode(cur.left)) return false;
8793
8802
  if (isRange) {
8794
8803
  const range = keyNodeEntryOrPredicate;
8795
- const leftS = this.isReverse ? range.high : range.low;
8796
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8804
+ const leftS = range.low;
8805
+ const leftI = range.includeLow;
8797
8806
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8798
8807
  }
8799
8808
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8807,8 +8816,8 @@ var _BST = class _BST extends BinaryTree {
8807
8816
  if (!this.isRealNode(cur.right)) return false;
8808
8817
  if (isRange) {
8809
8818
  const range = keyNodeEntryOrPredicate;
8810
- const rightS = this.isReverse ? range.low : range.high;
8811
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8819
+ const rightS = range.high;
8820
+ const rightI = range.includeHigh;
8812
8821
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8813
8822
  }
8814
8823
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8969,6 +8978,32 @@ var _BST = class _BST extends BinaryTree {
8969
8978
  else _iterate();
8970
8979
  return inserted;
8971
8980
  }
8981
+ /**
8982
+ * Returns the first node with a key greater than or equal to the given key.
8983
+ * This is equivalent to C++ std::lower_bound on a BST.
8984
+ * Supports RECURSIVE and ITERATIVE implementations.
8985
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
8986
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
8987
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8988
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
8989
+ * @returns The first node with key >= given key, or undefined if no such node exists.
8990
+ */
8991
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8992
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
8993
+ }
8994
+ /**
8995
+ * Returns the first node with a key strictly greater than the given key.
8996
+ * This is equivalent to C++ std::upper_bound on a BST.
8997
+ * Supports RECURSIVE and ITERATIVE implementations.
8998
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
8999
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9000
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9001
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9002
+ * @returns The first node with key > given key, or undefined if no such node exists.
9003
+ */
9004
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9005
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9006
+ }
8972
9007
  /**
8973
9008
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
8974
9009
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9103,31 +9138,171 @@ var _BST = class _BST extends BinaryTree {
9103
9138
  return out;
9104
9139
  }
9105
9140
  /**
9106
- * Deletes the first node found that satisfies the predicate.
9107
- * @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.
9141
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9108
9142
  *
9109
- * @param predicate - A function to test each [key, value] pair.
9110
- * @returns True if a node was deleted, false otherwise.
9143
+ * @remarks
9144
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9145
+ * Space Complexity: O(M) for storing matched nodes and result map.
9146
+ *
9147
+ * @template K - The key type.
9148
+ * @template V - The value type.
9149
+ *
9150
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9151
+ * - A key (type K): searches for exact key match using the comparator.
9152
+ * - A BSTNode: searches for the matching node in the tree.
9153
+ * - An entry tuple: searches for the key-value pair.
9154
+ * - A NodePredicate function: tests each node and returns true for matches.
9155
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9156
+ * - null or undefined: treated as no match, returns empty results.
9157
+ *
9158
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9159
+ * If false (default), searches for and deletes all matching nodes.
9160
+ *
9161
+ * @param startNode - The node to start the search from. Can be:
9162
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9163
+ * - null or undefined: defaults to the root, searching the entire tree.
9164
+ * - Default value: this._root (the tree's root).
9165
+ *
9166
+ * @param iterationType - Controls the internal traversal implementation:
9167
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9168
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9169
+ * - Default: this.iterationType (the tree's default iteration mode).
9170
+ *
9171
+ * @returns A Map<K, boolean> containing the deletion results:
9172
+ * - Key: the matched node's key.
9173
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9174
+ * - If no nodes match the search criteria, the returned map is empty.
9175
+ */
9176
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9177
+ const toDelete = this.search(
9178
+ keyNodeEntryOrPredicate,
9179
+ onlyOne,
9180
+ (node) => node,
9181
+ startNode,
9182
+ iterationType
9183
+ );
9184
+ let results = [];
9185
+ for (const node of toDelete) {
9186
+ const deleteInfo = this.delete(node);
9187
+ results = results.concat(deleteInfo);
9188
+ }
9189
+ return results;
9190
+ }
9191
+ /**
9192
+ * (Protected) Core bound search implementation supporting all parameter types.
9193
+ * Unified logic for both lowerBound and upperBound.
9194
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
9195
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9196
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9197
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9198
+ * @returns The first matching node, or undefined if no such node exists.
9111
9199
  */
9112
- deleteWhere(predicate) {
9113
- const stack = [];
9114
- let cur = this._root;
9115
- let index = 0;
9116
- while (stack.length > 0 || cur !== void 0) {
9117
- while (cur !== void 0 && cur !== null) {
9118
- stack.push(cur);
9119
- cur = cur.left;
9200
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
9201
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9202
+ return void 0;
9203
+ }
9204
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9205
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
9206
+ }
9207
+ let targetKey;
9208
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9209
+ targetKey = keyNodeEntryOrPredicate.key;
9210
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9211
+ const key = keyNodeEntryOrPredicate[0];
9212
+ if (key === null || key === void 0) {
9213
+ return void 0;
9120
9214
  }
9121
- const node = stack.pop();
9122
- if (!node) break;
9123
- const key = node.key;
9124
- const val = node.value;
9125
- if (predicate(key, val, index++, this)) {
9126
- return this._deleteByKey(key);
9215
+ targetKey = key;
9216
+ } else {
9217
+ targetKey = keyNodeEntryOrPredicate;
9218
+ }
9219
+ if (targetKey !== void 0) {
9220
+ return this._boundByKey(targetKey, isLower, iterationType);
9221
+ }
9222
+ return void 0;
9223
+ }
9224
+ /**
9225
+ * (Protected) Binary search for bound by key with pruning optimization.
9226
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9227
+ * For lowerBound: finds first node where key >= target.
9228
+ * For upperBound: finds first node where key > target.
9229
+ * @param key - The target key to search for.
9230
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9231
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9232
+ * @returns The first node matching the bound condition, or undefined if none exists.
9233
+ */
9234
+ _boundByKey(key, isLower, iterationType) {
9235
+ var _a, _b;
9236
+ if (iterationType === "RECURSIVE") {
9237
+ const dfs = /* @__PURE__ */ __name((cur) => {
9238
+ if (!this.isRealNode(cur)) return void 0;
9239
+ const cmp = this.comparator(cur.key, key);
9240
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9241
+ if (condition) {
9242
+ const leftResult = dfs(cur.left);
9243
+ return leftResult != null ? leftResult : cur;
9244
+ } else {
9245
+ return dfs(cur.right);
9246
+ }
9247
+ }, "dfs");
9248
+ return dfs(this.root);
9249
+ } else {
9250
+ let current = this.root;
9251
+ let result = void 0;
9252
+ while (this.isRealNode(current)) {
9253
+ const cmp = this.comparator(current.key, key);
9254
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9255
+ if (condition) {
9256
+ result = current;
9257
+ current = (_a = current.left) != null ? _a : void 0;
9258
+ } else {
9259
+ current = (_b = current.right) != null ? _b : void 0;
9260
+ }
9127
9261
  }
9128
- cur = node.right;
9262
+ return result;
9263
+ }
9264
+ }
9265
+ /**
9266
+ * (Protected) In-order traversal search by predicate.
9267
+ * Falls back to linear in-order traversal when predicate-based search is required.
9268
+ * Returns the first node that satisfies the predicate function.
9269
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
9270
+ * Time Complexity: O(n) since it may visit every node.
9271
+ * @param predicate - The predicate function to test nodes.
9272
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9273
+ * @returns The first node satisfying predicate, or undefined if none found.
9274
+ */
9275
+ _boundByPredicate(predicate, iterationType) {
9276
+ if (iterationType === "RECURSIVE") {
9277
+ let result = void 0;
9278
+ const dfs = /* @__PURE__ */ __name((cur) => {
9279
+ if (result || !this.isRealNode(cur)) return;
9280
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9281
+ if (!result && predicate(cur)) {
9282
+ result = cur;
9283
+ }
9284
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
9285
+ }, "dfs");
9286
+ dfs(this.root);
9287
+ return result;
9288
+ } else {
9289
+ const stack = [];
9290
+ let current = this.root;
9291
+ while (stack.length > 0 || this.isRealNode(current)) {
9292
+ if (this.isRealNode(current)) {
9293
+ stack.push(current);
9294
+ current = current.left;
9295
+ } else {
9296
+ const node = stack.pop();
9297
+ if (!this.isRealNode(node)) break;
9298
+ if (predicate(node)) {
9299
+ return node;
9300
+ }
9301
+ current = node.right;
9302
+ }
9303
+ }
9304
+ return void 0;
9129
9305
  }
9130
- return false;
9131
9306
  }
9132
9307
  /**
9133
9308
  * (Protected) Creates a new, empty instance of the same BST constructor.
@@ -9164,8 +9339,7 @@ var _BST = class _BST extends BinaryTree {
9164
9339
  _snapshotOptions() {
9165
9340
  return {
9166
9341
  ...super._snapshotOptions(),
9167
- specifyComparable: this.specifyComparable,
9168
- isReverse: this.isReverse
9342
+ comparator: this._comparator
9169
9343
  };
9170
9344
  }
9171
9345
  /**
@@ -9193,14 +9367,14 @@ var _BST = class _BST extends BinaryTree {
9193
9367
  }
9194
9368
  /**
9195
9369
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9196
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9370
+ * @remarks Time O(1) Space O(1)
9197
9371
  *
9198
9372
  * @param a - The first key.
9199
9373
  * @param b - The second key.
9200
9374
  * @returns A number (1, -1, or 0) representing the comparison.
9201
9375
  */
9202
9376
  _compare(a, b) {
9203
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9377
+ return this._comparator(a, b);
9204
9378
  }
9205
9379
  /**
9206
9380
  * (Private) Deletes a node by its key.