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
@@ -7262,6 +7262,17 @@ var BinaryTree = class extends IterableEntryBase {
7262
7262
  }
7263
7263
  return false;
7264
7264
  }
7265
+ /**
7266
+ * Adds or updates a new node to the tree.
7267
+ * @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).
7268
+ *
7269
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
7270
+ * @param [value] - The value, if providing just a key.
7271
+ * @returns True if the addition was successful, false otherwise.
7272
+ */
7273
+ set(keyNodeOrEntry, value) {
7274
+ return this.add(keyNodeOrEntry, value);
7275
+ }
7265
7276
  /**
7266
7277
  * Adds multiple items to the tree.
7267
7278
  * @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).
@@ -7289,6 +7300,17 @@ var BinaryTree = class extends IterableEntryBase {
7289
7300
  }
7290
7301
  return inserted;
7291
7302
  }
7303
+ /**
7304
+ * Adds or updates multiple items to the tree.
7305
+ * @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).
7306
+ *
7307
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
7308
+ * @param [values] - An optional parallel iterable of values.
7309
+ * @returns An array of booleans indicating the success of each individual `add` operation.
7310
+ */
7311
+ setMany(keysNodesEntriesOrRaws, values) {
7312
+ return this.addMany(keysNodesEntriesOrRaws, values);
7313
+ }
7292
7314
  /**
7293
7315
  * Merges another tree into this one by adding all its nodes.
7294
7316
  * @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`).
@@ -8612,9 +8634,13 @@ var BST = class extends BinaryTree {
8612
8634
  constructor(keysNodesEntriesOrRaws = [], options) {
8613
8635
  super([], options);
8614
8636
  if (options) {
8615
- const { specifyComparable, isReverse } = options;
8616
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8617
- if (isReverse !== void 0) this._isReverse = isReverse;
8637
+ if ("comparator" in options && options.comparator !== void 0) {
8638
+ this._comparator = options.comparator;
8639
+ } else {
8640
+ this._comparator = this._createDefaultComparator();
8641
+ }
8642
+ } else {
8643
+ this._comparator = this._createDefaultComparator();
8618
8644
  }
8619
8645
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8620
8646
  }
@@ -8628,40 +8654,33 @@ var BST = class extends BinaryTree {
8628
8654
  get root() {
8629
8655
  return this._root;
8630
8656
  }
8631
- _isReverse = false;
8632
8657
  /**
8633
- * Gets whether the tree's comparison logic is reversed.
8634
- * @remarks Time O(1)
8635
- *
8636
- * @returns True if the tree is reversed (e.g., a max-heap logic).
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.
8637
8661
  */
8638
- get isReverse() {
8639
- return this._isReverse;
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
+ };
8640
8677
  }
8641
8678
  /**
8642
- * The default comparator function.
8643
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8644
- */
8645
- _comparator = /* @__PURE__ */ __name((a, b) => {
8646
- if (isComparable(a) && isComparable(b)) {
8647
- if (a > b) return 1;
8648
- if (a < b) return -1;
8649
- return 0;
8650
- }
8651
- if (this._specifyComparable) {
8652
- const va = this._specifyComparable(a);
8653
- const vb = this._specifyComparable(b);
8654
- if (va > vb) return 1;
8655
- if (va < vb) return -1;
8656
- return 0;
8657
- }
8658
- if (typeof a === "object" || typeof b === "object") {
8659
- throw TypeError(
8660
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8661
- );
8662
- }
8663
- return 0;
8664
- }, "_comparator");
8679
+ * The comparator function used to determine the order of keys in the tree.
8680
+
8681
+ * @remarks Time O(1) Space O(1)
8682
+ */
8683
+ _comparator;
8665
8684
  /**
8666
8685
  * Gets the comparator function used by the tree.
8667
8686
  * @remarks Time O(1)
@@ -8671,16 +8690,6 @@ var BST = class extends BinaryTree {
8671
8690
  get comparator() {
8672
8691
  return this._comparator;
8673
8692
  }
8674
- _specifyComparable;
8675
- /**
8676
- * Gets the function used to extract a comparable value from a complex key.
8677
- * @remarks Time O(1)
8678
- *
8679
- * @returns The key-to-comparable conversion function.
8680
- */
8681
- get specifyComparable() {
8682
- return this._specifyComparable;
8683
- }
8684
8693
  /**
8685
8694
  * (Protected) Creates a new BST node.
8686
8695
  * @remarks Time O(1), Space O(1)
@@ -8721,7 +8730,7 @@ var BST = class extends BinaryTree {
8721
8730
  * @returns True if the key is valid, false otherwise.
8722
8731
  */
8723
8732
  isValidKey(key) {
8724
- return isComparable(key, this._specifyComparable !== void 0);
8733
+ return isComparable(key);
8725
8734
  }
