data-structure-typed 2.2.4 → 2.2.6
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 +1 -1
- package/dist/cjs/index.cjs +327 -57
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +329 -57
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +327 -57
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +329 -57
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +14 -57
- package/dist/types/data-structures/binary-tree/bst.d.ts +110 -96
- package/dist/umd/data-structure-typed.js +329 -57
- 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 +5 -3
- package/src/common/index.ts +2 -4
- package/src/data-structures/base/iterable-entry-base.ts +9 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +3 -1
- package/src/data-structures/binary-tree/binary-tree.ts +67 -0
- package/src/data-structures/binary-tree/bst.ts +658 -71
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +1385 -318
- 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 +13 -11
- package/tsup.config.js +6 -0
- package/tsup.leetcode.config.js +3 -0
- package/tsup.node.config.js +12 -0
|
@@ -256,6 +256,14 @@ var dataStructureTyped = (() => {
|
|
|
256
256
|
}
|
|
257
257
|
return accumulator;
|
|
258
258
|
}
|
|
259
|
+
/**
|
|
260
|
+
* Converts data structure to `[key, value]` pairs.
|
|
261
|
+
* @returns Array of entries.
|
|
262
|
+
* @remarks Time O(n), Space O(n)
|
|
263
|
+
*/
|
|
264
|
+
toArray() {
|
|
265
|
+
return [...this];
|
|
266
|
+
}
|
|
259
267
|
/**
|
|
260
268
|
* Visualize the iterable as an array of `[key, value]` pairs (or a custom string).
|
|
261
269
|
* @returns Array of entries (default) or a string.
|
|
@@ -269,7 +277,6 @@ var dataStructureTyped = (() => {
|
|
|
269
277
|
* @remarks Time O(n), Space O(n)
|
|
270
278
|
*/
|
|
271
279
|
print() {
|
|
272
|
-
console.log(this.toVisual());
|
|
273
280
|
}
|
|
274
281
|
};
|
|
275
282
|
|
|
@@ -489,7 +496,6 @@ var dataStructureTyped = (() => {
|
|
|
489
496
|
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
490
497
|
*/
|
|
491
498
|
print() {
|
|
492
|
-
console.log(this.toVisual());
|
|
493
499
|
}
|
|
494
500
|
};
|
|
495
501
|
|
|
@@ -6834,8 +6840,6 @@ var dataStructureTyped = (() => {
|
|
|
6834
6840
|
this.high = high;
|
|
6835
6841
|
this.includeLow = includeLow;
|
|
6836
6842
|
this.includeHigh = includeHigh;
|
|
6837
|
-
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
6838
|
-
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
6839
6843
|
}
|
|
6840
6844
|
// Determine whether a key is within the range
|
|
6841
6845
|
isInRange(key, comparator) {
|
|
@@ -8079,7 +8083,6 @@ var dataStructureTyped = (() => {
|
|
|
8079
8083
|
* @param [startNode=this._root] - The node to start printing from.
|
|
8080
8084
|
*/
|
|
8081
8085
|
print(options, startNode = this._root) {
|
|
8082
|
-
console.log(this.toVisual(startNode, options));
|
|
8083
8086
|
}
|
|
8084
8087
|
/**
|
|
8085
8088
|
* (Protected) Core DFS implementation.
|
|
@@ -8654,27 +8657,6 @@ var dataStructureTyped = (() => {
|
|
|
8654
8657
|
get root() {
|
|
8655
8658
|
return this._root;
|
|
8656
8659
|
}
|
|
8657
|
-
/**
|
|
8658
|
-
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
8659
|
-
* @remarks Time O(1) Space O(1)
|
|
8660
|
-
* @returns The default comparator function.
|
|
8661
|
-
*/
|
|
8662
|
-
_createDefaultComparator() {
|
|
8663
|
-
return (a, b) => {
|
|
8664
|
-
debugger;
|
|
8665
|
-
if (isComparable(a) && isComparable(b)) {
|
|
8666
|
-
if (a > b) return 1;
|
|
8667
|
-
if (a < b) return -1;
|
|
8668
|
-
return 0;
|
|
8669
|
-
}
|
|
8670
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8671
|
-
throw TypeError(
|
|
8672
|
-
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
8673
|
-
);
|
|
8674
|
-
}
|
|
8675
|
-
return 0;
|
|
8676
|
-
};
|
|
8677
|
-
}
|
|
8678
8660
|
/**
|
|
8679
8661
|
* Gets the comparator function used by the tree.
|
|
8680
8662
|
* @remarks Time O(1)
|
|
@@ -8992,31 +8974,141 @@ var dataStructureTyped = (() => {
|
|
|
8992
8974
|
else _iterate();
|
|
8993
8975
|
return inserted;
|
|
8994
8976
|
}
|
|
8995
|
-
|
|
8996
|
-
|
|
8997
|
-
|
|
8998
|
-
|
|
8999
|
-
|
|
9000
|
-
|
|
9001
|
-
|
|
9002
|
-
|
|
9003
|
-
|
|
9004
|
-
|
|
9005
|
-
|
|
9006
|
-
|
|
8977
|
+
ceiling(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8978
|
+
let actualCallback = void 0;
|
|
8979
|
+
let actualIterationType = this.iterationType;
|
|
8980
|
+
if (typeof callback === "string") {
|
|
8981
|
+
actualIterationType = callback;
|
|
8982
|
+
} else if (callback) {
|
|
8983
|
+
actualCallback = callback;
|
|
8984
|
+
if (iterationType) {
|
|
8985
|
+
actualIterationType = iterationType;
|
|
8986
|
+
}
|
|
8987
|
+
}
|
|
8988
|
+
const node = this._bound(keyNodeEntryOrPredicate, true, actualIterationType);
|
|
8989
|
+
if (!actualCallback) {
|
|
8990
|
+
return node == null ? void 0 : node.key;
|
|
8991
|
+
}
|
|
8992
|
+
return node ? actualCallback(node) : void 0;
|
|
9007
8993
|
}
|
|
9008
|
-
|
|
9009
|
-
|
|
9010
|
-
|
|
9011
|
-
|
|
9012
|
-
|
|
9013
|
-
|
|
9014
|
-
|
|
9015
|
-
|
|
9016
|
-
|
|
9017
|
-
|
|
9018
|
-
|
|
9019
|
-
|
|
8994
|
+
higher(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
8995
|
+
let actualCallback = void 0;
|
|
8996
|
+
let actualIterationType = this.iterationType;
|
|
8997
|
+
if (typeof callback === "string") {
|
|
8998
|
+
actualIterationType = callback;
|
|
8999
|
+
} else if (callback) {
|
|
9000
|
+
actualCallback = callback;
|
|
9001
|
+
if (iterationType) {
|
|
9002
|
+
actualIterationType = iterationType;
|
|
9003
|
+
}
|
|
9004
|
+
}
|
|
9005
|
+
const node = this._bound(keyNodeEntryOrPredicate, false, actualIterationType);
|
|
9006
|
+
if (!actualCallback) {
|
|
9007
|
+
return node == null ? void 0 : node.key;
|
|
9008
|
+
}
|
|
9009
|
+
return node ? actualCallback(node) : void 0;
|
|
9010
|
+
}
|
|
9011
|
+
floor(keyNodeEntryOrPredicate, callback = this._DEFAULT_NODE_CALLBACK, iterationType) {
|
|
9012
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9013
|
+
if (typeof callback === "string" || !callback) {
|
|
9014
|
+
return void 0;
|
|
9015
|
+
}
|
|
9016
|
+
return void 0;
|
|
9017
|
+
}
|
|
9018
|
+
let actualCallback = void 0;
|
|
9019
|
+
let actualIterationType = this.iterationType;
|
|
9020
|
+
if (typeof callback === "string") {
|
|
9021
|
+
actualIterationType = callback;
|
|
9022
|
+
} else if (callback) {
|
|
9023
|
+
actualCallback = callback;
|
|
9024
|
+
if (iterationType) {
|
|
9025
|
+
actualIterationType = iterationType;
|
|
9026
|
+
}
|
|
9027
|
+
}
|
|
9028
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9029
|
+
const node = this._floorByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9030
|
+
if (!actualCallback) {
|
|
9031
|
+
return node == null ? void 0 : node.key;
|
|
9032
|
+
}
|
|
9033
|
+
return node ? actualCallback(node) : void 0;
|
|
9034
|
+
}
|
|
9035
|
+
let targetKey;
|
|
9036
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9037
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9038
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9039
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9040
|
+
if (key === null || key === void 0) {
|
|
9041
|
+
if (typeof callback === "string" || !callback) {
|
|
9042
|
+
return void 0;
|
|
9043
|
+
}
|
|
9044
|
+
return void 0;
|
|
9045
|
+
}
|
|
9046
|
+
targetKey = key;
|
|
9047
|
+
} else {
|
|
9048
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9049
|
+
}
|
|
9050
|
+
if (targetKey !== void 0) {
|
|
9051
|
+
const node = this._floorByKey(targetKey, actualIterationType);
|
|
9052
|
+
if (!actualCallback) {
|
|
9053
|
+
return node == null ? void 0 : node.key;
|
|
9054
|
+
}
|
|
9055
|
+
return node ? actualCallback(node) : void 0;
|
|
9056
|
+
}
|
|
9057
|
+
if (typeof callback === "string" || !callback) {
|
|
9058
|
+
return void 0;
|
|
9059
|
+
}
|
|
9060
|
+
return void 0;
|
|
9061
|
+
}
|
|
9062
|
+
lower(keyNodeEntryOrPredicate, callback, iterationType) {
|
|
9063
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9064
|
+
if (typeof callback === "string" || !callback) {
|
|
9065
|
+
return void 0;
|
|
9066
|
+
}
|
|
9067
|
+
return void 0;
|
|
9068
|
+
}
|
|
9069
|
+
let actualCallback = void 0;
|
|
9070
|
+
let actualIterationType = this.iterationType;
|
|
9071
|
+
if (typeof callback === "string") {
|
|
9072
|
+
actualIterationType = callback;
|
|
9073
|
+
} else if (callback) {
|
|
9074
|
+
actualCallback = callback;
|
|
9075
|
+
if (iterationType) {
|
|
9076
|
+
actualIterationType = iterationType;
|
|
9077
|
+
}
|
|
9078
|
+
}
|
|
9079
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9080
|
+
const node = this._lowerByPredicate(keyNodeEntryOrPredicate, actualIterationType);
|
|
9081
|
+
if (!actualCallback) {
|
|
9082
|
+
return node == null ? void 0 : node.key;
|
|
9083
|
+
}
|
|
9084
|
+
return node ? actualCallback(node) : void 0;
|
|
9085
|
+
}
|
|
9086
|
+
let targetKey;
|
|
9087
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9088
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9089
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9090
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9091
|
+
if (key === null || key === void 0) {
|
|
9092
|
+
if (typeof callback === "string" || !callback) {
|
|
9093
|
+
return void 0;
|
|
9094
|
+
}
|
|
9095
|
+
return void 0;
|
|
9096
|
+
}
|
|
9097
|
+
targetKey = key;
|
|
9098
|
+
} else {
|
|
9099
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9100
|
+
}
|
|
9101
|
+
if (targetKey !== void 0) {
|
|
9102
|
+
const node = this._lowerByKey(targetKey, actualIterationType);
|
|
9103
|
+
if (!actualCallback) {
|
|
9104
|
+
return node == null ? void 0 : node.key;
|
|
9105
|
+
}
|
|
9106
|
+
return node ? actualCallback(node) : void 0;
|
|
9107
|
+
}
|
|
9108
|
+
if (typeof callback === "string" || !callback) {
|
|
9109
|
+
return void 0;
|
|
9110
|
+
}
|
|
9111
|
+
return void 0;
|
|
9020
9112
|
}
|
|
9021
9113
|
/**
|
|
9022
9114
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
@@ -9188,13 +9280,7 @@ var dataStructureTyped = (() => {
|
|
|
9188
9280
|
* - If no nodes match the search criteria, the returned map is empty.
|
|
9189
9281
|
*/
|
|
9190
9282
|
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9191
|
-
const toDelete = this.search(
|
|
9192
|
-
keyNodeEntryOrPredicate,
|
|
9193
|
-
onlyOne,
|
|
9194
|
-
(node) => node,
|
|
9195
|
-
startNode,
|
|
9196
|
-
iterationType
|
|
9197
|
-
);
|
|
9283
|
+
const toDelete = this.search(keyNodeEntryOrPredicate, onlyOne, (node) => node, startNode, iterationType);
|
|
9198
9284
|
let results = [];
|
|
9199
9285
|
for (const node of toDelete) {
|
|
9200
9286
|
const deleteInfo = this.delete(node);
|
|
@@ -9202,6 +9288,192 @@ var dataStructureTyped = (() => {
|
|
|
9202
9288
|
}
|
|
9203
9289
|
return results;
|
|
9204
9290
|
}
|
|
9291
|
+
/**
|
|
9292
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
9293
|
+
* @remarks Time O(1) Space O(1)
|
|
9294
|
+
* @returns The default comparator function.
|
|
9295
|
+
*/
|
|
9296
|
+
_createDefaultComparator() {
|
|
9297
|
+
return (a, b) => {
|
|
9298
|
+
if (isComparable(a) && isComparable(b)) {
|
|
9299
|
+
if (a > b) return 1;
|
|
9300
|
+
if (a < b) return -1;
|
|
9301
|
+
return 0;
|
|
9302
|
+
}
|
|
9303
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
9304
|
+
throw TypeError(
|
|
9305
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
9306
|
+
);
|
|
9307
|
+
}
|
|
9308
|
+
return 0;
|
|
9309
|
+
};
|
|
9310
|
+
}
|
|
9311
|
+
/**
|
|
9312
|
+
* (Protected) Binary search for floor by key with pruning optimization.
|
|
9313
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9314
|
+
* Finds first node where key <= target.
|
|
9315
|
+
* @remarks Time O(h) where h is tree height.
|
|
9316
|
+
*
|
|
9317
|
+
* @param key - The target key to search for.
|
|
9318
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9319
|
+
* @returns The first node with key <= target, or undefined if none exists.
|
|
9320
|
+
*/
|
|
9321
|
+
_floorByKey(key, iterationType) {
|
|
9322
|
+
var _a, _b;
|
|
9323
|
+
if (iterationType === "RECURSIVE") {
|
|
9324
|
+
const dfs = (cur) => {
|
|
9325
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9326
|
+
const cmp = this.comparator(cur.key, key);
|
|
9327
|
+
if (cmp <= 0) {
|
|
9328
|
+
const rightResult = dfs(cur.right);
|
|
9329
|
+
return rightResult != null ? rightResult : cur;
|
|
9330
|
+
} else {
|
|
9331
|
+
return dfs(cur.left);
|
|
9332
|
+
}
|
|
9333
|
+
};
|
|
9334
|
+
return dfs(this.root);
|
|
9335
|
+
} else {
|
|
9336
|
+
let current = this.root;
|
|
9337
|
+
let result = void 0;
|
|
9338
|
+
while (this.isRealNode(current)) {
|
|
9339
|
+
const cmp = this.comparator(current.key, key);
|
|
9340
|
+
if (cmp <= 0) {
|
|
9341
|
+
result = current;
|
|
9342
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
9343
|
+
} else {
|
|
9344
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
9345
|
+
}
|
|
9346
|
+
}
|
|
9347
|
+
return result;
|
|
9348
|
+
}
|
|
9349
|
+
}
|
|
9350
|
+
/**
|
|
9351
|
+
* (Protected) In-order traversal search for floor by predicate.
|
|
9352
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9353
|
+
* Returns the last node that satisfies the predicate function.
|
|
9354
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
9355
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
9356
|
+
*
|
|
9357
|
+
* @param predicate - The predicate function to test nodes.
|
|
9358
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9359
|
+
* @returns The last node satisfying predicate (highest key), or undefined if none found.
|
|
9360
|
+
*/
|
|
9361
|
+
_floorByPredicate(predicate, iterationType) {
|
|
9362
|
+
if (iterationType === "RECURSIVE") {
|
|
9363
|
+
let result = void 0;
|
|
9364
|
+
const dfs = (cur) => {
|
|
9365
|
+
if (!this.isRealNode(cur)) return;
|
|
9366
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9367
|
+
if (predicate(cur)) {
|
|
9368
|
+
result = cur;
|
|
9369
|
+
}
|
|
9370
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
9371
|
+
};
|
|
9372
|
+
dfs(this.root);
|
|
9373
|
+
return result;
|
|
9374
|
+
} else {
|
|
9375
|
+
const stack = [];
|
|
9376
|
+
let current = this.root;
|
|
9377
|
+
let result = void 0;
|
|
9378
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9379
|
+
if (this.isRealNode(current)) {
|
|
9380
|
+
stack.push(current);
|
|
9381
|
+
current = current.left;
|
|
9382
|
+
} else {
|
|
9383
|
+
const node = stack.pop();
|
|
9384
|
+
if (!this.isRealNode(node)) break;
|
|
9385
|
+
if (predicate(node)) {
|
|
9386
|
+
result = node;
|
|
9387
|
+
}
|
|
9388
|
+
current = node.right;
|
|
9389
|
+
}
|
|
9390
|
+
}
|
|
9391
|
+
return result;
|
|
9392
|
+
}
|
|
9393
|
+
}
|
|
9394
|
+
/**
|
|
9395
|
+
* (Protected) Binary search for lower by key with pruning optimization.
|
|
9396
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9397
|
+
* Finds first node where key < target.
|
|
9398
|
+
* @remarks Time O(h) where h is tree height.
|
|
9399
|
+
*
|
|
9400
|
+
* @param key - The target key to search for.
|
|
9401
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9402
|
+
* @returns The first node with key < target, or undefined if none exists.
|
|
9403
|
+
*/
|
|
9404
|
+
_lowerByKey(key, iterationType) {
|
|
9405
|
+
var _a, _b;
|
|
9406
|
+
if (iterationType === "RECURSIVE") {
|
|
9407
|
+
const dfs = (cur) => {
|
|
9408
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9409
|
+
const cmp = this.comparator(cur.key, key);
|
|
9410
|
+
if (cmp < 0) {
|
|
9411
|
+
const rightResult = dfs(cur.right);
|
|
9412
|
+
return rightResult != null ? rightResult : cur;
|
|
9413
|
+
} else {
|
|
9414
|
+
return dfs(cur.left);
|
|
9415
|
+
}
|
|
9416
|
+
};
|
|
9417
|
+
return dfs(this.root);
|
|
9418
|
+
} else {
|
|
9419
|
+
let current = this.root;
|
|
9420
|
+
let result = void 0;
|
|
9421
|
+
while (this.isRealNode(current)) {
|
|
9422
|
+
const cmp = this.comparator(current.key, key);
|
|
9423
|
+
if (cmp < 0) {
|
|
9424
|
+
result = current;
|
|
9425
|
+
current = (_a = current.right) != null ? _a : void 0;
|
|
9426
|
+
} else {
|
|
9427
|
+
current = (_b = current.left) != null ? _b : void 0;
|
|
9428
|
+
}
|
|
9429
|
+
}
|
|
9430
|
+
return result;
|
|
9431
|
+
}
|
|
9432
|
+
}
|
|
9433
|
+
/**
|
|
9434
|
+
* (Protected) In-order traversal search for lower by predicate.
|
|
9435
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9436
|
+
* Returns the node that satisfies the predicate and appears last in in-order traversal.
|
|
9437
|
+
* @remarks Time Complexity: O(n) since it may visit every node.
|
|
9438
|
+
* Space Complexity: O(h) for recursion, O(h) for iterative stack.
|
|
9439
|
+
*
|
|
9440
|
+
* @param predicate - The predicate function to test nodes.
|
|
9441
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9442
|
+
* @returns The last node satisfying predicate (highest key < target), or undefined if none found.
|
|
9443
|
+
*/
|
|
9444
|
+
_lowerByPredicate(predicate, iterationType) {
|
|
9445
|
+
if (iterationType === "RECURSIVE") {
|
|
9446
|
+
let result = void 0;
|
|
9447
|
+
const dfs = (cur) => {
|
|
9448
|
+
if (!this.isRealNode(cur)) return;
|
|
9449
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9450
|
+
if (predicate(cur)) {
|
|
9451
|
+
result = cur;
|
|
9452
|
+
}
|
|
9453
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
9454
|
+
};
|
|
9455
|
+
dfs(this.root);
|
|
9456
|
+
return result;
|
|
9457
|
+
} else {
|
|
9458
|
+
const stack = [];
|
|
9459
|
+
let current = this.root;
|
|
9460
|
+
let result = void 0;
|
|
9461
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9462
|
+
if (this.isRealNode(current)) {
|
|
9463
|
+
stack.push(current);
|
|
9464
|
+
current = current.left;
|
|
9465
|
+
} else {
|
|
9466
|
+
const node = stack.pop();
|
|
9467
|
+
if (!this.isRealNode(node)) break;
|
|
9468
|
+
if (predicate(node)) {
|
|
9469
|
+
result = node;
|
|
9470
|
+
}
|
|
9471
|
+
current = node.right;
|
|
9472
|
+
}
|
|
9473
|
+
}
|
|
9474
|
+
return result;
|
|
9475
|
+
}
|
|
9476
|
+
}
|
|
9205
9477
|
/**
|
|
9206
9478
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
9207
9479
|
* Unified logic for both lowerBound and upperBound.
|