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
@@ -8614,36 +8614,20 @@ var _BST = class _BST extends BinaryTree {
8614
8614
  constructor(keysNodesEntriesOrRaws = [], options) {
8615
8615
  super([], options);
8616
8616
  __publicField(this, "_root");
8617
- __publicField(this, "_isReverse", false);
8618
8617
  /**
8619
- * The default comparator function.
8620
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
8621
- */
8622
- __publicField(this, "_comparator", /* @__PURE__ */ __name((a, b) => {
8623
- if (isComparable(a) && isComparable(b)) {
8624
- if (a > b) return 1;
8625
- if (a < b) return -1;
8626
- return 0;
8627
- }
8628
- if (this._specifyComparable) {
8629
- const va = this._specifyComparable(a);
8630
- const vb = this._specifyComparable(b);
8631
- if (va > vb) return 1;
8632
- if (va < vb) return -1;
8633
- return 0;
8634
- }
8635
- if (typeof a === "object" || typeof b === "object") {
8636
- throw TypeError(
8637
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
8638
- );
8639
- }
8640
- return 0;
8641
- }, "_comparator"));
8642
- __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");
8643
8623
  if (options) {
8644
- const { specifyComparable, isReverse } = options;
8645
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
8646
- 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();
8647
8631
  }
8648
8632
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
8649
8633
  }
@@ -8656,15 +8640,6 @@ var _BST = class _BST extends BinaryTree {
8656
8640
  get root() {
8657
8641
  return this._root;
8658
8642
  }
8659
- /**
8660
- * Gets whether the tree's comparison logic is reversed.
8661
- * @remarks Time O(1)
8662
- *
8663
- * @returns True if the tree is reversed (e.g., a max-heap logic).
8664
- */
8665
- get isReverse() {
8666
- return this._isReverse;
8667
- }
8668
8643
  /**
8669
8644
  * Gets the comparator function used by the tree.
8670
8645
  * @remarks Time O(1)
@@ -8674,15 +8649,6 @@ var _BST = class _BST extends BinaryTree {
8674
8649
  get comparator() {
8675
8650
  return this._comparator;
8676
8651
  }
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
8652
  /**
8687
8653
  * (Protected) Creates a new BST node.
8688
8654
  * @remarks Time O(1), Space O(1)
@@ -8724,7 +8690,7 @@ var _BST = class _BST extends BinaryTree {
8724
8690
  * @returns True if the key is valid, false otherwise.
8725
8691
  */
8726
8692
  isValidKey(key) {
8727
- return isComparable(key, this._specifyComparable !== void 0);
8693
+ return isComparable(key);
8728
8694
  }
8729
8695
  /**
8730
8696
  * Performs a Depth-First Search (DFS) traversal.
@@ -8814,8 +8780,8 @@ var _BST = class _BST extends BinaryTree {
8814
8780
  if (!this.isRealNode(cur.left)) return false;
8815
8781
  if (isRange) {
8816
8782
  const range = keyNodeEntryOrPredicate;
8817
- const leftS = this.isReverse ? range.high : range.low;
8818
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
8783
+ const leftS = range.low;
8784
+ const leftI = range.includeLow;
8819
8785
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
8820
8786
  }
8821
8787
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -8829,8 +8795,8 @@ var _BST = class _BST extends BinaryTree {
8829
8795
  if (!this.isRealNode(cur.right)) return false;
8830
8796
  if (isRange) {
8831
8797
  const range = keyNodeEntryOrPredicate;
8832
- const rightS = this.isReverse ? range.low : range.high;
8833
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
8798
+ const rightS = range.high;
8799
+ const rightI = range.includeHigh;
8834
8800
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
8835
8801
  }
8836
8802
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -9017,6 +8983,104 @@ var _BST = class _BST extends BinaryTree {
9017
8983
  upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9018
8984
  return this._bound(keyNodeEntryOrPredicate, false, iterationType);
9019
8985
  }
8986
+ /**
8987
+ * Returns the first node with a key greater than or equal to the given key.
8988
+ * This is equivalent to Java TreeMap.ceilingEntry().
8989
+ * Supports RECURSIVE and ITERATIVE implementations.
8990
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
8991
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
8992
+ *
8993
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
8994
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
8995
+ * @returns The first node with key >= given key, or undefined if no such node exists.
8996
+ */
8997
+ ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
8998
+ return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
8999
+ }
9000
+ /**
9001
+ * Returns the first node with a key strictly greater than the given key.
9002
+ * This is equivalent to Java TreeMap.higherEntry().
9003
+ * Supports RECURSIVE and ITERATIVE implementations.
9004
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9005
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9006
+ *
9007
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9008
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9009
+ * @returns The first node with key > given key, or undefined if no such node exists.
9010
+ */
9011
+ higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9012
+ return this.upperBound(keyNodeEntryOrPredicate, iterationType);
9013
+ }
9014
+ /**
9015
+ * Returns the first node with a key less than or equal to the given key.
9016
+ * This is equivalent to Java TreeMap.floorEntry().
9017
+ * Supports RECURSIVE and ITERATIVE implementations.
9018
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9019
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9020
+ *
9021
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9022
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9023
+ * @returns The first node with key <= given key, or undefined if no such node exists.
9024
+ */
9025
+ floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9026
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9027
+ return void 0;
9028
+ }
9029
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9030
+ return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
9031
+ }
9032
+ let targetKey;
9033
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9034
+ targetKey = keyNodeEntryOrPredicate.key;
9035
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9036
+ const key = keyNodeEntryOrPredicate[0];
9037
+ if (key === null || key === void 0) {
9038
+ return void 0;
9039
+ }
9040
+ targetKey = key;
9041
+ } else {
9042
+ targetKey = keyNodeEntryOrPredicate;
9043
+ }
9044
+ if (targetKey !== void 0) {
9045
+ return this._floorByKey(targetKey, iterationType);
9046
+ }
9047
+ return void 0;
9048
+ }
9049
+ /**
9050
+ * Returns the first node with a key strictly less than the given key.
9051
+ * This is equivalent to Java TreeMap.lowerEntry().
9052
+ * Supports RECURSIVE and ITERATIVE implementations.
9053
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
9054
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
9055
+ *
9056
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
9057
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
9058
+ * @returns The first node with key < given key, or undefined if no such node exists.
9059
+ */
9060
+ lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
9061
+ if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
9062
+ return void 0;
9063
+ }
9064
+ if (this._isPredicate(keyNodeEntryOrPredicate)) {
9065
+ return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
9066
+ }
9067
+ let targetKey;
9068
+ if (this.isNode(keyNodeEntryOrPredicate)) {
9069
+ targetKey = keyNodeEntryOrPredicate.key;
9070
+ } else if (this.isEntry(keyNodeEntryOrPredicate)) {
9071
+ const key = keyNodeEntryOrPredicate[0];
9072
+ if (key === null || key === void 0) {
9073
+ return void 0;
9074
+ }
9075
+ targetKey = key;
9076
+ } else {
9077
+ targetKey = keyNodeEntryOrPredicate;
9078
+ }
9079
+ if (targetKey !== void 0) {
9080
+ return this._lowerByKey(targetKey, iterationType);
9081
+ }
9082
+ return void 0;
9083
+ }
9020
9084
  /**
9021
9085
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
9022
9086
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -9151,31 +9215,236 @@ var _BST = class _BST extends BinaryTree {
9151
9215
  return out;
9152
9216
  }
9153
9217
  /**
9154
- * Deletes the first node found that satisfies the predicate.
9155
- * @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.
9218
+ * Deletes nodes that match a key, node, entry, predicate, or range.
9156
9219
  *
9157
- * @param predicate - A function to test each [key, value] pair.
9158
- * @returns True if a node was deleted, false otherwise.
9220
+ * @remarks
9221
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
9222
+ * Space Complexity: O(M) for storing matched nodes and result map.
9223
+ *
9224
+ * @template K - The key type.
9225
+ * @template V - The value type.
9226
+ *
9227
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
9228
+ * - A key (type K): searches for exact key match using the comparator.
9229
+ * - A BSTNode: searches for the matching node in the tree.
9230
+ * - An entry tuple: searches for the key-value pair.
9231
+ * - A NodePredicate function: tests each node and returns true for matches.
9232
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
9233
+ * - null or undefined: treated as no match, returns empty results.
9234
+ *
9235
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
9236
+ * If false (default), searches for and deletes all matching nodes.
9237
+ *
9238
+ * @param startNode - The node to start the search from. Can be:
9239
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
9240
+ * - null or undefined: defaults to the root, searching the entire tree.
9241
+ * - Default value: this._root (the tree's root).
9242
+ *
9243
+ * @param iterationType - Controls the internal traversal implementation:
9244
+ * - 'RECURSIVE': uses recursive function calls for traversal.
9245
+ * - 'ITERATIVE': uses explicit stack-based iteration.
9246
+ * - Default: this.iterationType (the tree's default iteration mode).
9247
+ *
9248
+ * @returns A Map<K, boolean> containing the deletion results:
9249
+ * - Key: the matched node's key.
9250
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
9251
+ * - If no nodes match the search criteria, the returned map is empty.
9252
+ */
9253
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
9254
+ const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
9255
+ let results = [];
9256
+ for (const node of toDelete) {
9257
+ const deleteInfo = this.delete(node);
9258
+ results = results.concat(deleteInfo);
9259
+ }
9260
+ return results;
9261
+ }
9262
+ /**
9263
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
9264
+ * @remarks Time O(1) Space O(1)
9265
+ * @returns The default comparator function.
9159
9266
  */
