undirected-graph-typed 1.54.2 → 1.54.3
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 +21 -20
- package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +25 -21
- package/dist/data-structures/binary-tree/avl-tree.js +12 -8
- package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/data-structures/binary-tree/binary-tree.js +239 -144
- package/dist/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/data-structures/binary-tree/bst.js +78 -122
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
- package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +4 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/utils/utils.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -15
- package/src/data-structures/binary-tree/avl-tree.ts +35 -29
- package/src/data-structures/binary-tree/binary-tree.ts +469 -252
- package/src/data-structures/binary-tree/bst.ts +141 -143
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +25 -17
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/utils/utils.ts +2 -2
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
BinaryTreeDeleteResult,
|
|
3
|
-
BTNRep,
|
|
4
|
-
CRUD,
|
|
5
|
-
EntryCallback,
|
|
6
|
-
OptNode,
|
|
7
|
-
OptNodeOrNull,
|
|
8
|
-
RBTNColor,
|
|
9
|
-
RedBlackTreeOptions
|
|
10
|
-
} from '../../types';
|
|
1
|
+
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, OptNode, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
11
2
|
import { BST, BSTNode } from './bst';
|
|
12
3
|
import { IBinaryTree } from '../../interfaces';
|
|
13
4
|
|
|
14
5
|
export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
6
|
+
override parent?: RedBlackTreeNode<K, V> = undefined;
|
|
7
|
+
|
|
15
8
|
/**
|
|
16
9
|
* The constructor initializes a node with a key, value, and color for a Red-Black Tree.
|
|
17
10
|
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
|
|
@@ -27,28 +20,26 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
27
20
|
this._color = color;
|
|
28
21
|
}
|
|
29
22
|
|
|
30
|
-
override
|
|
23
|
+
override _left?: RedBlackTreeNode<K, V> | null | undefined = undefined;
|
|
31
24
|
|
|
32
|
-
override
|
|
33
|
-
|
|
34
|
-
override get left(): OptNodeOrNull<RedBlackTreeNode<K, V>> {
|
|
25
|
+
override get left(): RedBlackTreeNode<K, V> | null | undefined {
|
|
35
26
|
return this._left;
|
|
36
27
|
}
|
|
37
28
|
|
|
38
|
-
override set left(v:
|
|
29
|
+
override set left(v: RedBlackTreeNode<K, V> | null | undefined) {
|
|
39
30
|
if (v) {
|
|
40
31
|
v.parent = this;
|
|
41
32
|
}
|
|
42
33
|
this._left = v;
|
|
43
34
|
}
|
|
44
35
|
|
|
45
|
-
override _right?:
|
|
36
|
+
override _right?: RedBlackTreeNode<K, V> | null | undefined = undefined;
|
|
46
37
|
|
|
47
|
-
override get right():
|
|
38
|
+
override get right(): RedBlackTreeNode<K, V> | null | undefined {
|
|
48
39
|
return this._right;
|
|
49
40
|
}
|
|
50
41
|
|
|
51
|
-
override set right(v:
|
|
42
|
+
override set right(v: RedBlackTreeNode<K, V> | null | undefined) {
|
|
52
43
|
if (v) {
|
|
53
44
|
v.parent = this;
|
|
54
45
|
}
|
|
@@ -60,12 +51,6 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
60
51
|
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
61
52
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
62
53
|
* @example
|
|
63
|
-
* // Find elements in a range
|
|
64
|
-
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
65
|
-
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7]
|
|
66
|
-
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7]
|
|
67
|
-
* console.log(bst.search(new Range(15, 20))); // [15, 18]
|
|
68
|
-
* @example
|
|
69
54
|
* // using Red-Black Tree as a price-based index for stock data
|
|
70
55
|
* // Define the structure of individual stock records
|
|
71
56
|
* interface StockRecord {
|
|
@@ -107,7 +92,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
107
92
|
* [200, 400], // Price range
|
|
108
93
|
* node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
109
94
|
* );
|
|
110
|
-
* console.log(stocksInRange); // ['GOOGL', '
|
|
95
|
+
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
111
96
|
*/
|
|
112
97
|
export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
113
98
|
extends BST<K, V, R, MK, MV, MR>
|
|
@@ -117,7 +102,7 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
117
102
|
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
|
|
118
103
|
* raw data.
|
|
119
104
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
120
|
-
* iterable that can contain either `
|
|
105
|
+
* iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It
|
|
121
106
|
* is used to initialize the Red-Black Tree with keys, nodes, entries, or
|
|
122
107
|
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
|
|
123
108
|
* V, R>`. It is an optional parameter that allows you to specify additional options for the
|
|
@@ -125,7 +110,9 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
125
110
|
* any other parameters that are specific to
|
|
126
111
|
*/
|
|
127
112
|
constructor(
|
|
128
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
113
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
114
|
+
K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
115
|
+
> = [],
|
|
129
116
|
options?: RedBlackTreeOptions<K, V, R>
|
|
130
117
|
) {
|
|
131
118
|
super([], options);
|
|
@@ -188,12 +175,14 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
188
175
|
* Space Complexity: O(1)
|
|
189
176
|
*
|
|
190
177
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
191
|
-
* @param {
|
|
192
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
178
|
+
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
179
|
+
* `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
193
180
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
194
181
|
* an instance of the `RedBlackTreeNode` class.
|
|
195
182
|
*/
|
|
196
|
-
override isNode(
|
|
183
|
+
override isNode(
|
|
184
|
+
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
185
|
+
): keyNodeOrEntry is RedBlackTreeNode<K, V> {
|
|
197
186
|
return keyNodeOrEntry instanceof RedBlackTreeNode;
|
|
198
187
|
}
|
|
199
188
|
|
|
@@ -215,8 +204,8 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
215
204
|
*
|
|
216
205
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
217
206
|
* added.
|
|
218
|
-
* @param {
|
|
219
|
-
* `keyNodeOrEntry` can accept a value of type `R` or `
|
|
207
|
+
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
208
|
+
* `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
220
209
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
221
210
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
222
211
|
* structure.
|
|
@@ -224,7 +213,10 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
224
213
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
225
214
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
226
215
|
*/
|
|
227
|
-
override add(
|
|
216
|
+
override add(
|
|
217
|
+
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
218
|
+
value?: V
|
|
219
|
+
): boolean {
|
|
228
220
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
229
221
|
if (!this.isRealNode(newNode)) return false;
|
|
230
222
|
|
|
@@ -254,7 +246,7 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
254
246
|
*
|
|
255
247
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
256
248
|
* a given predicate and maintain the binary search tree properties.
|
|
257
|
-
* @param {
|
|
249
|
+
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
258
250
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
259
251
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
260
252
|
* function that determines which node(s) should be deleted.
|
|
@@ -263,7 +255,7 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
263
255
|
* balancing is needed.
|
|
264
256
|
*/
|
|
265
257
|
override delete(
|
|
266
|
-
keyNodeOrEntry:
|
|
258
|
+
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
267
259
|
): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] {
|
|
268
260
|
if (keyNodeOrEntry === null) return [];
|
|
269
261
|
|
|
@@ -8,11 +8,9 @@
|
|
|
8
8
|
import type {
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNOptKeyOrNode,
|
|
11
|
-
BTNRep,
|
|
12
11
|
EntryCallback,
|
|
13
12
|
IterationType,
|
|
14
13
|
OptNode,
|
|
15
|
-
OptNodeOrNull,
|
|
16
14
|
RBTNColor,
|
|
17
15
|
TreeCounterOptions
|
|
18
16
|
} from '../../types';
|
|
@@ -20,6 +18,8 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
20
18
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
21
19
|
|
|
22
20
|
export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
21
|
+
override parent?: TreeCounterNode<K, V> = undefined;
|
|
22
|
+
|
|
23
23
|
/**
|
|
24
24
|
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
25
25
|
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
@@ -37,28 +37,26 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
|
|
|
37
37
|
this.count = count;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
override
|
|
41
|
-
|
|
42
|
-
override _left?: OptNodeOrNull<TreeCounterNode<K, V>> = undefined;
|
|
40
|
+
override _left?: TreeCounterNode<K, V> | null | undefined = undefined;
|
|
43
41
|
|
|
44
|
-
override get left():
|
|
42
|
+
override get left(): TreeCounterNode<K, V> | null | undefined {
|
|
45
43
|
return this._left;
|
|
46
44
|
}
|
|
47
45
|
|
|
48
|
-
override set left(v:
|
|
46
|
+
override set left(v: TreeCounterNode<K, V> | null | undefined) {
|
|
49
47
|
if (v) {
|
|
50
48
|
v.parent = this;
|
|
51
49
|
}
|
|
52
50
|
this._left = v;
|
|
53
51
|
}
|
|
54
52
|
|
|
55
|
-
override _right?:
|
|
53
|
+
override _right?: TreeCounterNode<K, V> | null | undefined = undefined;
|
|
56
54
|
|
|
57
|
-
override get right():
|
|
55
|
+
override get right(): TreeCounterNode<K, V> | null | undefined {
|
|
58
56
|
return this._right;
|
|
59
57
|
}
|
|
60
58
|
|
|
61
|
-
override set right(v:
|
|
59
|
+
override set right(v: TreeCounterNode<K, V> | null | undefined) {
|
|
62
60
|
if (v) {
|
|
63
61
|
v.parent = this;
|
|
64
62
|
}
|
|
@@ -83,7 +81,9 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
83
81
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
84
82
|
*/
|
|
85
83
|
constructor(
|
|
86
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
84
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
85
|
+
K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
86
|
+
> = [],
|
|
87
87
|
options?: TreeCounterOptions<K, V, R>
|
|
88
88
|
) {
|
|
89
89
|
super([], options);
|
|
@@ -111,7 +111,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
111
111
|
*/
|
|
112
112
|
getComputedCount(): number {
|
|
113
113
|
let sum = 0;
|
|
114
|
-
this.dfs(node => (sum += node.count));
|
|
114
|
+
this.dfs(node => (sum += node ? node.count : 0));
|
|
115
115
|
return sum;
|
|
116
116
|
}
|
|
117
117
|
|
|
@@ -152,12 +152,14 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
152
152
|
|
|
153
153
|
/**
|
|
154
154
|
* The function checks if the input is an instance of the TreeCounterNode class.
|
|
155
|
-
* @param {
|
|
156
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
155
|
+
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
156
|
+
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
157
157
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
158
158
|
* an instance of the `TreeCounterNode` class.
|
|
159
159
|
*/
|
|
160
|
-
override isNode(
|
|
160
|
+
override isNode(
|
|
161
|
+
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
162
|
+
): keyNodeOrEntry is TreeCounterNode<K, V> {
|
|
161
163
|
return keyNodeOrEntry instanceof TreeCounterNode;
|
|
162
164
|
}
|
|
163
165
|
|
|
@@ -167,7 +169,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
167
169
|
*
|
|
168
170
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
169
171
|
* the count and returning a boolean indicating success.
|
|
170
|
-
* @param {
|
|
172
|
+
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
|
|
171
173
|
* `keyNodeOrEntry` parameter can accept one of the following types:
|
|
172
174
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
173
175
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
@@ -177,7 +179,11 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
177
179
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
178
180
|
* was successful, and false otherwise.
|
|
179
181
|
*/
|
|
180
|
-
override add(
|
|
182
|
+
override add(
|
|
183
|
+
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
184
|
+
value?: V,
|
|
185
|
+
count = 1
|
|
186
|
+
): boolean {
|
|
181
187
|
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count);
|
|
182
188
|
const orgCount = newNode?.count || 0;
|
|
183
189
|
const isSuccessAdded = super.add(newNode, newValue);
|
|
@@ -196,7 +202,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
196
202
|
*
|
|
197
203
|
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
198
204
|
* structure, handling cases where nodes have children and maintaining balance in the tree.
|
|
199
|
-
* @param {
|
|
205
|
+
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
|
|
200
206
|
* parameter in the `delete` method is used to specify the condition or key based on which a node
|
|
201
207
|
* should be deleted from the binary tree. It can be a key, a node, or an entry.
|
|
202
208
|
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
@@ -206,7 +212,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
206
212
|
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<TreeCounterNode<K, V>>` objects.
|
|
207
213
|
*/
|
|
208
214
|
override delete(
|
|
209
|
-
keyNodeOrEntry:
|
|
215
|
+
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
210
216
|
ignoreCount = false
|
|
211
217
|
): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[] {
|
|
212
218
|
if (keyNodeOrEntry === null) return [];
|
|
@@ -340,8 +346,8 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
340
346
|
if (l > r) return;
|
|
341
347
|
const m = l + Math.floor((r - l) / 2);
|
|
342
348
|
const midNode = sorted[m];
|
|
343
|
-
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
344
|
-
else this.add(midNode.key, midNode.value, midNode.count);
|
|
349
|
+
if (this._isMapMode && midNode !== null) this.add(midNode.key, undefined, midNode.count);
|
|
350
|
+
else if (midNode !== null) this.add(midNode.key, midNode.value, midNode.count);
|
|
345
351
|
buildBalanceBST(l, m - 1);
|
|
346
352
|
buildBalanceBST(m + 1, r);
|
|
347
353
|
};
|
|
@@ -357,8 +363,8 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
357
363
|
if (l <= r) {
|
|
358
364
|
const m = l + Math.floor((r - l) / 2);
|
|
359
365
|
const midNode = sorted[m];
|
|
360
|
-
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
361
|
-
else this.add(midNode.key, midNode.value, midNode.count);
|
|
366
|
+
if (this._isMapMode && midNode !== null) this.add(midNode.key, undefined, midNode.count);
|
|
367
|
+
else if (midNode !== null) this.add(midNode.key, midNode.value, midNode.count);
|
|
362
368
|
stack.push([m + 1, r]);
|
|
363
369
|
stack.push([l, m - 1]);
|
|
364
370
|
}
|
|
@@ -377,7 +383,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
377
383
|
*/
|
|
378
384
|
override clone() {
|
|
379
385
|
const cloned = this.createTree();
|
|
380
|
-
this.bfs(node => cloned.add(node.key, undefined, node.count));
|
|
386
|
+
this.bfs(node => cloned.add(node === null ? null : node.key, undefined, node === null ? 0 : node.count));
|
|
381
387
|
if (this._isMapMode) cloned._store = this._store;
|
|
382
388
|
return cloned;
|
|
383
389
|
}
|
|
@@ -413,8 +419,8 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
413
419
|
/**
|
|
414
420
|
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
415
421
|
* node based on the input.
|
|
416
|
-
* @param {
|
|
417
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
422
|
+
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
|
|
423
|
+
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
|
|
418
424
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
419
425
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
420
426
|
* an existing node.
|
|
@@ -423,7 +429,7 @@ export class TreeCounter<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
423
429
|
* @returns either a TreeCounterNode<K, V> object or undefined.
|
|
424
430
|
*/
|
|
425
431
|
protected override _keyValueNodeOrEntryToNodeAndValue(
|
|
426
|
-
keyNodeOrEntry:
|
|
432
|
+
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
427
433
|
value?: V,
|
|
428
434
|
count = 1
|
|
429
435
|
): [TreeCounterNode<K, V> | undefined, V | undefined] {
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BTNOptKeyOrNull,
|
|
8
|
+
import type { BTNOptKeyOrNull, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
|
|
12
12
|
export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
|
|
13
|
+
override parent?: TreeMultiMapNode<K, V> = undefined;
|
|
14
|
+
|
|
13
15
|
/**
|
|
14
16
|
* This TypeScript constructor initializes an object with a key of type K and an array of values of
|
|
15
17
|
* type V.
|
|
@@ -23,28 +25,26 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
|
|
|
23
25
|
super(key, value);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
override
|
|
27
|
-
|
|
28
|
-
override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
|
|
28
|
+
override _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
29
29
|
|
|
30
|
-
override get left():
|
|
30
|
+
override get left(): TreeMultiMapNode<K, V> | null | undefined {
|
|
31
31
|
return this._left;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
override set left(v:
|
|
34
|
+
override set left(v: TreeMultiMapNode<K, V> | null | undefined) {
|
|
35
35
|
if (v) {
|
|
36
36
|
v.parent = this;
|
|
37
37
|
}
|
|
38
38
|
this._left = v;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
override _right?:
|
|
41
|
+
override _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
42
42
|
|
|
43
|
-
override get right():
|
|
43
|
+
override get right(): TreeMultiMapNode<K, V> | null | undefined {
|
|
44
44
|
return this._right;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
override set right(v:
|
|
47
|
+
override set right(v: TreeMultiMapNode<K, V> | null | undefined) {
|
|
48
48
|
if (v) {
|
|
49
49
|
v.parent = this;
|
|
50
50
|
}
|
|
@@ -57,8 +57,8 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
|
|
|
57
57
|
* @example
|
|
58
58
|
* // Find elements in a range
|
|
59
59
|
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
60
|
-
* console.log(tmm.search(new Range(5, 10))); // [5,
|
|
61
|
-
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12
|
|
60
|
+
* console.log(tmm.search(new Range(5, 10))); // [5, 7, 10]
|
|
61
|
+
* console.log(tmm.search(new Range(4, 12))); // [5, 7, 10, 12]
|
|
62
62
|
* console.log(tmm.search(new Range(15, 20))); // [15, 18]
|
|
63
63
|
*/
|
|
64
64
|
export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
@@ -77,7 +77,9 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
77
77
|
* additional options for configuring the TreeMultiMap instance.
|
|
78
78
|
*/
|
|
79
79
|
constructor(
|
|
80
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
80
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
81
|
+
K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
|
|
82
|
+
> = [],
|
|
81
83
|
options?: TreeMultiMapOptions<K, V[], R>
|
|
82
84
|
) {
|
|
83
85
|
super([], { ...options, isMapMode: true });
|
|
@@ -124,7 +126,7 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
124
126
|
return new TreeMultiMapNode<K, V>(key, []);
|
|
125
127
|
}
|
|
126
128
|
|
|
127
|
-
override add(node:
|
|
129
|
+
override add(node: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
128
130
|
|
|
129
131
|
override add(key: K, value: V): boolean;
|
|
130
132
|
|
|
@@ -134,7 +136,7 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
134
136
|
*
|
|
135
137
|
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to a
|
|
136
138
|
* TreeMultiMapNode, handling different input types and scenarios.
|
|
137
|
-
* @param {
|
|
139
|
+
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
138
140
|
* parameter in the `override add` method can be either a `BTNRep` object containing a key, an array
|
|
139
141
|
* of values, and a `TreeMultiMapNode`, or just a key.
|
|
140
142
|
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that
|
|
@@ -143,7 +145,10 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
143
145
|
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
144
146
|
* successful or not.
|
|
145
147
|
*/
|
|
146
|
-
override add(
|
|
148
|
+
override add(
|
|
149
|
+
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
150
|
+
value?: V
|
|
151
|
+
): boolean {
|
|
147
152
|
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
148
153
|
|
|
149
154
|
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
@@ -186,7 +191,7 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
186
191
|
*
|
|
187
192
|
* The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
|
|
188
193
|
* and deletes the entire node if no values are left for that key.
|
|
189
|
-
* @param {
|
|
194
|
+
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
190
195
|
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
|
|
191
196
|
* array of values, or just a key itself.
|
|
192
197
|
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
@@ -196,7 +201,10 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
196
201
|
* @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
|
|
197
202
|
* successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
|
|
198
203
|
*/
|
|
199
|
-
deleteValue(
|
|
204
|
+
deleteValue(
|
|
205
|
+
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
206
|
+
value: V
|
|
207
|
+
): boolean {
|
|
200
208
|
const values = this.get(keyNodeOrEntry);
|
|
201
209
|
if (Array.isArray(values)) {
|
|
202
210
|
const index = values.indexOf(value);
|
|
@@ -7,6 +7,7 @@ export type BinaryTreeOptions<K, V, R> = {
|
|
|
7
7
|
iterationType?: IterationType;
|
|
8
8
|
toEntryFn?: ToEntryFn<K, V, R>;
|
|
9
9
|
isMapMode?: boolean;
|
|
10
|
+
isDuplicate?: boolean;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
|
|
@@ -2,7 +2,7 @@ import type { BinaryTreeOptions } from './binary-tree';
|
|
|
2
2
|
import { Comparable } from '../../utils';
|
|
3
3
|
import { OptValue } from '../../common';
|
|
4
4
|
|
|
5
|
-
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
5
|
+
export type BSTOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'> & {
|
|
6
6
|
specifyComparable?: (key: K) => Comparable
|
|
7
7
|
isReverse?: boolean;
|
|
8
8
|
}
|
package/src/utils/utils.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { Comparable, ComparablePrimitive, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
|