min-heap-typed 2.1.0 → 2.1.1
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/dist/data-structures/binary-tree/avl-tree-counter.d.ts +4 -4
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/data-structures/binary-tree/avl-tree.d.ts +4 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/red-black-tree.js +1 -1
- package/dist/data-structures/binary-tree/tree-counter.d.ts +4 -4
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +5 -5
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -7
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -10
- package/src/data-structures/binary-tree/avl-tree.ts +4 -6
- package/src/data-structures/binary-tree/binary-tree.ts +9 -11
- package/src/data-structures/binary-tree/bst.ts +5 -10
- package/src/data-structures/binary-tree/red-black-tree.ts +5 -10
- package/src/data-structures/binary-tree/tree-counter.ts +4 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +5 -10
- package/src/interfaces/binary-tree.ts +2 -2
|
@@ -61,7 +61,7 @@ export declare class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K,
|
|
|
61
61
|
* @template V
|
|
62
62
|
* @template R
|
|
63
63
|
*/
|
|
64
|
-
export declare class AVLTreeCounter<K = any, V = any, R
|
|
64
|
+
export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
65
65
|
/**
|
|
66
66
|
* Create a AVLTreeCounter instance
|
|
67
67
|
* @remarks Time O(n), Space O(n)
|
|
@@ -132,7 +132,7 @@ export declare class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
132
132
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
133
133
|
* @returns A new AVLTreeCounter with mapped entries.
|
|
134
134
|
*/
|
|
135
|
-
map<MK = K, MV = V, MR
|
|
135
|
+
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>;
|
|
136
136
|
/**
|
|
137
137
|
* (Protected) Create an empty instance of the same concrete class.
|
|
138
138
|
* @remarks Time O(1), Space O(1)
|
|
@@ -142,7 +142,7 @@ export declare class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
142
142
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
143
143
|
* @returns An empty like-kind instance.
|
|
144
144
|
*/
|
|
145
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
145
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): this;
|
|
146
146
|
/**
|
|
147
147
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
148
148
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -153,7 +153,7 @@ export declare class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
153
153
|
* @param [options] - Options merged with the current snapshot.
|
|
154
154
|
* @returns A like-kind AVLTreeCounter built from the iterable.
|
|
155
155
|
*/
|
|
156
|
-
protected _createLike<TK = K, TV = V, TR
|
|
156
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): AVLTreeCounter<TK, TV, TR>;
|
|
157
157
|
/**
|
|
158
158
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
159
159
|
* @remarks Time O(1), Space O(1)
|
|
@@ -60,7 +60,7 @@ export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K
|
|
|
60
60
|
* @template V
|
|
61
61
|
* @template R
|
|
62
62
|
*/
|
|
63
|
-
export declare class AVLTreeMultiMap<K = any, V = any, R
|
|
63
|
+
export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
64
64
|
/**
|
|
65
65
|
* Create an AVLTreeMultiMap and optionally bulk-insert items.
|
|
66
66
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -98,7 +98,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R extends object = object
|
|
|
98
98
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
99
99
|
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
100
100
|
*/
|
|
101
|
-
map<MK = K, MVArr extends unknown[] = V[], MR
|
|
101
|
+
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>;
|
|
102
102
|
/**
|
|
103
103
|
* Create a new tree by mapping each [key, values] bucket.
|
|
104
104
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -110,7 +110,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R extends object = object
|
|
|
110
110
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
111
111
|
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
112
112
|
*/
|
|
113
|
-
map<MK = K, MV = V[], MR
|
|
113
|
+
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>;
|
|
114
114
|
/**
|
|
115
115
|
* (Protected) Create an empty instance of the same concrete class.
|
|
116
116
|
* @remarks Time O(1), Space O(1)
|
|
@@ -120,7 +120,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R extends object = object
|
|
|
120
120
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
121
121
|
* @returns An empty like-kind instance.
|
|
122
122
|
*/
|
|
123
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
123
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
|
|
124
124
|
/**
|
|
125
125
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
126
126
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -131,5 +131,5 @@ export declare class AVLTreeMultiMap<K = any, V = any, R extends object = object
|
|
|
131
131
|
* @param [options] - Options merged with the current snapshot.
|
|
132
132
|
* @returns A like-kind AVLTree built from the iterable.
|
|
133
133
|
*/
|
|
134
|
-
protected _createLike<TK = K, TV = V, TR
|
|
134
|
+
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>;
|
|
135
135
|
}
|
|
@@ -136,7 +136,7 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
136
136
|
* // { minute: 15, temperature: 58.6 }
|
|
137
137
|
* // ]
|
|
138
138
|
*/
|
|
139
|
-
export declare class AVLTree<K = any, V = any, R
|
|
139
|
+
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
140
140
|
/**
|
|
141
141
|
* Creates an instance of AVLTree.
|
|
142
142
|
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
@@ -200,7 +200,7 @@ export declare class AVLTree<K = any, V = any, R extends object = object> extend
|
|
|
200
200
|
* @param [thisArg] - `this` context for the callback.
|
|
201
201
|
* @returns A new, mapped AVLTree.
|
|
202
202
|
*/
|
|
203
|
-
map<MK = K, MV = V, MR
|
|
203
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
204
204
|
/**
|
|
205
205
|
* (Protected) Creates a new, empty instance of the same AVLTree constructor.
|
|
206
206
|
* @remarks Time O(1)
|
|
@@ -209,7 +209,7 @@ export declare class AVLTree<K = any, V = any, R extends object = object> extend
|
|
|
209
209
|
* @param [options] - Options for the new tree.
|
|
210
210
|
* @returns A new, empty tree.
|
|
211
211
|
*/
|
|
212
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
212
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
213
213
|
/**
|
|
214
214
|
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
215
215
|
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
@@ -219,7 +219,7 @@ export declare class AVLTree<K = any, V = any, R extends object = object> extend
|
|
|
219
219
|
* @param [options] - Options for the new tree.
|
|
220
220
|
* @returns A new AVLTree.
|
|
221
221
|
*/
|
|
222
|
-
protected _createLike<TK = K, TV = V, TR
|
|
222
|
+
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>>): AVLTree<TK, TV, TR>;
|
|
223
223
|
/**
|
|
224
224
|
* (Protected) Swaps properties of two nodes, including height.
|
|
225
225
|
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
@@ -186,7 +186,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
|
|
|
186
186
|
*
|
|
187
187
|
* console.log(evaluate(expressionTree.root)); // -27
|
|
188
188
|
*/
|
|
189
|
-
export declare class BinaryTree<K = any, V = any, R
|
|
189
|
+
export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R> {
|
|
190
190
|
iterationType: IterationType;
|
|
191
191
|
/**
|
|
192
192
|
* Creates an instance of BinaryTree.
|
|
@@ -596,7 +596,7 @@ export declare class BinaryTree<K = any, V = any, R extends object = object> ext
|
|
|
596
596
|
* @param [thisArg] - `this` context for the callback.
|
|
597
597
|
* @returns A new, mapped tree.
|
|
598
598
|
*/
|
|
599
|
-
map<MK = K, MV = V, MR
|
|
599
|
+
map<MK = K, MV = V, MR = any>(cb: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BinaryTree<MK, MV, MR>;
|
|
600
600
|
/**
|
|
601
601
|
* Generates a string representation of the tree for visualization.
|
|
602
602
|
* @remarks Time O(N), visits every node. Space O(N*H) or O(N^2) in the worst case, as the string width can grow significantly.
|
|
@@ -638,7 +638,7 @@ export declare class BinaryTree<K = any, V = any, R extends object = object> ext
|
|
|
638
638
|
* @template TK, TV, TR - Generic types for the options.
|
|
639
639
|
* @returns The options object.
|
|
640
640
|
*/
|
|
641
|
-
protected _snapshotOptions<TK = K, TV = V, TR
|
|
641
|
+
protected _snapshotOptions<TK = K, TV = V, TR = R>(): BinaryTreeOptions<TK, TV, TR>;
|
|
642
642
|
/**
|
|
643
643
|
* (Protected) Creates a new, empty instance of the same tree constructor.
|
|
644
644
|
* @remarks Time O(1)
|
|
@@ -647,7 +647,7 @@ export declare class BinaryTree<K = any, V = any, R extends object = object> ext
|
|
|
647
647
|
* @param [options] - Options for the new tree.
|
|
648
648
|
* @returns A new, empty tree.
|
|
649
649
|
*/
|
|
650
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
650
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BinaryTreeOptions<TK, TV, TR>>): this;
|
|
651
651
|
/**
|
|
652
652
|
* (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
|
|
653
653
|
* @remarks Time O(N) (or as per constructor) due to processing the iterable.
|
|
@@ -657,7 +657,7 @@ export declare class BinaryTree<K = any, V = any, R extends object = object> ext
|
|
|
657
657
|
* @param [options] - Options for the new tree.
|
|
658
658
|
* @returns A new tree.
|
|
659
659
|
*/
|
|
660
|
-
protected _createLike<TK = K, TV = V, TR
|
|
660
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BinaryTreeOptions<TK, TV, TR>>): BinaryTree<TK, TV, TR>;
|
|
661
661
|
/**
|
|
662
662
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
663
663
|
* @remarks Time O(1)
|
|
@@ -967,8 +967,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
967
967
|
getLeftMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
968
968
|
if (this.isNIL(startNode))
|
|
969
969
|
return callback(undefined);
|
|
970
|
-
|
|
971
|
-
if (!this.isRealNode(
|
|
970
|
+
const ensuredStartNode = this.ensureNode(startNode);
|
|
971
|
+
if (!this.isRealNode(ensuredStartNode))
|
|
972
972
|
return callback(undefined);
|
|
973
973
|
if (iterationType === 'RECURSIVE') {
|
|
974
974
|
const dfs = (cur) => {
|
|
@@ -977,7 +977,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
977
977
|
return cur;
|
|
978
978
|
return dfs(left);
|
|
979
979
|
};
|
|
980
|
-
return callback(dfs(
|
|
980
|
+
return callback(dfs(ensuredStartNode));
|
|
981
981
|
}
|
|
982
982
|
else {
|
|
983
983
|
// Iterative (trampolined to prevent stack overflow, though 'ITERATIVE' usually means a loop)
|
|
@@ -987,7 +987,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
987
987
|
return cur;
|
|
988
988
|
return (0, utils_1.makeTrampolineThunk)(() => dfs(left));
|
|
989
989
|
});
|
|
990
|
-
return callback(dfs(
|
|
990
|
+
return callback(dfs(ensuredStartNode));
|
|
991
991
|
}
|
|
992
992
|
}
|
|
993
993
|
/**
|
|
@@ -127,7 +127,7 @@ export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
127
127
|
* console.log(findLCA(5, 35)); // 15
|
|
128
128
|
* console.log(findLCA(20, 30)); // 25
|
|
129
129
|
*/
|
|
130
|
-
export declare class BST<K = any, V = any, R
|
|
130
|
+
export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
131
131
|
/**
|
|
132
132
|
* Creates an instance of BST.
|
|
133
133
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -341,7 +341,7 @@ export declare class BST<K = any, V = any, R extends object = object> extends Bi
|
|
|
341
341
|
* @param [thisArg] - `this` context for the callback.
|
|
342
342
|
* @returns A new, mapped BST.
|
|
343
343
|
*/
|
|
344
|
-
map<MK = K, MV = V, MR
|
|
344
|
+
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>;
|
|
345
345
|
/**
|
|
346
346
|
* Deletes the first node found that satisfies the predicate.
|
|
347
347
|
* @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.
|
|
@@ -358,7 +358,7 @@ export declare class BST<K = any, V = any, R extends object = object> extends Bi
|
|
|
358
358
|
* @param [options] - Options for the new BST.
|
|
359
359
|
* @returns A new, empty BST.
|
|
360
360
|
*/
|
|
361
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
361
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
362
362
|
/**
|
|
363
363
|
* (Protected) Creates a new instance of the same BST constructor, potentially with different generic types.
|
|
364
364
|
* @remarks Time O(N log N) or O(N^2) (from constructor) due to processing the iterable.
|
|
@@ -368,7 +368,7 @@ export declare class BST<K = any, V = any, R extends object = object> extends Bi
|
|
|
368
368
|
* @param [options] - Options for the new BST.
|
|
369
369
|
* @returns A new BST.
|
|
370
370
|
*/
|
|
371
|
-
protected _createLike<TK = K, TV = V, TR
|
|
371
|
+
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>>): BST<TK, TV, TR>;
|
|
372
372
|
/**
|
|
373
373
|
* (Protected) Snapshots the current BST's configuration options.
|
|
374
374
|
* @remarks Time O(1)
|
|
@@ -376,7 +376,7 @@ export declare class BST<K = any, V = any, R extends object = object> extends Bi
|
|
|
376
376
|
* @template TK, TV, TR - Generic types for the options.
|
|
377
377
|
* @returns The options object.
|
|
378
378
|
*/
|
|
379
|
-
protected _snapshotOptions<TK = K, TV = V, TR
|
|
379
|
+
protected _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR>;
|
|
380
380
|
/**
|
|
381
381
|
* (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
|
|
382
382
|
* @remarks Time O(1)
|
|
@@ -49,7 +49,7 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
49
49
|
set right(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
52
|
+
* RRRRRRRRRRRRRRRRRRRed-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
53
53
|
* @remarks Time O(1), Space O(1)
|
|
54
54
|
* @template K
|
|
55
55
|
* @template V
|
|
@@ -100,7 +100,7 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
100
100
|
* );
|
|
101
101
|
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
102
102
|
*/
|
|
103
|
-
export declare class RedBlackTree<K = any, V = any, R
|
|
103
|
+
export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
104
104
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
|
|
105
105
|
protected _root: RedBlackTreeNode<K, V> | undefined;
|
|
106
106
|
/**
|
|
@@ -157,9 +157,9 @@ export declare class RedBlackTree<K = any, V = any, R extends object = object> e
|
|
|
157
157
|
* @param [thisArg] - See parameter type for details.
|
|
158
158
|
* @returns A new RedBlackTree with mapped entries.
|
|
159
159
|
*/
|
|
160
|
-
map<MK = K, MV = V, MR
|
|
161
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
162
|
-
protected _createLike<TK = K, TV = V, TR
|
|
160
|
+
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>;
|
|
161
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
162
|
+
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>;
|
|
163
163
|
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
164
164
|
protected _replaceNode(oldNode: RedBlackTreeNode<K, V>, newNode: RedBlackTreeNode<K, V>): RedBlackTreeNode<K, V>;
|
|
165
165
|
/**
|
|
@@ -68,7 +68,7 @@ class RedBlackTreeNode extends bst_1.BSTNode {
|
|
|
68
68
|
}
|
|
69
69
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
71
|
+
* RRRRRRRRRRRRRRRRRRRed-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
72
72
|
* @remarks Time O(1), Space O(1)
|
|
73
73
|
* @template K
|
|
74
74
|
* @template V
|
|
@@ -64,7 +64,7 @@ export declare class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<
|
|
|
64
64
|
* @template V
|
|
65
65
|
* @template R
|
|
66
66
|
*/
|
|
67
|
-
export declare class TreeCounter<K = any, V = any, R
|
|
67
|
+
export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
68
68
|
/**
|
|
69
69
|
* Create a TreeCounter and optionally bulk-insert items.
|
|
70
70
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -134,7 +134,7 @@ export declare class TreeCounter<K = any, V = any, R extends object = object> ex
|
|
|
134
134
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
135
135
|
* @returns A new TreeCounter with mapped entries.
|
|
136
136
|
*/
|
|
137
|
-
map<MK = K, MV = V, MR
|
|
137
|
+
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>;
|
|
138
138
|
/**
|
|
139
139
|
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
140
140
|
* @remarks Time O(N), Space O(N)
|
|
@@ -150,7 +150,7 @@ export declare class TreeCounter<K = any, V = any, R extends object = object> ex
|
|
|
150
150
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
151
151
|
* @returns An empty like-kind instance.
|
|
152
152
|
*/
|
|
153
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
153
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
154
154
|
/**
|
|
155
155
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
156
156
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -161,7 +161,7 @@ export declare class TreeCounter<K = any, V = any, R extends object = object> ex
|
|
|
161
161
|
* @param [options] - Options merged with the current snapshot.
|
|
162
162
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
163
163
|
*/
|
|
164
|
-
protected _createLike<TK = K, TV = V, TR
|
|
164
|
+
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>;
|
|
165
165
|
/**
|
|
166
166
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
167
167
|
* @remarks Time O(1), Space O(1)
|
|
@@ -224,7 +224,7 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
224
224
|
* // ]
|
|
225
225
|
* // ]
|
|
226
226
|
*/
|
|
227
|
-
export declare class TreeMultiMap<K = any, V = any, R
|
|
227
|
+
export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
228
228
|
/**
|
|
229
229
|
* Create a TreeMultiMap and optionally bulk-insert items.
|
|
230
230
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -244,8 +244,8 @@ export declare class TreeMultiMap<K = any, V = any, R extends object = object> e
|
|
|
244
244
|
* @returns True if the value was removed; false if not found.
|
|
245
245
|
*/
|
|
246
246
|
deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
|
|
247
|
-
map<MK = K, MVArr extends unknown[] = V[], MR
|
|
248
|
-
map<MK = K, MV = V[], MR
|
|
247
|
+
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>;
|
|
248
|
+
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>;
|
|
249
249
|
/**
|
|
250
250
|
* (Protected) Create an empty instance of the same concrete class.
|
|
251
251
|
* @remarks Time O(1), Space O(1)
|
|
@@ -255,7 +255,7 @@ export declare class TreeMultiMap<K = any, V = any, R extends object = object> e
|
|
|
255
255
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
256
256
|
* @returns An empty like-kind instance.
|
|
257
257
|
*/
|
|
258
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
258
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
259
259
|
/**
|
|
260
260
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
261
261
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -266,5 +266,5 @@ export declare class TreeMultiMap<K = any, V = any, R extends object = object> e
|
|
|
266
266
|
* @param [options] - Options merged with the current snapshot.
|
|
267
267
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
268
268
|
*/
|
|
269
|
-
protected _createLike<TK = K, TV = V, TR
|
|
269
|
+
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>;
|
|
270
270
|
}
|
|
@@ -5,7 +5,7 @@ import type { BinaryTreeDeleteResult, BinaryTreeOptions, BTNRep, DFSOrderPattern
|
|
|
5
5
|
* K = key, V = value, R = raw/record used with toEntryFn (optional).
|
|
6
6
|
* Transforming methods like `map` use method-level generics MK/MV/MR.
|
|
7
7
|
*/
|
|
8
|
-
export interface IBinaryTree<K = any, V = any, R
|
|
8
|
+
export interface IBinaryTree<K = any, V = any, R = any> {
|
|
9
9
|
readonly size: number;
|
|
10
10
|
readonly root: BinaryTreeNode<K, V> | null | undefined;
|
|
11
11
|
readonly isMapMode: boolean;
|
|
@@ -53,7 +53,7 @@ export interface IBinaryTree<K = any, V = any, R extends object = object> {
|
|
|
53
53
|
search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean, callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
54
54
|
clone(): this;
|
|
55
55
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
|
|
56
|
-
map<MK = K, MV = V, MR
|
|
56
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): IBinaryTree<MK, MV, MR>;
|
|
57
57
|
merge(anotherTree: IBinaryTree<K, V, R>): void;
|
|
58
58
|
refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
|
|
59
59
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Min Heap",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^2.1.
|
|
135
|
+
"data-structure-typed": "^2.1.1"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -95,10 +95,7 @@ export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
|
95
95
|
* @template V
|
|
96
96
|
* @template R
|
|
97
97
|
*/
|
|
98
|
-
export class AVLTreeCounter<K = any, V = any, R extends
|
|
99
|
-
extends AVLTree<K, V, R>
|
|
100
|
-
implements IBinaryTree<K, V, R>
|
|
101
|
-
{
|
|
98
|
+
export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
102
99
|
/**
|
|
103
100
|
* Create a AVLTreeCounter instance
|
|
104
101
|
* @remarks Time O(n), Space O(n)
|
|
@@ -314,7 +311,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
314
311
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
315
312
|
* @returns A new AVLTreeCounter with mapped entries.
|
|
316
313
|
*/
|
|
317
|
-
override map<MK = K, MV = V, MR
|
|
314
|
+
override map<MK = K, MV = V, MR = any>(
|
|
318
315
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
319
316
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
320
317
|
thisArg?: unknown
|
|
@@ -337,7 +334,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
337
334
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
338
335
|
* @returns An empty like-kind instance.
|
|
339
336
|
*/
|
|
340
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
337
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(
|
|
341
338
|
options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>
|
|
342
339
|
): this {
|
|
343
340
|
const Ctor = this.constructor as unknown as new (
|
|
@@ -359,7 +356,7 @@ export class AVLTreeCounter<K = any, V = any, R extends object = object>
|
|
|
359
356
|
* @param [options] - Options merged with the current snapshot.
|
|
360
357
|
* @returns A like-kind AVLTreeCounter built from the iterable.
|
|
361
358
|
*/
|
|
362
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
359
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
363
360
|
iter: Iterable<
|
|
364
361
|
TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
365
362
|
> = [],
|
|
@@ -93,10 +93,7 @@ export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
|
93
93
|
* @template V
|
|
94
94
|
* @template R
|
|
95
95
|
*/
|
|
96
|
-
export class AVLTreeMultiMap<K = any, V = any, R extends
|
|
97
|
-
extends AVLTree<K, V[], R>
|
|
98
|
-
implements IBinaryTree<K, V[], R>
|
|
99
|
-
{
|
|
96
|
+
export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
100
97
|
/**
|
|
101
98
|
* Create an AVLTreeMultiMap and optionally bulk-insert items.
|
|
102
99
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -251,7 +248,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
251
248
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
252
249
|
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
253
250
|
*/
|
|
254
|
-
override map<MK = K, MVArr extends unknown[] = V[], MR
|
|
251
|
+
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
255
252
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
256
253
|
options?: Partial<AVLTreeOptions<MK, MVArr, MR>>,
|
|
257
254
|
thisArg?: unknown
|
|
@@ -268,7 +265,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
268
265
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
269
266
|
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
270
267
|
*/
|
|
271
|
-
override map<MK = K, MV = V[], MR
|
|
268
|
+
override map<MK = K, MV = V[], MR = any>(
|
|
272
269
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
273
270
|
options?: Partial<AVLTreeOptions<MK, MV, MR>>,
|
|
274
271
|
thisArg?: unknown
|
|
@@ -305,9 +302,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
305
302
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
306
303
|
* @returns An empty like-kind instance.
|
|
307
304
|
*/
|
|
308
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
309
|
-
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
310
|
-
): this {
|
|
305
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this {
|
|
311
306
|
const Ctor = this.constructor as unknown as new (
|
|
312
307
|
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
313
308
|
opts?: AVLTreeOptions<TK, TV, TR>
|
|
@@ -325,7 +320,7 @@ export class AVLTreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
325
320
|
* @param [options] - Options merged with the current snapshot.
|
|
326
321
|
* @returns A like-kind AVLTree built from the iterable.
|
|
327
322
|
*/
|
|
328
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
323
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
329
324
|
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
330
325
|
options?: Partial<AVLTreeOptions<TK, TV, TR>>
|
|
331
326
|
): AVLTree<TK, TV, TR> {
|
|
@@ -169,7 +169,7 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
169
169
|
* // { minute: 15, temperature: 58.6 }
|
|
170
170
|
* // ]
|
|
171
171
|
*/
|
|
172
|
-
export class AVLTree<K = any, V = any, R
|
|
172
|
+
export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
173
173
|
/**
|
|
174
174
|
* Creates an instance of AVLTree.
|
|
175
175
|
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
@@ -301,7 +301,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
301
301
|
* @param [thisArg] - `this` context for the callback.
|
|
302
302
|
* @returns A new, mapped AVLTree.
|
|
303
303
|
*/
|
|
304
|
-
override map<MK = K, MV = V, MR
|
|
304
|
+
override map<MK = K, MV = V, MR = any>(
|
|
305
305
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
306
306
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
307
307
|
thisArg?: unknown
|
|
@@ -325,9 +325,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
325
325
|
* @param [options] - Options for the new tree.
|
|
326
326
|
* @returns A new, empty tree.
|
|
327
327
|
*/
|
|
328
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
329
|
-
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
330
|
-
): this {
|
|
328
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
331
329
|
const Ctor = this.constructor as unknown as new (
|
|
332
330
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
333
331
|
opts?: BSTOptions<TK, TV, TR>
|
|
@@ -344,7 +342,7 @@ export class AVLTree<K = any, V = any, R extends object = object> extends BST<K,
|
|
|
344
342
|
* @param [options] - Options for the new tree.
|
|
345
343
|
* @returns A new AVLTree.
|
|
346
344
|
*/
|
|
347
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
345
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
348
346
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
349
347
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
350
348
|
): AVLTree<TK, TV, TR> {
|
|
@@ -266,7 +266,7 @@ export class BinaryTreeNode<K = any, V = any> {
|
|
|
266
266
|
*
|
|
267
267
|
* console.log(evaluate(expressionTree.root)); // -27
|
|
268
268
|
*/
|
|
269
|
-
export class BinaryTree<K = any, V = any, R
|
|
269
|
+
export class BinaryTree<K = any, V = any, R = any>
|
|
270
270
|
extends IterableEntryBase<K, V | undefined>
|
|
271
271
|
implements IBinaryTree<K, V, R>
|
|
272
272
|
{
|
|
@@ -1178,9 +1178,9 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1178
1178
|
iterationType: IterationType = this.iterationType
|
|
1179
1179
|
): ReturnType<C> {
|
|
1180
1180
|
if (this.isNIL(startNode)) return callback(undefined);
|
|
1181
|
-
|
|
1181
|
+
const ensuredStartNode = this.ensureNode(startNode);
|
|
1182
1182
|
|
|
1183
|
-
if (!this.isRealNode(
|
|
1183
|
+
if (!this.isRealNode(ensuredStartNode)) return callback(undefined);
|
|
1184
1184
|
if (iterationType === 'RECURSIVE') {
|
|
1185
1185
|
const dfs = (cur: BinaryTreeNode<K, V>): BinaryTreeNode<K, V> => {
|
|
1186
1186
|
const { left } = cur;
|
|
@@ -1188,7 +1188,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1188
1188
|
return dfs(left);
|
|
1189
1189
|
};
|
|
1190
1190
|
|
|
1191
|
-
return callback(dfs(
|
|
1191
|
+
return callback(dfs(ensuredStartNode));
|
|
1192
1192
|
} else {
|
|
1193
1193
|
// Iterative (trampolined to prevent stack overflow, though 'ITERATIVE' usually means a loop)
|
|
1194
1194
|
const dfs = makeTrampoline((cur: BinaryTreeNode<K, V>): Trampoline<BinaryTreeNode<K, V>> => {
|
|
@@ -1197,7 +1197,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1197
1197
|
return makeTrampolineThunk(() => dfs(left));
|
|
1198
1198
|
});
|
|
1199
1199
|
|
|
1200
|
-
return callback(dfs(
|
|
1200
|
+
return callback(dfs(ensuredStartNode));
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
1203
|
|
|
@@ -1689,7 +1689,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1689
1689
|
* @param [thisArg] - `this` context for the callback.
|
|
1690
1690
|
* @returns A new, mapped tree.
|
|
1691
1691
|
*/
|
|
1692
|
-
map<MK = K, MV = V, MR
|
|
1692
|
+
map<MK = K, MV = V, MR = any>(
|
|
1693
1693
|
cb: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
1694
1694
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
1695
1695
|
thisArg?: unknown
|
|
@@ -1948,7 +1948,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1948
1948
|
* @template TK, TV, TR - Generic types for the options.
|
|
1949
1949
|
* @returns The options object.
|
|
1950
1950
|
*/
|
|
1951
|
-
protected _snapshotOptions<TK = K, TV = V, TR
|
|
1951
|
+
protected _snapshotOptions<TK = K, TV = V, TR = R>(): BinaryTreeOptions<TK, TV, TR> {
|
|
1952
1952
|
return {
|
|
1953
1953
|
iterationType: this.iterationType,
|
|
1954
1954
|
toEntryFn: this.toEntryFn as unknown as BinaryTreeOptions<TK, TV, TR>['toEntryFn'],
|
|
@@ -1965,9 +1965,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1965
1965
|
* @param [options] - Options for the new tree.
|
|
1966
1966
|
* @returns A new, empty tree.
|
|
1967
1967
|
*/
|
|
1968
|
-
protected _createInstance<TK = K, TV = V, TR
|
|
1969
|
-
options?: Partial<BinaryTreeOptions<TK, TV, TR>>
|
|
1970
|
-
): this {
|
|
1968
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BinaryTreeOptions<TK, TV, TR>>): this {
|
|
1971
1969
|
const Ctor = this.constructor as unknown as new (
|
|
1972
1970
|
iter?: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
1973
1971
|
opts?: BinaryTreeOptions<TK, TV, TR>
|
|
@@ -1984,7 +1982,7 @@ export class BinaryTree<K = any, V = any, R extends object = object>
|
|
|
1984
1982
|
* @param [options] - Options for the new tree.
|
|
1985
1983
|
* @returns A new tree.
|
|
1986
1984
|
*/
|
|
1987
|
-
protected _createLike<TK = K, TV = V, TR
|
|
1985
|
+
protected _createLike<TK = K, TV = V, TR = R>(
|
|
1988
1986
|
iter: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
1989
1987
|
options?: Partial<BinaryTreeOptions<TK, TV, TR>>
|
|
1990
1988
|
): BinaryTree<TK, TV, TR> {
|
|
@@ -165,10 +165,7 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
165
165
|
* console.log(findLCA(5, 35)); // 15
|
|
166
166
|
* console.log(findLCA(20, 30)); // 25
|
|
167
167
|
*/
|
|
168
|
-
export class BST<K = any, V = any, R extends
|
|
169
|
-
extends BinaryTree<K, V, R>
|
|
170
|
-
implements IBinaryTree<K, V, R>
|
|
171
|
-
{
|
|
168
|
+
export class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
172
169
|
/**
|
|
173
170
|
* Creates an instance of BST.
|
|
174
171
|
* @remarks Time O(N log N) or O(N^2) depending on `isBalanceAdd` in `addMany` and input order. Space O(N).
|
|
@@ -811,7 +808,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
811
808
|
* @param [thisArg] - `this` context for the callback.
|
|
812
809
|
* @returns A new, mapped BST.
|
|
813
810
|
*/
|
|
814
|
-
override map<MK = K, MV = V, MR
|
|
811
|
+
override map<MK = K, MV = V, MR = any>(
|
|
815
812
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
816
813
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
817
814
|
thisArg?: unknown
|
|
@@ -864,9 +861,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
864
861
|
* @param [options] - Options for the new BST.
|
|
865
862
|
* @returns A new, empty BST.
|
|
866
863
|
*/
|
|
867
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
868
|
-
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
869
|
-
): this {
|
|
864
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
870
865
|
const Ctor = this.constructor as unknown as new (
|
|
871
866
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
872
867
|
opts?: BSTOptions<TK, TV, TR>
|
|
@@ -883,7 +878,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
883
878
|
* @param [options] - Options for the new BST.
|
|
884
879
|
* @returns A new BST.
|
|
885
880
|
*/
|
|
886
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
881
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
887
882
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
888
883
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
889
884
|
): BST<TK, TV, TR> {
|
|
@@ -901,7 +896,7 @@ export class BST<K = any, V = any, R extends object = object>
|
|
|
901
896
|
* @template TK, TV, TR - Generic types for the options.
|
|
902
897
|
* @returns The options object.
|
|
903
898
|
*/
|
|
904
|
-
protected override _snapshotOptions<TK = K, TV = V, TR
|
|
899
|
+
protected override _snapshotOptions<TK = K, TV = V, TR = R>(): BSTOptions<TK, TV, TR> {
|
|
905
900
|
return {
|
|
906
901
|
...super._snapshotOptions<TK, TV, TR>(),
|
|
907
902
|
specifyComparable: this.specifyComparable as BSTOptions<TK, TV, TR>['specifyComparable'],
|
|
@@ -89,7 +89,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* RRRRRRRRRRRRRRRRRRRed-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
93
93
|
* @remarks Time O(1), Space O(1)
|
|
94
94
|
* @template K
|
|
95
95
|
* @template V
|
|
@@ -141,10 +141,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
141
141
|
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
142
142
|
*/
|
|
143
143
|
|
|
144
|
-
export class RedBlackTree<K = any, V = any, R extends
|
|
145
|
-
extends BST<K, V, R>
|
|
146
|
-
implements IBinaryTree<K, V, R>
|
|
147
|
-
{
|
|
144
|
+
export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
148
145
|
constructor(
|
|
149
146
|
keysNodesEntriesOrRaws: Iterable<
|
|
150
147
|
K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
@@ -324,7 +321,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
324
321
|
* @returns A new RedBlackTree with mapped entries.
|
|
325
322
|
*/
|
|
326
323
|
|
|
327
|
-
override map<MK = K, MV = V, MR
|
|
324
|
+
override map<MK = K, MV = V, MR = any>(
|
|
328
325
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
329
326
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
330
327
|
thisArg?: unknown
|
|
@@ -338,9 +335,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
338
335
|
return out;
|
|
339
336
|
}
|
|
340
337
|
|
|
341
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
342
|
-
options?: Partial<RedBlackTreeOptions<TK, TV, TR>>
|
|
343
|
-
): this {
|
|
338
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this {
|
|
344
339
|
const Ctor = this.constructor as unknown as new (
|
|
345
340
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
346
341
|
opts?: RedBlackTreeOptions<TK, TV, TR>
|
|
@@ -348,7 +343,7 @@ export class RedBlackTree<K = any, V = any, R extends object = object>
|
|
|
348
343
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
349
344
|
}
|
|
350
345
|
|
|
351
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
346
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
352
347
|
iter: Iterable<
|
|
353
348
|
TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
354
349
|
> = [],
|
|
@@ -100,10 +100,7 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
100
100
|
* @template V
|
|
101
101
|
* @template R
|
|
102
102
|
*/
|
|
103
|
-
export class TreeCounter<K = any, V = any, R extends
|
|
104
|
-
extends RedBlackTree<K, V, R>
|
|
105
|
-
implements IBinaryTree<K, V, R>
|
|
106
|
-
{
|
|
103
|
+
export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
107
104
|
/**
|
|
108
105
|
* Create a TreeCounter and optionally bulk-insert items.
|
|
109
106
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -342,7 +339,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
342
339
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
343
340
|
* @returns A new TreeCounter with mapped entries.
|
|
344
341
|
*/
|
|
345
|
-
override map<MK = K, MV = V, MR
|
|
342
|
+
override map<MK = K, MV = V, MR = any>(
|
|
346
343
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
347
344
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
348
345
|
thisArg?: unknown
|
|
@@ -377,9 +374,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
377
374
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
378
375
|
* @returns An empty like-kind instance.
|
|
379
376
|
*/
|
|
380
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
381
|
-
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
382
|
-
): this {
|
|
377
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
383
378
|
const Ctor = this.constructor as unknown as new (
|
|
384
379
|
iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
385
380
|
opts?: BSTOptions<TK, TV, TR>
|
|
@@ -397,7 +392,7 @@ export class TreeCounter<K = any, V = any, R extends object = object>
|
|
|
397
392
|
* @param [options] - Options merged with the current snapshot.
|
|
398
393
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
399
394
|
*/
|
|
400
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
395
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
401
396
|
iter: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
402
397
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
403
398
|
): TreeCounter<TK, TV, TR> {
|
|
@@ -250,10 +250,7 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
|
|
|
250
250
|
* // ]
|
|
251
251
|
* // ]
|
|
252
252
|
*/
|
|
253
|
-
export class TreeMultiMap<K = any, V = any, R extends
|
|
254
|
-
extends RedBlackTree<K, V[], R>
|
|
255
|
-
implements IBinaryTree<K, V[], R>
|
|
256
|
-
{
|
|
253
|
+
export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
257
254
|
/**
|
|
258
255
|
* Create a TreeMultiMap and optionally bulk-insert items.
|
|
259
256
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -365,13 +362,13 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
365
362
|
return false;
|
|
366
363
|
}
|
|
367
364
|
|
|
368
|
-
override map<MK = K, MVArr extends unknown[] = V[], MR
|
|
365
|
+
override map<MK = K, MVArr extends unknown[] = V[], MR = any>(
|
|
369
366
|
callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>,
|
|
370
367
|
options?: Partial<RedBlackTreeOptions<MK, MVArr, MR>>,
|
|
371
368
|
thisArg?: unknown
|
|
372
369
|
): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
373
370
|
|
|
374
|
-
override map<MK = K, MV = V[], MR
|
|
371
|
+
override map<MK = K, MV = V[], MR = any>(
|
|
375
372
|
callback: EntryCallback<K, V[] | undefined, [MK, MV]>,
|
|
376
373
|
options?: Partial<RedBlackTreeOptions<MK, MV, MR>>,
|
|
377
374
|
thisArg?: unknown
|
|
@@ -408,9 +405,7 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
408
405
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
409
406
|
* @returns An empty like-kind instance.
|
|
410
407
|
*/
|
|
411
|
-
protected override _createInstance<TK = K, TV = V, TR
|
|
412
|
-
options?: Partial<RedBlackTreeOptions<TK, TV, TR>>
|
|
413
|
-
): this {
|
|
408
|
+
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this {
|
|
414
409
|
const Ctor = this.constructor as unknown as new (
|
|
415
410
|
iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
416
411
|
opts?: RedBlackTreeOptions<TK, TV, TR>
|
|
@@ -428,7 +423,7 @@ export class TreeMultiMap<K = any, V = any, R extends object = object>
|
|
|
428
423
|
* @param [options] - Options merged with the current snapshot.
|
|
429
424
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
430
425
|
*/
|
|
431
|
-
protected override _createLike<TK = K, TV = V, TR
|
|
426
|
+
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
432
427
|
iter: Iterable<
|
|
433
428
|
TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR
|
|
434
429
|
> = [],
|
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
* K = key, V = value, R = raw/record used with toEntryFn (optional).
|
|
19
19
|
* Transforming methods like `map` use method-level generics MK/MV/MR.
|
|
20
20
|
*/
|
|
21
|
-
export interface IBinaryTree<K = any, V = any, R
|
|
21
|
+
export interface IBinaryTree<K = any, V = any, R = any> {
|
|
22
22
|
// ---- state ----
|
|
23
23
|
readonly size: number;
|
|
24
24
|
readonly root: BinaryTreeNode<K, V> | null | undefined;
|
|
@@ -232,7 +232,7 @@ export interface IBinaryTree<K = any, V = any, R extends object = object> {
|
|
|
232
232
|
|
|
233
233
|
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
|
|
234
234
|
|
|
235
|
-
map<MK = K, MV = V, MR
|
|
235
|
+
map<MK = K, MV = V, MR = any>(
|
|
236
236
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
237
237
|
options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
|
|
238
238
|
thisArg?: unknown
|