red-black-tree-typed 2.2.2 → 2.2.3
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/README.md +92 -37
- package/dist/cjs/index.cjs +163 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +164 -0
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +163 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +164 -0
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- 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/umd/red-black-tree-typed.js +164 -0
- package/dist/umd/red-black-tree-typed.js.map +1 -1
- package/dist/umd/red-black-tree-typed.min.js +3 -3
- package/dist/umd/red-black-tree-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +96 -2
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +322 -13
- package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- 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
|
@@ -77,53 +77,99 @@ export declare class TrieNode {
|
|
|
77
77
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
78
78
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
79
79
|
* @example
|
|
80
|
-
* //
|
|
81
|
-
*
|
|
80
|
+
* // basic Trie creation and add words
|
|
81
|
+
* // Create a simple Trie with initial words
|
|
82
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
82
83
|
*
|
|
83
|
-
* //
|
|
84
|
-
*
|
|
85
|
-
*
|
|
84
|
+
* // Verify size
|
|
85
|
+
* console.log(trie.size); // 3;
|
|
86
|
+
*
|
|
87
|
+
* // Check if words exist
|
|
88
|
+
* console.log(trie.has('apple')); // true;
|
|
89
|
+
* console.log(trie.has('app')); // true;
|
|
90
|
+
*
|
|
91
|
+
* // Add a new word
|
|
92
|
+
* trie.add('application');
|
|
93
|
+
* console.log(trie.size); // 4;
|
|
86
94
|
* @example
|
|
87
|
-
* //
|
|
88
|
-
*
|
|
89
|
-
* '/home/user/documents/file1.txt',
|
|
90
|
-
* '/home/user/documents/file2.txt',
|
|
91
|
-
* '/home/user/pictures/photo.jpg',
|
|
92
|
-
* '/home/user/pictures/vacation/',
|
|
93
|
-
* '/home/user/downloads'
|
|
94
|
-
* ]);
|
|
95
|
+
* // Trie getWords and prefix search
|
|
96
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
95
97
|
*
|
|
96
|
-
* //
|
|
97
|
-
*
|
|
98
|
+
* // Get all words with prefix 'app'
|
|
99
|
+
* const appWords = trie.getWords('app');
|
|
100
|
+
* console.log(appWords); // contains 'app';
|
|
101
|
+
* console.log(appWords); // contains 'apple';
|
|
102
|
+
* console.log(appWords); // contains 'apply';
|
|
103
|
+
* console.log(appWords); // contains 'application';
|
|
104
|
+
* expect(appWords).not.toContain('apricot');
|
|
105
|
+
* @example
|
|
106
|
+
* // Trie isPrefix and isAbsolutePrefix checks
|
|
107
|
+
* const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
|
|
98
108
|
*
|
|
99
|
-
* //
|
|
100
|
-
*
|
|
101
|
-
* console.log(
|
|
109
|
+
* // Check if string is a prefix of any word
|
|
110
|
+
* console.log(trie.hasPrefix('tri')); // true;
|
|
111
|
+
* console.log(trie.hasPrefix('tr')); // true;
|
|
112
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
113
|
+
*
|
|
114
|
+
* // Check if string is an absolute prefix (not a complete word)
|
|
115
|
+
* console.log(trie.hasPurePrefix('tri')); // true;
|
|
116
|
+
* console.log(trie.hasPurePrefix('tree')); // false; // 'tree' is a complete word
|
|
117
|
+
*
|
|
118
|
+
* // Verify size
|
|
119
|
+
* console.log(trie.size); // 5;
|
|
102
120
|
* @example
|
|
103
|
-
* //
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* 'closure',
|
|
113
|
-
* 'const',
|
|
114
|
-
* 'constructor'
|
|
115
|
-
* ]);
|
|
121
|
+
* // Trie delete and iteration
|
|
122
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
123
|
+
*
|
|
124
|
+
* // Delete a word
|
|
125
|
+
* trie.delete('card');
|
|
126
|
+
* console.log(trie.has('card')); // false;
|
|
127
|
+
*
|
|
128
|
+
* // Word with same prefix still exists
|
|
129
|
+
* console.log(trie.has('care')); // true;
|
|
116
130
|
*
|
|
117
|
-
* //
|
|
118
|
-
* console.log(
|
|
119
|
-
* console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
|
|
120
|
-
* console.log(autocomplete.getWords('con')); // ['constructor', 'const']
|
|
131
|
+
* // Size decreased
|
|
132
|
+
* console.log(trie.size); // 5;
|
|
121
133
|
*
|
|
122
|
-
* //
|
|
123
|
-
*
|
|
134
|
+
* // Iterate through all words
|
|
135
|
+
* const allWords = [...trie];
|
|
136
|
+
* console.log(allWords.length); // 5;
|
|
137
|
+
* @example
|
|
138
|
+
* // Trie for autocomplete search index
|
|
139
|
+
* // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
|
|
140
|
+
* const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
|
|
141
|
+
*
|
|
142
|
+
* // User types 'j' - get all suggestions
|
|
143
|
+
* const jResults = searchIndex.getWords('j');
|
|
144
|
+
* console.log(jResults); // contains 'javascript';
|
|
145
|
+
* console.log(jResults); // contains 'java';
|
|
146
|
+
* console.log(jResults.length); // 2;
|
|
147
|
+
*
|
|
148
|
+
* // User types 'ja' - get more specific suggestions
|
|
149
|
+
* const jaResults = searchIndex.getWords('ja');
|
|
150
|
+
* console.log(jaResults); // contains 'javascript';
|
|
151
|
+
* console.log(jaResults); // contains 'java';
|
|
152
|
+
* console.log(jaResults.length); // 2;
|
|
153
|
+
*
|
|
154
|
+
* // User types 'jav' - even more specific
|
|
155
|
+
* const javResults = searchIndex.getWords('jav');
|
|
156
|
+
* console.log(javResults); // contains 'javascript';
|
|
157
|
+
* console.log(javResults); // contains 'java';
|
|
158
|
+
* console.log(javResults.length); // 2;
|
|
159
|
+
*
|
|
160
|
+
* // Check for common prefix
|
|
161
|
+
*
|
|
162
|
+
* console.log(searchIndex.hasCommonPrefix('ja')); // false; // Not all words start with 'ja'
|
|
163
|
+
*
|
|
164
|
+
* // Total words in index
|
|
165
|
+
* console.log(searchIndex.size); // 8;
|
|
166
|
+
*
|
|
167
|
+
* // Get height (depth of tree)
|
|
168
|
+
* const height = searchIndex.getHeight();
|
|
169
|
+
* console.log(typeof height); // 'number';
|
|
124
170
|
* @example
|
|
125
171
|
* // Dictionary: Case-insensitive word lookup
|
|
126
|
-
*
|
|
172
|
+
* // Create a case-insensitive dictionary
|
|
127
173
|
* const dictionary = new Trie<string>([], { caseSensitive: false });
|
|
128
174
|
*
|
|
129
175
|
* // Add words with mixed casing
|
|
@@ -132,14 +178,30 @@ export declare class TrieNode {
|
|
|
132
178
|
* dictionary.add('JavaScript');
|
|
133
179
|
*
|
|
134
180
|
* // Test lookups with different casings
|
|
135
|
-
* console.log(dictionary.has('hello')); // true
|
|
136
|
-
* console.log(dictionary.has('HELLO')); // true
|
|
137
|
-
* console.log(dictionary.has('Hello')); // true
|
|
138
|
-
* console.log(dictionary.has('javascript')); // true
|
|
139
|
-
* console.log(dictionary.has('JAVASCRIPT')); // true
|
|
181
|
+
* console.log(dictionary.has('hello')); // true;
|
|
182
|
+
* console.log(dictionary.has('HELLO')); // true;
|
|
183
|
+
* console.log(dictionary.has('Hello')); // true;
|
|
184
|
+
* console.log(dictionary.has('javascript')); // true;
|
|
185
|
+
* console.log(dictionary.has('JAVASCRIPT')); // true;
|
|
186
|
+
* @example
|
|
187
|
+
* // File System Path Operations
|
|
188
|
+
* const fileSystem = new Trie<string>([
|
|
189
|
+
* '/home/user/documents/file1.txt',
|
|
190
|
+
* '/home/user/documents/file2.txt',
|
|
191
|
+
* '/home/user/pictures/photo.jpg',
|
|
192
|
+
* '/home/user/pictures/vacation/',
|
|
193
|
+
* '/home/user/downloads'
|
|
194
|
+
* ]);
|
|
195
|
+
*
|
|
196
|
+
* // Find common directory prefix
|
|
197
|
+
* console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/';
|
|
198
|
+
*
|
|
199
|
+
* // List all files in a directory
|
|
200
|
+
* const documentsFiles = fileSystem.getWords('/home/user/documents/');
|
|
201
|
+
* console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt'];
|
|
140
202
|
* @example
|
|
141
203
|
* // IP Address Routing Table
|
|
142
|
-
*
|
|
204
|
+
* // Add IP address prefixes and their corresponding routes
|
|
143
205
|
* const routes = {
|
|
144
206
|
* '192.168.1': 'LAN_SUBNET_1',
|
|
145
207
|
* '192.168.2': 'LAN_SUBNET_2',
|
|
@@ -150,13 +212,13 @@ export declare class TrieNode {
|
|
|
150
212
|
* const ipRoutingTable = new Trie<string>(Object.keys(routes));
|
|
151
213
|
*
|
|
152
214
|
* // Check IP address prefix matching
|
|
153
|
-
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
|
|
154
|
-
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
|
|
215
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true;
|
|
216
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true;
|
|
155
217
|
*
|
|
156
218
|
* // Validate IP address belongs to subnet
|
|
157
219
|
* const ip = '192.168.1.100';
|
|
158
220
|
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
159
|
-
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
221
|
+
* console.log(ipRoutingTable.hasPrefix(subnet)); // true;
|
|
160
222
|
*/
|
|
161
223
|
export declare class Trie<R = any> extends IterableElementBase<string, R> {
|
|
162
224
|
/**
|
|
@@ -17,6 +17,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
|
|
|
17
17
|
createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
|
|
18
18
|
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
|
|
19
19
|
add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
20
|
+
set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
|
|
20
21
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
21
22
|
delete(keyNodeEntryRawOrPredicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
|
|
22
23
|
clear(): void;
|
|
@@ -1477,6 +1477,17 @@ var redBlackTreeTyped = (() => {
|
|
|
1477
1477
|
}
|
|
1478
1478
|
return false;
|
|
1479
1479
|
}
|
|
1480
|
+
/**
|
|
1481
|
+
* Adds or updates a new node to the tree.
|
|
1482
|
+
* @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).
|
|
1483
|
+
*
|
|
1484
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
1485
|
+
* @param [value] - The value, if providing just a key.
|
|
1486
|
+
* @returns True if the addition was successful, false otherwise.
|
|
1487
|
+
*/
|
|
1488
|
+
set(keyNodeOrEntry, value) {
|
|
1489
|
+
return this.add(keyNodeOrEntry, value);
|
|
1490
|
+
}
|
|
1480
1491
|
/**
|
|
1481
1492
|
* Adds multiple items to the tree.
|
|
1482
1493
|
* @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).
|
|
@@ -1504,6 +1515,17 @@ var redBlackTreeTyped = (() => {
|
|
|
1504
1515
|
}
|
|
1505
1516
|
return inserted;
|
|
1506
1517
|
}
|
|
1518
|
+
/**
|
|
1519
|
+
* Adds or updates multiple items to the tree.
|
|
1520
|
+
* @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).
|
|
1521
|
+
*
|
|
1522
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
1523
|
+
* @param [values] - An optional parallel iterable of values.
|
|
1524
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
1525
|
+
*/
|
|
1526
|
+
setMany(keysNodesEntriesOrRaws, values) {
|
|
1527
|
+
return this.addMany(keysNodesEntriesOrRaws, values);
|
|
1528
|
+
}
|
|
1507
1529
|
/**
|
|
1508
1530
|
* Merges another tree into this one by adding all its nodes.
|
|
1509
1531
|
* @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`).
|
|
@@ -3193,6 +3215,32 @@ var redBlackTreeTyped = (() => {
|
|
|
3193
3215
|
else _iterate();
|
|
3194
3216
|
return inserted;
|
|
3195
3217
|
}
|
|
3218
|
+
/**
|
|
3219
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
3220
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
3221
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3222
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3223
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3224
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3225
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3226
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
3227
|
+
*/
|
|
3228
|
+
lowerBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3229
|
+
return this._bound(keyNodeEntryOrPredicate, true, iterationType);
|
|
3230
|
+
}
|
|
3231
|
+
/**
|
|
3232
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
3233
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
3234
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
3235
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
3236
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
3237
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3238
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
3239
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
3240
|
+
*/
|
|
3241
|
+
upperBound(keyNodeEntryOrPredicate, iterationType = this.iterationType) {
|
|
3242
|
+
return this._bound(keyNodeEntryOrPredicate, false, iterationType);
|
|
3243
|
+
}
|
|
3196
3244
|
/**
|
|
3197
3245
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
3198
3246
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -3353,6 +3401,122 @@ var redBlackTreeTyped = (() => {
|
|
|
3353
3401
|
}
|
|
3354
3402
|
return false;
|
|
3355
3403
|
}
|
|
3404
|
+
/**
|
|
3405
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
3406
|
+
* Unified logic for both lowerBound and upperBound.
|
|
3407
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
3408
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
3409
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3410
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3411
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
3412
|
+
*/
|
|
3413
|
+
_bound(keyNodeEntryOrPredicate, isLower, iterationType) {
|
|
3414
|
+
if (keyNodeEntryOrPredicate === null || keyNodeEntryOrPredicate === void 0) {
|
|
3415
|
+
return void 0;
|
|
3416
|
+
}
|
|
3417
|
+
if (this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
3418
|
+
return this._boundByPredicate(keyNodeEntryOrPredicate, iterationType);
|
|
3419
|
+
}
|
|
3420
|
+
let targetKey;
|
|
3421
|
+
if (this.isNode(keyNodeEntryOrPredicate)) {
|
|
3422
|
+
targetKey = keyNodeEntryOrPredicate.key;
|
|
3423
|
+
} else if (this.isEntry(keyNodeEntryOrPredicate)) {
|
|
3424
|
+
const key = keyNodeEntryOrPredicate[0];
|
|
3425
|
+
if (key === null || key === void 0) {
|
|
3426
|
+
return void 0;
|
|
3427
|
+
}
|
|
3428
|
+
targetKey = key;
|
|
3429
|
+
} else {
|
|
3430
|
+
targetKey = keyNodeEntryOrPredicate;
|
|
3431
|
+
}
|
|
3432
|
+
if (targetKey !== void 0) {
|
|
3433
|
+
return this._boundByKey(targetKey, isLower, iterationType);
|
|
3434
|
+
}
|
|
3435
|
+
return void 0;
|
|
3436
|
+
}
|
|
3437
|
+
/**
|
|
3438
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
3439
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
3440
|
+
* For lowerBound: finds first node where key >= target.
|
|
3441
|
+
* For upperBound: finds first node where key > target.
|
|
3442
|
+
* @param key - The target key to search for.
|
|
3443
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
3444
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3445
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
3446
|
+
*/
|
|
3447
|
+
_boundByKey(key, isLower, iterationType) {
|
|
3448
|
+
var _a, _b;
|
|
3449
|
+
if (iterationType === "RECURSIVE") {
|
|
3450
|
+
const dfs = (cur) => {
|
|
3451
|
+
if (!this.isRealNode(cur)) return void 0;
|
|
3452
|
+
const cmp = this.comparator(cur.key, key);
|
|
3453
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3454
|
+
if (condition) {
|
|
3455
|
+
const leftResult = dfs(cur.left);
|
|
3456
|
+
return leftResult != null ? leftResult : cur;
|
|
3457
|
+
} else {
|
|
3458
|
+
return dfs(cur.right);
|
|
3459
|
+
}
|
|
3460
|
+
};
|
|
3461
|
+
return dfs(this.root);
|
|
3462
|
+
} else {
|
|
3463
|
+
let current = this.root;
|
|
3464
|
+
let result = void 0;
|
|
3465
|
+
while (this.isRealNode(current)) {
|
|
3466
|
+
const cmp = this.comparator(current.key, key);
|
|
3467
|
+
const condition = isLower ? cmp >= 0 : cmp > 0;
|
|
3468
|
+
if (condition) {
|
|
3469
|
+
result = current;
|
|
3470
|
+
current = (_a = current.left) != null ? _a : void 0;
|
|
3471
|
+
} else {
|
|
3472
|
+
current = (_b = current.right) != null ? _b : void 0;
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
return result;
|
|
3476
|
+
}
|
|
3477
|
+
}
|
|
3478
|
+
/**
|
|
3479
|
+
* (Protected) In-order traversal search by predicate.
|
|
3480
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
3481
|
+
* Returns the first node that satisfies the predicate function.
|
|
3482
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
3483
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
3484
|
+
* @param predicate - The predicate function to test nodes.
|
|
3485
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
3486
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
3487
|
+
*/
|
|
3488
|
+
_boundByPredicate(predicate, iterationType) {
|
|
3489
|
+
if (iterationType === "RECURSIVE") {
|
|
3490
|
+
let result = void 0;
|
|
3491
|
+
const dfs = (cur) => {
|
|
3492
|
+
if (result || !this.isRealNode(cur)) return;
|
|
3493
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
3494
|
+
if (!result && predicate(cur)) {
|
|
3495
|
+
result = cur;
|
|
3496
|
+
}
|
|
3497
|
+
if (!result && this.isRealNode(cur.right)) dfs(cur.right);
|
|
3498
|
+
};
|
|
3499
|
+
dfs(this.root);
|
|
3500
|
+
return result;
|
|
3501
|
+
} else {
|
|
3502
|
+
const stack = [];
|
|
3503
|
+
let current = this.root;
|
|
3504
|
+
while (stack.length > 0 || this.isRealNode(current)) {
|
|
3505
|
+
if (this.isRealNode(current)) {
|
|
3506
|
+
stack.push(current);
|
|
3507
|
+
current = current.left;
|
|
3508
|
+
} else {
|
|
3509
|
+
const node = stack.pop();
|
|
3510
|
+
if (!this.isRealNode(node)) break;
|
|
3511
|
+
if (predicate(node)) {
|
|
3512
|
+
return node;
|
|
3513
|
+
}
|
|
3514
|
+
current = node.right;
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
return void 0;
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3356
3520
|
/**
|
|
3357
3521
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
3358
3522
|
* @remarks Time O(1)
|