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.
- package/CHANGELOG.md +3 -1
- package/README.md +70 -11
- package/dist/cjs/index.cjs +341 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +343 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +341 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +343 -75
- 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 +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +142 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- 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 +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/data-structure-typed.js +343 -75
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-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 +9 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +495 -85
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +459 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
|
@@ -8628,36 +8628,20 @@ var dataStructureTyped = (() => {
|
|
|
8628
8628
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8629
8629
|
super([], options);
|
|
8630
8630
|
__publicField(this, "_root");
|
|
8631
|
-
__publicField(this, "_isReverse", false);
|
|
8632
8631
|
/**
|
|
8633
|
-
|
|
8634
|
-
|
|
8635
|
-
|
|
8636
|
-
|
|
8637
|
-
|
|
8638
|
-
if (a > b) return 1;
|
|
8639
|
-
if (a < b) return -1;
|
|
8640
|
-
return 0;
|
|
8641
|
-
}
|
|
8642
|
-
if (this._specifyComparable) {
|
|
8643
|
-
const va = this._specifyComparable(a);
|
|
8644
|
-
const vb = this._specifyComparable(b);
|
|
8645
|
-
if (va > vb) return 1;
|
|
8646
|
-
if (va < vb) return -1;
|
|
8647
|
-
return 0;
|
|
8648
|
-
}
|
|
8649
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8650
|
-
throw TypeError(
|
|
8651
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8652
|
-
);
|
|
8653
|
-
}
|
|
8654
|
-
return 0;
|
|
8655
|
-
});
|
|
8656
|
-
__publicField(this, "_specifyComparable");
|
|
8632
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
8633
|
+
|
|
8634
|
+
* @remarks Time O(1) Space O(1)
|
|
8635
|
+
*/
|
|
8636
|
+
__publicField(this, "_comparator");
|
|
8657
8637
|
if (options) {
|
|
8658
|
-
|
|
8659
|
-
|
|
8660
|
-
|
|
8638
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
8639
|
+
this._comparator = options.comparator;
|
|
8640
|
+
} else {
|
|
8641
|
+
this._comparator = this._createDefaultComparator();
|
|
8642
|
+
}
|
|
8643
|
+
} else {
|
|
8644
|
+
this._comparator = this._createDefaultComparator();
|
|
8661
8645
|
}
|
|
8662
8646
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8663
8647
|
}
|
|
@@ -8670,15 +8654,6 @@ var dataStructureTyped = (() => {
|
|
|
8670
8654
|
get root() {
|
|
8671
8655
|
return this._root;
|
|
8672
8656
|
}
|
|
8673
|
-
/**
|
|
8674
|
-
* Gets whether the tree's comparison logic is reversed.
|
|
8675
|
-
* @remarks Time O(1)
|
|
8676
|
-
*
|
|
8677
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
8678
|
-
*/
|
|
8679
|
-
get isReverse() {
|
|
8680
|
-
return this._isReverse;
|
|
8681
|
-
}
|
|
8682
8657
|
/**
|
|
8683
8658
|
* Gets the comparator function used by the tree.
|
|
8684
8659
|
* @remarks Time O(1)
|
|
@@ -8688,15 +8663,6 @@ var dataStructureTyped = (() => {
|
|
|
8688
8663
|
get comparator() {
|
|
8689
8664
|
return this._comparator;
|
|
8690
8665
|
}
|
|
8691
|
-
/**
|
|
8692
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
8693
|
-
* @remarks Time O(1)
|
|
8694
|
-
*
|
|
8695
|
-
* @returns The key-to-comparable conversion function.
|
|
8696
|
-
*/
|
|
8697
|
-
get specifyComparable() {
|
|
8698
|
-
return this._specifyComparable;
|
|
8699
|
-
}
|
|
8700
8666
|
/**
|
|
8701
8667
|
* (Protected) Creates a new BST node.
|
|
8702
8668
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8738,7 +8704,7 @@ var dataStructureTyped = (() => {
|
|
|
8738
8704
|
* @returns True if the key is valid, false otherwise.
|
|
8739
8705
|
*/
|
|
8740
8706
|
isValidKey(key) {
|
|
8741
|
-
return isComparable(key
|
|
8707
|
+
return isComparable(key);
|
|
8742
8708
|
}
|
|
8743
8709
|
/**
|
|
8744
8710
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -8828,8 +8794,8 @@ var dataStructureTyped = (() => {
|
|
|
8828
8794
|
if (!this.isRealNode(cur.left)) return false;
|
|
8829
8795
|
if (isRange) {
|
|
8830
8796
|
const range = keyNodeEntryOrPredicate;
|
|
8831
|
-
const leftS =
|
|
8832
|
-
const leftI =
|
|
8797
|
+
const leftS = range.low;
|
|
8798
|
+
const leftI = range.includeLow;
|
|
8833
8799
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
8834
8800
|
}
|
|
8835
8801
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -8843,8 +8809,8 @@ var dataStructureTyped = (() => {
|
|
|
8843
8809
|
if (!this.isRealNode(cur.right)) return false;
|
|
8844
8810
|
if (isRange) {
|
|
8845
8811
|
const range = keyNodeEntryOrPredicate;
|
|
8846
|
-
const rightS =
|
|
8847
|
-
const rightI =
|
|
8812
|
+
const rightS = range.high;
|
|
8813
|
+
const rightI = range.includeHigh;
|
|
8848
8814
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
8849
8815
|
}
|
|
8850
8816
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -9031,6 +8997,104 @@ var dataStructureTyped = (() => {
|
|
|
9031
8997
|
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9032
8998
|
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
9033
8999
|
}
|
|
9000
|
+
/**
|
|
9001
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
9002
|
+
* This is equivalent to Java TreeMap.ceilingEntry().
|
|
9003
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9004
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9005
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9006
|
+
*
|
|
9007
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9008
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9009
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
9010
|
+
*/
|
|
9011
|
+
ceilingEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9012
|
+
return this.lowerBound(keyNodeEntryOrPredicate, iterationType);
|
|
9013
|
+
}
|
|
9014
|
+
/**
|
|
9015
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
9016
|
+
* This is equivalent to Java TreeMap.higherEntry().
|
|
9017
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9018
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9019
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9020
|
+
*
|
|
9021
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9022
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9023
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
9024
|
+
*/
|
|
9025
|
+
higherEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9026
|
+
return this.upperBound(keyNodeEntryOrPredicate, iterationType);
|
|
9027
|
+
}
|
|
9028
|
+
/**
|
|
9029
|
+
* Returns the first node with a key less than or equal to the given key.
|
|
9030
|
+
* This is equivalent to Java TreeMap.floorEntry().
|
|
9031
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9032
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9033
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9034
|
+
*
|
|
9035
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9036
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9037
|
+
* @returns The first node with key <= given key, or undefined if no such node exists.
|
|
9038
|
+
*/
|
|
9039
|
+
floorEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9040
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9041
|
+
return void 0;
|
|
9042
|
+
}
|
|
9043
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9044
|
+
return this._floorByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
9045
|
+
}
|
|
9046
|
+
let targetKey;
|
|
9047
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9048
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9049
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9050
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9051
|
+
if (key === null || key === void 0) {
|
|
9052
|
+
return void 0;
|
|
9053
|
+
}
|
|
9054
|
+
targetKey = key;
|
|
9055
|
+
} else {
|
|
9056
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9057
|
+
}
|
|
9058
|
+
if (targetKey !== void 0) {
|
|
9059
|
+
return this._floorByKey(targetKey, iterationType);
|
|
9060
|
+
}
|
|
9061
|
+
return void 0;
|
|
9062
|
+
}
|
|
9063
|
+
/**
|
|
9064
|
+
* Returns the first node with a key strictly less than the given key.
|
|
9065
|
+
* This is equivalent to Java TreeMap.lowerEntry().
|
|
9066
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9067
|
+
* @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9068
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9069
|
+
*
|
|
9070
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9071
|
+
* @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
|
|
9072
|
+
* @returns The first node with key < given key, or undefined if no such node exists.
|
|
9073
|
+
*/
|
|
9074
|
+
lowerEntry(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9075
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9076
|
+
return void 0;
|
|
9077
|
+
}
|
|
9078
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9079
|
+
return this._lowerByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
9080
|
+
}
|
|
9081
|
+
let targetKey;
|
|
9082
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9083
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9084
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9085
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9086
|
+
if (key === null || key === void 0) {
|
|
9087
|
+
return void 0;
|
|
9088
|
+
}
|
|
9089
|
+
targetKey = key;
|
|
9090
|
+
} else {
|
|
9091
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9092
|
+
}
|
|
9093
|
+
if (targetKey !== void 0) {
|
|
9094
|
+
return this._lowerByKey(targetKey, iterationType);
|
|
9095
|
+
}
|
|
9096
|
+
return void 0;
|
|
9097
|
+
}
|
|
9034
9098
|
/**
|
|
9035
9099
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
9036
9100
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -9165,31 +9229,236 @@ var dataStructureTyped = (() => {
|
|
|
9165
9229
|
return out;
|
|
9166
9230
|
}
|
|
9167
9231
|
/**
|
|
9168
|
-
* Deletes
|
|
9169
|
-
* @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.
|
|
9232
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
9170
9233
|
*
|
|
9171
|
-
* @
|
|
9172
|
-
*
|
|
9234
|
+
* @remarks
|
|
9235
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
9236
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
9237
|
+
*
|
|
9238
|
+
* @template K - The key type.
|
|
9239
|
+
* @template V - The value type.
|
|
9240
|
+
*
|
|
9241
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
9242
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
9243
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
9244
|
+
* - An entry tuple: searches for the key-value pair.
|
|
9245
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
9246
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
9247
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
9248
|
+
*
|
|
9249
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
9250
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
9251
|
+
*
|
|
9252
|
+
* @param startNode - The node to start the search from. Can be:
|
|
9253
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
9254
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
9255
|
+
* - Default value: this._root (the tree's root).
|
|
9256
|
+
*
|
|
9257
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
9258
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
9259
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
9260
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
9261
|
+
*
|
|
9262
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
9263
|
+
* - Key: the matched node's key.
|
|
9264
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
9265
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
9266
|
+
*/
|
|
9267
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9268
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
9269
|
+
let results = [];
|
|
9270
|
+
for (const node of toDelete) {
|
|
9271
|
+
const deleteInfo = this.delete(node);
|
|
9272
|
+
results = results.concat(deleteInfo);
|
|
9273
|
+
}
|
|
9274
|
+
return results;
|
|
9275
|
+
}
|
|
9276
|
+
/**
|
|
9277
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
9278
|
+
* @remarks Time O(1) Space O(1)
|
|
9279
|
+
* @returns The default comparator function.
|
|
9173
9280
|
*/
|
|
9174
|
-
|
|
9175
|
-
|
|
9176
|
-
|
|
9177
|
-
|
|
9178
|
-
|
|
9179
|
-
|
|
9180
|
-
|
|
9181
|
-
cur = cur.left;
|
|
9281
|
+
_createDefaultComparator() {
|
|
9282
|
+
return (a, b) => {
|
|
9283
|
+
debugger;
|
|
9284
|
+
if (isComparable(a) && isComparable(b)) {
|
|
9285
|
+
if (a > b) return 1;
|
|
9286
|
+
if (a < b) return -1;
|
|
9287
|
+
return 0;
|
|
9182
9288
|
}
|
|
9183
|
-
|
|
9184
|
-
|
|
9185
|
-
|
|
9186
|
-
|
|
9187
|
-
if (predicate(key, val, index++, this)) {
|
|
9188
|
-
return this._deleteByKey(key);
|
|
9289
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
9290
|
+
throw TypeError(
|
|
9291
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
9292
|
+
);
|
|
9189
9293
|
}
|
|
9190
|
-
|
|
9294
|
+
return 0;
|
|
9295
|
+
};
|
|
9296
|
+
}
|
|
9297
|
+
/**
|
|
9298
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
9299
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9300
|
+
* Finds first node where key <= target.
|
|
9301
|
+
* @remarks Time O(h) where h is tree height.
|
|
9302
|
+
*
|
|
9303
|
+
* @param key - The target key to search for.
|
|
9304
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9305
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
9306
|
+
*/
|
|
9307
|
+
_floorByKey(key, iterationType) {
|
|
9308
|
+
var _a, _b;
|
|
9309
|
+
if (iterationType === "RECURSIVE") {
|
|
9310
|
+
const dfs = (cur) => {
|
|
9311
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9312
|
+
const cmp = this.comparator(cur.key, key);
|
|
9313
|
+
if (cmp <= 0) {
|
|
9314
|
+
const rightResult = dfs(cur.right);
|
|
9315
|
+
return rightResult != null ? rightResult : cur;
|
|
9316
|
+
} else {
|
|
9317
|
+
return dfs(cur.left);
|
|
9318
|
+
}
|
|
9319
|
+
};
|
|
9320
|
+
return dfs(this.root);
|
|
9321
|
+
} else {
|
|
9322
|
+
let current = this.root;
|
|
9323
|
+
let result = void 0;
|
|
9324
|
+
while (this.isRealNode(current)) {
|
|
9325
|
+
const cmp = this.comparator(current.key, key);
|
|
9326
|
+
if (cmp <= 0) {
|
|
9327
|
+
result = current;
|
|
9328
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
9329
|
+
} else {
|
|
9330
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
9331
|
+
}
|
|
9332
|
+
}
|
|
9333
|
+
return result;
|
|
9334
|
+
}
|
|
9335
|
+
}
|
|
9336
|
+
/**
|
|
9337
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
9338
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9339
|
+
* Returns the last node that satisfies the predicate function.
|
|
9340
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
9341
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
9342
|
+
*
|
|
9343
|
+
* @param predicate - The predicate function to test nodes.
|
|
9344
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9345
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
9346
|
+
*/
|
|
9347
|
+
_floorByPredicate(predicate, iterationType) {
|
|
9348
|
+
if (iterationType === "RECURSIVE") {
|
|
9349
|
+
let result = void 0;
|
|
9350
|
+
const dfs = (cur) => {
|
|
9351
|
+
if (!this.isRealNode(cur)) return;
|
|
9352
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9353
|
+
if (predicate(cur)) {
|
|
9354
|
+
result = cur;
|
|
9355
|
+
}
|
|
9356
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
9357
|
+
};
|
|
9358
|
+
dfs(this.root);
|
|
9359
|
+
return result;
|
|
9360
|
+
} else {
|
|
9361
|
+
const stack = [];
|
|
9362
|
+
let current = this.root;
|
|
9363
|
+
let result = void 0;
|
|
9364
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9365
|
+
if (this.isRealNode(current)) {
|
|
9366
|
+
stack.push(current);
|
|
9367
|
+
current = current.left;
|
|
9368
|
+
} else {
|
|
9369
|
+
const node = stack.pop();
|
|
9370
|
+
if (!this.isRealNode(node)) break;
|
|
9371
|
+
if (predicate(node)) {
|
|
9372
|
+
result = node;
|
|
9373
|
+
}
|
|
9374
|
+
current = node.right;
|
|
9375
|
+
}
|
|
9376
|
+
}
|
|
9377
|
+
return result;
|
|
9378
|
+
}
|
|
9379
|
+
}
|
|
9380
|
+
/**
|
|
9381
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
9382
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9383
|
+
* Finds first node where key < target.
|
|
9384
|
+
* @remarks Time O(h) where h is tree height.
|
|
9385
|
+
*
|
|
9386
|
+
* @param key - The target key to search for.
|
|
9387
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9388
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
9389
|
+
*/
|
|
9390
|
+
_lowerByKey(key, iterationType) {
|
|
9391
|
+
var _a, _b;
|
|
9392
|
+
if (iterationType === "RECURSIVE") {
|
|
9393
|
+
const dfs = (cur) => {
|
|
9394
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9395
|
+
const cmp = this.comparator(cur.key, key);
|
|
9396
|
+
if (cmp < 0) {
|
|
9397
|
+
const rightResult = dfs(cur.right);
|
|
9398
|
+
return rightResult != null ? rightResult : cur;
|
|
9399
|
+
} else {
|
|
9400
|
+
return dfs(cur.left);
|
|
9401
|
+
}
|
|
9402
|
+
};
|
|
9403
|
+
return dfs(this.root);
|
|
9404
|
+
} else {
|
|
9405
|
+
let current = this.root;
|
|
9406
|
+
let result = void 0;
|
|
9407
|
+
while (this.isRealNode(current)) {
|
|
9408
|
+
const cmp = this.comparator(current.key, key);
|
|
9409
|
+
if (cmp < 0) {
|
|
9410
|
+
result = current;
|
|
9411
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
9412
|
+
} else {
|
|
9413
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
9414
|
+
}
|
|
9415
|
+
}
|
|
9416
|
+
return result;
|
|
9417
|
+
}
|
|
9418
|
+
}
|
|
9419
|
+
/**
|
|
9420
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
9421
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9422
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
9423
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
9424
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
9425
|
+
*
|
|
9426
|
+
* @param predicate - The predicate function to test nodes.
|
|
9427
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9428
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
9429
|
+
*/
|
|
9430
|
+
_lowerByPredicate(predicate, iterationType) {
|
|
9431
|
+
if (iterationType === "RECURSIVE") {
|
|
9432
|
+
let result = void 0;
|
|
9433
|
+
const dfs = (cur) => {
|
|
9434
|
+
if (!this.isRealNode(cur)) return;
|
|
9435
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9436
|
+
if (predicate(cur)) {
|
|
9437
|
+
result = cur;
|
|
9438
|
+
}
|
|
9439
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
9440
|
+
};
|
|
9441
|
+
dfs(this.root);
|
|
9442
|
+
return result;
|
|
9443
|
+
} else {
|
|
9444
|
+
const stack = [];
|
|
9445
|
+
let current = this.root;
|
|
9446
|
+
let result = void 0;
|
|
9447
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9448
|
+
if (this.isRealNode(current)) {
|
|
9449
|
+
stack.push(current);
|
|
9450
|
+
current = current.left;
|
|
9451
|
+
} else {
|
|
9452
|
+
const node = stack.pop();
|
|
9453
|
+
if (!this.isRealNode(node)) break;
|
|
9454
|
+
if (predicate(node)) {
|
|
9455
|
+
result = node;
|
|
9456
|
+
}
|
|
9457
|
+
current = node.right;
|
|
9458
|
+
}
|
|
9459
|
+
}
|
|
9460
|
+
return result;
|
|
9191
9461
|
}
|
|
9192
|
-
return false;
|
|
9193
9462
|
}
|
|
9194
9463
|
/**
|
|
9195
9464
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -9342,8 +9611,7 @@ var dataStructureTyped = (() => {
|
|
|
9342
9611
|
_snapshotOptions() {
|
|
9343
9612
|
return {
|
|
9344
9613
|
...super._snapshotOptions(),
|
|
9345
|
-
|
|
9346
|
-
isReverse: this.isReverse
|
|
9614
|
+
comparator: this._comparator
|
|
9347
9615
|
};
|
|
9348
9616
|
}
|
|
9349
9617
|
/**
|
|
@@ -9371,14 +9639,14 @@ var dataStructureTyped = (() => {
|
|
|
9371
9639
|
}
|
|
9372
9640
|
/**
|
|
9373
9641
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
9374
|
-
* @remarks Time O(1)
|
|
9642
|
+
* @remarks Time O(1) Space O(1)
|
|
9375
9643
|
*
|
|
9376
9644
|
* @param a - The first key.
|
|
9377
9645
|
* @param b - The second key.
|
|
9378
9646
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
9379
9647
|
*/
|
|
9380
9648
|
_compare(a, b) {
|
|
9381
|
-
return this.
|
|
9649
|
+
return this._comparator(a, b);
|
|
9382
9650
|
}
|
|
9383
9651
|
/**
|
|
9384
9652
|
* (Private) Deletes a node by its key.
|