9160
- deleteWhere(predicate) {
9161
- const stack = [];
9162
- let cur = this._root;
9163
- let index = 0;
9164
- while (stack.length > 0 || cur !== void 0) {
9165
- while (cur !== void 0 && cur !== null) {
9166
- stack.push(cur);
9167
- cur = cur.left;
9267
+ _createDefaultComparator() {
9268
+ return (a, b) => {
9269
+ debugger;
9270
+ if (isComparable(a) && isComparable(b)) {
9271
+ if (a > b) return 1;
9272
+ if (a < b) return -1;
9273
+ return 0;
9274
+ }
9275
+ if (typeof a === "object" || typeof b === "object") {
9276
+ throw TypeError(
9277
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
9278
+ );
9279
+ }
9280
+ return 0;
9281
+ };
9282
+ }
9283
+ /**
9284
+ * (Protected) Binary search for floor by key with pruning optimization.
9285
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9286
+ * Finds first node where key <= target.
9287
+ * @remarks Time O(h) where h is tree height.
9288
+ *
9289
+ * @param key - The target key to search for.
9290
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9291
+ * @returns The first node with key <= target, or undefined if none exists.
9292
+ */
9293
+ _floorByKey(key, iterationType) {
9294
+ var _a, _b;
9295
+ if (iterationType === "RECURSIVE") {
9296
+ const dfs = /* @__PURE__ */ __name((cur) => {
9297
+ if (!this.isRealNode(cur)) return void 0;
9298
+ const cmp = this.comparator(cur.key, key);
9299
+ if (cmp <= 0) {
9300
+ const rightResult = dfs(cur.right);
9301
+ return rightResult != null ? rightResult : cur;
9302
+ } else {
9303
+ return dfs(cur.left);
9304
+ }
9305
+ }, "dfs");
9306
+ return dfs(this.root);
9307
+ } else {
9308
+ let current = this.root;
9309
+ let result = void 0;
9310
+ while (this.isRealNode(current)) {
9311
+ const cmp = this.comparator(current.key, key);
9312
+ if (cmp <= 0) {
9313
+ result = current;
9314
+ current = (_a = current.right) != null ? _a : void 0;
9315
+ } else {
9316
+ current = (_b = current.left) != null ? _b : void 0;
9317
+ }
9168
9318
  }
9169
- const node = stack.pop();
9170
- if (!node) break;
9171
- const key = node.key;
9172
- const val = node.value;
9173
- if (predicate(key, val, index++, this)) {
9174
- return this._deleteByKey(key);
9319
+ return result;
9320
+ }
9321
+ }
9322
+ /**
9323
+ * (Protected) In-order traversal search for floor by predicate.
9324
+ * Falls back to linear in-order traversal when predicate-based search is required.
9325
+ * Returns the last node that satisfies the predicate function.
9326
+ * @remarks Time Complexity: O(n) since it may visit every node.
9327
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9328
+ *
9329
+ * @param predicate - The predicate function to test nodes.
9330
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9331
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
9332
+ */
9333
+ _floorByPredicate(predicate, iterationType) {
9334
+ if (iterationType === "RECURSIVE") {
9335
+ let result = void 0;
9336
+ const dfs = /* @__PURE__ */ __name((cur) => {
9337
+ if (!this.isRealNode(cur)) return;
9338
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9339
+ if (predicate(cur)) {
9340
+ result = cur;
9341
+ }
9342
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9343
+ }, "dfs");
9344
+ dfs(this.root);
9345
+ return result;
9346
+ } else {
9347
+ const stack = [];
9348
+ let current = this.root;
9349
+ let result = void 0;
9350
+ while (stack.length > 0 || this.isRealNode(current)) {
9351
+ if (this.isRealNode(current)) {
9352
+ stack.push(current);
9353
+ current = current.left;
9354
+ } else {
9355
+ const node = stack.pop();
9356
+ if (!this.isRealNode(node)) break;
9357
+ if (predicate(node)) {
9358
+ result = node;
9359
+ }
9360
+ current = node.right;
9361
+ }
9175
9362
  }
9176
- cur = node.right;
9363
+ return result;
9364
+ }
9365
+ }
9366
+ /**
9367
+ * (Protected) Binary search for lower by key with pruning optimization.
9368
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
9369
+ * Finds first node where key < target.
9370
+ * @remarks Time O(h) where h is tree height.
9371
+ *
9372
+ * @param key - The target key to search for.
9373
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9374
+ * @returns The first node with key < target, or undefined if none exists.
9375
+ */
9376
+ _lowerByKey(key, iterationType) {
9377
+ var _a, _b;
9378
+ if (iterationType === "RECURSIVE") {
9379
+ const dfs = /* @__PURE__ */ __name((cur) => {
9380
+ if (!this.isRealNode(cur)) return void 0;
9381
+ const cmp = this.comparator(cur.key, key);
9382
+ if (cmp < 0) {
9383
+ const rightResult = dfs(cur.right);
9384
+ return rightResult != null ? rightResult : cur;
9385
+ } else {
9386
+ return dfs(cur.left);
9387
+ }
9388
+ }, "dfs");
9389
+ return dfs(this.root);
9390
+ } else {
9391
+ let current = this.root;
9392
+ let result = void 0;
9393
+ while (this.isRealNode(current)) {
9394
+ const cmp = this.comparator(current.key, key);
9395
+ if (cmp < 0) {
9396
+ result = current;
9397
+ current = (_a = current.right) != null ? _a : void 0;
9398
+ } else {
9399
+ current = (_b = current.left) != null ? _b : void 0;
9400
+ }
9401
+ }
9402
+ return result;
9403
+ }
9404
+ }
9405
+ /**
9406
+ * (Protected) In-order traversal search for lower by predicate.
9407
+ * Falls back to linear in-order traversal when predicate-based search is required.
9408
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
9409
+ * @remarks Time Complexity: O(n) since it may visit every node.
9410
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
9411
+ *
9412
+ * @param predicate - The predicate function to test nodes.
9413
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
9414
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
9415
+ */
9416
+ _lowerByPredicate(predicate, iterationType) {
9417
+ if (iterationType === "RECURSIVE") {
9418
+ let result = void 0;
9419
+ const dfs = /* @__PURE__ */ __name((cur) => {
9420
+ if (!this.isRealNode(cur)) return;
9421
+ if (this.isRealNode(cur.left)) dfs(cur.left);
9422
+ if (predicate(cur)) {
9423
+ result = cur;
9424
+ }
9425
+ if (this.isRealNode(cur.right)) dfs(cur.right);
9426
+ }, "dfs");
9427
+ dfs(this.root);
9428
+ return result;
9429
+ } else {
9430
+ const stack = [];
9431
+ let current = this.root;
9432
+ let result = void 0;
9433
+ while (stack.length > 0 || this.isRealNode(current)) {
9434
+ if (this.isRealNode(current)) {
9435
+ stack.push(current);
9436
+ current = current.left;
9437
+ } else {
9438
+ const node = stack.pop();
9439
+ if (!this.isRealNode(node)) break;
9440
+ if (predicate(node)) {
9441
+ result = node;
9442
+ }
9443
+ current = node.right;
9444
+ }
9445
+ }
9446
+ return result;
9177
9447
  }
9178
- return false;
9179
9448
  }
9180
9449
  /**
9181
9450
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -9328,8 +9597,7 @@ var _BST = class _BST extends BinaryTree {
9328
9597
  _snapshotOptions() {
9329
9598
  return {
9330
9599
  ...super._snapshotOptions(),
9331
- specifyComparable: this.specifyComparable,
9332
- isReverse: this.isReverse
9600
+ comparator: this._comparator
9333
9601
  };
9334
9602
  }
9335
9603
  /**
@@ -9357,14 +9625,14 @@ var _BST = class _BST extends BinaryTree {
9357
9625
  }
9358
9626
  /**
9359
9627
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
9360
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
9628
+ * @remarks Time O(1) Space O(1)
9361
9629
  *
9362
9630
  * @param a - The first key.
9363
9631
  * @param b - The second key.
9364
9632
  * @returns A number (1, -1, or 0) representing the comparison.
9365
9633
  */
9366
9634
  _compare(a, b) {
9367
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
9635
+ return this._comparator(a, b);
9368
9636
  }
9369
9637
  /**
9370
9638
  * (Private) Deletes a node by its key.