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