data-structure-typed 2.2.3 → 2.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -1
- package/README.md +70 -11
- package/dist/cjs/index.cjs +85 -75
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +85 -75
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +85 -75
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +85 -75
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +46 -26
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/data-structure-typed.js +85 -75
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +4 -5
- package/src/data-structures/binary-tree/bst.ts +111 -82
- package/src/data-structures/binary-tree/red-black-tree.ts +1 -2
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +7 -8
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +5 -8
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +2 -2
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult,
|
|
8
|
+
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree } from './avl-tree';
|
|
11
11
|
/**
|
|
@@ -186,7 +186,7 @@ export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K
|
|
|
186
186
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
187
187
|
* @returns A new AVLTreeCounter with mapped entries.
|
|
188
188
|
*/
|
|
189
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
189
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<AVLTreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<MK, MV, MR>;
|
|
190
190
|
/**
|
|
191
191
|
* (Protected) Create an empty instance of the same concrete class.
|
|
192
192
|
* @remarks Time O(1), Space O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapOptions,
|
|
8
|
+
import type { AVLTreeMultiMapOptions, ElemOf, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -160,7 +160,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
160
160
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
161
161
|
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
162
162
|
*/
|
|
163
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
163
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
164
164
|
/**
|
|
165
165
|
* Create a new tree by mapping each [key, values] bucket.
|
|
166
166
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -172,7 +172,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
172
172
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
173
173
|
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
174
174
|
*/
|
|
175
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
175
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
176
176
|
/**
|
|
177
177
|
* (Protected) Create an empty instance of the same concrete class.
|
|
178
178
|
* @remarks Time O(1), Space O(1)
|
|
@@ -182,7 +182,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
182
182
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
183
183
|
* @returns An empty like-kind instance.
|
|
184
184
|
*/
|
|
185
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
185
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): this;
|
|
186
186
|
/**
|
|
187
187
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
188
188
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -193,5 +193,5 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
193
193
|
* @param [options] - Options merged with the current snapshot.
|
|
194
194
|
* @returns A like-kind AVLTree built from the iterable.
|
|
195
195
|
*/
|
|
196
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
196
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeMultiMapOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
197
197
|
}
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST } from './bst';
|
|
9
9
|
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
10
|
-
import { BSTOptions } from '../../types';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
/**
|
|
13
12
|
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
@@ -359,7 +358,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
359
358
|
* @param [options] - Options for the new tree.
|
|
360
359
|
* @returns A new, empty tree.
|
|
361
360
|
*/
|
|
362
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
361
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
|
|
363
362
|
/**
|
|
364
363
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
365
364
|
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
@@ -369,7 +368,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
369
368
|
* @param [options] - Options for the new tree.
|
|
370
369
|
* @returns A new AVLTree.
|
|
371
370
|
*/
|
|
372
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
371
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
373
372
|
/**
|
|
374
373
|
* (Protected) Swaps properties of two nodes, including height.
|
|
375
374
|
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparator, CP, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
|
|
9
9
|
import { BinaryTree } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -279,7 +279,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
279
279
|
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
280
280
|
* @param [options] - Configuration options for the BST, including comparator.
|
|
281
281
|
*/
|
|
282
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode
|
|
282
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
|
|
283
283
|
protected _root?: BSTNode<K, V>;
|
|
284
284
|
/**
|
|
285
285
|
* Gets the root node of the tree.
|
|
@@ -288,17 +288,16 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
288
288
|
* @returns The root node.
|
|
289
289
|
*/
|
|
290
290
|
get root(): OptNode<BSTNode<K, V>>;
|
|
291
|
-
protected _isReverse: boolean;
|
|
292
291
|
/**
|
|
293
|
-
*
|
|
294
|
-
* @remarks Time O(1)
|
|
295
|
-
*
|
|
296
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
292
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
293
|
+
* @remarks Time O(1) Space O(1)
|
|
294
|
+
* @returns The default comparator function.
|
|
297
295
|
*/
|
|
298
|
-
|
|
296
|
+
protected _createDefaultComparator(): Comparator<K>;
|
|
299
297
|
/**
|
|
300
|
-
* The
|
|
301
|
-
|
|
298
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
299
|
+
|
|
300
|
+
* @remarks Time O(1) Space O(1)
|
|
302
301
|
*/
|
|
303
302
|
protected _comparator: Comparator<K>;
|
|
304
303
|
/**
|
|
@@ -308,14 +307,6 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
308
307
|
* @returns The comparator function.
|
|
309
308
|
*/
|
|
310
309
|
get comparator(): Comparator<K>;
|
|
311
|
-
protected _specifyComparable?: (key: K) => Comparable;
|
|
312
|
-
/**
|
|
313
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
314
|
-
* @remarks Time O(1)
|
|
315
|
-
*
|
|
316
|
-
* @returns The key-to-comparable conversion function.
|
|
317
|
-
*/
|
|
318
|
-
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
319
310
|
/**
|
|
320
311
|
* (Protected) Creates a new BST node.
|
|
321
312
|
* @remarks Time O(1), Space O(1)
|
|
@@ -507,15 +498,44 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
507
498
|
* @param [thisArg] - `this` context for the callback.
|
|
508
499
|
* @returns A new, mapped BST.
|
|
509
500
|
*/
|
|
510
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
501
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BSTOptions<MK, MV, MR>>, thisArg?: unknown): BST<MK, MV, MR>;
|
|
511
502
|
/**
|
|
512
|
-
* Deletes
|
|
513
|
-
*
|
|
503
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
504
|
+
*
|
|
505
|
+
* @remarks
|
|
506
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
507
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
508
|
+
*
|
|
509
|
+
* @template K - The key type.
|
|
510
|
+
* @template V - The value type.
|
|
511
|
+
*
|
|
512
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
513
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
514
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
515
|
+
* - An entry tuple: searches for the key-value pair.
|
|
516
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
517
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
518
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
519
|
+
*
|
|
520
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
521
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
522
|
+
*
|
|
523
|
+
* @param startNode - The node to start the search from. Can be:
|
|
524
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
525
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
526
|
+
* - Default value: this._root (the tree's root).
|
|
527
|
+
*
|
|
528
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
529
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
530
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
531
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
514
532
|
*
|
|
515
|
-
* @
|
|
516
|
-
*
|
|
533
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
534
|
+
* - Key: the matched node's key.
|
|
535
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
536
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
517
537
|
*/
|
|
518
|
-
deleteWhere(
|
|
538
|
+
deleteWhere(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeDeleteResult<BSTNode<K, V>>[];
|
|
519
539
|
/**
|
|
520
540
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
521
541
|
* Unified logic for both lowerBound and upperBound.
|
|
@@ -593,7 +613,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
593
613
|
protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
|
|
594
614
|
/**
|
|
595
615
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
596
|
-
* @remarks Time O(1)
|
|
616
|
+
* @remarks Time O(1) Space O(1)
|
|
597
617
|
*
|
|
598
618
|
* @param a - The first key.
|
|
599
619
|
* @param b - The second key.
|
|
@@ -607,5 +627,5 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
607
627
|
* @param key - The key of the node to delete.
|
|
608
628
|
* @returns True if the node was found and deleted, false otherwise.
|
|
609
629
|
*/
|
|
610
|
-
|
|
630
|
+
protected _deleteByKey(key: K): boolean;
|
|
611
631
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult,
|
|
8
|
+
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
@@ -261,7 +261,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
261
261
|
* @param [thisArg] - See parameter type for details.
|
|
262
262
|
* @returns A new RedBlackTree with mapped entries.
|
|
263
263
|
*/
|
|
264
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
264
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
265
265
|
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
266
266
|
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
267
267
|
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult,
|
|
9
|
-
import { BSTOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
10
9
|
import { BSTNode } from './bst';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -188,7 +187,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
188
187
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
189
188
|
* @returns A new TreeCounter with mapped entries.
|
|
190
189
|
*/
|
|
191
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
190
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<TreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): TreeCounter<MK, MV, MR>;
|
|
192
191
|
/**
|
|
193
192
|
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
194
193
|
* @remarks Time O(N), Space O(N)
|
|
@@ -204,7 +203,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
204
203
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
205
204
|
* @returns An empty like-kind instance.
|
|
206
205
|
*/
|
|
207
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
206
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this;
|
|
208
207
|
/**
|
|
209
208
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
210
209
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -215,7 +214,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
215
214
|
* @param [options] - Options merged with the current snapshot.
|
|
216
215
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
217
216
|
*/
|
|
218
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
217
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeCounterOptions<TK, TV, TR>>): TreeCounter<TK, TV, TR>;
|
|
219
218
|
/**
|
|
220
219
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
221
220
|
* @remarks Time O(1), Space O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor,
|
|
8
|
+
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -307,8 +307,8 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
307
307
|
* @returns True if the value was removed; false if not found.
|
|
308
308
|
*/
|
|
309
309
|
deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
|
|
310
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
311
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
310
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
311
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<TreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
312
312
|
/**
|
|
313
313
|
* (Protected) Create an empty instance of the same concrete class.
|
|
314
314
|
* @remarks Time O(1), Space O(1)
|
|
@@ -318,7 +318,7 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
318
318
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
319
319
|
* @returns An empty like-kind instance.
|
|
320
320
|
*/
|
|
321
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
321
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this;
|
|
322
322
|
/**
|
|
323
323
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
324
324
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -329,5 +329,5 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
329
329
|
* @param [options] - Options merged with the current snapshot.
|
|
330
330
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
331
331
|
*/
|
|
332
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
332
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
333
333
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export type BSTOptions<K, V, R> =
|
|
5
|
-
|
|
6
|
-
isReverse?: boolean;
|
|
2
|
+
import type { Comparator, OptValue } from '../../common';
|
|
3
|
+
type BSTBaseOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'>;
|
|
4
|
+
export type BSTOptions<K, V, R> = BSTBaseOptions<K, V, R> & {
|
|
5
|
+
comparator?: Comparator<K>;
|
|
7
6
|
};
|
|
8
7
|
export type BSTNOptKey<K> = K | undefined;
|
|
9
8
|
export type OptNode<NODE> = NODE | undefined;
|
|
10
9
|
export type BSTNEntry<K, V> = [BSTNOptKey<K>, OptValue<V>];
|
|
11
10
|
export type BSTNOptKeyOrNode<K, NODE> = BSTNOptKey<K> | NODE;
|
|
12
11
|
export type BSTNRep<K, V, NODE> = BSTNEntry<K, V> | BSTNOptKeyOrNode<K, NODE>;
|
|
12
|
+
export {};
|
|
@@ -8628,36 +8628,20 @@ var dataStructureTyped = (() => {
|
|
|
8628
8628
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
8629
8629
|
super([], options);
|
|
8630
8630
|
__publicField(this, "_root");
|
|
8631
|
-
__publicField(this, "_isReverse", false);
|
|
8632
8631
|
/**
|
|
8633
|
-
|
|
8634
|
-
|
|
8635
|
-
|
|
8636
|
-
|
|
8637
|
-
|
|
8638
|
-
if (a > b) return 1;
|
|
8639
|
-
if (a < b) return -1;
|
|
8640
|
-
return 0;
|
|
8641
|
-
}
|
|
8642
|
-
if (this._specifyComparable) {
|
|
8643
|
-
const va = this._specifyComparable(a);
|
|
8644
|
-
const vb = this._specifyComparable(b);
|
|
8645
|
-
if (va > vb) return 1;
|
|
8646
|
-
if (va < vb) return -1;
|
|
8647
|
-
return 0;
|
|
8648
|
-
}
|
|
8649
|
-
if (typeof a === "object" || typeof b === "object") {
|
|
8650
|
-
throw TypeError(
|
|
8651
|
-
`When comparing object types, a custom specifyComparable must be defined in the constructor's options.`
|
|
8652
|
-
);
|
|
8653
|
-
}
|
|
8654
|
-
return 0;
|
|
8655
|
-
});
|
|
8656
|
-
__publicField(this, "_specifyComparable");
|
|
8632
|
+
* The comparator function used to determine the order of keys in the tree.
|
|
8633
|
+
|
|
8634
|
+
* @remarks Time O(1) Space O(1)
|
|
8635
|
+
*/
|
|
8636
|
+
__publicField(this, "_comparator");
|
|
8657
8637
|
if (options) {
|
|
8658
|
-
|
|
8659
|
-
|
|
8660
|
-
|
|
8638
|
+
if ("comparator" in options && options.comparator !== void 0) {
|
|
8639
|
+
this._comparator = options.comparator;
|
|
8640
|
+
} else {
|
|
8641
|
+
this._comparator = this._createDefaultComparator();
|
|
8642
|
+
}
|
|
8643
|
+
} else {
|
|
8644
|
+
this._comparator = this._createDefaultComparator();
|
|
8661
8645
|
}
|
|
8662
8646
|
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
8663
8647
|
}
|
|
@@ -8671,13 +8655,25 @@ var dataStructureTyped = (() => {
|
|
|
8671
8655
|
return this._root;
|
|
8672
8656
|
}
|
|
8673
8657
|
/**
|
|
8674
|
-
*
|
|
8675
|
-
* @remarks Time O(1)
|
|
8676
|
-
*
|
|
8677
|
-
* @returns True if the tree is reversed (e.g., a max-heap logic).
|
|
8658
|
+
* (Protected) Creates the default comparator function for keys that don't have a custom comparator.
|
|
8659
|
+
* @remarks Time O(1) Space O(1)
|
|
8660
|
+
* @returns The default comparator function.
|
|
8678
8661
|
*/
|
|
8679
|
-
|
|
8680
|
-
return
|
|
8662
|
+
_createDefaultComparator() {
|
|
8663
|
+
return (a, b) => {
|
|
8664
|
+
debugger;
|
|
8665
|
+
if (isComparable(a) && isComparable(b)) {
|
|
8666
|
+
if (a > b) return 1;
|
|
8667
|
+
if (a < b) return -1;
|
|
8668
|
+
return 0;
|
|
8669
|
+
}
|
|
8670
|
+
if (typeof a === "object" || typeof b === "object") {
|
|
8671
|
+
throw TypeError(
|
|
8672
|
+
`When comparing object type keys, a custom comparator must be provided in the constructor's options!`
|
|
8673
|
+
);
|
|
8674
|
+
}
|
|
8675
|
+
return 0;
|
|
8676
|
+
};
|
|
8681
8677
|
}
|
|
8682
8678
|
/**
|
|
8683
8679
|
* Gets the comparator function used by the tree.
|
|
@@ -8688,15 +8684,6 @@ var dataStructureTyped = (() => {
|
|
|
8688
8684
|
get comparator() {
|
|
8689
8685
|
return this._comparator;
|
|
8690
8686
|
}
|
|
8691
|
-
/**
|
|
8692
|
-
* Gets the function used to extract a comparable value from a complex key.
|
|
8693
|
-
* @remarks Time O(1)
|
|
8694
|
-
*
|
|
8695
|
-
* @returns The key-to-comparable conversion function.
|
|
8696
|
-
*/
|
|
8697
|
-
get specifyComparable() {
|
|
8698
|
-
return this._specifyComparable;
|
|
8699
|
-
}
|
|
8700
8687
|
/**
|
|
8701
8688
|
* (Protected) Creates a new BST node.
|
|
8702
8689
|
* @remarks Time O(1), Space O(1)
|
|
@@ -8738,7 +8725,7 @@ var dataStructureTyped = (() => {
|
|
|
8738
8725
|
* @returns True if the key is valid, false otherwise.
|
|
8739
8726
|
*/
|
|
8740
8727
|
isValidKey(key) {
|
|
8741
|
-
return isComparable(key
|
|
8728
|
+
return isComparable(key);
|
|
8742
8729
|
}
|
|
8743
8730
|
/**
|
|
8744
8731
|
* Performs a Depth-First Search (DFS) traversal.
|
|
@@ -8828,8 +8815,8 @@ var dataStructureTyped = (() => {
|
|
|
8828
8815
|
if (!this.isRealNode(cur.left)) return false;
|
|
8829
8816
|
if (isRange) {
|
|
8830
8817
|
const range = keyNodeEntryOrPredicate;
|
|
8831
|
-
const leftS =
|
|
8832
|
-
const leftI =
|
|
8818
|
+
const leftS = range.low;
|
|
8819
|
+
const leftI = range.includeLow;
|
|
8833
8820
|
return leftI && this._compare(cur.key, leftS) >= 0 || !leftI && this._compare(cur.key, leftS) > 0;
|
|
8834
8821
|
}
|
|
8835
8822
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -8843,8 +8830,8 @@ var dataStructureTyped = (() => {
|
|
|
8843
8830
|
if (!this.isRealNode(cur.right)) return false;
|
|
8844
8831
|
if (isRange) {
|
|
8845
8832
|
const range = keyNodeEntryOrPredicate;
|
|
8846
|
-
const rightS =
|
|
8847
|
-
const rightI =
|
|
8833
|
+
const rightS = range.high;
|
|
8834
|
+
const rightI = range.includeHigh;
|
|
8848
8835
|
return rightI && this._compare(cur.key, rightS) <= 0 || !rightI && this._compare(cur.key, rightS) < 0;
|
|
8849
8836
|
}
|
|
8850
8837
|
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) {
|
|
@@ -9165,31 +9152,55 @@ var dataStructureTyped = (() => {
|
|
|
9165
9152
|
return out;
|
|
9166
9153
|
}
|
|
9167
9154
|
/**
|
|
9168
|
-
* Deletes
|
|
9169
|
-
* @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.
|
|
9155
|
+
* Deletes nodes that match a key, node, entry, predicate, or range.
|
|
9170
9156
|
*
|
|
9171
|
-
* @
|
|
9172
|
-
*
|
|
9173
|
-
|
|
9174
|
-
|
|
9175
|
-
|
|
9176
|
-
|
|
9177
|
-
|
|
9178
|
-
|
|
9179
|
-
|
|
9180
|
-
|
|
9181
|
-
|
|
9182
|
-
|
|
9183
|
-
|
|
9184
|
-
|
|
9185
|
-
|
|
9186
|
-
|
|
9187
|
-
|
|
9188
|
-
|
|
9189
|
-
|
|
9190
|
-
|
|
9157
|
+
* @remarks
|
|
9158
|
+
* Time Complexity: O(N) for search + O(M log N) for M deletions, where N is tree size.
|
|
9159
|
+
* Space Complexity: O(M) for storing matched nodes and result map.
|
|
9160
|
+
*
|
|
9161
|
+
* @template K - The key type.
|
|
9162
|
+
* @template V - The value type.
|
|
9163
|
+
*
|
|
9164
|
+
* @param keyNodeEntryOrPredicate - The search criteria. Can be one of:
|
|
9165
|
+
* - A key (type K): searches for exact key match using the comparator.
|
|
9166
|
+
* - A BSTNode: searches for the matching node in the tree.
|
|
9167
|
+
* - An entry tuple: searches for the key-value pair.
|
|
9168
|
+
* - A NodePredicate function: tests each node and returns true for matches.
|
|
9169
|
+
* - A Range object: searches for nodes whose keys fall within the specified range (inclusive/exclusive based on range settings).
|
|
9170
|
+
* - null or undefined: treated as no match, returns empty results.
|
|
9171
|
+
*
|
|
9172
|
+
* @param onlyOne - If true, stops the search after finding the first match and only deletes that one node.
|
|
9173
|
+
* If false (default), searches for and deletes all matching nodes.
|
|
9174
|
+
*
|
|
9175
|
+
* @param startNode - The node to start the search from. Can be:
|
|
9176
|
+
* - A key, node, or entry: the method resolves it to a node and searches from that subtree.
|
|
9177
|
+
* - null or undefined: defaults to the root, searching the entire tree.
|
|
9178
|
+
* - Default value: this._root (the tree's root).
|
|
9179
|
+
*
|
|
9180
|
+
* @param iterationType - Controls the internal traversal implementation:
|
|
9181
|
+
* - 'RECURSIVE': uses recursive function calls for traversal.
|
|
9182
|
+
* - 'ITERATIVE': uses explicit stack-based iteration.
|
|
9183
|
+
* - Default: this.iterationType (the tree's default iteration mode).
|
|
9184
|
+
*
|
|
9185
|
+
* @returns A Map<K, boolean> containing the deletion results:
|
|
9186
|
+
* - Key: the matched node's key.
|
|
9187
|
+
* - Value: true if the deletion succeeded, false if it failed (e.g., key not found during deletion phase).
|
|
9188
|
+
* - If no nodes match the search criteria, the returned map is empty.
|
|
9189
|
+
*/
|
|
9190
|
+
deleteWhere(keyNodeEntryOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
9191
|
+
const toDelete = this.search(
|
|
9192
|
+
keyNodeEntryOrPredicate,
|
|
9193
|
+
onlyOne,
|
|
9194
|
+
(node) => node,
|
|
9195
|
+
startNode,
|
|
9196
|
+
iterationType
|
|
9197
|
+
);
|
|
9198
|
+
let results = [];
|
|
9199
|
+
for (const node of toDelete) {
|
|
9200
|
+
const deleteInfo = this.delete(node);
|
|
9201
|
+
results = results.concat(deleteInfo);
|
|
9191
9202
|
}
|
|
9192
|
-
return
|
|
9203
|
+
return results;
|
|
9193
9204
|
}
|
|
9194
9205
|
/**
|
|
9195
9206
|
* (Protected) Core bound search implementation supporting all parameter types.
|
|
@@ -9342,8 +9353,7 @@ var dataStructureTyped = (() => {
|
|
|
9342
9353
|
_snapshotOptions() {
|
|
9343
9354
|
return {
|
|
9344
9355
|
...super._snapshotOptions(),
|
|
9345
|
-
|
|
9346
|
-
isReverse: this.isReverse
|
|
9356
|
+
comparator: this._comparator
|
|
9347
9357
|
};
|
|
9348
9358
|
}
|
|
9349
9359
|
/**
|
|
@@ -9371,14 +9381,14 @@ var dataStructureTyped = (() => {
|
|
|
9371
9381
|
}
|
|
9372
9382
|
/**
|
|
9373
9383
|
* (Protected) Compares two keys using the tree's comparator and reverse setting.
|
|
9374
|
-
* @remarks Time O(1)
|
|
9384
|
+
* @remarks Time O(1) Space O(1)
|
|
9375
9385
|
*
|
|
9376
9386
|
* @param a - The first key.
|
|
9377
9387
|
* @param b - The second key.
|
|
9378
9388
|
* @returns A number (1, -1, or 0) representing the comparison.
|
|
9379
9389
|
*/
|
|
9380
9390
|
_compare(a, b) {
|
|
9381
|
-
return this.
|
|
9391
|
+
return this._comparator(a, b);
|
|
9382
9392
|
}
|
|
9383
9393
|
/**
|
|
9384
9394
|
* (Private) Deletes a node by its key.
|