min-priority-queue-typed 2.2.3 → 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/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/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 +4 -5
- package/src/data-structures/binary-tree/bst.ts +111 -82
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -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 { AVLTreeCounterOptions, BinaryTreeDeleteResult,
|
|
8
|
+
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree } from './avl-tree';
|
|
11
11
|
/**
|
|
@@ -186,7 +186,7 @@ export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K
|
|
|
186
186
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
187
187
|
* @returns A new AVLTreeCounter with mapped entries.
|
|
188
188
|
*/
|
|
189
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
189
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<MK, MV, MR>;
|
|
190
190
|
/**
|
|
191
191
|
* (Protected) Create an empty instance of the same concrete class.
|
|
192
192
|
* @remarks Time O(1), Space O(1)
|
|
@@ -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 { AVLTreeMultiMapOptions,
|
|
8
|
+
import type { AVLTreeMultiMapOptions, ElemOf, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -160,7 +160,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
160
160
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
161
161
|
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
162
162
|
*/
|
|
163
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
163
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
164
164
|
/**
|
|
165
165
|
* Create a new tree by mapping each [key, values] bucket.
|
|
166
166
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -172,7 +172,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
172
172
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
173
173
|
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
174
174
|
*/
|
|
175
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
175
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
176
176
|
/**
|
|
177
177
|
* (Protected) Create an empty instance of the same concrete class.
|
|
178
178
|
* @remarks Time O(1), Space O(1)
|
|
@@ -182,7 +182,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
182
182
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
183
183
|
* @returns An empty like-kind instance.
|
|
184
184
|
*/
|
|
185
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
185
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this;
|
|
186
186
|
/**
|
|
187
187
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
188
188
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -193,5 +193,5 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
193
193
|
* @param [options] - Options merged with the current snapshot.
|
|
194
194
|
* @returns A like-kind AVLTree built from the iterable.
|
|
195
195
|
*/
|
|
196
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
196
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
197
197
|
}
|
|
@@ -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.
|
|
@@ -359,7 +358,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
359
358
|
* @param [options] - Options for the new tree.
|
|
360
359
|
* @returns A new, empty tree.
|
|
361
360
|
*/
|
|
362
|
-
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;
|
|
363
362
|
/**
|
|
364
363
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
365
364
|
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
@@ -369,7 +368,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
369
368
|
* @param [options] - Options for the new tree.
|
|
370
369
|
* @returns A new AVLTree.
|
|
371
370
|
*/
|
|
372
|
-
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>;
|
|
373
372
|
/**
|
|
374
373
|
* (Protected) Swaps properties of two nodes, including height.
|
|
375
374
|
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
@@ -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';
|
|
@@ -279,7 +279,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
279
279
|
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
280
280
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
281
281
|
*/
|
|
282
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode
|
|
282
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
283
283
|
protected _root?: BSTNode<K, V>;
|
|
284
284
|
/**
|
|
285
285
|
* Gets the root node of the tree.
|
|
@@ -288,17 +288,16 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
288
288
|
* @returns The root node.
|
|
289
289
|
*/
|
|
290
290
|
get root(): OptNode<BSTNode<K, V>>;
|
|
291
|
-
protected _isReverse: boolean;
|
|
292
291
|
/**
|
|
293
|
-
*
|
|
294
|
-
* @remarks Time O(1)
|
|
295
|
-
*
|
|
296
|
-
* @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.
|
|
297
295
|
*/
|
|
298
|
-
|
|
296
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
299
297
|
/**
|
|
300
|
-
* The
|
|
301
|
-
|
|
298
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
299
|
+
|
|
300
|
+
* @remarks Time O(1) Space O(1)
|
|
302
301
|
*/
|
|
303
302
|
protected _comparator: Comparator<K>;
|
|
304
303
|
/**
|
|
@@ -308,14 +307,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
308
307
|
* @returns The comparator function.
|
|
309
308
|
*/
|
|
310
309
|
get comparator(): Comparator<K>;
|
|
311
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
312
|
-
/**
|
|
313
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
314
|
-
* @remarks Time O(1)
|
|
315
|
-
*
|
|
316
|
-
* @returns The key-to-comparable conversion function.
|
|
317
|
-
*/
|
|
318
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
319
310
|
/**
|
|
320
311
|
* (Protected) Creates a new BST node.
|
|
321
312
|
* @remarks Time O(1), Space O(1)
|
|
@@ -507,15 +498,44 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
507
498
|
* @param [thisArg] - `this` context for the callback.
|
|
508
499
|
* @returns A new, mapped BST.
|
|
509
500
|
*/
|
|
510
|
-
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>;
|
|
511
502
|
/**
|
|
512
|
-
* Deletes
|
|
513
|
-
*
|
|
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.
|
|
519
|
+
*
|
|
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).
|
|
514
532
|
*
|
|
515
|
-
* @
|
|
516
|
-
*
|
|
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.
|
|
517
537
|
*/
|
|
518
|
-
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>>[];
|
|
519
539
|
/**
|
|
520
540
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
521
541
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -593,7 +613,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
593
613
|
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
594
614
|
/**
|
|
595
615
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
596
|
-
* @remarks Time O(1)
|
|
616
|
+
* @remarks Time O(1) Space O(1)
|
|
597
617
|
*
|
|
598
618
|
* @param a - The first key.
|
|
599
619
|
* @param b - The second key.
|
|
@@ -607,5 +627,5 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
607
627
|
* @param key - The key of the node to delete.
|
|
608
628
|
* @returns True if the node was found and deleted, false otherwise.
|
|
609
629
|
*/
|
|
610
|
-
|
|
630
|
+
protected _deleteByKey(key: K): boolean;
|
|
611
631
|
}
|
|
@@ -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 { BinaryTreeDeleteResult,
|
|
8
|
+
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
@@ -261,7 +261,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
261
261
|
* @param [thisArg] - See parameter type for details.
|
|
262
262
|
* @returns A new RedBlackTree with mapped entries.
|
|
263
263
|
*/
|
|
264
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
264
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
265
265
|
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
266
266
|
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
267
267
|
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult,
|
|
9
|
-
import { BSTOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
10
9
|
import { BSTNode } from './bst';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -188,7 +187,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
188
187
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
189
188
|
* @returns A new TreeCounter with mapped entries.
|
|
190
189
|
*/
|
|
191
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
190
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<TreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): TreeCounter<MK, MV, MR>;
|
|
192
191
|
/**
|
|
193
192
|
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
194
193
|
* @remarks Time O(N), Space O(N)
|
|
@@ -204,7 +203,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
204
203
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
205
204
|
* @returns An empty like-kind instance.
|
|
206
205
|
*/
|
|
207
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
206
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this;
|
|
208
207
|
/**
|
|
209
208
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
210
209
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -215,7 +214,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
215
214
|
* @param [options] - Options merged with the current snapshot.
|
|
216
215
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
217
216
|
*/
|
|
218
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
217
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeCounterOptions<TK, TV, TR>>): TreeCounter<TK, TV, TR>;
|
|
219
218
|
/**
|
|
220
219
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
221
220
|
* @remarks Time O(1), Space O(1)
|
|
@@ -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 { ElemOf, EntryCallback, FamilyPosition, RBTNColor,
|
|
8
|
+
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -307,8 +307,8 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
307
307
|
* @returns True if the value was removed; false if not found.
|
|
308
308
|
*/
|
|
309
309
|
deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
|
|
310
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
311
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
310
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
311
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<TreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
312
312
|
/**
|
|
313
313
|
* (Protected) Create an empty instance of the same concrete class.
|
|
314
314
|
* @remarks Time O(1), Space O(1)
|
|
@@ -318,7 +318,7 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
318
318
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
319
319
|
* @returns An empty like-kind instance.
|
|
320
320
|
*/
|
|
321
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
321
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this;
|
|
322
322
|
/**
|
|
323
323
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
324
324
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -329,5 +329,5 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
329
329
|
* @param [options] - Options merged with the current snapshot.
|
|
330
330
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
331
331
|
*/
|
|
332
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
332
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
333
333
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export type BSTOptions<K, V, R> =
|
|
5
|
-
|
|
6
|
-
isReverse?: boolean;
|
|
2
|
+
import type { Comparator, OptValue } from '../../common';
|
|
3
|
+
type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
|
|
4
|
+
export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
|
|
5
|
+
comparator?: Comparator<K>;
|
|
7
6
|
};
|
|
8
7
|
export type BSTNOptKey<K> = K | undefined;
|
|
9
8
|
export type OptNode<NODE> = NODE | undefined;
|
|
10
9
|
export type BSTNEntry<K, V> = [BSTNOptKey<K>, OptValue<V>];
|
|
11
10
|
export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE;
|
|
12
11
|
export type BSTNRep<K, V, NODE> = BSTNEntry<K, V> | BSTNOptKeyOrNode<K, NODE>;
|
|
12
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-priority-queue-typed",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.4",
|
|
4
4
|
"description": "Min Priority Queue",
|
|
5
5
|
"browser": "dist/umd/max-priority-queue-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/max-priority-queue-typed.min.js",
|
|
@@ -163,6 +163,6 @@
|
|
|
163
163
|
"typescript": "^4.9.5"
|
|
164
164
|
},
|
|
165
165
|
"dependencies": {
|
|
166
|
-
"data-structure-typed": "^2.2.
|
|
166
|
+
"data-structure-typed": "^2.2.4"
|
|
167
167
|
}
|
|
168
168
|
}
|
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
import type {
|
|
10
10
|
AVLTreeCounterOptions,
|
|
11
11
|
BinaryTreeDeleteResult,
|
|
12
|
-
BinaryTreeOptions,
|
|
13
12
|
BSTNOptKeyOrNode,
|
|
14
13
|
EntryCallback,
|
|
15
14
|
FamilyPosition,
|
|
@@ -404,7 +403,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
404
403
|
*/
|
|
405
404
|
override map<MK = K, MV = V, MR = any>(
|
|
406
405
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
407
|
-
options?: Partial<
|
|
406
|
+
options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>,
|
|
408
407
|
thisArg?: unknown
|
|
409
408
|
): AVLTreeCounter<MK, MV, MR> {
|
|
410
409
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
AVLTreeMultiMapOptions,
|
|
11
|
-
AVLTreeOptions,
|
|
12
11
|
BTNOptKeyOrNull,
|
|
13
12
|
ElemOf,
|
|
14
13
|
EntryCallback,
|
|
@@ -354,7 +353,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
354
353
|
*/
|
|
355
354
|
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
356
355
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
357
|
-
options?: Partial<
|
|
356
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>,
|
|
358
357
|
thisArg?: unknown
|
|
359
358
|
): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
360
359
|
|
|
@@ -371,7 +370,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
371
370
|
*/
|
|
372
371
|
override map<MK = K, MV = V[], MR = any>(
|
|
373
372
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
374
|
-
options?: Partial<
|
|
373
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
375
374
|
thisArg?: unknown
|
|
376
375
|
): AVLTree<MK, MV, MR>;
|
|
377
376
|
|
|
@@ -388,7 +387,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
388
387
|
*/
|
|
389
388
|
override map<MK, MV, MR extends object>(
|
|
390
389
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
391
|
-
options?: Partial<
|
|
390
|
+
options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>,
|
|
392
391
|
thisArg?: unknown
|
|
393
392
|
): AVLTree<MK, MV, MR> {
|
|
394
393
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -406,10 +405,10 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
406
405
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
407
406
|
* @returns An empty like-kind instance.
|
|
408
407
|
*/
|
|
409
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
408
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this {
|
|
410
409
|
const Ctor = this.constructor as unknown as new (
|
|
411
410
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
412
|
-
opts?:
|
|
411
|
+
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
413
412
|
) => AVLTree<TK, TV, TR>;
|
|
414
413
|
return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
|
|
415
414
|
}
|
|
@@ -426,11 +425,11 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
426
425
|
*/
|
|
427
426
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
428
427
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
429
|
-
options?: Partial<
|
|
428
|
+
options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
|
|
430
429
|
): AVLTree<TK, TV, TR> {
|
|
431
430
|
const Ctor = this.constructor as unknown as new (
|
|
432
431
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
433
|
-
opts?:
|
|
432
|
+
opts?: AVLTreeMultiMapOptions<TK, TV, TR>
|
|
434
433
|
) => AVLTree<TK, TV, TR>;
|
|
435
434
|
return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
|
|
436
435
|
}
|
|
@@ -17,7 +17,6 @@ import type {
|
|
|
17
17
|
IterationType,
|
|
18
18
|
RBTNColor
|
|
19
19
|
} from '../../types';
|
|
20
|
-
import { BSTOptions } from '../../types';
|
|
21
20
|
import { IBinaryTree } from '../../interfaces';
|
|
22
21
|
|
|
23
22
|
/**
|
|
@@ -512,10 +511,10 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
512
511
|
* @param [options] - Options for the new tree.
|
|
513
512
|
* @returns A new, empty tree.
|
|
514
513
|
*/
|
|
515
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
514
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
|
|
516
515
|
const Ctor = this.constructor as unknown as new (
|
|
517
516
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
518
|
-
opts?:
|
|
517
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
519
518
|
) => this;
|
|
520
519
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
521
520
|
}
|
|
@@ -531,11 +530,11 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
531
530
|
*/
|
|
532
531
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
533
532
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
534
|
-
options?: Partial<
|
|
533
|
+
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
535
534
|
): AVLTree<TK, TV, TR> {
|
|
536
535
|
const Ctor = this.constructor as unknown as new (
|
|
537
536
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
538
|
-
opts?:
|
|
537
|
+
opts?: AVLTreeOptions<TK, TV, TR>
|
|
539
538
|
) => AVLTree<TK, TV, TR>;
|
|
540
539
|
return new Ctor(iter, { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) });
|
|
541
540
|
}
|
|
@@ -7,11 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
-
|
|
10
|
+
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNOptKeyOrNode,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNRep,
|
|
14
|
-
Comparable,
|
|
15
14
|
Comparator,
|
|
16
15
|
CP,
|
|
17
16
|
DFSOrderPattern,
|
|
@@ -356,20 +355,28 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
356
355
|
*/
|
|
357
356
|
constructor(
|
|
358
357
|
keysNodesEntriesOrRaws: Iterable<
|
|
359
|
-
K | BSTNode
|
|
358
|
+
K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R
|
|
360
359
|
> = [],
|
|
361
360
|
options?: BSTOptions<K, V, R>
|
|
362
361
|
) {
|
|
363
362
|
super([], options);
|
|
364
363
|
|
|
365
364
|
if (options) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
if (
|
|
365
|
+
|
|
366
|
+
// Use the 'in' operator to check if the field is present
|
|
367
|
+
if ('comparator' in options && options.comparator !== undefined) {
|
|
368
|
+
this._comparator = options.comparator;
|
|
369
|
+
} else {
|
|
370
|
+
this._comparator = this._createDefaultComparator();
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
this._comparator = this._createDefaultComparator();
|
|
369
374
|
}
|
|
375
|
+
|
|
370
376
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
371
377
|
}
|
|
372
378
|
|
|
379
|
+
|
|
373
380
|
protected override _root?: BSTNode<K, V> = undefined;
|
|
374
381
|
|
|
375
382
|
/**
|
|
@@ -382,42 +389,40 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
382
389
|
return this._root;
|
|
383
390
|
}
|
|
384
391
|
|
|
385
|
-
protected _isReverse: boolean = false;
|
|
386
|
-
|
|
387
392
|
/**
|
|
388
|
-
*
|
|
389
|
-
* @remarks Time O(1)
|
|
390
|
-
*
|
|
391
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
393
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
394
|
+
* @remarks Time O(1) Space O(1)
|
|
395
|
+
* @returns The default comparator function.
|
|
392
396
|
*/
|
|
393
|
-
|
|
394
|
-
return
|
|
397
|
+
protected _createDefaultComparator(): Comparator<K> {
|
|
398
|
+
return (a: K, b: K): number => {
|
|
399
|
+
debugger
|
|
400
|
+
// If both keys are comparable (primitive types), use direct comparison
|
|
401
|
+
if (isComparable(a) && isComparable(b)) {
|
|
402
|
+
if (a > b) return 1;
|
|
403
|
+
if (a < b) return -1;
|
|
404
|
+
return 0;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// If keys are objects and no comparator is provided, throw an error
|
|
408
|
+
if (typeof a === 'object' || typeof b === 'object') {
|
|
409
|
+
throw TypeError(
|
|
410
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Default: keys are equal (fallback case)
|
|
415
|
+
return 0;
|
|
416
|
+
};
|
|
395
417
|
}
|
|
396
418
|
|
|
397
419
|
/**
|
|
398
|
-
* The
|
|
399
|
-
|
|
420
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
421
|
+
|
|
422
|
+
* @remarks Time O(1) Space O(1)
|
|
400
423
|
*/
|
|
401
|
-
protected _comparator: Comparator<K
|
|
402
|
-
|
|
403
|
-
if (a > b) return 1;
|
|
404
|
-
if (a < b) return -1;
|
|
405
|
-
return 0;
|
|
406
|
-
}
|
|
407
|
-
if (this._specifyComparable) {
|
|
408
|
-
const va = this._specifyComparable(a);
|
|
409
|
-
const vb = this._specifyComparable(b);
|
|
410
|
-
if (va > vb) return 1;
|
|
411
|
-
if (va < vb) return -1;
|
|
412
|
-
return 0;
|
|
413
|
-
}
|
|
414
|
-
if (typeof a === 'object' || typeof b === 'object') {
|
|
415
|
-
throw TypeError(
|
|
416
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
417
|
-
);
|
|
418
|
-
}
|
|
419
|
-
return 0;
|
|
420
|
-
};
|
|
424
|
+
protected _comparator: Comparator<K>;
|
|
425
|
+
|
|
421
426
|
|
|
422
427
|
/**
|
|
423
428
|
* Gets the comparator function used by the tree.
|
|
@@ -429,18 +434,6 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
429
434
|
return this._comparator;
|
|
430
435
|
}
|
|
431
436
|
|
|
432
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
436
|
-
* @remarks Time O(1)
|
|
437
|
-
*
|
|
438
|
-
* @returns The key-to-comparable conversion function.
|
|
439
|
-
*/
|
|
440
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined {
|
|
441
|
-
return this._specifyComparable;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
437
|
/**
|
|
445
438
|
* (Protected) Creates a new BST node.
|
|
446
439
|
* @remarks Time O(1), Space O(1)
|
|
@@ -489,7 +482,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
489
482
|
* @returns True if the key is valid, false otherwise.
|
|
490
483
|
*/
|
|
491
484
|
override isValidKey(key: any): key is K {
|
|
492
|
-
return isComparable(key
|
|
485
|
+
return isComparable(key);
|
|
493
486
|
}
|
|
494
487
|
|
|
495
488
|
/**
|
|
@@ -625,8 +618,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
625
618
|
if (isRange) {
|
|
626
619
|
// Range search: Only go left if the current key is >= the lower bound
|
|
627
620
|
const range = keyNodeEntryOrPredicate as Range<K>;
|
|
628
|
-
const leftS =
|
|
629
|
-
const leftI =
|
|
621
|
+
const leftS = range.low;
|
|
622
|
+
const leftI = range.includeLow;
|
|
630
623
|
return (leftI && this._compare(cur.key, leftS) >= 0) || (!leftI && this._compare(cur.key, leftS) > 0);
|
|
631
624
|
}
|
|
632
625
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -643,8 +636,8 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
643
636
|
if (isRange) {
|
|
644
637
|
// Range search: Only go right if current key <= upper bound
|
|
645
638
|
const range = keyNodeEntryOrPredicate as Range<K>;
|
|
646
|
-
const rightS =
|
|
647
|
-
const rightI =
|
|
639
|
+
const rightS = range.high;
|
|
640
|
+
const rightI = range.includeHigh;
|
|
648
641
|
return (rightI && this._compare(cur.key, rightS) <= 0) || (!rightI && this._compare(cur.key, rightS) < 0);
|
|
649
642
|
}
|
|
650
643
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -1037,7 +1030,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1037
1030
|
*/
|
|
1038
1031
|
override map<MK = K, MV = V, MR = any>(
|
|
1039
1032
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
1040
|
-
options?: Partial<
|
|
1033
|
+
options?: Partial<BSTOptions<MK, MV, MR>>,
|
|
1041
1034
|
thisArg?: unknown
|
|
1042
1035
|
): BST<MK, MV, MR> {
|
|
1043
1036
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -1050,36 +1043,73 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1050
1043
|
}
|
|
1051
1044
|
|
|
1052
1045
|
/**
|
|
1053
|
-
* Deletes
|
|
1054
|
-
*
|
|
1046
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
1047
|
+
*
|
|
1048
|
+
* @remarks
|
|
1049
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
1050
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
1051
|
+
*
|
|
1052
|
+
* @template K - The key type.
|
|
1053
|
+
* @template V - The value type.
|
|
1054
|
+
*
|
|
1055
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
1056
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
1057
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
1058
|
+
* - An entry tuple: searches for the key-value pair.
|
|
1059
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
1060
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
1061
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
1062
|
+
*
|
|
1063
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
1064
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
1065
|
+
*
|
|
1066
|
+
* @param startNode - The node to start the search from. Can be:
|
|
1067
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
1068
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
1069
|
+
* - Default value: this._root (the tree's root).
|
|
1070
|
+
*
|
|
1071
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
1072
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
1073
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
1074
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
1055
1075
|
*
|
|
1056
|
-
* @
|
|
1057
|
-
*
|
|
1076
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
1077
|
+
* - Key: the matched node's key.
|
|
1078
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
1079
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
1058
1080
|
*/
|
|
1059
|
-
deleteWhere(
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1081
|
+
deleteWhere(
|
|
1082
|
+
keyNodeEntryOrPredicate:
|
|
1083
|
+
| K
|
|
1084
|
+
| BSTNode<K, V>
|
|
1085
|
+
| [K | null | undefined, V | undefined]
|
|
1086
|
+
| null
|
|
1087
|
+
| undefined
|
|
1088
|
+
| NodePredicate<BSTNode<K, V>>
|
|
1089
|
+
| Range<K>,
|
|
1090
|
+
onlyOne = false,
|
|
1091
|
+
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root,
|
|
1092
|
+
iterationType: IterationType = this.iterationType
|
|
1093
|
+
): BinaryTreeDeleteResult<BSTNode<K, V>>[] {
|
|
1063
1094
|
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
if (!node) break;
|
|
1095
|
+
const toDelete = this.search (
|
|
1096
|
+
keyNodeEntryOrPredicate,
|
|
1097
|
+
onlyOne,
|
|
1098
|
+
(node) => node,
|
|
1099
|
+
startNode,
|
|
1100
|
+
iterationType
|
|
1101
|
+
);
|
|
1072
1102
|
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
}
|
|
1078
|
-
cur = node.right as BSTNode<K, V> | null | undefined;
|
|
1103
|
+
let results : BinaryTreeDeleteResult<BSTNode<K, V>>[] = [];
|
|
1104
|
+
for (const node of toDelete) {
|
|
1105
|
+
const deleteInfo = this.delete(node);
|
|
1106
|
+
results = results.concat(deleteInfo);
|
|
1079
1107
|
}
|
|
1080
|
-
|
|
1108
|
+
|
|
1109
|
+
return results;
|
|
1081
1110
|
}
|
|
1082
1111
|
|
|
1112
|
+
|
|
1083
1113
|
/**
|
|
1084
1114
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
1085
1115
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -1300,8 +1330,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1300
1330
|
protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
|
|
1301
1331
|
return {
|
|
1302
1332
|
...super._snapshotOptions<TK, TV, TR>(),
|
|
1303
|
-
|
|
1304
|
-
isReverse: this.isReverse as BSTOptions<TK, TV, TR>['isReverse']
|
|
1333
|
+
comparator: this._comparator as unknown as BSTOptions<TK, TV, TR>['comparator'],
|
|
1305
1334
|
};
|
|
1306
1335
|
}
|
|
1307
1336
|
|
|
@@ -1335,14 +1364,14 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1335
1364
|
|
|
1336
1365
|
/**
|
|
1337
1366
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
1338
|
-
* @remarks Time O(1)
|
|
1367
|
+
* @remarks Time O(1) Space O(1)
|
|
1339
1368
|
*
|
|
1340
1369
|
* @param a - The first key.
|
|
1341
1370
|
* @param b - The second key.
|
|
1342
1371
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
1343
1372
|
*/
|
|
1344
1373
|
protected _compare(a: K, b: K) {
|
|
1345
|
-
return this.
|
|
1374
|
+
return this._comparator(a, b);
|
|
1346
1375
|
}
|
|
1347
1376
|
|
|
1348
1377
|
/**
|
|
@@ -1352,7 +1381,7 @@ export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implemen
|
|
|
1352
1381
|
* @param key - The key of the node to delete.
|
|
1353
1382
|
* @returns True if the node was found and deleted, false otherwise.
|
|
1354
1383
|
*/
|
|
1355
|
-
|
|
1384
|
+
protected _deleteByKey(key: K): boolean {
|
|
1356
1385
|
let node = this._root as BSTNode<K, V> | undefined;
|
|
1357
1386
|
|
|
1358
1387
|
// 1. Find the node
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
|
-
BinaryTreeOptions,
|
|
12
11
|
CRUD,
|
|
13
12
|
EntryCallback,
|
|
14
13
|
FamilyPosition,
|
|
@@ -463,7 +462,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
463
462
|
|
|
464
463
|
override map<MK = K, MV = V, MR = any>(
|
|
465
464
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
466
|
-
options?: Partial<
|
|
465
|
+
options?: Partial<RedBlackTreeOptions<MK, MV, MR>>,
|
|
467
466
|
thisArg?: unknown
|
|
468
467
|
): RedBlackTree<MK, MV, MR> {
|
|
469
468
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
|
-
BinaryTreeOptions,
|
|
12
11
|
BSTNOptKeyOrNode,
|
|
13
12
|
EntryCallback,
|
|
14
13
|
FamilyPosition,
|
|
@@ -17,7 +16,6 @@ import type {
|
|
|
17
16
|
RBTNColor,
|
|
18
17
|
TreeCounterOptions
|
|
19
18
|
} from '../../types';
|
|
20
|
-
import { BSTOptions } from '../../types';
|
|
21
19
|
import { BSTNode } from './bst';
|
|
22
20
|
import { IBinaryTree } from '../../interfaces';
|
|
23
21
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -432,7 +430,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
432
430
|
*/
|
|
433
431
|
override map<MK = K, MV = V, MR = any>(
|
|
434
432
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
435
|
-
options?: Partial<
|
|
433
|
+
options?: Partial<TreeCounterOptions<MK, MV, MR>>,
|
|
436
434
|
thisArg?: unknown
|
|
437
435
|
): TreeCounter<MK, MV, MR> {
|
|
438
436
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -465,10 +463,10 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
465
463
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
466
464
|
* @returns An empty like-kind instance.
|
|
467
465
|
*/
|
|
468
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
466
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this {
|
|
469
467
|
const Ctor = this.constructor as unknown as new (
|
|
470
468
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
471
|
-
opts?:
|
|
469
|
+
opts?: TreeCounterOptions<TK, TV, TR>
|
|
472
470
|
) => this;
|
|
473
471
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
474
472
|
}
|
|
@@ -485,11 +483,11 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
|
|
|
485
483
|
*/
|
|
486
484
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
487
485
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
488
|
-
options?: Partial<
|
|
486
|
+
options?: Partial<TreeCounterOptions<TK, TV, TR>>
|
|
489
487
|
): TreeCounter<TK, TV, TR> {
|
|
490
488
|
const Ctor = this.constructor as unknown as new (
|
|
491
489
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
492
|
-
opts?:
|
|
490
|
+
opts?: TreeCounterOptions<TK, TV, TR>
|
|
493
491
|
) => TreeCounter<TK, TV, TR>;
|
|
494
492
|
return new Ctor(iter as unknown as Iterable<TK | any>, {
|
|
495
493
|
...this._snapshotOptions<TK, TV, TR>(),
|
|
@@ -12,7 +12,6 @@ import type {
|
|
|
12
12
|
EntryCallback,
|
|
13
13
|
FamilyPosition,
|
|
14
14
|
RBTNColor,
|
|
15
|
-
RedBlackTreeOptions,
|
|
16
15
|
TreeMultiMapOptions
|
|
17
16
|
} from '../../types';
|
|
18
17
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
@@ -476,13 +475,13 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
476
475
|
|
|
477
476
|
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
478
477
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
479
|
-
options?: Partial<
|
|
478
|
+
options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>,
|
|
480
479
|
thisArg?: unknown
|
|
481
480
|
): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
482
481
|
|
|
483
482
|
override map<MK = K, MV = V[], MR = any>(
|
|
484
483
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
485
|
-
options?: Partial<
|
|
484
|
+
options?: Partial<TreeMultiMapOptions<MK, MV, MR>>,
|
|
486
485
|
thisArg?: unknown
|
|
487
486
|
): RedBlackTree<MK, MV, MR>;
|
|
488
487
|
|
|
@@ -499,7 +498,7 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
499
498
|
*/
|
|
500
499
|
override map<MK, MV, MR extends object>(
|
|
501
500
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
502
|
-
options?: Partial<
|
|
501
|
+
options?: Partial<TreeMultiMapOptions<MK, MV, MR>>,
|
|
503
502
|
thisArg?: unknown
|
|
504
503
|
): RedBlackTree<MK, MV, MR> {
|
|
505
504
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
@@ -517,10 +516,10 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
517
516
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
518
517
|
* @returns An empty like-kind instance.
|
|
519
518
|
*/
|
|
520
|
-
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
519
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this {
|
|
521
520
|
const Ctor = this.constructor as unknown as new (
|
|
522
521
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
523
|
-
opts?:
|
|
522
|
+
opts?: TreeMultiMapOptions<TK, TV, TR>
|
|
524
523
|
) => RedBlackTree<TK, TV, TR>;
|
|
525
524
|
return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
|
|
526
525
|
}
|
|
@@ -539,11 +538,11 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
|
|
|
539
538
|
iter: Iterable<
|
|
540
539
|
TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
541
540
|
> = [],
|
|
542
|
-
options?: Partial<
|
|
541
|
+
options?: Partial<TreeMultiMapOptions<TK, TV, TR>>
|
|
543
542
|
): RedBlackTree<TK, TV, TR> {
|
|
544
543
|
const Ctor = this.constructor as unknown as new (
|
|
545
544
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
546
|
-
opts?:
|
|
545
|
+
opts?: TreeMultiMapOptions<TK, TV, TR>
|
|
547
546
|
) => RedBlackTree<TK, TV, TR>;
|
|
548
547
|
return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
|
|
549
548
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
2
|
-
import {
|
|
3
|
-
import { OptValue } from '../../common';
|
|
2
|
+
import type { Comparator, OptValue } from '../../common';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
|
|
5
|
+
|
|
6
|
+
export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
|
|
7
|
+
comparator?: Comparator<K>;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export type BSTNOptKey<K> = K | undefined;
|