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