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