avl-tree-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/avl-tree-typed.js +85 -75
  18. package/dist/umd/avl-tree-typed.js.map +1 -1
  19. package/dist/umd/avl-tree-typed.min.js +2 -2
  20. package/dist/umd/avl-tree-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 {};
@@ -2831,36 +2831,20 @@ var avlTreeTyped = (() => {
2831
2831
  constructor(keysNodesEntriesOrRaws = [], options) {
2832
2832
  super([], options);
2833
2833
  __publicField(this, "_root");
2834
- __publicField(this, "_isReverse", false);
2835
2834
  /**
2836
- * The default comparator function.
2837
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
2838
- */
2839
- __publicField(this, "_comparator", (a, b) => {
2840
- if (isComparable(a) && isComparable(b)) {
2841
- if (a > b) return 1;
2842
- if (a < b) return -1;
2843
- return 0;
2844
- }
2845
- if (this._specifyComparable) {
2846
- const va = this._specifyComparable(a);
2847
- const vb = this._specifyComparable(b);
2848
- if (va > vb) return 1;
2849
- if (va < vb) return -1;
2850
- return 0;
2851
- }
2852
- if (typeof a === "object" || typeof b === "object") {
2853
- throw TypeError(
2854
- `When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
2855
- );
2856
- }
2857
- return 0;
2858
- });
2859
- __publicField(this, "_specifyComparable");
2835
+ * The comparator function used to determine the order of keys in the tree.
2836
+
2837
+ * @remarks Time O(1) Space O(1)
2838
+ */
2839
+ __publicField(this, "_comparator");
2860
2840
  if (options) {
2861
- const { specifyComparable, isReverse } = options;
2862
- if (typeof specifyComparable === "function") this._specifyComparable = specifyComparable;
2863
- if (isReverse !== void 0) this._isReverse = isReverse;
2841
+ if ("comparator" in options && options.comparator !== void 0) {
2842
+ this._comparator = options.comparator;
2843
+ } else {
2844
+ this._comparator = this._createDefaultComparator();
2845
+ }
2846
+ } else {
2847
+ this._comparator = this._createDefaultComparator();
2864
2848
  }
2865
2849
  if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
2866
2850
  }
@@ -2874,13 +2858,25 @@ var avlTreeTyped = (() => {
2874
2858
  return this._root;
2875
2859
  }
2876
2860
  /**
2877
- * Gets whether the tree's comparison logic is reversed.
2878
- * @remarks Time O(1)
2879
- *
2880
- * @returns True if the tree is reversed (e.g., a max-heap logic).
2861
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
2862
+ * @remarks Time O(1) Space O(1)
2863
+ * @returns The default comparator function.
2881
2864
  */
2882
- get isReverse() {
2883
- return this._isReverse;
2865
+ _createDefaultComparator() {
2866
+ return (a, b) => {
2867
+ debugger;
2868
+ if (isComparable(a) && isComparable(b)) {
2869
+ if (a > b) return 1;
2870
+ if (a < b) return -1;
2871
+ return 0;
2872
+ }
2873
+ if (typeof a === "object" || typeof b === "object") {
2874
+ throw TypeError(
2875
+ `When comparing object type keys, a custom comparator must be provided in the constructor's options!`
2876
+ );
2877
+ }
2878
+ return 0;
2879
+ };
2884
2880
  }
2885
2881
  /**
2886
2882
  * Gets the comparator function used by the tree.
@@ -2891,15 +2887,6 @@ var avlTreeTyped = (() => {
2891
2887
  get comparator() {
2892
2888
  return this._comparator;
2893
2889
  }
2894
- /**
2895
- * Gets the function used to extract a comparable value from a complex key.
2896
- * @remarks Time O(1)
2897
- *
2898
- * @returns The key-to-comparable conversion function.
2899
- */
2900
- get specifyComparable() {
2901
- return this._specifyComparable;
2902
- }
2903
2890
  /**
2904
2891
  * (Protected) Creates a new BST node.
2905
2892
  * @remarks Time O(1), Space O(1)
@@ -2941,7 +2928,7 @@ var avlTreeTyped = (() => {
2941
2928
  * @returns True if the key is valid, false otherwise.
2942
2929
  */
2943
2930
  isValidKey(key) {
2944
- return isComparable(key, this._specifyComparable !== void 0);
2931
+ return isComparable(key);
2945
2932
  }
2946
2933
  /**
2947
2934
  * Performs a Depth-First Search (DFS) traversal.
@@ -3031,8 +3018,8 @@ var avlTreeTyped = (() => {
3031
3018
  if (!this.isRealNode(cur.left)) return false;
3032
3019
  if (isRange) {
3033
3020
  const range = keyNodeEntryOrPredicate;
3034
- const leftS = this.isReverse ? range.high : range.low;
3035
- const leftI = this.isReverse ? range.includeHigh : range.includeLow;
3021
+ const leftS = range.low;
3022
+ const leftI = range.includeLow;
3036
3023
  return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
3037
3024
  }
3038
3025
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3046,8 +3033,8 @@ var avlTreeTyped = (() => {
3046
3033
  if (!this.isRealNode(cur.right)) return false;
3047
3034
  if (isRange) {
3048
3035
  const range = keyNodeEntryOrPredicate;
3049
- const rightS = this.isReverse ? range.low : range.high;
3050
- const rightI = this.isReverse ? range.includeLow : range.includeHigh;
3036
+ const rightS = range.high;
3037
+ const rightI = range.includeHigh;
3051
3038
  return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
3052
3039
  }
3053
3040
  if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
@@ -3368,31 +3355,55 @@ var avlTreeTyped = (() => {
3368
3355
  return out;
3369
3356
  }
3370
3357
  /**
3371
- * Deletes the first node found that satisfies the predicate.
3372
- * @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.
3358
+ * Deletes nodes that match a key, node, entry, predicate, or range.
3373
3359
  *
3374
- * @param predicate - A function to test each [key, value] pair.
3375
- * @returns True if a node was deleted, false otherwise.
3376
- */
3377
- deleteWhere(predicate) {
3378
- const stack = [];
3379
- let cur = this._root;
3380
- let index = 0;
3381
- while (stack.length > 0 || cur !== void 0) {
3382
- while (cur !== void 0 && cur !== null) {
3383
- stack.push(cur);
3384
- cur = cur.left;
3385
- }
3386
- const node = stack.pop();
3387
- if (!node) break;
3388
- const key = node.key;
3389
- const val = node.value;
3390
- if (predicate(key, val, index++, this)) {
3391
- return this._deleteByKey(key);
3392
- }
3393
- cur = node.right;
3360
+ * @remarks
3361
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
3362
+ * Space Complexity: O(M) for storing matched nodes and result map.
3363
+ *
3364
+ * @template K - The key type.
3365
+ * @template V - The value type.
3366
+ *
3367
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
3368
+ * - A key (type K): searches for exact key match using the comparator.
3369
+ * - A BSTNode: searches for the matching node in the tree.
3370
+ * - An entry tuple: searches for the key-value pair.
3371
+ * - A NodePredicate function: tests each node and returns true for matches.
3372
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
3373
+ * - null or undefined: treated as no match, returns empty results.
3374
+ *
3375
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
3376
+ * If false (default), searches for and deletes all matching nodes.
3377
+ *
3378
+ * @param startNode - The node to start the search from. Can be:
3379
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
3380
+ * - null or undefined: defaults to the root, searching the entire tree.
3381
+ * - Default value: this._root (the tree's root).
3382
+ *
3383
+ * @param iterationType - Controls the internal traversal implementation:
3384
+ * - 'RECURSIVE': uses recursive function calls for traversal.
3385
+ * - 'ITERATIVE': uses explicit stack-based iteration.
3386
+ * - Default: this.iterationType (the tree's default iteration mode).
3387
+ *
3388
+ * @returns A Map<K, boolean> containing the deletion results:
3389
+ * - Key: the matched node's key.
3390
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
3391
+ * - If no nodes match the search criteria, the returned map is empty.
3392
+ */
3393
+ deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
3394
+ const toDelete = this.search(
3395
+ keyNodeEntryOrPredicate,
3396
+ onlyOne,
3397
+ (node) => node,
3398
+ startNode,
3399
+ iterationType
3400
+ );
3401
+ let results = [];
3402
+ for (const node of toDelete) {
3403
+ const deleteInfo = this.delete(node);
3404
+ results = results.concat(deleteInfo);
3394
3405
  }
3395
- return false;
3406
+ return results;
3396
3407
  }
3397
3408
  /**
3398
3409
  * (Protected) Core bound search implementation supporting all parameter types.
@@ -3545,8 +3556,7 @@ var avlTreeTyped = (() => {
3545
3556
  _snapshotOptions() {
3546
3557
  return {
3547
3558
  ...super._snapshotOptions(),
3548
- specifyComparable: this.specifyComparable,
3549
- isReverse: this.isReverse
3559
+ comparator: this._comparator
3550
3560
  };
3551
3561
  }
3552
3562
  /**
@@ -3574,14 +3584,14 @@ var avlTreeTyped = (() => {
3574
3584
  }
3575
3585
  /**
3576
3586
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
3577
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
3587
+ * @remarks Time O(1) Space O(1)
3578
3588
  *
3579
3589
  * @param a - The first key.
3580
3590
  * @param b - The second key.
3581
3591
  * @returns A number (1, -1, or 0) representing the comparison.
3582
3592
  */
3583
3593
  _compare(a, b) {
3584
- return this._isReverse ? -this._comparator(a, b) : this._comparator(a, b);
3594
+ return this._comparator(a, b);
3585
3595
  }
3586
3596
  /**
3587
3597
  * (Private) Deletes a node by its key.