data-structure-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/CHANGELOG.md +3 -1
- package/README.md +355 -1672
- package/README_CN.md +509 -0
- package/SECURITY.md +962 -11
- package/SECURITY.zh-CN.md +966 -0
- package/SPECIFICATION.md +689 -30
- package/SPECIFICATION.zh-CN.md +715 -0
- package/SPONSOR.zh-CN.md +62 -0
- package/SPONSOR_POLISHED.md +62 -0
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +215 -172
- 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/data-structure-typed.js +246 -72
- 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 +3 -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/test/performance/benchmark-runner.ts +14 -11
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +8 -8
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +8 -8
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -5
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +10 -10
- package/test/performance/reportor.ts +2 -1
- package/test/performance/single-suite-runner.ts +7 -4
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +117 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +166 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +771 -16
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +90 -38
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +133 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +149 -3
- package/test/unit/data-structures/heap/heap.test.ts +182 -47
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +118 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +121 -0
- package/test/unit/data-structures/queue/deque.test.ts +98 -67
- package/test/unit/data-structures/queue/queue.test.ts +85 -51
- package/test/unit/data-structures/stack/stack.test.ts +142 -33
- package/test/unit/data-structures/trie/trie.test.ts +135 -39
- package/tsup.leetcode.config.js +99 -0
- package/typedoc.json +2 -1
- package/POSTS_zh-CN.md +0 -54
- package/README_zh-CN.md +0 -1208
- package/SPECIFICATION_zh-CN.md +0 -81
|
@@ -7251,6 +7251,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7251
7251
|
}
|
|
7252
7252
|
return false;
|
|
7253
7253
|
}
|
|
7254
|
+
/**
|
|
7255
|
+
* Adds or updates a new node to the tree.
|
|
7256
|
+
* @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).
|
|
7257
|
+
*
|
|
7258
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
7259
|
+
* @param [value] - The value, if providing just a key.
|
|
7260
|
+
* @returns True if the addition was successful, false otherwise.
|
|
7261
|
+
*/
|
|
7262
|
+
set(keyNodeOrEntry, value) {
|
|
7263
|
+
return this.add(keyNodeOrEntry, value);
|
|
7264
|
+
}
|
|
7254
7265
|
/**
|
|
7255
7266
|
* Adds multiple items to the tree.
|
|
7256
7267
|
* @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).
|
|
@@ -7278,6 +7289,17 @@ var _BinaryTree = class _BinaryTree extends IterableEntryBase {
|
|
|
7278
7289
|
}
|
|
7279
7290
|
return inserted;
|
|
7280
7291
|
}
|
|
7292
|
+
/**
|
|
7293
|
+
* Adds or updates multiple items to the tree.
|
|
7294
|
+
* @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).
|
|
7295
|
+
*
|
|
7296
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
7297
|
+
* @param [values] - An optional parallel iterable of values.
|
|
7298
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
7299
|
+
*/
|
|
7300
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
7301
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
7302
|
+
}
|
|
7281
7303
|
/**
|
|
7282
7304
|
* Merges another tree into this one by adding all its nodes.
|
|
7283
7305
|
* @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`).
|
|
@@ -8594,36 +8616,20 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8594
8616
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8595
8617
|
super([], options);
|
|
8596
8618
|
__publicField(this, "_root");
|
|
8597
|
-
__publicField(this, "_isReverse", false);
|
|
8598
8619
|
/**
|
|
8599
|
-
|
|
8600
|
-
|
|
8601
|
-
|
|
8602
|
-
|
|
8603
|
-
|
|
8604
|
-
if (a > b) return 1;
|
|
8605
|
-
if (a < b) return -1;
|
|
8606
|
-
return 0;
|
|
8607
|
-
}
|
|
8608
|
-
if (this._specifyComparable) {
|
|
8609
|
-
const va = this._specifyComparable(a);
|
|
8610
|
-
const vb = this._specifyComparable(b);
|
|
8611
|
-
if (va > vb) return 1;
|
|
8612
|
-
if (va < vb) return -1;
|
|
8613
|
-
return 0;
|
|
8614
|
-
}
|
|
8615
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8616
|
-
throw TypeError(
|
|
8617
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8618
|
-
);
|
|
8619
|
-
}
|
|
8620
|
-
return 0;
|
|
8621
|
-
}, "_comparator"));
|
|
8622
|
-
__publicField(this, "_specifyComparable");
|
|
8620
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
8621
|
+
|
|
8622
|
+
* @remarks Time O(1) Space O(1)
|
|
8623
|
+
*/
|
|
8624
|
+
__publicField(this, "_comparator");
|
|
8623
8625
|
if (options) {
|
|
8624
|
-
|
|
8625
|
-
|
|
8626
|
-
|
|
8626
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
8627
|
+
this._comparator = options.comparator;
|
|
8628
|
+
} else {
|
|
8629
|
+
this._comparator = this._createDefaultComparator();
|
|
8630
|
+
}
|
|
8631
|
+
} else {
|
|
8632
|
+
this._comparator = this._createDefaultComparator();
|
|
8627
8633
|
}
|
|
8628
8634
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8629
8635
|
}
|
|
@@ -8637,13 +8643,25 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8637
8643
|
return this._root;
|
|
8638
8644
|
}
|
|
8639
8645
|
/**
|
|
8640
|
-
*
|
|
8641
|
-
* @remarks Time O(1)
|
|
8642
|
-
*
|
|
8643
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
8646
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
8647
|
+
* @remarks Time O(1) Space O(1)
|
|
8648
|
+
* @returns The default comparator function.
|
|
8644
8649
|
*/
|
|
8645
|
-
|
|
8646
|
-
return
|
|
8650
|
+
_createDefaultComparator() {
|
|
8651
|
+
return (a, b) => {
|
|
8652
|
+
debugger;
|
|
8653
|
+
if (isComparable(a) && isComparable(b)) {
|
|
8654
|
+
if (a > b) return 1;
|
|
8655
|
+
if (a < b) return -1;
|
|
8656
|
+
return 0;
|
|
8657
|
+
}
|
|
8658
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
8659
|
+
throw TypeError(
|
|
8660
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
8661
|
+
);
|
|
8662
|
+
}
|
|
8663
|
+
return 0;
|
|
8664
|
+
};
|
|
8647
8665
|
}
|
|
8648
8666
|
/**
|
|
8649
8667
|
* Gets the comparator function used by the tree.
|
|
@@ -8654,15 +8672,6 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8654
8672
|
get comparator() {
|
|
8655
8673
|
return this._comparator;
|
|
8656
8674
|
}
|
|
8657
|
-
/**
|
|
8658
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
8659
|
-
* @remarks Time O(1)
|
|
8660
|
-
*
|
|
8661
|
-
* @returns The key-to-comparable conversion function.
|
|
8662
|
-
*/
|
|
8663
|
-
get specifyComparable() {
|
|
8664
|
-
return this._specifyComparable;
|
|
8665
|
-
}
|
|
8666
8675
|
/**
|
|
8667
8676
|
* (Protected) Creates a new BST node.
|
|
8668
8677
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8704,7 +8713,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8704
8713
|
* @returns True if the key is valid, false otherwise.
|
|
8705
8714
|
*/
|
|
8706
8715
|
isValidKey(key) {
|
|
8707
|
-
return isComparable(key
|
|
8716
|
+
return isComparable(key);
|
|
8708
8717
|
}
|
|
8709
8718
|
/**
|
|
8710
8719
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -8794,8 +8803,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8794
8803
|
if (!this.isRealNode(cur.left)) return false;
|
|
8795
8804
|
if (isRange) {
|
|
8796
8805
|
const range = keyNodeEntryOrPredicate;
|
|
8797
|
-
const leftS =
|
|
8798
|
-
const leftI =
|
|
8806
|
+
const leftS = range.low;
|
|
8807
|
+
const leftI = range.includeLow;
|
|
8799
8808
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
8800
8809
|
}
|
|
8801
8810
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -8809,8 +8818,8 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8809
8818
|
if (!this.isRealNode(cur.right)) return false;
|
|
8810
8819
|
if (isRange) {
|
|
8811
8820
|
const range = keyNodeEntryOrPredicate;
|
|
8812
|
-
const rightS =
|
|
8813
|
-
const rightI =
|
|
8821
|
+
const rightS = range.high;
|
|
8822
|
+
const rightI = range.includeHigh;
|
|
8814
8823
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
8815
8824
|
}
|
|
8816
8825
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -8971,6 +8980,32 @@ var _BST = class _BST extends BinaryTree {
|
|
|
8971
8980
|
else _iterate();
|
|
8972
8981
|
return inserted;
|
|
8973
8982
|
}
|
|
8983
|
+
/**
|
|
8984
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
8985
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
8986
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
8987
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
8988
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
8989
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
8990
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
8991
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
8992
|
+
*/
|
|
8993
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
8994
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
8995
|
+
}
|
|
8996
|
+
/**
|
|
8997
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
8998
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
8999
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
9000
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
9001
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
9002
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9003
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
9004
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
9005
|
+
*/
|
|
9006
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
9007
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
9008
|
+
}
|
|
8974
9009
|
/**
|
|
8975
9010
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
8976
9011
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -9105,31 +9140,171 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9105
9140
|
return out;
|
|
9106
9141
|
}
|
|
9107
9142
|
/**
|
|
9108
|
-
* Deletes
|
|
9109
|
-
* @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.
|
|
9143
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
9110
9144
|
*
|
|
9111
|
-
* @
|
|
9112
|
-
*
|
|
9145
|
+
* @remarks
|
|
9146
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
9147
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
9148
|
+
*
|
|
9149
|
+
* @template K - The key type.
|
|
9150
|
+
* @template V - The value type.
|
|
9151
|
+
*
|
|
9152
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
9153
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
9154
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
9155
|
+
* - An entry tuple: searches for the key-value pair.
|
|
9156
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
9157
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
9158
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
9159
|
+
*
|
|
9160
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
9161
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
9162
|
+
*
|
|
9163
|
+
* @param startNode - The node to start the search from. Can be:
|
|
9164
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
9165
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
9166
|
+
* - Default value: this._root (the tree's root).
|
|
9167
|
+
*
|
|
9168
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
9169
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
9170
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
9171
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
9172
|
+
*
|
|
9173
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
9174
|
+
* - Key: the matched node's key.
|
|
9175
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
9176
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
9177
|
+
*/
|
|
9178
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9179
|
+
const toDelete = this.search(
|
|
9180
|
+
keyNodeEntryOrPredicate,
|
|
9181
|
+
onlyOne,
|
|
9182
|
+
(node) => node,
|
|
9183
|
+
startNode,
|
|
9184
|
+
iterationType
|
|
9185
|
+
);
|
|
9186
|
+
let results = [];
|
|
9187
|
+
for (const node of toDelete) {
|
|
9188
|
+
const deleteInfo = this.delete(node);
|
|
9189
|
+
results = results.concat(deleteInfo);
|
|
9190
|
+
}
|
|
9191
|
+
return results;
|
|
9192
|
+
}
|
|
9193
|
+
/**
|
|
9194
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
9195
|
+
* Unified logic for both lowerBound and upperBound.
|
|
9196
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
9197
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
9198
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
9199
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9200
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
9113
9201
|
*/
|
|
9114
|
-
|
|
9115
|
-
|
|
9116
|
-
|
|
9117
|
-
|
|
9118
|
-
|
|
9119
|
-
|
|
9120
|
-
|
|
9121
|
-
|
|
9202
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
9203
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
9204
|
+
return void 0;
|
|
9205
|
+
}
|
|
9206
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
9207
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
9208
|
+
}
|
|
9209
|
+
let targetKey;
|
|
9210
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
9211
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
9212
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
9213
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
9214
|
+
if (key === null || key === void 0) {
|
|
9215
|
+
return void 0;
|
|
9122
9216
|
}
|
|
9123
|
-
|
|
9124
|
-
|
|
9125
|
-
|
|
9126
|
-
|
|
9127
|
-
|
|
9128
|
-
|
|
9217
|
+
targetKey = key;
|
|
9218
|
+
} else {
|
|
9219
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
9220
|
+
}
|
|
9221
|
+
if (targetKey !== void 0) {
|
|
9222
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
9223
|
+
}
|
|
9224
|
+
return void 0;
|
|
9225
|
+
}
|
|
9226
|
+
/**
|
|
9227
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
9228
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
9229
|
+
* For lowerBound: finds first node where key >= target.
|
|
9230
|
+
* For upperBound: finds first node where key > target.
|
|
9231
|
+
* @param key - The target key to search for.
|
|
9232
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
9233
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9234
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
9235
|
+
*/
|
|
9236
|
+
_boundByKey(key, isLower, iterationType) {
|
|
9237
|
+
var _a, _b;
|
|
9238
|
+
if (iterationType === "RECURSIVE") {
|
|
9239
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
9240
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
9241
|
+
const cmp = this.comparator(cur.key, key);
|
|
9242
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
9243
|
+
if (condition) {
|
|
9244
|
+
const leftResult = dfs(cur.left);
|
|
9245
|
+
return leftResult != null ? leftResult : cur;
|
|
9246
|
+
} else {
|
|
9247
|
+
return dfs(cur.right);
|
|
9248
|
+
}
|
|
9249
|
+
}, "dfs");
|
|
9250
|
+
return dfs(this.root);
|
|
9251
|
+
} else {
|
|
9252
|
+
let current = this.root;
|
|
9253
|
+
let result = void 0;
|
|
9254
|
+
while (this.isRealNode(current)) {
|
|
9255
|
+
const cmp = this.comparator(current.key, key);
|
|
9256
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
9257
|
+
if (condition) {
|
|
9258
|
+
result = current;
|
|
9259
|
+
current = (_a = current.left) != null ? _a : void 0;
|
|
9260
|
+
} else {
|
|
9261
|
+
current = (_b = current.right) != null ? _b : void 0;
|
|
9262
|
+
}
|
|
9129
9263
|
}
|
|
9130
|
-
|
|
9264
|
+
return result;
|
|
9265
|
+
}
|
|
9266
|
+
}
|
|
9267
|
+
/**
|
|
9268
|
+
* (Protected) In-order traversal search by predicate.
|
|
9269
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
9270
|
+
* Returns the first node that satisfies the predicate function.
|
|
9271
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
9272
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
9273
|
+
* @param predicate - The predicate function to test nodes.
|
|
9274
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
9275
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
9276
|
+
*/
|
|
9277
|
+
_boundByPredicate(predicate, iterationType) {
|
|
9278
|
+
if (iterationType === "RECURSIVE") {
|
|
9279
|
+
let result = void 0;
|
|
9280
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
9281
|
+
if (result || !this.isRealNode(cur)) return;
|
|
9282
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
9283
|
+
if (!result && predicate(cur)) {
|
|
9284
|
+
result = cur;
|
|
9285
|
+
}
|
|
9286
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
9287
|
+
}, "dfs");
|
|
9288
|
+
dfs(this.root);
|
|
9289
|
+
return result;
|
|
9290
|
+
} else {
|
|
9291
|
+
const stack = [];
|
|
9292
|
+
let current = this.root;
|
|
9293
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
9294
|
+
if (this.isRealNode(current)) {
|
|
9295
|
+
stack.push(current);
|
|
9296
|
+
current = current.left;
|
|
9297
|
+
} else {
|
|
9298
|
+
const node = stack.pop();
|
|
9299
|
+
if (!this.isRealNode(node)) break;
|
|
9300
|
+
if (predicate(node)) {
|
|
9301
|
+
return node;
|
|
9302
|
+
}
|
|
9303
|
+
current = node.right;
|
|
9304
|
+
}
|
|
9305
|
+
}
|
|
9306
|
+
return void 0;
|
|
9131
9307
|
}
|
|
9132
|
-
return false;
|
|
9133
9308
|
}
|
|
9134
9309
|
/**
|
|
9135
9310
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
@@ -9166,8 +9341,7 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9166
9341
|
_snapshotOptions() {
|
|
9167
9342
|
return {
|
|
9168
9343
|
...super._snapshotOptions(),
|
|
9169
|
-
|
|
9170
|
-
isReverse: this.isReverse
|
|
9344
|
+
comparator: this._comparator
|
|
9171
9345
|
};
|
|
9172
9346
|
}
|
|
9173
9347
|
/**
|
|
@@ -9195,14 +9369,14 @@ var _BST = class _BST extends BinaryTree {
|
|
|
9195
9369
|
}
|
|
9196
9370
|
/**
|
|
9197
9371
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
9198
|
-
* @remarks Time O(1)
|
|
9372
|
+
* @remarks Time O(1) Space O(1)
|
|
9199
9373
|
*
|
|
9200
9374
|
* @param a - The first key.
|
|
9201
9375
|
* @param b - The second key.
|
|
9202
9376
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
9203
9377
|
*/
|
|
9204
9378
|
_compare(a, b) {
|
|
9205
|
-
return this.
|
|
9379
|
+
return this._comparator(a, b);
|
|
9206
9380
|
}
|
|
9207
9381
|
/**
|
|
9208
9382
|
* (Private) Deletes a node by its key.
|