tree-multimap-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.
Files changed (29) hide show
  1. package/dist/cjs/index.cjs +85 -75
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +85 -75
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +85 -75
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +85 -75
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
  14. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  15. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
  16. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  17. package/dist/umd/tree-multimap-typed.js +85 -75
  18. package/dist/umd/tree-multimap-typed.js.map +1 -1
  19. package/dist/umd/tree-multimap-typed.min.js +3 -3
  20. package/dist/umd/tree-multimap-typed.min.js.map +1 -1
  21. package/package.json +2 -2
  22. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  23. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  24. package/src/data-structures/binary-tree/avl-tree.ts +4 -5
  25. package/src/data-structures/binary-tree/bst.ts +111 -82
  26. package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
  27. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  28. package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
  29. 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, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
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<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<MK, MV, MR>;
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, AVLTreeOptions, ElemOf, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
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<AVLTreeOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
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<AVLTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
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<AVLTreeOptions<TK, TV, TR>>): this;
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<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
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<BSTOptions<TK, TV, TR>>): this;
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<BSTOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
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 { BinaryTreeOptions, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
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<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
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
- * Gets whether the tree's comparison logic is reversed.
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
- get isReverse(): boolean;
296
+ protected _createDefaultComparator(): Comparator<K>;
299
297
  /**
300
- * The default comparator function.
301
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
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<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
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 the first node found that satisfies the predicate.
513
- * @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
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
- * @param predicate - A function to test each [key, value] pair.
516
- * @returns True if a node was deleted, false otherwise.
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(predicate: (key: K, value: V | undefined, index: number, tree: this) => boolean): boolean;
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) (or O(C) if `specifyComparable` is used).
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
- private _deleteByKey;
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, BinaryTreeOptions, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
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<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
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, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
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<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): TreeCounter<MK, MV, MR>;
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<BSTOptions<TK, TV, TR>>): this;
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<BSTOptions<TK, TV, TR>>): TreeCounter<TK, TV, TR>;
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, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
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<RedBlackTreeOptions<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<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
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<RedBlackTreeOptions<TK, TV, TR>>): this;
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<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
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 { Comparable } from '../../utils';
3
- import { OptValue } from '../../common';
4
- export type BSTOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'> & {
5
- specifyComparable?: (key: K) => Comparable;
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 {};
@@ -2840,36 +2840,20 @@ var treeMultimapTyped = (() => {
2840
2840
  constructor(keysNodesEntriesOrRaws = [], options) {
2841
2841
  super([], options);
2842
2842
  __publicField(this, "_root");
2843
- __publicField(this, "_isReverse", false);
2844
2843
  /**
2845
- * The default comparator function.
2846
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2847
- */
2848
- __publicField(this, "_comparator", (a, b) => {
2849
- if (isComparable(a) && isComparable(b)) {
2850
- if (a > b) return 1;
2851
- if (a < b) return -1;
2852
- return 0;
2853
- }
2854
- if (this._specifyComparable) {
2855
- const va = this._specifyComparable(a);
2856
- const vb = this._specifyComparable(b);
2857
- if (va > vb) return 1;
2858
- if (va < vb) return -1;
2859
- return 0;
2860
- }
2861
- if (typeof a === "object" || typeof b === "object") {
2862
- throw TypeError(
2863
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2864
- );
2865
- }
2866
- return 0;
2867
- });
2868
- __publicField(this, "_specifyComparable");
2844
+ * The comparator function used to determine the order of keys in the tree.
2845
+
2846
+ * @remarks Time O(1) Space O(1)
2847
+ */
2848
+ __publicField(this, "_comparator");
2869
2849
  if (options) {
2870
- const { specifyComparable, isReverse } = options;
2871
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2872
- if (isReverse !== void 0) this._isReverse = isReverse;
2850
+ if ("comparator" in options && options.comparator !== void 0) {
2851
+ this._comparator = options.comparator;
2852
+ } else {
2853
+ this._comparator = this._createDefaultComparator();
2854
+ }
2855
+ } else {
2856
+ this._comparator = this._createDefaultComparator();
2873
2857
  }
2874
2858
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2875
2859
  }
