binary-tree-typed 2.2.3 → 2.2.5

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.
@@ -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,10 @@ 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).
297
- */
298
- get isReverse(): boolean;
299
- /**
300
- * The default comparator function.
301
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used, C is complexity of that function).
292
+ * The comparator function used to determine the order of keys in the tree.
293
+
294
+ * @remarks Time O(1) Space O(1)
302
295
  */
303
296
  protected _comparator: Comparator<K>;
304
297
  /**
@@ -308,14 +301,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
308
301
  * @returns The comparator function.
309
302
  */
310
303
  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
304
  /**
320
305
  * (Protected) Creates a new BST node.
321
306
  * @remarks Time O(1), Space O(1)
@@ -466,6 +451,54 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
466
451
  * @returns The first node with key > given key, or undefined if no such node exists.
467
452
  */
468
453
  upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
454
+ /**
455
+ * Returns the first node with a key greater than or equal to the given key.
456
+ * This is equivalent to Java TreeMap.ceilingEntry().
457
+ * Supports RECURSIVE and ITERATIVE implementations.
458
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
459
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
460
+ *
461
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
462
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
463
+ * @returns The first node with key >= given key, or undefined if no such node exists.
464
+ */
465
+ ceilingEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
466
+ /**
467
+ * Returns the first node with a key strictly greater than the given key.
468
+ * This is equivalent to Java TreeMap.higherEntry().
469
+ * Supports RECURSIVE and ITERATIVE implementations.
470
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
471
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
472
+ *
473
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
474
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
475
+ * @returns The first node with key > given key, or undefined if no such node exists.
476
+ */
477
+ higherEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
478
+ /**
479
+ * Returns the first node with a key less than or equal to the given key.
480
+ * This is equivalent to Java TreeMap.floorEntry().
481
+ * Supports RECURSIVE and ITERATIVE implementations.
482
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
483
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
484
+ *
485
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
486
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
487
+ * @returns The first node with key <= given key, or undefined if no such node exists.
488
+ */
489
+ floorEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
490
+ /**
491
+ * Returns the first node with a key strictly less than the given key.
492
+ * This is equivalent to Java TreeMap.lowerEntry().
493
+ * Supports RECURSIVE and ITERATIVE implementations.
494
+ * @remarks Time Complexity: O(log n) on average, O(h) where h is tree height.
495
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
496
+ *
497
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
498
+ * @param [iterationType=this.iterationType] - The iteration type (RECURSIVE or ITERATIVE).
499
+ * @returns The first node with key < given key, or undefined if no such node exists.
500
+ */
501
+ lowerEntry(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
469
502
  /**
470
503
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
471
504
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -507,15 +540,96 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
507
540
  * @param [thisArg] - `this` context for the callback.
508
541
  * @returns A new, mapped BST.
509
542
  */
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>;
543
+ 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>;
544
+ /**
545
+ * Deletes nodes that match a key, node, entry, predicate, or range.
546
+ *
547
+ * @remarks
548
+ * Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
549
+ * Space Complexity: O(M) for storing matched nodes and result map.
550
+ *
551
+ * @template K - The key type.
552
+ * @template V - The value type.
553
+ *
554
+ * @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
555
+ * - A key (type K): searches for exact key match using the comparator.
556
+ * - A BSTNode: searches for the matching node in the tree.
557
+ * - An entry tuple: searches for the key-value pair.
558
+ * - A NodePredicate function: tests each node and returns true for matches.
559
+ * - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
560
+ * - null or undefined: treated as no match, returns empty results.
561
+ *
562
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
563
+ * If false (default), searches for and deletes all matching nodes.
564
+ *
565
+ * @param startNode - The node to start the search from. Can be:
566
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
567
+ * - null or undefined: defaults to the root, searching the entire tree.
568
+ * - Default value: this._root (the tree's root).
569
+ *
570
+ * @param iterationType - Controls the internal traversal implementation:
571
+ * - 'RECURSIVE': uses recursive function calls for traversal.
572
+ * - 'ITERATIVE': uses explicit stack-based iteration.
573
+ * - Default: this.iterationType (the tree's default iteration mode).
574
+ *
575
+ * @returns A Map<K, boolean> containing the deletion results:
576
+ * - Key: the matched node's key.
577
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
578
+ * - If no nodes match the search criteria, the returned map is empty.
579
+ */
580
+ 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>>[];
581
+ /**
582
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
583
+ * @remarks Time O(1) Space O(1)
584
+ * @returns The default comparator function.
585
+ */
586
+ protected _createDefaultComparator(): Comparator<K>;
587
+ /**
588
+ * (Protected) Binary search for floor by key with pruning optimization.
589
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
590
+ * Finds first node where key <= target.
591
+ * @remarks Time O(h) where h is tree height.
592
+ *
593
+ * @param key - The target key to search for.
594
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
595
+ * @returns The first node with key <= target, or undefined if none exists.
596
+ */
597
+ protected _floorByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
511
598
  /**
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.
599
+ * (Protected) In-order traversal search for floor by predicate.
600
+ * Falls back to linear in-order traversal when predicate-based search is required.
601
+ * Returns the last node that satisfies the predicate function.
602
+ * @remarks Time Complexity: O(n) since it may visit every node.
603
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
604
+ *
605
+ * @param predicate - The predicate function to test nodes.
606
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
607
+ * @returns The last node satisfying predicate (highest key), or undefined if none found.
608
+ */
609
+ protected _floorByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
610
+ /**
611
+ * (Protected) Binary search for lower by key with pruning optimization.
612
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
613
+ * Finds first node where key < target.
614
+ * @remarks Time O(h) where h is tree height.
514
615
  *
515
- * @param predicate - A function to test each [key, value] pair.
516
- * @returns True if a node was deleted, false otherwise.
616
+ * @param key - The target key to search for.
617
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
618
+ * @returns The first node with key < target, or undefined if none exists.
619
+ */
620
+ protected _lowerByKey(key: K, iterationType: IterationType): BSTNode<K, V> | undefined;
621
+ /**
622
+ * (Protected) In-order traversal search for lower by predicate.
623
+ * Falls back to linear in-order traversal when predicate-based search is required.
624
+ * Returns the node that satisfies the predicate and appears last in in-order traversal.
625
+ * @remarks Time Complexity: O(n) since it may visit every node.
626
+ * Space Complexity: O(h) for recursion, O(h) for iterative stack.
627
+ *
628
+ * @param predicate - The predicate function to test nodes.
629
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
630
+ * @returns The last node satisfying predicate (highest key < target), or undefined if none found.
517
631
  */
518
- deleteWhere(predicate: (key: K, value: V | undefined, index: number, tree: this) => boolean): boolean;
632
+ protected _lowerByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
519
633
  /**
520
634
  * (Protected) Core bound search implementation supporting all parameter types.
521
635
  * Unified logic for both lowerBound and upperBound.
@@ -593,7 +707,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
593
707
  protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
594
708
  /**
595
709
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
596
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
710
+ * @remarks Time O(1) Space O(1)
597
711
  *
598
712
  * @param a - The first key.
599
713
  * @param b - The second key.
@@ -607,5 +721,5 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
607
721
  * @param key - The key of the node to delete.
608
722
  * @returns True if the node was found and deleted, false otherwise.
609
723
  */
610
- private _deleteByKey;
724
+ protected _deleteByKey(key: K): boolean;
611
725
  }