8726
8735
  /**
8727
8736
  * Performs a Depth-First Search (DFS) traversal.
@@ -8810,8 +8819,8 @@ var BST = class extends BinaryTree {
8810
8819
  if (!this.isRealNode(cur.left)) return false;
8811
8820
  if (isRange) {
8812
8821
  const range = keyNodeEntryOrPredicate;
8813
- const leftS = this.isReverse ? range.high : range.low;
8814
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8822
+ const leftS = range.low;
8823
+ const leftI = range.includeLow;
8815
8824
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8816
8825
  }
8817
8826
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8825,8 +8834,8 @@ var BST = class extends BinaryTree {
8825
8834
  if (!this.isRealNode(cur.right)) return false;
8826
8835
  if (isRange) {
8827
8836
  const range = keyNodeEntryOrPredicate;
8828
- const rightS = this.isReverse ? range.low : range.high;
8829
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8837
+ const rightS = range.high;
8838
+ const rightI = range.includeHigh;
8830
8839
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8831
8840
  }
8832
8841
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8987,6 +8996,32 @@ var BST = class extends BinaryTree {
8987
8996
  else _iterate();
8988
8997
  return inserted;
8989
8998
  }
8999
+ /**
9000
+ * Returns the first node with a key greater than or equal to the given key.
9001
+ * This is equivalent to C++ std::lower_bound on a BST.
9002
+ * Supports RECURSIVE and ITERATIVE implementations.
9003
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
9004
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9005
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9006
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9007
+ * @returns The first node with key >= given key, or undefined if no such node exists.
9008
+ */
9009
+ lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9010
+ return this._bound(keyNodeEntryOrPredicate, true, iterationType);
9011
+ }
9012
+ /**
9013
+ * Returns the first node with a key strictly greater than the given key.
9014
+ * This is equivalent to C++ std::upper_bound on a BST.
9015
+ * Supports RECURSIVE and ITERATIVE implementations.
9016
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
9017
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9018
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9019
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
9020
+ * @returns The first node with key > given key, or undefined if no such node exists.
9021
+ */
9022
+ upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9023
+ return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9024
+ }
8990
9025
  /**
8991
9026
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
8992
9027
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9121,31 +9156,170 @@ var BST = class extends BinaryTree {
9121
9156
  return out;
9122
9157
  }
9123
9158
  /**
9124
- * Deletes the first node found that satisfies the predicate.
9125
- * @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.
9159
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9126
9160
  *
9127
- * @param predicate - A function to test each [key, value] pair.
9128
- * @returns True if a node was deleted, false otherwise.
9161
+ * @remarks
9162
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9163
+ * Space Complexity: O(M) for storing matched nodes and result map.
9164
+ *
9165
+ * @template K - The key type.
9166
+ * @template V - The value type.
9167
+ *
9168
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9169
+ * - A key (type K): searches for exact key match using the comparator.
9170
+ * - A BSTNode: searches for the matching node in the tree.
9171
+ * - An entry tuple: searches for the key-value pair.
9172
+ * - A NodePredicate function: tests each node and returns true for matches.
9173
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9174
+ * - null or undefined: treated as no match, returns empty results.
9175
+ *
9176
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9177
+ * If false (default), searches for and deletes all matching nodes.
9178
+ *
9179
+ * @param startNode - The node to start the search from. Can be:
9180
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9181
+ * - null or undefined: defaults to the root, searching the entire tree.
9182
+ * - Default value: this._root (the tree's root).
9183
+ *
9184
+ * @param iterationType - Controls the internal traversal implementation:
9185
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9186
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9187
+ * - Default: this.iterationType (the tree's default iteration mode).
9188
+ *
9189
+ * @returns A Map<K, boolean> containing the deletion results:
9190
+ * - Key: the matched node's key.
9191
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9192
+ * - If no nodes match the search criteria, the returned map is empty.
9193
+ */
9194
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9195
+ const toDelete = this.search(
9196
+ keyNodeEntryOrPredicate,
9197
+ onlyOne,
9198
+ (node) => node,
9199
+ startNode,
9200
+ iterationType
9201
+ );
9202
+ let results = [];
9203
+ for (const node of toDelete) {
9204
+ const deleteInfo = this.delete(node);
9205
+ results = results.concat(deleteInfo);
9206
+ }
9207
+ return results;
9208
+ }
9209
+ /**
9210
+ * (Protected) Core bound search implementation supporting all parameter types.
9211
+ * Unified logic for both lowerBound and upperBound.
9212
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
9213
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9214
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9215
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9216
+ * @returns The first matching node, or undefined if no such node exists.
9129
9217
  */
