tree-multimap-typed 2.2.2 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/cjs/index.cjs +245 -72
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +246 -72
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +245 -72
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +246 -72
  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 +98 -5
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
  17. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  18. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  19. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  20. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  21. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  22. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  23. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  24. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  25. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  26. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  27. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  28. package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
  29. package/dist/umd/tree-multimap-typed.js +246 -72
  30. package/dist/umd/tree-multimap-typed.js.map +1 -1
  31. package/dist/umd/tree-multimap-typed.min.js +3 -3
  32. package/dist/umd/tree-multimap-typed.min.js.map +1 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
  35. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
  36. package/src/data-structures/binary-tree/avl-tree.ts +100 -7
  37. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  38. package/src/data-structures/binary-tree/bst.ts +431 -93
  39. package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
  40. package/src/data-structures/binary-tree/tree-counter.ts +5 -7
  41. package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
  42. package/src/data-structures/graph/directed-graph.ts +126 -1
  43. package/src/data-structures/graph/undirected-graph.ts +160 -1
  44. package/src/data-structures/hash/hash-map.ts +110 -27
  45. package/src/data-structures/heap/heap.ts +107 -58
  46. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  47. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  48. package/src/data-structures/queue/deque.ts +95 -67
  49. package/src/data-structures/queue/queue.ts +90 -34
  50. package/src/data-structures/stack/stack.ts +58 -40
  51. package/src/data-structures/trie/trie.ts +109 -47
  52. package/src/interfaces/binary-tree.ts +2 -0
  53. 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 { 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';
@@ -126,8 +126,57 @@ export declare class BSTNode<K = any, V = any> {
126
126
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
127
127
  *
128
128
  * @example
129
+ * // basic BST creation and add operation
130
+ * // Create a simple BST with numeric keys
131
+ * const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
132
+ *
133
+ * bst.print();
134
+ * // _______8__________
135
+ * // / \
136
+ * // ___4___ ____12_____
137
+ * // / \ / \
138
+ * // _2_ _6_ _10__ _14__
139
+ * // / \ / \ / \ / \
140
+ * // 1 3 5 7 9 11 13 15__
141
+ * // \
142
+ * // 16
143
+ *
144
+ * // Verify size
145
+ * console.log(bst.size); // 16;
146
+ *
147
+ * // Add new elements
148
+ * bst.add(17);
149
+ * bst.add(0);
150
+ * console.log(bst.size); // 18;
151
+ *
152
+ * // Verify keys are searchable
153
+ * console.log(bst.has(11)); // true;
154
+ * console.log(bst.has(100)); // false;
155
+ * @example
156
+ * // BST delete and search after deletion
157
+ * const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
158
+ *
159
+ * // Delete a leaf node
160
+ * bst.delete(1);
161
+ * console.log(bst.has(1)); // false;
162
+ *
163
+ * // Delete a node with one child
164
+ * bst.delete(2);
165
+ * console.log(bst.has(2)); // false;
166
+ *
167
+ * // Delete a node with two children
168
+ * bst.delete(3);
169
+ * console.log(bst.has(3)); // false;
170
+ *
171
+ * // Size decreases with each deletion
172
+ * console.log(bst.size); // 13;
173
+ *
174
+ * // Other nodes remain searchable
175
+ * console.log(bst.has(11)); // true;
176
+ * console.log(bst.has(15)); // true;
177
+ * @example
129
178
  * // Merge 3 sorted datasets
130
- * const dataset1 = new BST<number, string>([
179
+ * const dataset1 = new BST<number, string>([
131
180
  * [1, 'A'],
132
181
  * [7, 'G']
133
182
  * ]);
@@ -147,18 +196,58 @@ export declare class BSTNode<K = any, V = any> {
147
196
  * merged.merge(dataset3);
148
197
  *
149
198
  * // Verify merged dataset is in sorted order
150
- * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
199
+ * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
151
200
  * @example
152
- * // Find elements in a range
153
- * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
154
- * console.log(bst.search(new Range(5, 10))); // [5, 7, 10]
155
- * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12']
156
- * console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10]
157
- * console.log(bst.rangeSearch([15, 20])); // [15, 18]
158
- * console.log(bst.search(new Range(15, 20, false))); // [18]
201
+ * // BST with custom objects for expression evaluation
202
+ * interface Expression {
203
+ * id: number;
204
+ * operator: string;
205
+ * precedence: number;
206
+ * }
207
+ *
208
+ * // BST efficiently stores and retrieves operators by precedence
209
+ * const operatorTree = new BST<number, Expression>(
210
+ * [
211
+ * [1, { id: 1, operator: '+', precedence: 1 }],
212
+ * [2, { id: 2, operator: '*', precedence: 2 }],
213
+ * [3, { id: 3, operator: '/', precedence: 2 }],
214
+ * [4, { id: 4, operator: '-', precedence: 1 }],
215
+ * [5, { id: 5, operator: '^', precedence: 3 }]
216
+ * ],
217
+ * { isMapMode: false }
218
+ * );
219
+ *
220
+ * console.log(operatorTree.size); // 5;
221
+ *
222
+ * // Quick lookup of operators
223
+ * const mult = operatorTree.get(2);
224
+ * console.log(mult?.operator); // '*';
225
+ * console.log(mult?.precedence); // 2;
226
+ *
227
+ * // Check if operator exists
228
+ * console.log(operatorTree.has(5)); // true;
229
+ * console.log(operatorTree.has(99)); // false;
230
+ *
231
+ * // Retrieve operator by precedence level
232
+ * const expNode = operatorTree.getNode(3);
233
+ * console.log(expNode?.key); // 3;
234
+ * console.log(expNode?.value?.precedence); // 2;
235
+ *
236
+ * // Delete operator and verify
237
+ * operatorTree.delete(1);
238
+ * console.log(operatorTree.has(1)); // false;
239
+ * console.log(operatorTree.size); // 4;
240
+ *
241
+ * // Get tree height for optimization analysis
242
+ * const treeHeight = operatorTree.getHeight();
243
+ * console.log(treeHeight); // > 0;
244
+ *
245
+ * // Remaining operators are still accessible
246
+ * const remaining = operatorTree.get(2);
247
+ * console.log(remaining); // defined;
159
248
  * @example
160
249
  * // Find lowest common ancestor
161
- * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
250
+ * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
162
251
  *
163
252
  * // LCA helper function
164
253
  * const findLCA = (num1: number, num2: number): number | undefined => {
@@ -178,9 +267,9 @@ export declare class BSTNode<K = any, V = any> {
178
267
  * }
179
268
  *
180
269
  * // Assertions
181
- * console.log(findLCA(3, 10)); // 7
182
- * console.log(findLCA(5, 35)); // 15
183
- * console.log(findLCA(20, 30)); // 25
270
+ * console.log(findLCA(3, 10)); // 7;
271
+ * console.log(findLCA(5, 35)); // 15;
272
+ * console.log(findLCA(20, 30)); // 25;
184
273
  */
185
274
  export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
186
275
  /**
@@ -190,7 +279,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
190
279
  * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
191
280
  * @param [options] - Configuration options for the BST, including comparator.
192
281
  */
193
- constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode<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>);
194
283
  protected _root?: BSTNode<K, V>;
195
284
  /**
196
285
  * Gets the root node of the tree.
@@ -199,17 +288,16 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
199
288
  * @returns The root node.
200
289
  */
201
290
  get root(): OptNode<BSTNode<K, V>>;
202
- protected _isReverse: boolean;
203
291
  /**
204
- * Gets whether the tree's comparison logic is reversed.
205
- * @remarks Time O(1)
206
- *
207
- * @returns True if the tree is reversed (e.g., a max-heap logic).
292
+ * (Protected) Creates the default comparator function for keys that don't have a custom comparator.
293
+ * @remarks Time O(1) Space O(1)
294
+ * @returns The default comparator function.
208
295
  */
209
- get isReverse(): boolean;
296
+ protected _createDefaultComparator(): Comparator<K>;
210
297
  /**
211
- * The default comparator function.
212
- * @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)
213
301
  */
214
302
  protected _comparator: Comparator<K>;
215
303
  /**
@@ -219,14 +307,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
219
307
  * @returns The comparator function.
220
308
  */
221
309
  get comparator(): Comparator<K>;
222
- protected _specifyComparable?: (key: K) => Comparable;
223
- /**
224
- * Gets the function used to extract a comparable value from a complex key.
225
- * @remarks Time O(1)
226
- *
227
- * @returns The key-to-comparable conversion function.
228
- */
229
- get specifyComparable(): ((key: K) => Comparable) | undefined;
230
310
  /**
231
311
  * (Protected) Creates a new BST node.
232
312
  * @remarks Time O(1), Space O(1)
@@ -355,6 +435,28 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
355
435
  * @returns An array of booleans indicating the success of each individual `add` operation.
356
436
  */
357
437
  addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
438
+ /**
439
+ * Returns the first node with a key greater than or equal to the given key.
440
+ * This is equivalent to C++ std::lower_bound on a BST.
441
+ * Supports RECURSIVE and ITERATIVE implementations.
442
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
443
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
444
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
445
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
446
+ * @returns The first node with key >= given key, or undefined if no such node exists.
447
+ */
448
+ lowerBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
449
+ /**
450
+ * Returns the first node with a key strictly greater than the given key.
451
+ * This is equivalent to C++ std::upper_bound on a BST.
452
+ * Supports RECURSIVE and ITERATIVE implementations.
453
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
454
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
455
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
456
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
457
+ * @returns The first node with key > given key, or undefined if no such node exists.
458
+ */
459
+ upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
358
460
  /**
359
461
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
360
462
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -396,15 +498,76 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
396
498
  * @param [thisArg] - `this` context for the callback.
397
499
  * @returns A new, mapped BST.
398
500
  */
399
- map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<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>;
400
502
  /**
401
- * Deletes the first node found that satisfies the predicate.
402
- * @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.
403
519
  *
404
- * @param predicate - A function to test each [key, value] pair.
405
- * @returns True if a node was deleted, false otherwise.
520
+ * @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
521
+ * If false (default), searches for and deletes all matching nodes.
522
+ *
523
+ * @param startNode - The node to start the search from. Can be:
524
+ * - A key, node, or entry: the method resolves it to a node and searches from that subtree.
525
+ * - null or undefined: defaults to the root, searching the entire tree.
526
+ * - Default value: this._root (the tree's root).
527
+ *
528
+ * @param iterationType - Controls the internal traversal implementation:
529
+ * - 'RECURSIVE': uses recursive function calls for traversal.
530
+ * - 'ITERATIVE': uses explicit stack-based iteration.
531
+ * - Default: this.iterationType (the tree's default iteration mode).
532
+ *
533
+ * @returns A Map<K, boolean> containing the deletion results:
534
+ * - Key: the matched node's key.
535
+ * - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
536
+ * - If no nodes match the search criteria, the returned map is empty.
406
537
  */
407
- deleteWhere(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>>[];
539
+ /**
540
+ * (Protected) Core bound search implementation supporting all parameter types.
541
+ * Unified logic for both lowerBound and upperBound.
542
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
543
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
544
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
545
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
546
+ * @returns The first matching node, or undefined if no such node exists.
547
+ */
548
+ protected _bound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
549
+ /**
550
+ * (Protected) Binary search for bound by key with pruning optimization.
551
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
552
+ * For lowerBound: finds first node where key >= target.
553
+ * For upperBound: finds first node where key > target.
554
+ * @param key - The target key to search for.
555
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
556
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
557
+ * @returns The first node matching the bound condition, or undefined if none exists.
558
+ */
559
+ protected _boundByKey(key: K, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
560
+ /**
561
+ * (Protected) In-order traversal search by predicate.
562
+ * Falls back to linear in-order traversal when predicate-based search is required.
563
+ * Returns the first node that satisfies the predicate function.
564
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
565
+ * Time Complexity: O(n) since it may visit every node.
566
+ * @param predicate - The predicate function to test nodes.
567
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
568
+ * @returns The first node satisfying predicate, or undefined if none found.
569
+ */
570
+ protected _boundByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
408
571
  /**
409
572
  * (Protected) Creates a new, empty instance of the same BST constructor.
410
573
  * @remarks Time O(1)
@@ -450,7 +613,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
450
613
  protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
451
614
  /**
452
615
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
453
- * @remarks Time O(1) (or O(C) if `specifyComparable` is used).
616
+ * @remarks Time O(1) Space O(1)
454
617
  *
455
618
  * @param a - The first key.
456
619
  * @param b - The second key.
@@ -464,5 +627,5 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
464
627
  * @param key - The key of the node to delete.
465
628
  * @returns True if the node was found and deleted, false otherwise.
466
629
  */
467
- private _deleteByKey;
630
+ protected _deleteByKey(key: K): boolean;
468
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> {
@@ -112,48 +112,97 @@ export declare class RedBlackTreeNode<K = any, V = any> {
112
112
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
113
113
  *
114
114
  * @example
115
- * // using Red-Black Tree as a price-based index for stock data
116
- * // Define the structure of individual stock records
117
- * interface StockRecord {
118
- * price: number; // Stock price (key for indexing)
119
- * symbol: string; // Stock ticker symbol
120
- * volume: number; // Trade volume
115
+ * // basic Red-Black Tree with simple number keys
116
+ * // Create a simple Red-Black Tree with numeric keys
117
+ * const tree = new RedBlackTree([5, 2, 8, 1, 9]);
118
+ *
119
+ * tree.print();
120
+ * // _2___
121
+ * // / \
122
+ * // 1 _8_
123
+ * // / \
124
+ * // 5 9
125
+ *
126
+ * // Verify the tree maintains sorted order
127
+ * console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
128
+ *
129
+ * // Check size
130
+ * console.log(tree.size); // 5;
131
+ * @example
132
+ * // Red-Black Tree with key-value pairs for lookups
133
+ * interface Employee {
134
+ * id: number;
135
+ * name: string;
121
136
  * }
122
137
  *
123
- * // Simulate stock market data as it might come from an external feed
124
- * const marketStockData: StockRecord[] = [
125
- * { price: 142.5, symbol: 'AAPL', volume: 1000000 },
126
- * { price: 335.2, symbol: 'MSFT', volume: 800000 },
127
- * { price: 3285.04, symbol: 'AMZN', volume: 500000 },
128
- * { price: 267.98, symbol: 'META', volume: 750000 },
129
- * { price: 234.57, symbol: 'GOOGL', volume: 900000 }
130
- * ];
138
+ * // Create tree with employee data
139
+ * const employees = new RedBlackTree<number, Employee>([
140
+ * [1, { id: 1, name: 'Alice' }],
141
+ * [3, { id: 3, name: 'Charlie' }],
142
+ * [2, { id: 2, name: 'Bob' }]
143
+ * ]);
144
+ *
145
+ * // Retrieve employee by ID
146
+ * const alice = employees.get(1);
147
+ * console.log(alice?.name); // 'Alice';
148
+ *
149
+ * // Verify sorted order by ID
150
+ * console.log([...employees.keys()]); // [1, 2, 3];
151
+ * @example
152
+ * // Red-Black Tree range search for filtering
153
+ * interface Product {
154
+ * name: string;
155
+ * price: number;
156
+ * }
131
157
  *
132
- * // Extend the stock record type to include metadata for database usage
133
- * type StockTableRecord = StockRecord & { lastUpdated: Date };
158
+ * const products = new RedBlackTree<number, Product>([
159
+ * [10, { name: 'Item A', price: 10 }],
160
+ * [25, { name: 'Item B', price: 25 }],
161
+ * [40, { name: 'Item C', price: 40 }],
162
+ * [50, { name: 'Item D', price: 50 }]
163
+ * ]);
134
164
  *
135
- * // Create a Red-Black Tree to index stock records by price
136
- * // Simulates a database index with stock price as the key for quick lookups
137
- * const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
138
- * toEntryFn: stockRecord => [
139
- * stockRecord.price, // Use stock price as the key
140
- * {
141
- * ...stockRecord,
142
- * lastUpdated: new Date() // Add a timestamp for when the record was indexed
143
- * }
144
- * ]
165
+ * // Find products in price range [20, 45]
166
+ * const pricesInRange = products.rangeSearch([20, 45], node => {
167
+ * return products.get(node)?.name;
145
168
  * });
146
169
  *
147
- * // Query the stock with the highest price
148
- * const highestPricedStock = priceIndex.getRightMost();
149
- * console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
170
+ * console.log(pricesInRange); // ['Item B', 'Item C'];
171
+ * @example
172
+ * // Red-Black Tree as database index for stock market data
173
+ * interface StockPrice {
174
+ * symbol: string;
175
+ * volume: number;
176
+ * timestamp: Date;
177
+ * }
178
+ *
179
+ * // Simulate real-time stock price index
180
+ * const priceIndex = new RedBlackTree<number, StockPrice>([
181
+ * [142.5, { symbol: 'AAPL', volume: 1000000, timestamp: new Date() }],
182
+ * [335.2, { symbol: 'MSFT', volume: 800000, timestamp: new Date() }],
183
+ * [3285.04, { symbol: 'AMZN', volume: 500000, timestamp: new Date() }],
184
+ * [267.98, { symbol: 'META', volume: 750000, timestamp: new Date() }],
185
+ * [234.57, { symbol: 'GOOGL', volume: 900000, timestamp: new Date() }]
186
+ * ]);
187
+ *
188
+ * // Find highest-priced stock
189
+ * const maxPrice = priceIndex.getRightMost();
190
+ * console.log(priceIndex.get(maxPrice)?.symbol); // 'AMZN';
191
+ *
192
+ * // Find stocks in price range [200, 400] for portfolio balancing
193
+ * const stocksInRange = priceIndex.rangeSearch([200, 400], node => {
194
+ * const stock = priceIndex.get(node);
195
+ * return {
196
+ * symbol: stock?.symbol,
197
+ * price: node,
198
+ * volume: stock?.volume
199
+ * };
200
+ * });
150
201
  *
151
- * // Query stocks within a specific price range (200 to 400)
152
- * const stocksInRange = priceIndex.rangeSearch(
153
- * [200, 400], // Price range
154
- * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
155
- * );
156
- * console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
202
+ * console.log(stocksInRange.length); // 3;
203
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'GOOGL')); // true;
204
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'META')); // true;
205
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'MSFT')); // true;
157
206
  */
158
207
  export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
159
208
  constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
@@ -212,7 +261,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
212
261
  * @param [thisArg] - See parameter type for details.
213
262
  * @returns A new RedBlackTree with mapped entries.
214
263
  */
215
- 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>;
216
265
  protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
217
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>;
218
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
  /**
@@ -116,7 +116,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
116
116
  *
117
117
  * @example
118
118
  * // players ranked by score with their equipment
119
- * type Equipment = {
119
+ * type Equipment = {
120
120
  * name: string; // Equipment name
121
121
  * quality: 'legendary' | 'epic' | 'rare' | 'common';
122
122
  * level: number;
@@ -277,7 +277,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
277
277
  * // },
278
278
  * // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
279
279
  * // ]
280
- * // ]
280
+ * // ];
281
281
  */
282
282
  export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
283
283
  /**
@@ -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
  }