max-priority-queue-typed 2.2.2 → 2.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- 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/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST } from './bst';
|
|
9
9
|
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
10
|
-
import { BSTOptions } from '../../types';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
/**
|
|
13
12
|
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
@@ -128,8 +127,102 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
128
127
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
129
128
|
*
|
|
130
129
|
* @example
|
|
130
|
+
* // basic AVLTree creation and add operation
|
|
131
|
+
* // Create a simple AVLTree with initial values
|
|
132
|
+
* const tree = new AVLTree([5, 2, 8, 1, 9]);
|
|
133
|
+
*
|
|
134
|
+
* tree.print();
|
|
135
|
+
* // _2___
|
|
136
|
+
* // / \
|
|
137
|
+
* // 1 _8_
|
|
138
|
+
* // / \
|
|
139
|
+
* // 5 9
|
|
140
|
+
*
|
|
141
|
+
* // Verify the tree maintains sorted order
|
|
142
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
143
|
+
*
|
|
144
|
+
* // Check size
|
|
145
|
+
* console.log(tree.size); // 5;
|
|
146
|
+
*
|
|
147
|
+
* // Add a new element
|
|
148
|
+
* tree.add(3);
|
|
149
|
+
* console.log(tree.size); // 6;
|
|
150
|
+
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
151
|
+
* @example
|
|
152
|
+
* // AVLTree has and get operations
|
|
153
|
+
* const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
154
|
+
*
|
|
155
|
+
* // Check if element exists
|
|
156
|
+
* console.log(tree.has(6)); // true;
|
|
157
|
+
* console.log(tree.has(99)); // false;
|
|
158
|
+
*
|
|
159
|
+
* // Get node by key
|
|
160
|
+
* const node = tree.getNode(6);
|
|
161
|
+
* console.log(node?.key); // 6;
|
|
162
|
+
*
|
|
163
|
+
* // Verify tree is balanced
|
|
164
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
165
|
+
* @example
|
|
166
|
+
* // AVLTree delete and balance verification
|
|
167
|
+
* const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
168
|
+
*
|
|
169
|
+
* // Delete an element
|
|
170
|
+
* tree.delete(10);
|
|
171
|
+
* console.log(tree.has(10)); // false;
|
|
172
|
+
*
|
|
173
|
+
* // Tree should remain balanced after deletion
|
|
174
|
+
* console.log(tree.isAVLBalanced()); // true;
|
|
175
|
+
*
|
|
176
|
+
* // Size decreased
|
|
177
|
+
* console.log(tree.size); // 15;
|
|
178
|
+
*
|
|
179
|
+
* // Remaining elements are still sorted
|
|
180
|
+
* const keys = [...tree.keys()];
|
|
181
|
+
* console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
|
|
182
|
+
* @example
|
|
183
|
+
* // AVLTree for university ranking system with strict balance
|
|
184
|
+
* interface University {
|
|
185
|
+
* name: string;
|
|
186
|
+
* rank: number;
|
|
187
|
+
* students: number;
|
|
188
|
+
* }
|
|
189
|
+
*
|
|
190
|
+
* // AVLTree provides highest search efficiency with strict balance
|
|
191
|
+
* // (every node's left/right subtrees differ by at most 1 in height)
|
|
192
|
+
* const universityTree = new AVLTree<number, University>([
|
|
193
|
+
* [1, { name: 'MIT', rank: 1, students: 1200 }],
|
|
194
|
+
* [5, { name: 'Stanford', rank: 5, students: 1800 }],
|
|
195
|
+
* [3, { name: 'Harvard', rank: 3, students: 2300 }],
|
|
196
|
+
* [2, { name: 'Caltech', rank: 2, students: 400 }],
|
|
197
|
+
* [4, { name: 'CMU', rank: 4, students: 1500 }]
|
|
198
|
+
* ]);
|
|
199
|
+
*
|
|
200
|
+
* // Quick lookup by rank
|
|
201
|
+
* const mit = universityTree.get(1);
|
|
202
|
+
* console.log(mit?.name); // 'MIT';
|
|
203
|
+
*
|
|
204
|
+
* const cmulevel = universityTree.getHeight(4);
|
|
205
|
+
* console.log(typeof cmulevel); // 'number';
|
|
206
|
+
*
|
|
207
|
+
* // Tree maintains strict balance during insertions and deletions
|
|
208
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
209
|
+
*
|
|
210
|
+
* // Add more universities
|
|
211
|
+
* universityTree.add(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
212
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
213
|
+
*
|
|
214
|
+
* // Delete and verify balance is maintained
|
|
215
|
+
* universityTree.delete(2);
|
|
216
|
+
* console.log(universityTree.has(2)); // false;
|
|
217
|
+
* console.log(universityTree.isAVLBalanced()); // true;
|
|
218
|
+
*
|
|
219
|
+
* // Get all remaining universities in rank order
|
|
220
|
+
* const remainingRanks = [...universityTree.keys()];
|
|
221
|
+
* console.log(remainingRanks); // [1, 3, 4, 5, 6];
|
|
222
|
+
* console.log(universityTree.size); // 5;
|
|
223
|
+
* @example
|
|
131
224
|
* // Find elements in a range
|
|
132
|
-
*
|
|
225
|
+
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
133
226
|
* type Datum = { timestamp: Date; temperature: number };
|
|
134
227
|
* // Fixed dataset of CPU temperature readings
|
|
135
228
|
* const cpuData: Datum[] = [
|
|
@@ -190,7 +283,7 @@ export declare class AVLTreeNode<K = any, V = any> {
|
|
|
190
283
|
* // { minute: 13, temperature: 60.2 },
|
|
191
284
|
* // { minute: 14, temperature: 59.8 },
|
|
192
285
|
* // { minute: 15, temperature: 58.6 }
|
|
193
|
-
* // ]
|
|
286
|
+
* // ];
|
|
194
287
|
*/
|
|
195
288
|
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
196
289
|
/**
|
|
@@ -265,7 +358,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
265
358
|
* @param [options] - Options for the new tree.
|
|
266
359
|
* @returns A new, empty tree.
|
|
267
360
|
*/
|
|
268
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
361
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
|
|
269
362
|
/**
|
|
270
363
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
271
364
|
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
@@ -275,7 +368,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
275
368
|
* @param [options] - Options for the new tree.
|
|
276
369
|
* @returns A new AVLTree.
|
|
277
370
|
*/
|
|
278
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
371
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
279
372
|
/**
|
|
280
373
|
* (Protected) Swaps properties of two nodes, including height.
|
|
281
374
|
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
@@ -125,8 +125,86 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
125
125
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
126
126
|
*
|
|
127
127
|
* @example
|
|
128
|
+
* // basic BinaryTree creation and insertion
|
|
129
|
+
* // Create a BinaryTree with entries
|
|
130
|
+
* const entries: [number, string][] = [
|
|
131
|
+
* [6, 'six'],
|
|
132
|
+
* [1, 'one'],
|
|
133
|
+
* [2, 'two'],
|
|
134
|
+
* [7, 'seven'],
|
|
135
|
+
* [5, 'five'],
|
|
136
|
+
* [3, 'three'],
|
|
137
|
+
* [4, 'four'],
|
|
138
|
+
* [9, 'nine'],
|
|
139
|
+
* [8, 'eight']
|
|
140
|
+
* ];
|
|
141
|
+
*
|
|
142
|
+
* const tree = new BinaryTree(entries);
|
|
143
|
+
*
|
|
144
|
+
* // Verify size
|
|
145
|
+
* console.log(tree.size); // 9;
|
|
146
|
+
*
|
|
147
|
+
* // Add new element
|
|
148
|
+
* tree.add(10, 'ten');
|
|
149
|
+
* console.log(tree.size); // 10;
|
|
150
|
+
* @example
|
|
151
|
+
* // BinaryTree get and has operations
|
|
152
|
+
* const tree = new BinaryTree(
|
|
153
|
+
* [
|
|
154
|
+
* [5, 'five'],
|
|
155
|
+
* [3, 'three'],
|
|
156
|
+
* [7, 'seven'],
|
|
157
|
+
* [1, 'one'],
|
|
158
|
+
* [4, 'four'],
|
|
159
|
+
* [6, 'six'],
|
|
160
|
+
* [8, 'eight']
|
|
161
|
+
* ],
|
|
162
|
+
* { isMapMode: false }
|
|
163
|
+
* );
|
|
164
|
+
*
|
|
165
|
+
* // Check if key exists
|
|
166
|
+
* console.log(tree.has(5)); // true;
|
|
167
|
+
* console.log(tree.has(10)); // false;
|
|
168
|
+
*
|
|
169
|
+
* // Get value by key
|
|
170
|
+
* console.log(tree.get(3)); // 'three';
|
|
171
|
+
* console.log(tree.get(7)); // 'seven';
|
|
172
|
+
* console.log(tree.get(100)); // undefined;
|
|
173
|
+
*
|
|
174
|
+
* // Get node structure
|
|
175
|
+
* const node = tree.getNode(5);
|
|
176
|
+
* console.log(node?.key); // 5;
|
|
177
|
+
* console.log(node?.value); // 'five';
|
|
178
|
+
* @example
|
|
179
|
+
* // BinaryTree level-order traversal
|
|
180
|
+
* const tree = new BinaryTree([
|
|
181
|
+
* [1, 'one'],
|
|
182
|
+
* [2, 'two'],
|
|
183
|
+
* [3, 'three'],
|
|
184
|
+
* [4, 'four'],
|
|
185
|
+
* [5, 'five'],
|
|
186
|
+
* [6, 'six'],
|
|
187
|
+
* [7, 'seven']
|
|
188
|
+
* ]);
|
|
189
|
+
*
|
|
190
|
+
* // Binary tree maintains level-order insertion
|
|
191
|
+
* // Complete binary tree structure
|
|
192
|
+
* console.log(tree.size); // 7;
|
|
193
|
+
*
|
|
194
|
+
* // Verify all keys are present
|
|
195
|
+
* console.log(tree.has(1)); // true;
|
|
196
|
+
* console.log(tree.has(4)); // true;
|
|
197
|
+
* console.log(tree.has(7)); // true;
|
|
198
|
+
*
|
|
199
|
+
* // Iterate through tree
|
|
200
|
+
* const keys: number[] = [];
|
|
201
|
+
* for (const [key] of tree) {
|
|
202
|
+
* keys.push(key);
|
|
203
|
+
* }
|
|
204
|
+
* console.log(keys.length); // 7;
|
|
205
|
+
* @example
|
|
128
206
|
* // determine loan approval using a decision tree
|
|
129
|
-
*
|
|
207
|
+
* // Decision tree structure
|
|
130
208
|
* const loanDecisionTree = new BinaryTree<string>(
|
|
131
209
|
* ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
|
|
132
210
|
* { isDuplicate: true }
|
|
@@ -148,19 +226,19 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
148
226
|
* }
|
|
149
227
|
*
|
|
150
228
|
* // Test case 1: Stable income and good credit score
|
|
151
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
|
|
229
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved';
|
|
152
230
|
*
|
|
153
231
|
* // Test case 2: Stable income but poor credit score
|
|
154
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
|
|
232
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected';
|
|
155
233
|
*
|
|
156
234
|
* // Test case 3: No stable income
|
|
157
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
|
|
235
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected';
|
|
158
236
|
*
|
|
159
237
|
* // Test case 4: No stable income and poor credit score
|
|
160
|
-
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
|
|
238
|
+
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected';
|
|
161
239
|
* @example
|
|
162
240
|
* // evaluate the arithmetic expression represented by the binary tree
|
|
163
|
-
*
|
|
241
|
+
* const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
|
|
164
242
|
*
|
|
165
243
|
* function evaluate(node?: BinaryTreeNode<number | string> | null): number {
|
|
166
244
|
* if (!node) return 0;
|
|
@@ -185,7 +263,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
185
263
|
* }
|
|
186
264
|
* }
|
|
187
265
|
*
|
|
188
|
-
* console.log(evaluate(expressionTree.root)); // -27
|
|
266
|
+
* console.log(evaluate(expressionTree.root)); // -27;
|
|
189
267
|
*/
|
|
190
268
|
export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R> {
|
|
191
269
|
iterationType: IterationType;
|
|
@@ -360,6 +438,15 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
360
438
|
* @returns True if the addition was successful, false otherwise.
|
|
361
439
|
*/
|
|
362
440
|
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
441
|
+
/**
|
|
442
|
+
* Adds or updates a new node to the tree.
|
|
443
|
+
* @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).
|
|
444
|
+
*
|
|
445
|
+
* @param keyNodeOrEntry - The key, node, or entry to add or update.
|
|
446
|
+
* @param [value] - The value, if providing just a key.
|
|
447
|
+
* @returns True if the addition was successful, false otherwise.
|
|
448
|
+
*/
|
|
449
|
+
set(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
363
450
|
/**
|
|
364
451
|
* Adds multiple items to the tree.
|
|
365
452
|
* @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).
|
|
@@ -369,6 +456,15 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
369
456
|
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
370
457
|
*/
|
|
371
458
|
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
459
|
+
/**
|
|
460
|
+
* Adds or updates multiple items to the tree.
|
|
461
|
+
* @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).
|
|
462
|
+
*
|
|
463
|
+
* @param keysNodesEntriesOrRaws - An iterable of items to add or update.
|
|
464
|
+
* @param [values] - An optional parallel iterable of values.
|
|
465
|
+
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
466
|
+
*/
|
|
467
|
+
setMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
|
|
372
468
|
/**
|
|
373
469
|
* Merges another tree into this one by adding all its nodes.
|
|
374
470
|
* @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`).
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, CP, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
|
|
9
9
|
import { BinaryTree } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -126,8 +126,57 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
126
126
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
127
127
|
*
|
|
128
128
|
* @example
|
|
129
|
+
* // basic BST creation and add operation
|
|
130
|
+
* // Create a simple BST with numeric keys
|
|
131
|
+
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
132
|
+
*
|
|
133
|
+
* bst.print();
|
|
134
|
+
* // _______8__________
|
|
135
|
+
* // / \
|
|
136
|
+
* // ___4___ ____12_____
|
|
137
|
+
* // / \ / \
|
|
138
|
+
* // _2_ _6_ _10__ _14__
|
|
139
|
+
* // / \ / \ / \ / \
|
|
140
|
+
* // 1 3 5 7 9 11 13 15__
|
|
141
|
+
* // \
|
|
142
|
+
* // 16
|
|
143
|
+
*
|
|
144
|
+
* // Verify size
|
|
145
|
+
* console.log(bst.size); // 16;
|
|
146
|
+
*
|
|
147
|
+
* // Add new elements
|
|
148
|
+
* bst.add(17);
|
|
149
|
+
* bst.add(0);
|
|
150
|
+
* console.log(bst.size); // 18;
|
|
151
|
+
*
|
|
152
|
+
* // Verify keys are searchable
|
|
153
|
+
* console.log(bst.has(11)); // true;
|
|
154
|
+
* console.log(bst.has(100)); // false;
|
|
155
|
+
* @example
|
|
156
|
+
* // BST delete and search after deletion
|
|
157
|
+
* const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
158
|
+
*
|
|
159
|
+
* // Delete a leaf node
|
|
160
|
+
* bst.delete(1);
|
|
161
|
+
* console.log(bst.has(1)); // false;
|
|
162
|
+
*
|
|
163
|
+
* // Delete a node with one child
|
|
164
|
+
* bst.delete(2);
|
|
165
|
+
* console.log(bst.has(2)); // false;
|
|
166
|
+
*
|
|
167
|
+
* // Delete a node with two children
|
|
168
|
+
* bst.delete(3);
|
|
169
|
+
* console.log(bst.has(3)); // false;
|
|
170
|
+
*
|
|
171
|
+
* // Size decreases with each deletion
|
|
172
|
+
* console.log(bst.size); // 13;
|
|
173
|
+
*
|
|
174
|
+
* // Other nodes remain searchable
|
|
175
|
+
* console.log(bst.has(11)); // true;
|
|
176
|
+
* console.log(bst.has(15)); // true;
|
|
177
|
+
* @example
|
|
129
178
|
* // Merge 3 sorted datasets
|
|
130
|
-
*
|
|
179
|
+
* const dataset1 = new BST<number, string>([
|
|
131
180
|
* [1, 'A'],
|
|
132
181
|
* [7, 'G']
|
|
133
182
|
* ]);
|
|
@@ -147,18 +196,58 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
147
196
|
* merged.merge(dataset3);
|
|
148
197
|
*
|
|
149
198
|
* // Verify merged dataset is in sorted order
|
|
150
|
-
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
|
|
199
|
+
* console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
|
|
151
200
|
* @example
|
|
152
|
-
* //
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
201
|
+
* // BST with custom objects for expression evaluation
|
|
202
|
+
* interface Expression {
|
|
203
|
+
* id: number;
|
|
204
|
+
* operator: string;
|
|
205
|
+
* precedence: number;
|
|
206
|
+
* }
|
|
207
|
+
*
|
|
208
|
+
* // BST efficiently stores and retrieves operators by precedence
|
|
209
|
+
* const operatorTree = new BST<number, Expression>(
|
|
210
|
+
* [
|
|
211
|
+
* [1, { id: 1, operator: '+', precedence: 1 }],
|
|
212
|
+
* [2, { id: 2, operator: '*', precedence: 2 }],
|
|
213
|
+
* [3, { id: 3, operator: '/', precedence: 2 }],
|
|
214
|
+
* [4, { id: 4, operator: '-', precedence: 1 }],
|
|
215
|
+
* [5, { id: 5, operator: '^', precedence: 3 }]
|
|
216
|
+
* ],
|
|
217
|
+
* { isMapMode: false }
|
|
218
|
+
* );
|
|
219
|
+
*
|
|
220
|
+
* console.log(operatorTree.size); // 5;
|
|
221
|
+
*
|
|
222
|
+
* // Quick lookup of operators
|
|
223
|
+
* const mult = operatorTree.get(2);
|
|
224
|
+
* console.log(mult?.operator); // '*';
|
|
225
|
+
* console.log(mult?.precedence); // 2;
|
|
226
|
+
*
|
|
227
|
+
* // Check if operator exists
|
|
228
|
+
* console.log(operatorTree.has(5)); // true;
|
|
229
|
+
* console.log(operatorTree.has(99)); // false;
|
|
230
|
+
*
|
|
231
|
+
* // Retrieve operator by precedence level
|
|
232
|
+
* const expNode = operatorTree.getNode(3);
|
|
233
|
+
* console.log(expNode?.key); // 3;
|
|
234
|
+
* console.log(expNode?.value?.precedence); // 2;
|
|
235
|
+
*
|
|
236
|
+
* // Delete operator and verify
|
|
237
|
+
* operatorTree.delete(1);
|
|
238
|
+
* console.log(operatorTree.has(1)); // false;
|
|
239
|
+
* console.log(operatorTree.size); // 4;
|
|
240
|
+
*
|
|
241
|
+
* // Get tree height for optimization analysis
|
|
242
|
+
* const treeHeight = operatorTree.getHeight();
|
|
243
|
+
* console.log(treeHeight); // > 0;
|
|
244
|
+
*
|
|
245
|
+
* // Remaining operators are still accessible
|
|
246
|
+
* const remaining = operatorTree.get(2);
|
|
247
|
+
* console.log(remaining); // defined;
|
|
159
248
|
* @example
|
|
160
249
|
* // Find lowest common ancestor
|
|
161
|
-
*
|
|
250
|
+
* const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
162
251
|
*
|
|
163
252
|
* // LCA helper function
|
|
164
253
|
* const findLCA = (num1: number, num2: number): number | undefined => {
|
|
@@ -178,9 +267,9 @@ export declare class BSTNode<K = any, V = any> {
|
|
|
178
267
|
* }
|
|
179
268
|
*
|
|
180
269
|
* // Assertions
|
|
181
|
-
* console.log(findLCA(3, 10)); // 7
|
|
182
|
-
* console.log(findLCA(5, 35)); // 15
|
|
183
|
-
* console.log(findLCA(20, 30)); // 25
|
|
270
|
+
* console.log(findLCA(3, 10)); // 7;
|
|
271
|
+
* console.log(findLCA(5, 35)); // 15;
|
|
272
|
+
* console.log(findLCA(20, 30)); // 25;
|
|
184
273
|
*/
|
|
185
274
|
export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
186
275
|
/**
|
|
@@ -190,7 +279,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
190
279
|
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
191
280
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
192
281
|
*/
|
|
193
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode
|
|
282
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
194
283
|
protected _root?: BSTNode<K, V>;
|
|
195
284
|
/**
|
|
196
285
|
* Gets the root node of the tree.
|
|
@@ -199,17 +288,16 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
199
288
|
* @returns The root node.
|
|
200
289
|
*/
|
|
201
290
|
get root(): OptNode<BSTNode<K, V>>;
|
|
202
|
-
protected _isReverse: boolean;
|
|
203
291
|
/**
|
|
204
|
-
*
|
|
205
|
-
* @remarks Time O(1)
|
|
206
|
-
*
|
|
207
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
292
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
293
|
+
* @remarks Time O(1) Space O(1)
|
|
294
|
+
* @returns The default comparator function.
|
|
208
295
|
*/
|
|
209
|
-
|
|
296
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
210
297
|
/**
|
|
211
|
-
* The
|
|
212
|
-
|
|
298
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
299
|
+
|
|
300
|
+
* @remarks Time O(1) Space O(1)
|
|
213
301
|
*/
|
|
214
302
|
protected _comparator: Comparator<K>;
|
|
215
303
|
/**
|
|
@@ -219,14 +307,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
219
307
|
* @returns The comparator function.
|
|
220
308
|
*/
|
|
221
309
|
get comparator(): Comparator<K>;
|
|
222
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
223
|
-
/**
|
|
224
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
225
|
-
* @remarks Time O(1)
|
|
226
|
-
*
|
|
227
|
-
* @returns The key-to-comparable conversion function.
|
|
228
|
-
*/
|
|
229
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
230
310
|
/**
|
|
231
311
|
* (Protected) Creates a new BST node.
|
|
232
312
|
* @remarks Time O(1), Space O(1)
|
|
@@ -355,6 +435,28 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
355
435
|
* @returns An array of booleans indicating the success of each individual `add` operation.
|
|
356
436
|
*/
|
|
357
437
|
addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
438
|
+
/**
|
|
439
|
+
* Returns the first node with a key greater than or equal to the given key.
|
|
440
|
+
* This is equivalent to C++ std::lower_bound on a BST.
|
|
441
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
442
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
443
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
444
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
445
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
446
|
+
* @returns The first node with key >= given key, or undefined if no such node exists.
|
|
447
|
+
*/
|
|
448
|
+
lowerBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
449
|
+
/**
|
|
450
|
+
* Returns the first node with a key strictly greater than the given key.
|
|
451
|
+
* This is equivalent to C++ std::upper_bound on a BST.
|
|
452
|
+
* Supports RECURSIVE and ITERATIVE implementations.
|
|
453
|
+
* Time Complexity: O(log n) on average, O(h) where h is tree height.
|
|
454
|
+
* Space Complexity: O(h) for recursion, O(1) for iteration.
|
|
455
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
456
|
+
* @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
|
|
457
|
+
* @returns The first node with key > given key, or undefined if no such node exists.
|
|
458
|
+
*/
|
|
459
|
+
upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
|
|
358
460
|
/**
|
|
359
461
|
* Traverses the tree and returns nodes that are lesser or greater than a target node.
|
|
360
462
|
* @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
|
|
@@ -396,15 +498,76 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
396
498
|
* @param [thisArg] - `this` context for the callback.
|
|
397
499
|
* @returns A new, mapped BST.
|
|
398
500
|
*/
|
|
399
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
501
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BSTOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
|
|
400
502
|
/**
|
|
401
|
-
* Deletes
|
|
402
|
-
*
|
|
503
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
504
|
+
*
|
|
505
|
+
* @remarks
|
|
506
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
507
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
508
|
+
*
|
|
509
|
+
* @template K - The key type.
|
|
510
|
+
* @template V - The value type.
|
|
511
|
+
*
|
|
512
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
513
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
514
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
515
|
+
* - An entry tuple: searches for the key-value pair.
|
|
516
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
517
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
518
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
403
519
|
*
|
|
404
|
-
* @param
|
|
405
|
-
*
|
|
520
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
521
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
522
|
+
*
|
|
523
|
+
* @param startNode - The node to start the search from. Can be:
|
|
524
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
525
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
526
|
+
* - Default value: this._root (the tree's root).
|
|
527
|
+
*
|
|
528
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
529
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
530
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
531
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
532
|
+
*
|
|
533
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
534
|
+
* - Key: the matched node's key.
|
|
535
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
536
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
406
537
|
*/
|
|
407
|
-
deleteWhere(
|
|
538
|
+
deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
|
|
539
|
+
/**
|
|
540
|
+
* (Protected) Core bound search implementation supporting all parameter types.
|
|
541
|
+
* Unified logic for both lowerBound and upperBound.
|
|
542
|
+
* Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
|
|
543
|
+
* @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
|
|
544
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
545
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
546
|
+
* @returns The first matching node, or undefined if no such node exists.
|
|
547
|
+
*/
|
|
548
|
+
protected _bound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
549
|
+
/**
|
|
550
|
+
* (Protected) Binary search for bound by key with pruning optimization.
|
|
551
|
+
* Performs standard BST binary search, choosing left or right subtree based on comparator result.
|
|
552
|
+
* For lowerBound: finds first node where key >= target.
|
|
553
|
+
* For upperBound: finds first node where key > target.
|
|
554
|
+
* @param key - The target key to search for.
|
|
555
|
+
* @param isLower - True for lowerBound (>=), false for upperBound (>).
|
|
556
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
557
|
+
* @returns The first node matching the bound condition, or undefined if none exists.
|
|
558
|
+
*/
|
|
559
|
+
protected _boundByKey(key: K, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
560
|
+
/**
|
|
561
|
+
* (Protected) In-order traversal search by predicate.
|
|
562
|
+
* Falls back to linear in-order traversal when predicate-based search is required.
|
|
563
|
+
* Returns the first node that satisfies the predicate function.
|
|
564
|
+
* Note: Predicate-based search cannot leverage BST's binary search optimization.
|
|
565
|
+
* Time Complexity: O(n) since it may visit every node.
|
|
566
|
+
* @param predicate - The predicate function to test nodes.
|
|
567
|
+
* @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
|
|
568
|
+
* @returns The first node satisfying predicate, or undefined if none found.
|
|
569
|
+
*/
|
|
570
|
+
protected _boundByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
|
|
408
571
|
/**
|
|
409
572
|
* (Protected) Creates a new, empty instance of the same BST constructor.
|
|
410
573
|
* @remarks Time O(1)
|
|
@@ -450,7 +613,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
450
613
|
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
451
614
|
/**
|
|
452
615
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
453
|
-
* @remarks Time O(1)
|
|
616
|
+
* @remarks Time O(1) Space O(1)
|
|
454
617
|
*
|
|
455
618
|
* @param a - The first key.
|
|
456
619
|
* @param b - The second key.
|
|
@@ -464,5 +627,5 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
464
627
|
* @param key - The key of the node to delete.
|
|
465
628
|
* @returns True if the node was found and deleted, false otherwise.
|
|
466
629
|
*/
|
|
467
|
-
|
|
630
|
+
protected _deleteByKey(key: K): boolean;
|
|
468
631
|
}
|