@@ -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 {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binary-tree-typed",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "Binary Tree. Javascript & Typescript Data Structure.",
5
5
  "browser": "dist/umd/binary-tree-typed.min.js",
6
6
  "umd:main": "dist/umd/binary-tree-typed.min.js",
@@ -178,6 +178,6 @@
178
178
  "typescript": "^4.9.5"
179
179
  },
180
180
  "dependencies": {
181
- "data-structure-typed": "^2.2.3"
181
+ "data-structure-typed": "^2.2.5"
182
182
  }
183
183
  }
@@ -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<BinaryTreeOptions<MK, MV, MR>>,
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<AVLTreeOptions<MK, MVArr, MR>>,
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<AVLTreeOptions<MK, MV, MR>>,
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<AVLTreeOptions<MK, MV, MR>>,
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,12 @@ 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<AVLTreeOptions<TK, TV, TR>>): this {
408
+ protected override _createInstance<TK = K, TV = V, TR = R>(
409
+ options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
410
+ ): this {
410
411
  const Ctor = this.constructor as unknown as new (
411
412
  iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
412
- opts?: AVLTreeOptions<TK, TV, TR>
413
+ opts?: AVLTreeMultiMapOptions<TK, TV, TR>
413
414
  ) => AVLTree<TK, TV, TR>;
414
415
  return new Ctor([], { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) }) as unknown as this;
415
416
  }
@@ -426,11 +427,11 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
426
427
  */
427
428
  protected override _createLike<TK = K, TV = V, TR = R>(
428
429
  iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
429
- options?: Partial<AVLTreeOptions<TK, TV, TR>>
430
+ options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>
430
431
  ): AVLTree<TK, TV, TR> {
431
432
  const Ctor = this.constructor as unknown as new (
432
433
  iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
433
- opts?: AVLTreeOptions<TK, TV, TR>
434
+ opts?: AVLTreeMultiMapOptions<TK, TV, TR>
434
435
  ) => AVLTree<TK, TV, TR>;
435
436
  return new Ctor(iter, { ...(this._snapshotOptions?.<TK, TV, TR>() ?? {}), ...(options ?? {}) });
436
437
  }
@@ -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<BSTOptions<TK, TV, TR>>): this {
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?: BSTOptions<TK, TV, TR>
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<BSTOptions<TK, TV, TR>>
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?: BSTOptions<TK, TV, TR>
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
  }