avl-tree-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/avl-tree-typed.js +246 -72
- package/dist/umd/avl-tree-typed.js.map +1 -1
- package/dist/umd/avl-tree-typed.min.js +3 -3
- package/dist/umd/avl-tree-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
|
@@ -1459,6 +1459,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1459
1459
|
}
|
|
1460
1460
|
return false;
|
|
1461
1461
|
}
|
|
1462
|
+
/**
|
|
1463
|
+
* Adds or updates a new node to the tree.
|
|
1464
|
+
* @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).
|
|
1465
|
+
*
|
|
1466
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
1467
|
+
* @param [value] - The value, if providing just a key.
|
|
1468
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1469
|
+
*/
|
|
1470
|
+
set(keyNodeOrEntry, value) {
|
|
1471
|
+
return this.add(keyNodeOrEntry, value);
|
|
1472
|
+
}
|
|
1462
1473
|
/**
|
|
1463
1474
|
* Adds multiple items to the tree.
|
|
1464
1475
|
* @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).
|
|
@@ -1486,6 +1497,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
1486
1497
|
}
|
|
1487
1498
|
return inserted;
|
|
1488
1499
|
}
|
|
1500
|
+
/**
|
|
1501
|
+
* Adds or updates multiple items to the tree.
|
|
1502
|
+
* @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).
|
|
1503
|
+
*
|
|
1504
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1505
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1506
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1507
|
+
*/
|
|
1508
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1509
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1510
|
+
}
|
|
1489
1511
|
/**
|
|
1490
1512
|
* Merges another tree into this one by adding all its nodes.
|
|
1491
1513
|
* @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`).
|
|
@@ -2802,36 +2824,20 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2802
2824
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
2803
2825
|
super([], options);
|
|
2804
2826
|
__publicField(this, "_root");
|
|
2805
|
-
__publicField(this, "_isReverse", false);
|
|
2806
2827
|
/**
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
if (a > b) return 1;
|
|
2813
|
-
if (a < b) return -1;
|
|
2814
|
-
return 0;
|
|
2815
|
-
}
|
|
2816
|
-
if (this._specifyComparable) {
|
|
2817
|
-
const va = this._specifyComparable(a);
|
|
2818
|
-
const vb = this._specifyComparable(b);
|
|
2819
|
-
if (va > vb) return 1;
|
|
2820
|
-
if (va < vb) return -1;
|
|
2821
|
-
return 0;
|
|
2822
|
-
}
|
|
2823
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
2824
|
-
throw TypeError(
|
|
2825
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
2826
|
-
);
|
|
2827
|
-
}
|
|
2828
|
-
return 0;
|
|
2829
|
-
}, "_comparator"));
|
|
2830
|
-
__publicField(this, "_specifyComparable");
|
|
2828
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
2829
|
+
|
|
2830
|
+
* @remarks Time O(1) Space O(1)
|
|
2831
|
+
*/
|
|
2832
|
+
__publicField(this, "_comparator");
|
|
2831
2833
|
if (options) {
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2834
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
2835
|
+
this._comparator = options.comparator;
|
|
2836
|
+
} else {
|
|
2837
|
+
this._comparator = this._createDefaultComparator();
|
|
2838
|
+
}
|
|
2839
|
+
} else {
|
|
2840
|
+
this._comparator = this._createDefaultComparator();
|
|
2835
2841
|
}
|
|
2836
2842
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
2837
2843
|
}
|
|
@@ -2845,13 +2851,25 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2845
2851
|
return this._root;
|
|
2846
2852
|
}
|
|
2847
2853
|
/**
|
|
2848
|
-
*
|
|
2849
|
-
* @remarks Time O(1)
|
|
2850
|
-
*
|
|
2851
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
2854
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
2855
|
+
* @remarks Time O(1) Space O(1)
|
|
2856
|
+
* @returns The default comparator function.
|
|
2852
2857
|
*/
|
|
2853
|
-
|
|
2854
|
-
return
|
|
2858
|
+
_createDefaultComparator() {
|
|
2859
|
+
return (a, b) => {
|
|
2860
|
+
debugger;
|
|
2861
|
+
if (isComparable(a) && isComparable(b)) {
|
|
2862
|
+
if (a > b) return 1;
|
|
2863
|
+
if (a < b) return -1;
|
|
2864
|
+
return 0;
|
|
2865
|
+
}
|
|
2866
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
2867
|
+
throw TypeError(
|
|
2868
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
2869
|
+
);
|
|
2870
|
+
}
|
|
2871
|
+
return 0;
|
|
2872
|
+
};
|
|
2855
2873
|
}
|
|
2856
2874
|
/**
|
|
2857
2875
|
* Gets the comparator function used by the tree.
|
|
@@ -2862,15 +2880,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2862
2880
|
get comparator() {
|
|
2863
2881
|
return this._comparator;
|
|
2864
2882
|
}
|
|
2865
|
-
/**
|
|
2866
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
2867
|
-
* @remarks Time O(1)
|
|
2868
|
-
*
|
|
2869
|
-
* @returns The key-to-comparable conversion function.
|
|
2870
|
-
*/
|
|
2871
|
-
get specifyComparable() {
|
|
2872
|
-
return this._specifyComparable;
|
|
2873
|
-
}
|
|
2874
2883
|
/**
|
|
2875
2884
|
* (Protected) Creates a new BST node.
|
|
2876
2885
|
* @remarks Time O(1), Space O(1)
|
|
@@ -2912,7 +2921,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
2912
2921
|
* @returns True if the key is valid, false otherwise.
|
|
2913
2922
|
*/
|
|
2914
2923
|
isValidKey(key) {
|
|
2915
|
-
return isComparable(key
|
|
2924
|
+
return isComparable(key);
|
|
2916
2925
|
}
|
|
2917
2926
|
/**
|
|
2918
2927
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -3002,8 +3011,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3002
3011
|
if (!this.isRealNode(cur.left)) return false;
|
|
3003
3012
|
if (isRange) {
|
|
3004
3013
|
const range = keyNodeEntryOrPredicate;
|
|
3005
|
-
const leftS =
|
|
3006
|
-
const leftI =
|
|
3014
|
+
const leftS = range.low;
|
|
3015
|
+
const leftI = range.includeLow;
|
|
3007
3016
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
3008
3017
|
}
|
|
3009
3018
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3017,8 +3026,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3017
3026
|
if (!this.isRealNode(cur.right)) return false;
|
|
3018
3027
|
if (isRange) {
|
|
3019
3028
|
const range = keyNodeEntryOrPredicate;
|
|
3020
|
-
const rightS =
|
|
3021
|
-
const rightI =
|
|
3029
|
+
const rightS = range.high;
|
|
3030
|
+
const rightI = range.includeHigh;
|
|
3022
3031
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
3023
3032
|
}
|
|
3024
3033
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -3179,6 +3188,32 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3179
3188
|
else _iterate();
|
|
3180
3189
|
return inserted;
|
|
3181
3190
|
}
|
|
3191
|
+
/**
|
|
3192
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3193
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
3194
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3195
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3196
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3197
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3198
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3199
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3200
|
+
*/
|
|
3201
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3202
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
3203
|
+
}
|
|
3204
|
+
/**
|
|
3205
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3206
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
3207
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3208
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3209
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3210
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3211
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3212
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3213
|
+
*/
|
|
3214
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3215
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3216
|
+
}
|
|
3182
3217
|
/**
|
|
3183
3218
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3184
3219
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3313,31 +3348,171 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3313
3348
|
return out;
|
|
3314
3349
|
}
|
|
3315
3350
|
/**
|
|
3316
|
-
* Deletes
|
|
3317
|
-
* @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.
|
|
3351
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
3318
3352
|
*
|
|
3319
|
-
* @
|
|
3320
|
-
*
|
|
3353
|
+
* @remarks
|
|
3354
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
3355
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
3356
|
+
*
|
|
3357
|
+
* @template K - The key type.
|
|
3358
|
+
* @template V - The value type.
|
|
3359
|
+
*
|
|
3360
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
3361
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
3362
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
3363
|
+
* - An entry tuple: searches for the key-value pair.
|
|
3364
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
3365
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
3366
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
3367
|
+
*
|
|
3368
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
3369
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
3370
|
+
*
|
|
3371
|
+
* @param startNode - The node to start the search from. Can be:
|
|
3372
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
3373
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
3374
|
+
* - Default value: this._root (the tree's root).
|
|
3375
|
+
*
|
|
3376
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
3377
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
3378
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
3379
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
3380
|
+
*
|
|
3381
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
3382
|
+
* - Key: the matched node's key.
|
|
3383
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
3384
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
3385
|
+
*/
|
|
3386
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
3387
|
+
const toDelete = this.search(
|
|
3388
|
+
keyNodeEntryOrPredicate,
|
|
3389
|
+
onlyOne,
|
|
3390
|
+
(node) => node,
|
|
3391
|
+
startNode,
|
|
3392
|
+
iterationType
|
|
3393
|
+
);
|
|
3394
|
+
let results = [];
|
|
3395
|
+
for (const node of toDelete) {
|
|
3396
|
+
const deleteInfo = this.delete(node);
|
|
3397
|
+
results = results.concat(deleteInfo);
|
|
3398
|
+
}
|
|
3399
|
+
return results;
|
|
3400
|
+
}
|
|
3401
|
+
/**
|
|
3402
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3403
|
+
* Unified logic for both lowerBound and upperBound.
|
|
3404
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
3405
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3406
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3407
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3408
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
3321
3409
|
*/
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3410
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
3411
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3412
|
+
return void 0;
|
|
3413
|
+
}
|
|
3414
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3415
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3416
|
+
}
|
|
3417
|
+
let targetKey;
|
|
3418
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3419
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3420
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3421
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3422
|
+
if (key === null || key === void 0) {
|
|
3423
|
+
return void 0;
|
|
3424
|
+
}
|
|
3425
|
+
targetKey = key;
|
|
3426
|
+
} else {
|
|
3427
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3428
|
+
}
|
|
3429
|
+
if (targetKey !== void 0) {
|
|
3430
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
3431
|
+
}
|
|
3432
|
+
return void 0;
|
|
3433
|
+
}
|
|
3434
|
+
/**
|
|
3435
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
3436
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3437
|
+
* For lowerBound: finds first node where key >= target.
|
|
3438
|
+
* For upperBound: finds first node where key > target.
|
|
3439
|
+
* @param key - The target key to search for.
|
|
3440
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3441
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3442
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
3443
|
+
*/
|
|
3444
|
+
_boundByKey(key, isLower, iterationType) {
|
|
3445
|
+
var _a, _b;
|
|
3446
|
+
if (iterationType === "RECURSIVE") {
|
|
3447
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3448
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3449
|
+
const cmp = this.comparator(cur.key, key);
|
|
3450
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3451
|
+
if (condition) {
|
|
3452
|
+
const leftResult = dfs(cur.left);
|
|
3453
|
+
return leftResult != null ? leftResult : cur;
|
|
3454
|
+
} else {
|
|
3455
|
+
return dfs(cur.right);
|
|
3456
|
+
}
|
|
3457
|
+
}, "dfs");
|
|
3458
|
+
return dfs(this.root);
|
|
3459
|
+
} else {
|
|
3460
|
+
let current = this.root;
|
|
3461
|
+
let result = void 0;
|
|
3462
|
+
while (this.isRealNode(current)) {
|
|
3463
|
+
const cmp = this.comparator(current.key, key);
|
|
3464
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3465
|
+
if (condition) {
|
|
3466
|
+
result = current;
|
|
3467
|
+
current = (_a = current.left) != null ? _a : void 0;
|
|
3468
|
+
} else {
|
|
3469
|
+
current = (_b = current.right) != null ? _b : void 0;
|
|
3470
|
+
}
|
|
3330
3471
|
}
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3472
|
+
return result;
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
/**
|
|
3476
|
+
* (Protected) In-order traversal search by predicate.
|
|
3477
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3478
|
+
* Returns the first node that satisfies the predicate function.
|
|
3479
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
3480
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
3481
|
+
* @param predicate - The predicate function to test nodes.
|
|
3482
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3483
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
3484
|
+
*/
|
|
3485
|
+
_boundByPredicate(predicate, iterationType) {
|
|
3486
|
+
if (iterationType === "RECURSIVE") {
|
|
3487
|
+
let result = void 0;
|
|
3488
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
3489
|
+
if (result || !this.isRealNode(cur)) return;
|
|
3490
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3491
|
+
if (!result && predicate(cur)) {
|
|
3492
|
+
result = cur;
|
|
3493
|
+
}
|
|
3494
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
3495
|
+
}, "dfs");
|
|
3496
|
+
dfs(this.root);
|
|
3497
|
+
return result;
|
|
3498
|
+
} else {
|
|
3499
|
+
const stack = [];
|
|
3500
|
+
let current = this.root;
|
|
3501
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3502
|
+
if (this.isRealNode(current)) {
|
|
3503
|
+
stack.push(current);
|
|
3504
|
+
current = current.left;
|
|
3505
|
+
} else {
|
|
3506
|
+
const node = stack.pop();
|
|
3507
|
+
if (!this.isRealNode(node)) break;
|
|
3508
|
+
if (predicate(node)) {
|
|
3509
|
+
return node;
|
|
3510
|
+
}
|
|
3511
|
+
current = node.right;
|
|
3512
|
+
}
|
|
3337
3513
|
}
|
|
3338
|
-
|
|
3514
|
+
return void 0;
|
|
3339
3515
|
}
|
|
3340
|
-
return false;
|
|
3341
3516
|
}
|
|
3342
3517
|
/**
|
|
3343
3518
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
@@ -3374,8 +3549,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3374
3549
|
_snapshotOptions() {
|
|
3375
3550
|
return {
|
|
3376
3551
|
...super._snapshotOptions(),
|
|
3377
|
-
|
|
3378
|
-
isReverse: this.isReverse
|
|
3552
|
+
comparator: this._comparator
|
|
3379
3553
|
};
|
|
3380
3554
|
}
|
|
3381
3555
|
/**
|
|
@@ -3403,14 +3577,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
3403
3577
|
}
|
|
3404
3578
|
/**
|
|
3405
3579
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
3406
|
-
* @remarks Time O(1)
|
|
3580
|
+
* @remarks Time O(1) Space O(1)
|
|
3407
3581
|
*
|
|
3408
3582
|
* @param a - The first key.
|
|
3409
3583
|
* @param b - The second key.
|
|
3410
3584
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
3411
3585
|
*/
|
|
3412
3586
|
_compare(a, b) {
|
|
3413
|
-
return this.
|
|
3587
|
+
return this._comparator(a, b);
|
|
3414
3588
|
}
|
|
3415
3589
|
/**
|
|
3416
3590
|
* (Private) Deletes a node by its key.
|