9130
- deleteWhere(predicate) {
9131
- const stack = [];
9132
- let cur = this._root;
9133
- let index = 0;
9134
- while (stack.length > 0 || cur !== void 0) {
9135
- while (cur !== void 0 && cur !== null) {
9136
- stack.push(cur);
9137
- cur = cur.left;
9218
+ _bound(keyNodeEntryOrPredicate, isLower, iterationType) {
9219
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9220
+ return void 0;
9221
+ }
9222
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9223
+ return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
9224
+ }
9225
+ let targetKey;
9226
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9227
+ targetKey = keyNodeEntryOrPredicate.key;
9228
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9229
+ const key = keyNodeEntryOrPredicate[0];
9230
+ if (key === null || key === void 0) {
9231
+ return void 0;
9232
+ }
9233
+ targetKey = key;
9234
+ } else {
9235
+ targetKey = keyNodeEntryOrPredicate;
9236
+ }
9237
+ if (targetKey !== void 0) {
9238
+ return this._boundByKey(targetKey, isLower, iterationType);
9239
+ }
9240
+ return void 0;
9241
+ }
9242
+ /**
9243
+ * (Protected) Binary search for bound by key with pruning optimization.
9244
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9245
+ * For lowerBound: finds first node where key >= target.
9246
+ * For upperBound: finds first node where key > target.
9247
+ * @param key - The target key to search for.
9248
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
9249
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9250
+ * @returns The first node matching the bound condition, or undefined if none exists.
9251
+ */
9252
+ _boundByKey(key, isLower, iterationType) {
9253
+ if (iterationType === "RECURSIVE") {
9254
+ const dfs = /* @__PURE__ */ __name((cur) => {
9255
+ if (!this.isRealNode(cur)) return void 0;
9256
+ const cmp = this.comparator(cur.key, key);
9257
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9258
+ if (condition) {
9259
+ const leftResult = dfs(cur.left);
9260
+ return leftResult ?? cur;
9261
+ } else {
9262
+ return dfs(cur.right);
9263
+ }
9264
+ }, "dfs");
9265
+ return dfs(this.root);
9266
+ } else {
9267
+ let current = this.root;
9268
+ let result = void 0;
9269
+ while (this.isRealNode(current)) {
9270
+ const cmp = this.comparator(current.key, key);
9271
+ const condition = isLower ? cmp >= 0 : cmp > 0;
9272
+ if (condition) {
9273
+ result = current;
9274
+ current = current.left ?? void 0;
9275
+ } else {
9276
+ current = current.right ?? void 0;
9277
+ }
9138
9278
  }
9139
- const node = stack.pop();
9140
- if (!node) break;
9141
- const key = node.key;
9142
- const val = node.value;
9143
- if (predicate(key, val, index++, this)) {
9144
- return this._deleteByKey(key);
9279
+ return result;
9280
+ }
9281
+ }
9282
+ /**
9283
+ * (Protected) In-order traversal search by predicate.
9284
+ * Falls back to linear in-order traversal when predicate-based search is required.
9285
+ * Returns the first node that satisfies the predicate function.
9286
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
9287
+ * Time Complexity: O(n) since it may visit every node.
9288
+ * @param predicate - The predicate function to test nodes.
9289
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9290
+ * @returns The first node satisfying predicate, or undefined if none found.
9291
+ */
9292
+ _boundByPredicate(predicate, iterationType) {
9293
+ if (iterationType === "RECURSIVE") {
9294
+ let result = void 0;
9295
+ const dfs = /* @__PURE__ */ __name((cur) => {
9296
+ if (result || !this.isRealNode(cur)) return;
9297
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9298
+ if (!result && predicate(cur)) {
9299
+ result = cur;
9300
+ }
9301
+ if (!result && this.isRealNode(cur.right)) dfs(cur.right);
9302
+ }, "dfs");
9303
+ dfs(this.root);
9304
+ return result;
9305
+ } else {
9306
+ const stack = [];
9307
+ let current = this.root;
9308
+ while (stack.length > 0 || this.isRealNode(current)) {
9309
+ if (this.isRealNode(current)) {
9310
+ stack.push(current);
9311
+ current = current.left;
9312
+ } else {
9313
+ const node = stack.pop();
9314
+ if (!this.isRealNode(node)) break;
9315
+ if (predicate(node)) {
9316
+ return node;
9317
+ }
9318
+ current = node.right;
9319
+ }
9145
9320
  }
9146
- cur = node.right;
9321
+ return void 0;
9147
9322
  }
9148
- return false;
9149
9323
  }
9150
9324
  /**
9151
9325
  * (Protected) Creates a new, empty instance of the same BST constructor.
@@ -9182,8 +9356,7 @@ var BST = class extends BinaryTree {
9182
9356
  _snapshotOptions() {
9183
9357
  return {
9184
9358
  ...super._snapshotOptions(),
9185
- specifyComparable: this.specifyComparable,
9186
- isReverse: this.isReverse
9359
+ comparator: this._comparator
9187
9360
  };
9188
9361
  }
9189
9362
  /**
@@ -9211,14 +9384,14 @@ var BST = class extends BinaryTree {
9211
9384
  }
9212
9385
  /**
9213
9386
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9214
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9387
+ * @remarks Time O(1) Space O(1)
9215
9388
  *
9216
9389
  * @param a - The first key.
9217
9390
  * @param b - The second key.
9218
9391
  * @returns A number (1, -1, or 0) representing the comparison.
9219
9392
  */
9220
9393
  _compare(a, b) {
9221
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9394
+ return this._comparator(a, b);
9222
9395
  }
9223
9396
  /**
9224
9397
  * (Private) Deletes a node by its key.