stack-typed 1.49.5 → 1.49.6
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.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
|
@@ -10,9 +10,8 @@ import {
|
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNKeyOrNode,
|
|
12
12
|
BTNCallback,
|
|
13
|
-
BTNExemplar,
|
|
14
|
-
BTNKeyOrNode,
|
|
15
13
|
IterationType,
|
|
14
|
+
KeyOrNodeOrEntry,
|
|
16
15
|
RBTNColor,
|
|
17
16
|
RBTreeOptions,
|
|
18
17
|
RedBlackTreeNested,
|
|
@@ -53,20 +52,20 @@ export class RedBlackTree<
|
|
|
53
52
|
|
|
54
53
|
/**
|
|
55
54
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
56
|
-
* initializes the tree with optional
|
|
57
|
-
* @param [
|
|
58
|
-
* objects. It represents the initial
|
|
55
|
+
* initializes the tree with optional nodes and options.
|
|
56
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
57
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
59
58
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
60
|
-
*
|
|
59
|
+
* nodes to the
|
|
61
60
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
62
61
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
63
62
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
64
63
|
*/
|
|
65
|
-
constructor(
|
|
64
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
|
|
66
65
|
super([], options);
|
|
67
66
|
|
|
68
67
|
this._root = this.Sentinel;
|
|
69
|
-
if (
|
|
68
|
+
if (nodes) super.addMany(nodes);
|
|
70
69
|
}
|
|
71
70
|
|
|
72
71
|
protected _root: N;
|
|
@@ -113,62 +112,73 @@ export class RedBlackTree<
|
|
|
113
112
|
}
|
|
114
113
|
|
|
115
114
|
/**
|
|
116
|
-
* The function
|
|
117
|
-
* @param
|
|
118
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
119
|
-
* class.
|
|
120
|
-
*/
|
|
121
|
-
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
122
|
-
return exemplar instanceof RedBlackTreeNode;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
127
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
128
|
-
* data type.
|
|
129
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
130
|
-
*/
|
|
131
|
-
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
132
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
137
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
115
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
116
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
138
117
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
139
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
118
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
140
119
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
141
120
|
* @returns a node of type N or undefined.
|
|
142
121
|
*/
|
|
143
|
-
override exemplarToNode(
|
|
122
|
+
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
|
|
144
123
|
let node: N | undefined;
|
|
145
124
|
|
|
146
|
-
if (
|
|
125
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
147
126
|
return;
|
|
148
|
-
} else if (this.isNode(
|
|
149
|
-
node =
|
|
150
|
-
} else if (this.isEntry(
|
|
151
|
-
const [key, value] =
|
|
127
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
128
|
+
node = keyOrNodeOrEntry;
|
|
129
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
130
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
152
131
|
if (key === undefined || key === null) {
|
|
153
132
|
return;
|
|
154
133
|
} else {
|
|
155
134
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
156
135
|
}
|
|
157
|
-
} else if (this.isNotNodeInstance(
|
|
158
|
-
node = this.createNode(
|
|
136
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
137
|
+
node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
|
|
159
138
|
} else {
|
|
160
139
|
return;
|
|
161
140
|
}
|
|
162
141
|
return node;
|
|
163
142
|
}
|
|
164
143
|
|
|
144
|
+
/**
|
|
145
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
146
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
147
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
148
|
+
* class.
|
|
149
|
+
*/
|
|
150
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
151
|
+
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
152
|
+
}
|
|
153
|
+
|
|
165
154
|
/**
|
|
166
155
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
167
156
|
* Space Complexity: O(1)
|
|
168
157
|
*/
|
|
169
158
|
|
|
159
|
+
override isRealNode(node: N | undefined): node is N {
|
|
160
|
+
if (node === this.Sentinel || node === undefined) return false;
|
|
161
|
+
return node instanceof RedBlackTreeNode;
|
|
162
|
+
}
|
|
163
|
+
|
|
170
164
|
/**
|
|
171
|
-
*
|
|
165
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
166
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
167
|
+
* data type.
|
|
168
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
169
|
+
*/
|
|
170
|
+
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
171
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Time Complexity: O(log n)
|
|
176
|
+
* Space Complexity: O(1)
|
|
177
|
+
* on average (where n is the number of nodes in the tree)
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(log n)
|
|
172
182
|
* Space Complexity: O(1)
|
|
173
183
|
*
|
|
174
184
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -179,9 +189,9 @@ export class RedBlackTree<
|
|
|
179
189
|
* being added to the binary search tree.
|
|
180
190
|
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
181
191
|
*/
|
|
182
|
-
override add(keyOrNodeOrEntry:
|
|
192
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
|
|
183
193
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
184
|
-
if (newNode === undefined) return;
|
|
194
|
+
if (newNode === undefined) return false;
|
|
185
195
|
|
|
186
196
|
newNode.left = this.Sentinel;
|
|
187
197
|
newNode.right = this.Sentinel;
|
|
@@ -200,7 +210,7 @@ export class RedBlackTree<
|
|
|
200
210
|
if (newNode !== x) {
|
|
201
211
|
this._replaceNode(x, newNode);
|
|
202
212
|
}
|
|
203
|
-
return;
|
|
213
|
+
return false;
|
|
204
214
|
}
|
|
205
215
|
}
|
|
206
216
|
}
|
|
@@ -217,25 +227,27 @@ export class RedBlackTree<
|
|
|
217
227
|
if (newNode.parent === undefined) {
|
|
218
228
|
newNode.color = RBTNColor.BLACK;
|
|
219
229
|
this._size++;
|
|
220
|
-
return;
|
|
230
|
+
return false;
|
|
221
231
|
}
|
|
222
232
|
|
|
223
233
|
if (newNode.parent.parent === undefined) {
|
|
224
234
|
this._size++;
|
|
225
|
-
return;
|
|
235
|
+
return false;
|
|
226
236
|
}
|
|
227
237
|
|
|
228
238
|
this._fixInsert(newNode);
|
|
229
239
|
this._size++;
|
|
240
|
+
return true;
|
|
230
241
|
}
|
|
231
242
|
|
|
232
243
|
/**
|
|
233
|
-
* Time Complexity: O(log n)
|
|
244
|
+
* Time Complexity: O(log n)
|
|
234
245
|
* Space Complexity: O(1)
|
|
246
|
+
* on average (where n is the number of nodes in the tree)
|
|
235
247
|
*/
|
|
236
248
|
|
|
237
249
|
/**
|
|
238
|
-
* Time Complexity: O(log n)
|
|
250
|
+
* Time Complexity: O(log n)
|
|
239
251
|
* Space Complexity: O(1)
|
|
240
252
|
*
|
|
241
253
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -310,16 +322,6 @@ export class RedBlackTree<
|
|
|
310
322
|
return ans;
|
|
311
323
|
}
|
|
312
324
|
|
|
313
|
-
/**
|
|
314
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
315
|
-
* Space Complexity: O(1)
|
|
316
|
-
*/
|
|
317
|
-
|
|
318
|
-
override isRealNode(node: N | undefined): node is N {
|
|
319
|
-
if (node === this.Sentinel || node === undefined) return false;
|
|
320
|
-
return node instanceof RedBlackTreeNode;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
325
|
getNode<C extends BTNCallback<N, K>>(
|
|
324
326
|
identifier: K,
|
|
325
327
|
callback?: C,
|
|
@@ -342,12 +344,12 @@ export class RedBlackTree<
|
|
|
342
344
|
): N | undefined;
|
|
343
345
|
|
|
344
346
|
/**
|
|
345
|
-
* Time Complexity: O(log n)
|
|
347
|
+
* Time Complexity: O(log n)
|
|
346
348
|
* Space Complexity: O(1)
|
|
347
349
|
*/
|
|
348
350
|
|
|
349
351
|
/**
|
|
350
|
-
* Time Complexity: O(log n)
|
|
352
|
+
* Time Complexity: O(log n)
|
|
351
353
|
* Space Complexity: O(1)
|
|
352
354
|
*
|
|
353
355
|
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
@@ -407,7 +409,7 @@ export class RedBlackTree<
|
|
|
407
409
|
}
|
|
408
410
|
|
|
409
411
|
/**
|
|
410
|
-
* Time Complexity: O(
|
|
412
|
+
* Time Complexity: O(1)
|
|
411
413
|
* Space Complexity: O(1)
|
|
412
414
|
*/
|
|
413
415
|
|
|
@@ -489,12 +491,12 @@ export class RedBlackTree<
|
|
|
489
491
|
}
|
|
490
492
|
|
|
491
493
|
/**
|
|
492
|
-
* Time Complexity: O(log n)
|
|
494
|
+
* Time Complexity: O(log n)
|
|
493
495
|
* Space Complexity: O(1)
|
|
494
496
|
*/
|
|
495
497
|
|
|
496
498
|
/**
|
|
497
|
-
* Time Complexity: O(log n)
|
|
499
|
+
* Time Complexity: O(log n)
|
|
498
500
|
* Space Complexity: O(1)
|
|
499
501
|
*
|
|
500
502
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -585,12 +587,12 @@ export class RedBlackTree<
|
|
|
585
587
|
}
|
|
586
588
|
|
|
587
589
|
/**
|
|
588
|
-
* Time Complexity: O(log n)
|
|
590
|
+
* Time Complexity: O(log n)
|
|
589
591
|
* Space Complexity: O(1)
|
|
590
592
|
*/
|
|
591
593
|
|
|
592
594
|
/**
|
|
593
|
-
* Time Complexity: O(log n)
|
|
595
|
+
* Time Complexity: O(log n)
|
|
594
596
|
* Space Complexity: O(1)
|
|
595
597
|
*
|
|
596
598
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -9,8 +9,7 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
-
|
|
13
|
-
BTNKeyOrNode,
|
|
12
|
+
KeyOrNodeOrEntry,
|
|
14
13
|
TreeMultimapNested,
|
|
15
14
|
TreeMultimapNodeNested,
|
|
16
15
|
TreeMultimapOptions
|
|
@@ -53,9 +52,9 @@ export class TreeMultimap<
|
|
|
53
52
|
>
|
|
54
53
|
extends AVLTree<K, V, N, TREE>
|
|
55
54
|
implements IBinaryTree<K, V, N, TREE> {
|
|
56
|
-
constructor(
|
|
55
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
|
|
57
56
|
super([], options);
|
|
58
|
-
if (
|
|
57
|
+
if (nodes) this.addMany(nodes);
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
private _count = 0;
|
|
@@ -89,28 +88,8 @@ export class TreeMultimap<
|
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
/**
|
|
92
|
-
* The function
|
|
93
|
-
* @param
|
|
94
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
95
|
-
* class.
|
|
96
|
-
*/
|
|
97
|
-
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
98
|
-
return exemplar instanceof TreeMultimapNode;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
103
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
104
|
-
* data type.
|
|
105
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
106
|
-
*/
|
|
107
|
-
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
108
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
113
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
91
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
92
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
114
93
|
* can be one of the following:
|
|
115
94
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
116
95
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -119,21 +98,21 @@ export class TreeMultimap<
|
|
|
119
98
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
120
99
|
* @returns a node of type `N` or `undefined`.
|
|
121
100
|
*/
|
|
122
|
-
override exemplarToNode(
|
|
101
|
+
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): N | undefined {
|
|
123
102
|
let node: N | undefined;
|
|
124
|
-
if (
|
|
103
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
125
104
|
return;
|
|
126
|
-
} else if (this.isNode(
|
|
127
|
-
node =
|
|
128
|
-
} else if (this.isEntry(
|
|
129
|
-
const [key, value] =
|
|
105
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
106
|
+
node = keyOrNodeOrEntry;
|
|
107
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
108
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
130
109
|
if (key === undefined || key === null) {
|
|
131
110
|
return;
|
|
132
111
|
} else {
|
|
133
112
|
node = this.createNode(key, value, count);
|
|
134
113
|
}
|
|
135
|
-
} else if (this.isNotNodeInstance(
|
|
136
|
-
node = this.createNode(
|
|
114
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
115
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
137
116
|
} else {
|
|
138
117
|
return;
|
|
139
118
|
}
|
|
@@ -141,13 +120,34 @@ export class TreeMultimap<
|
|
|
141
120
|
}
|
|
142
121
|
|
|
143
122
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
123
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
124
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
125
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
126
|
+
* class.
|
|
146
127
|
*/
|
|
128
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
129
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
134
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
135
|
+
* data type.
|
|
136
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
137
|
+
*/
|
|
138
|
+
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
139
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
140
|
+
}
|
|
147
141
|
|
|
148
142
|
/**
|
|
149
|
-
* Time Complexity: O(log n)
|
|
150
|
-
* Space Complexity: O(1)
|
|
143
|
+
* Time Complexity: O(log n)
|
|
144
|
+
* Space Complexity: O(1)
|
|
145
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Time Complexity: O(log n)
|
|
150
|
+
* Space Complexity: O(1)
|
|
151
151
|
*
|
|
152
152
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
153
153
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -161,26 +161,27 @@ export class TreeMultimap<
|
|
|
161
161
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
162
162
|
* was not successful.
|
|
163
163
|
*/
|
|
164
|
-
override add(keyOrNodeOrEntry:
|
|
164
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count = 1): boolean {
|
|
165
165
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
166
|
-
if (newNode === undefined) return;
|
|
166
|
+
if (newNode === undefined) return false;
|
|
167
167
|
|
|
168
168
|
const orgNodeCount = newNode?.count || 0;
|
|
169
169
|
const inserted = super.add(newNode);
|
|
170
170
|
if (inserted) {
|
|
171
171
|
this._count += orgNodeCount;
|
|
172
172
|
}
|
|
173
|
-
return
|
|
173
|
+
return true;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
|
-
* Time Complexity: O(k log n)
|
|
178
|
-
* Space Complexity: O(1)
|
|
177
|
+
* Time Complexity: O(k log n)
|
|
178
|
+
* Space Complexity: O(1)
|
|
179
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
179
180
|
*/
|
|
180
181
|
|
|
181
182
|
/**
|
|
182
|
-
* Time Complexity: O(k log n)
|
|
183
|
-
* Space Complexity: O(1)
|
|
183
|
+
* Time Complexity: O(k log n)
|
|
184
|
+
* Space Complexity: O(1)
|
|
184
185
|
*
|
|
185
186
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
186
187
|
* structure.
|
|
@@ -188,18 +189,19 @@ export class TreeMultimap<
|
|
|
188
189
|
* either keys, nodes, or entries.
|
|
189
190
|
* @returns The method is returning an array of type `N | undefined`.
|
|
190
191
|
*/
|
|
191
|
-
override addMany(keysOrNodesOrEntries: Iterable<
|
|
192
|
+
override addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[] {
|
|
192
193
|
return super.addMany(keysOrNodesOrEntries);
|
|
193
194
|
}
|
|
194
195
|
|
|
195
196
|
/**
|
|
196
|
-
* Time Complexity: O(
|
|
197
|
-
* Space Complexity: O(
|
|
197
|
+
* Time Complexity: O(n log n)
|
|
198
|
+
* Space Complexity: O(n)
|
|
199
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
198
200
|
*/
|
|
199
201
|
|
|
200
202
|
/**
|
|
201
|
-
* Time Complexity: O(n log n)
|
|
202
|
-
* Space Complexity: O(n)
|
|
203
|
+
* Time Complexity: O(n log n)
|
|
204
|
+
* Space Complexity: O(n)
|
|
203
205
|
*
|
|
204
206
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
205
207
|
* tree using either a recursive or iterative approach.
|
|
@@ -247,13 +249,14 @@ export class TreeMultimap<
|
|
|
247
249
|
}
|
|
248
250
|
|
|
249
251
|
/**
|
|
250
|
-
* Time Complexity: O(k log n)
|
|
251
|
-
* Space Complexity: O(1)
|
|
252
|
+
* Time Complexity: O(k log n)
|
|
253
|
+
* Space Complexity: O(1)
|
|
254
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
252
255
|
*/
|
|
253
256
|
|
|
254
257
|
/**
|
|
255
|
-
* Time Complexity: O(log n)
|
|
256
|
-
* Space Complexity: O(1)
|
|
258
|
+
* Time Complexity: O(k log n)
|
|
259
|
+
* Space Complexity: O(1)
|
|
257
260
|
*
|
|
258
261
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
259
262
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -331,11 +334,14 @@ export class TreeMultimap<
|
|
|
331
334
|
}
|
|
332
335
|
|
|
333
336
|
/**
|
|
334
|
-
* Time Complexity: O(
|
|
335
|
-
* Space Complexity: O(
|
|
337
|
+
* Time Complexity: O(1)
|
|
338
|
+
* Space Complexity: O(1)
|
|
336
339
|
*/
|
|
337
340
|
|
|
338
341
|
/**
|
|
342
|
+
* Time Complexity: O(1)
|
|
343
|
+
* Space Complexity: O(1)
|
|
344
|
+
*
|
|
339
345
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
340
346
|
*/
|
|
341
347
|
override clear() {
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
BinaryTreeNodeNested,
|
|
6
6
|
BinaryTreeOptions,
|
|
7
7
|
BTNCallback,
|
|
8
|
-
|
|
8
|
+
KeyOrNodeOrEntry
|
|
9
9
|
} from '../types';
|
|
10
10
|
|
|
11
11
|
export interface IBinaryTree<
|
|
@@ -18,9 +18,9 @@ export interface IBinaryTree<
|
|
|
18
18
|
|
|
19
19
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
20
20
|
|
|
21
|
-
add(keyOrNodeOrEntry:
|
|
21
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
22
22
|
|
|
23
|
-
addMany(nodes: Iterable<
|
|
23
|
+
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
|
|
24
24
|
|
|
25
25
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
|
26
26
|
}
|
package/src/types/common.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export enum BSTVariant {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
STANDARD = 'STANDARD',
|
|
3
|
+
INVERSE = 'INVERSE'
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export enum CP {
|
|
@@ -54,7 +54,7 @@ export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
|
|
54
54
|
|
|
55
55
|
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|
56
56
|
|
|
57
|
-
export type
|
|
57
|
+
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
58
58
|
|
|
59
59
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
60
60
|
|