tree-multimap-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.
- package/dist/cjs/index.cjs +245 -72
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +246 -72
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +245 -72
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +246 -72
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/tree-multimap-typed.js +246 -72
- package/dist/umd/tree-multimap-typed.js.map +1 -1
- package/dist/umd/tree-multimap-typed.min.js +3 -3
- package/dist/umd/tree-multimap-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -1466,6 +1466,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1466
1466
|
}
|
|
1467
1467
|
return false;
|
|
1468
1468
|
}
|
|
1469
|
+
/**
|
|
1470
|
+
* Adds or updates a new node to the tree.
|
|
1471
|
+
* @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).
|
|
1472
|
+
*
|
|
1473
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
1474
|
+
* @param [value] - The value, if providing just a key.
|
|
1475
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1476
|
+
*/
|
|
1477
|
+
set(keyNodeOrEntry, value) {
|
|
1478
|
+
return this.add(keyNodeOrEntry, value);
|
|
1479
|
+
}
|
|
1469
1480
|
/**
|
|
1470
1481
|
* Adds multiple items to the tree.
|
|
1471
1482
|
* @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).
|
|
@@ -1493,6 +1504,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1493
1504
|
}
|
|
1494
1505
|
return inserted;
|
|
1495
1506
|
}
|
|
1507
|
+
/**
|
|
1508
|
+
* Adds or updates multiple items to the tree.
|
|
1509
|
+
* @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).
|
|
1510
|
+
*
|
|
1511
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1512
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1513
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1514
|
+
*/
|
|
1515
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1516
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1517
|
+
}
|
|
1496
1518
|
/**
|
|
1497
1519
|
* Merges another tree into this one by adding all its nodes.
|
|
1498
1520
|
* @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`).
|
|
@@ -2809,36 +2831,20 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2809
2831
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2810
2832
|
super([], options);
|
|
2811
2833
|
__publicField(this, "_root");
|
|
2812
|
-
__publicField(this, "_isReverse", false);
|
|
2813
2834
|
/**
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
if (a > b) return 1;
|
|
2820
|
-
if (a < b) return -1;
|
|
2821
|
-
return 0;
|
|
2822
|
-
}
|
|
2823
|
-
if (this._specifyComparable) {
|
|
2824
|
-
const va = this._specifyComparable(a);
|
|
2825
|
-
const vb = this._specifyComparable(b);
|
|
2826
|
-
if (va > vb) return 1;
|
|
2827
|
-
if (va < vb) return -1;
|
|
2828
|
-
return 0;
|
|
2829
|
-
}
|
|
2830
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2831
|
-
throw TypeError(
|
|
2832
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2833
|
-
);
|
|
2834
|
-
}
|
|
2835
|
-
return 0;
|
|
2836
|
-
}, "_comparator"));
|
|
2837
|
-
__publicField(this, "_specifyComparable");
|
|
2835
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2836
|
+
|
|
2837
|
+
* @remarks Time O(1) Space O(1)
|
|
2838
|
+
*/
|
|
2839
|
+
__publicField(this, "_comparator");
|
|
2838
2840
|
if (options) {
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2841
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2842
|
+
this._comparator = options.comparator;
|
|
2843
|
+
} else {
|
|
2844
|
+
this._comparator = this._createDefaultComparator();
|
|
2845
|
+
}
|
|
2846
|
+
} else {
|
|
2847
|
+
this._comparator = this._createDefaultComparator();
|
|
2842
2848
|
}
|
|
2843
2849
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2844
2850
|
}
|
|
@@ -2852,13 +2858,25 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2852
2858
|
return this._root;
|
|
2853
2859
|
}
|
|
2854
2860
|
/**
|
|
2855
|
-
*
|
|
2856
|
-
* @remarks Time O(1)
|
|
2857
|
-
*
|
|
2858
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2861
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2862
|
+
* @remarks Time O(1) Space O(1)
|
|
2863
|
+
* @returns The default comparator function.
|
|
2859
2864
|
*/
|
|
2860
|
-
|
|
2861
|
-
return
|
|
2865
|
+
_createDefaultComparator() {
|
|
2866
|
+
return (a, b) => {
|
|
2867
|
+
debugger;
|
|
2868
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2869
|
+
if (a > b) return 1;
|
|
2870
|
+
if (a < b) return -1;
|
|
2871
|
+
return 0;
|
|
2872
|
+
}
|
|
2873
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2874
|
+
throw TypeError(
|
|
2875
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2876
|
+
);
|
|
2877
|
+
}
|
|
2878
|
+
return 0;
|
|
2879
|
+
};
|
|
2862
2880
|
}
|
|
2863
2881
|
/**
|
|
2864
2882
|
* Gets the comparator function used by the tree.
|
|
@@ -2869,15 +2887,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2869
2887
|
get comparator() {
|
|
2870
2888
|
return this._comparator;
|
|
2871
2889
|
}
|
|
2872
|
-
/**
|
|
2873
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2874
|
-
* @remarks Time O(1)
|
|
2875
|
-
*
|
|
2876
|
-
* @returns The key-to-comparable conversion function.
|
|
2877
|
-
*/
|
|
2878
|
-
get specifyComparable() {
|
|
2879
|
-
return this._specifyComparable;
|
|
2880
|
-
}
|
|
2881
2890
|
/**
|
|
2882
2891
|
* (Protected) Creates a new BST node.
|
|
2883
2892
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2919,7 +2928,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2919
2928
|
* @returns True if the key is valid, false otherwise.
|
|
2920
2929
|
*/
|
|
2921
2930
|
isValidKey(key) {
|
|
2922
|
-
return isComparable(key
|
|
2931
|
+
return isComparable(key);
|
|
2923
2932
|
}
|
|
2924
2933
|
/**
|
|
2925
2934
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3009,8 +3018,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3009
3018
|
if (!this.isRealNode(cur.left)) return false;
|
|
3010
3019
|
if (isRange) {
|
|
3011
3020
|
const range = keyNodeEntryOrPredicate;
|
|
3012
|
-
const leftS =
|
|
3013
|
-
const leftI =
|
|
3021
|
+
const leftS = range.low;
|
|
3022
|
+
const leftI = range.includeLow;
|
|
3014
3023
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
3015
3024
|
}
|
|
3016
3025
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3024,8 +3033,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3024
3033
|
if (!this.isRealNode(cur.right)) return false;
|
|
3025
3034
|
if (isRange) {
|
|
3026
3035
|
const range = keyNodeEntryOrPredicate;
|
|
3027
|
-
const rightS =
|
|
3028
|
-
const rightI =
|
|
3036
|
+
const rightS = range.high;
|
|
3037
|
+
const rightI = range.includeHigh;
|
|
3029
3038
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
3030
3039
|
}
|
|
3031
3040
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3186,6 +3195,32 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3186
3195
|
else _iterate();
|
|
3187
3196
|
return inserted;
|
|
3188
3197
|
}
|
|
3198
|
+
/**
|
|
3199
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3200
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
3201
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3202
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3203
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3204
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3205
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3206
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3207
|
+
*/
|
|
3208
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3209
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
3210
|
+
}
|
|
3211
|
+
/**
|
|
3212
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3213
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
3214
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3215
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3216
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3217
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3218
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3219
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3220
|
+
*/
|
|
3221
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3222
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3223
|
+
}
|
|
3189
3224
|
/**
|
|
3190
3225
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3191
3226
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3320,31 +3355,171 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3320
3355
|
return out;
|
|
3321
3356
|
}
|
|
3322
3357
|
/**
|
|
3323
|
-
* Deletes
|
|
3324
|
-
* @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.
|
|
3358
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
3325
3359
|
*
|
|
3326
|
-
* @
|
|
3327
|
-
*
|
|
3360
|
+
* @remarks
|
|
3361
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
3362
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
3363
|
+
*
|
|
3364
|
+
* @template K - The key type.
|
|
3365
|
+
* @template V - The value type.
|
|
3366
|
+
*
|
|
3367
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
3368
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
3369
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
3370
|
+
* - An entry tuple: searches for the key-value pair.
|
|
3371
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
3372
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
3373
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
3374
|
+
*
|
|
3375
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
3376
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
3377
|
+
*
|
|
3378
|
+
* @param startNode - The node to start the search from. Can be:
|
|
3379
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
3380
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
3381
|
+
* - Default value: this._root (the tree's root).
|
|
3382
|
+
*
|
|
3383
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
3384
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
3385
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
3386
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
3387
|
+
*
|
|
3388
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
3389
|
+
* - Key: the matched node's key.
|
|
3390
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
3391
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
3392
|
+
*/
|
|
3393
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3394
|
+
const toDelete = this.search(
|
|
3395
|
+
keyNodeEntryOrPredicate,
|
|
3396
|
+
onlyOne,
|
|
3397
|
+
(node) => node,
|
|
3398
|
+
startNode,
|
|
3399
|
+
iterationType
|
|
3400
|
+
);
|
|
3401
|
+
let results = [];
|
|
3402
|
+
for (const node of toDelete) {
|
|
3403
|
+
const deleteInfo = this.delete(node);
|
|
3404
|
+
results = results.concat(deleteInfo);
|
|
3405
|
+
}
|
|
3406
|
+
return results;
|
|
3407
|
+
}
|
|
3408
|
+
/**
|
|
3409
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3410
|
+
* Unified logic for both lowerBound and upperBound.
|
|
3411
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
3412
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3413
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3414
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3415
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
3328
3416
|
*/
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3417
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
3418
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3419
|
+
return void 0;
|
|
3420
|
+
}
|
|
3421
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3422
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3423
|
+
}
|
|
3424
|
+
let targetKey;
|
|
3425
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3426
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3427
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3428
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3429
|
+
if (key === null || key === void 0) {
|
|
3430
|
+
return void 0;
|
|
3431
|
+
}
|
|
3432
|
+
targetKey = key;
|
|
3433
|
+
} else {
|
|
3434
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3435
|
+
}
|
|
3436
|
+
if (targetKey !== void 0) {
|
|
3437
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
3438
|
+
}
|
|
3439
|
+
return void 0;
|
|
3440
|
+
}
|
|
3441
|
+
/**
|
|
3442
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
3443
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3444
|
+
* For lowerBound: finds first node where key >= target.
|
|
3445
|
+
* For upperBound: finds first node where key > target.
|
|
3446
|
+
* @param key - The target key to search for.
|
|
3447
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3448
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3449
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
3450
|
+
*/
|
|
3451
|
+
_boundByKey(key, isLower, iterationType) {
|
|
3452
|
+
var _a, _b;
|
|
3453
|
+
if (iterationType === "RECURSIVE") {
|
|
3454
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3455
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3456
|
+
const cmp = this.comparator(cur.key, key);
|
|
3457
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3458
|
+
if (condition) {
|
|
3459
|
+
const leftResult = dfs(cur.left);
|
|
3460
|
+
return leftResult != null ? leftResult : cur;
|
|
3461
|
+
} else {
|
|
3462
|
+
return dfs(cur.right);
|
|
3463
|
+
}
|
|
3464
|
+
}, "dfs");
|
|
3465
|
+
return dfs(this.root);
|
|
3466
|
+
} else {
|
|
3467
|
+
let current = this.root;
|
|
3468
|
+
let result = void 0;
|
|
3469
|
+
while (this.isRealNode(current)) {
|
|
3470
|
+
const cmp = this.comparator(current.key, key);
|
|
3471
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3472
|
+
if (condition) {
|
|
3473
|
+
result = current;
|
|
3474
|
+
current = (_a = current.left) != null ? _a : void 0;
|
|
3475
|
+
} else {
|
|
3476
|
+
current = (_b = current.right) != null ? _b : void 0;
|
|
3477
|
+
}
|
|
3337
3478
|
}
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3479
|
+
return result;
|
|
3480
|
+
}
|
|
3481
|
+
}
|
|
3482
|
+
/**
|
|
3483
|
+
* (Protected) In-order traversal search by predicate.
|
|
3484
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3485
|
+
* Returns the first node that satisfies the predicate function.
|
|
3486
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
3487
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
3488
|
+
* @param predicate - The predicate function to test nodes.
|
|
3489
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3490
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
3491
|
+
*/
|
|
3492
|
+
_boundByPredicate(predicate, iterationType) {
|
|
3493
|
+
if (iterationType === "RECURSIVE") {
|
|
3494
|
+
let result = void 0;
|
|
3495
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3496
|
+
if (result || !this.isRealNode(cur)) return;
|
|
3497
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3498
|
+
if (!result && predicate(cur)) {
|
|
3499
|
+
result = cur;
|
|
3500
|
+
}
|
|
3501
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
3502
|
+
}, "dfs");
|
|
3503
|
+
dfs(this.root);
|
|
3504
|
+
return result;
|
|
3505
|
+
} else {
|
|
3506
|
+
const stack = [];
|
|
3507
|
+
let current = this.root;
|
|
3508
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3509
|
+
if (this.isRealNode(current)) {
|
|
3510
|
+
stack.push(current);
|
|
3511
|
+
current = current.left;
|
|
3512
|
+
} else {
|
|
3513
|
+
const node = stack.pop();
|
|
3514
|
+
if (!this.isRealNode(node)) break;
|
|
3515
|
+
if (predicate(node)) {
|
|
3516
|
+
return node;
|
|
3517
|
+
}
|
|
3518
|
+
current = node.right;
|
|
3519
|
+
}
|
|
3344
3520
|
}
|
|
3345
|
-
|
|
3521
|
+
return void 0;
|
|
3346
3522
|
}
|
|
3347
|
-
return false;
|
|
3348
3523
|
}
|
|
3349
3524
|
/**
|
|
3350
3525
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
@@ -3381,8 +3556,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3381
3556
|
_snapshotOptions() {
|
|
3382
3557
|
return {
|
|
3383
3558
|
...super._snapshotOptions(),
|
|
3384
|
-
|
|
3385
|
-
isReverse: this.isReverse
|
|
3559
|
+
comparator: this._comparator
|
|
3386
3560
|
};
|
|
3387
3561
|
}
|
|
3388
3562
|
/**
|
|
@@ -3410,14 +3584,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3410
3584
|
}
|
|
3411
3585
|
/**
|
|
3412
3586
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3413
|
-
* @remarks Time O(1)
|
|
3587
|
+
* @remarks Time O(1) Space O(1)
|
|
3414
3588
|
*
|
|
3415
3589
|
* @param a - The first key.
|
|
3416
3590
|
* @param b - The second key.
|
|
3417
3591
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3418
3592
|
*/
|
|
3419
3593
|
_compare(a, b) {
|
|
3420
|
-
return this.
|
|
3594
|
+
return this._comparator(a, b);
|
|
3421
3595
|
}
|
|
3422
3596
|
/**
|
|
3423
3597
|
* (Private) Deletes a node by its key.
|