@@ -2883,13 +2867,25 @@ var treeMultimapTyped = (() => {
2883
2867
  return this._root;
2884
2868
  }
2885
2869
  /**
2886
- * Gets whether the tree's comparison logic is reversed.
2887
- * @remarks Time O(1)
2888
- *
2889
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2870
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2871
+ * @remarks Time O(1) Space O(1)
2872
+ * @returns The default comparator function.
2890
2873
  */
2891
- get isReverse() {
2892
- return this._isReverse;
2874
+ _createDefaultComparator() {
2875
+ return (a, b) => {
2876
+ debugger;
2877
+ if (isComparable(a) && isComparable(b)) {
2878
+ if (a > b) return 1;
2879
+ if (a < b) return -1;
2880
+ return 0;
2881
+ }
2882
+ if (typeof a === "object" || typeof b === "object") {
2883
+ throw TypeError(
2884
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2885
+ );
2886
+ }
2887
+ return 0;
2888
+ };
2893
2889
  }
2894
2890
  /**
2895
2891
  * Gets the comparator function used by the tree.
@@ -2900,15 +2896,6 @@ var treeMultimapTyped = (() => {
2900
2896
  get comparator() {
2901
2897
  return this._comparator;
2902
2898
  }
2903
- /**
2904
- * Gets the function used to extract a comparable value from a complex key.
2905
- * @remarks Time O(1)
2906
- *
2907
- * @returns The key-to-comparable conversion function.
2908
- */
2909
- get specifyComparable() {
2910
- return this._specifyComparable;
2911
- }
2912
2899
  /**
2913
2900
  * (Protected) Creates a new BST node.
2914
2901
  * @remarks Time O(1), Space O(1)
@@ -2950,7 +2937,7 @@ var treeMultimapTyped = (() => {
2950
2937
  * @returns True if the key is valid, false otherwise.
2951
2938
  */
2952
2939
  isValidKey(key) {
2953
- return isComparable(key, this._specifyComparable !== void 0);
2940
+ return isComparable(key);
2954
2941
  }
2955
2942
  /**
2956
2943
  * Performs a Depth-First Search (DFS) traversal.
@@ -3040,8 +3027,8 @@ var treeMultimapTyped = (() => {
3040
3027
  if (!this.isRealNode(cur.left)) return false;
3041
3028
  if (isRange) {
3042
3029
  const range = keyNodeEntryOrPredicate;
3043
- const leftS = this.isReverse ? range.high : range.low;
3044
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3030
+ const leftS = range.low;
3031
+ const leftI = range.includeLow;
3045
3032
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3046
3033
  }
3047
3034
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3055,8 +3042,8 @@ var treeMultimapTyped = (() => {
3055
3042
  if (!this.isRealNode(cur.right)) return false;
3056
3043
  if (isRange) {
3057
3044
  const range = keyNodeEntryOrPredicate;
3058
- const rightS = this.isReverse ? range.low : range.high;
3059
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3045
+ const rightS = range.high;
3046
+ const rightI = range.includeHigh;
3060
3047
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3061
3048
  }
3062
3049
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3377,31 +3364,55 @@ var treeMultimapTyped = (() => {
3377
3364
  return out;
3378
3365
  }
3379
3366
  /**
3380
- * Deletes the first node found that satisfies the predicate.
3381
- * @remarks Performs an in-order traversal. Time O(N) worst-case (O(log N) to find + O(log N) to delete). Space O(log N) for stack.
3367
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3382
3368
  *
3383
- * @param predicate - A function to test each [key, value] pair.
3384
- * @returns True if a node was deleted, false otherwise.
3385
- */
3386
- deleteWhere(predicate) {
3387
- const stack = [];
3388
- let cur = this._root;
3389
- let index = 0;
3390
- while (stack.length > 0 || cur !== void 0) {
3391
- while (cur !== void 0 && cur !== null) {
3392
- stack.push(cur);
3393
- cur = cur.left;
3394
- }
3395
- const node = stack.pop();
3396
- if (!node) break;
3397
- const key = node.key;
3398
- const val = node.value;
3399
- if (predicate(key, val, index++, this)) {
3400
- return this._deleteByKey(key);
3401
- }
3402
- cur = node.right;
3369
+ * @remarks
3370
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3371
+ * Space Complexity: O(M) for storing matched nodes and result map.
3372
+ *
3373
+ * @template K - The key type.
3374
+ * @template V - The value type.
3375
+ *
3376
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3377
+ * - A key (type K): searches for exact key match using the comparator.
3378
+ * - A BSTNode: searches for the matching node in the tree.
3379
+ * - An entry tuple: searches for the key-value pair.
3380
+ * - A NodePredicate function: tests each node and returns true for matches.
3381
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3382
+ * - null or undefined: treated as no match, returns empty results.
3383
+ *
3384
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3385
+ * If false (default), searches for and deletes all matching nodes.
3386
+ *
3387
+ * @param startNode - The node to start the search from. Can be:
3388
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3389
+ * - null or undefined: defaults to the root, searching the entire tree.
3390
+ * - Default value: this._root (the tree's root).
3391
+ *
3392
+ * @param iterationType - Controls the internal traversal implementation:
3393
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3394
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3395
+ * - Default: this.iterationType (the tree's default iteration mode).
3396
+ *
3397
+ * @returns A Map<K, boolean> containing the deletion results:
3398
+ * - Key: the matched node's key.
3399
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3400
+ * - If no nodes match the search criteria, the returned map is empty.
3401
+ */
3402
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3403
+ const toDelete = this.search(
3404
+ keyNodeEntryOrPredicate,
3405
+ onlyOne,
3406
+ (node) => node,
3407
+ startNode,
3408
+ iterationType
3409
+ );
3410
+ let results = [];
3411
+ for (const node of toDelete) {
3412
+ const deleteInfo = this.delete(node);
3413
+ results = results.concat(deleteInfo);
3403
3414
  }
3404
- return false;
3415
+ return results;
3405
3416
  }
3406
3417
  /**
3407
3418
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -3554,8 +3565,7 @@ var treeMultimapTyped = (() => {
3554
3565
  _snapshotOptions() {
3555
3566
  return {
3556
3567
  ...super._snapshotOptions(),
3557
- specifyComparable: this.specifyComparable,
3558
- isReverse: this.isReverse
3568
+ comparator: this._comparator
3559
3569
  };
3560
3570
  }
3561
3571
  /**
@@ -3583,14 +3593,14 @@ var treeMultimapTyped = (() => {
3583
3593
  }
3584
3594
  /**
3585
3595
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3586
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3596
+ * @remarks Time O(1) Space O(1)
3587
3597
  *
3588
3598
  * @param a - The first key.
3589
3599
  * @param b - The second key.
3590
3600
  * @returns A number (1, -1, or 0) representing the comparison.
3591
3601
  */
3592
3602
  _compare(a, b) {
3593
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3603
+ return this._comparator(a, b);
3594
3604
  }
3595
3605
  /**
3596
3606
  * (Private) Deletes a node by its key.