min-heap-typed 1.48.4 → 1.48.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/base/iterable-base.d.ts +6 -6
- package/dist/data-structures/base/iterable-base.js +3 -3
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +24 -22
- package/dist/data-structures/binary-tree/binary-tree.js +35 -22
- package/dist/data-structures/binary-tree/bst.d.ts +30 -22
- package/dist/data-structures/binary-tree/bst.js +38 -26
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +52 -52
- package/dist/data-structures/graph/abstract-graph.js +78 -78
- package/dist/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/data-structures/graph/directed-graph.js +56 -56
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +29 -29
- package/dist/data-structures/graph/undirected-graph.js +57 -57
- package/dist/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/base/base.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -4
- package/src/data-structures/binary-tree/binary-tree.ts +47 -27
- package/src/data-structures/binary-tree/bst.ts +52 -32
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +26 -18
- package/src/data-structures/graph/abstract-graph.ts +82 -82
- package/src/data-structures/graph/directed-graph.ts +56 -56
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +59 -59
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/base/base.ts +3 -3
|
@@ -95,19 +95,22 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
95
95
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
96
96
|
*/
|
|
97
97
|
|
|
98
|
+
|
|
98
99
|
/**
|
|
99
100
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
100
101
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
101
102
|
*
|
|
102
103
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
103
104
|
* a new node.
|
|
104
|
-
* @param keyOrNodeOrEntry - The
|
|
105
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
105
106
|
* entry.
|
|
106
|
-
* @
|
|
107
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
108
|
+
* being added to the binary tree.
|
|
109
|
+
* @returns The method is returning either the inserted node or undefined.
|
|
107
110
|
*/
|
|
108
|
-
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
111
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
|
109
112
|
if (keyOrNodeOrEntry === null) return undefined;
|
|
110
|
-
const inserted = super.add(keyOrNodeOrEntry);
|
|
113
|
+
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
111
114
|
if (inserted) this._balancePath(inserted);
|
|
112
115
|
return inserted;
|
|
113
116
|
}
|
|
@@ -19,15 +19,15 @@ import {
|
|
|
19
19
|
BinaryTreePrintOptions,
|
|
20
20
|
BiTreeDeleteResult,
|
|
21
21
|
DFSOrderPattern,
|
|
22
|
+
EntryCallback,
|
|
22
23
|
FamilyPosition,
|
|
23
24
|
IterationType,
|
|
24
|
-
NodeDisplayLayout
|
|
25
|
-
PairCallback
|
|
25
|
+
NodeDisplayLayout
|
|
26
26
|
} from '../../types';
|
|
27
27
|
import { IBinaryTree } from '../../interfaces';
|
|
28
28
|
import { trampoline } from '../../utils';
|
|
29
29
|
import { Queue } from '../queue';
|
|
30
|
-
import {
|
|
30
|
+
import { IterableEntryBase } from "../base";
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Represents a node in a binary tree.
|
|
@@ -104,7 +104,7 @@ export class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K, V, N>
|
|
|
104
104
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
105
105
|
*/
|
|
106
106
|
|
|
107
|
-
export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends
|
|
107
|
+
export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined>
|
|
108
108
|
|
|
109
109
|
implements IBinaryTree<K, V, N, TREE> {
|
|
110
110
|
iterationType = IterationType.ITERATIVE
|
|
@@ -184,13 +184,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
184
184
|
}
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
|
-
* The function `exemplarToNode` converts an exemplar
|
|
188
|
-
*
|
|
189
|
-
* @param
|
|
190
|
-
* function. It
|
|
191
|
-
*
|
|
187
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
188
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
189
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
190
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
|
|
191
|
+
* is provided, it will be `undefined`.
|
|
192
|
+
* @returns a value of type N (node), or null, or undefined.
|
|
192
193
|
*/
|
|
193
|
-
exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
194
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
194
195
|
if (exemplar === undefined) return;
|
|
195
196
|
|
|
196
197
|
let node: N | null | undefined;
|
|
@@ -208,7 +209,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
208
209
|
} else if (this.isNode(exemplar)) {
|
|
209
210
|
node = exemplar;
|
|
210
211
|
} else if (this.isNotNodeInstance(exemplar)) {
|
|
211
|
-
node = this.createNode(exemplar);
|
|
212
|
+
node = this.createNode(exemplar, value);
|
|
212
213
|
} else {
|
|
213
214
|
return;
|
|
214
215
|
}
|
|
@@ -230,18 +231,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
230
231
|
* Space Complexity O(1)
|
|
231
232
|
*/
|
|
232
233
|
|
|
234
|
+
|
|
233
235
|
/**
|
|
234
236
|
* Time Complexity O(log n) - O(n)
|
|
235
237
|
* Space Complexity O(1)
|
|
236
238
|
*
|
|
237
|
-
* The `add` function adds a new node to a binary tree, either by
|
|
238
|
-
*
|
|
239
|
-
* @
|
|
239
|
+
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
240
|
+
* existing node with the same key.
|
|
241
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
242
|
+
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
243
|
+
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
240
244
|
*/
|
|
241
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
245
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
|
242
246
|
|
|
243
247
|
let inserted: N | null | undefined;
|
|
244
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
248
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
245
249
|
if (newNode === undefined) return;
|
|
246
250
|
|
|
247
251
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
@@ -284,22 +288,38 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
284
288
|
* Time Complexity: O(k log n) - O(k * n)
|
|
285
289
|
* Space Complexity: O(1)
|
|
286
290
|
*
|
|
287
|
-
* The
|
|
288
|
-
*
|
|
289
|
-
* @param nodes -
|
|
290
|
-
*
|
|
291
|
-
* @returns The function `addMany` returns an array of
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>): (N | null | undefined)[] {
|
|
291
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
292
|
+
* adds each node with its corresponding value to the data structure.
|
|
293
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
294
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
295
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
296
|
+
*/
|
|
297
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
|
|
295
298
|
// TODO not sure addMany not be run multi times
|
|
296
299
|
const inserted: (N | null | undefined)[] = [];
|
|
300
|
+
|
|
301
|
+
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
302
|
+
if (values) {
|
|
303
|
+
valuesIterator = values[Symbol.iterator]();
|
|
304
|
+
}
|
|
305
|
+
|
|
297
306
|
for (const kne of nodes) {
|
|
298
|
-
|
|
307
|
+
let value: V | undefined | null = undefined;
|
|
308
|
+
|
|
309
|
+
if (valuesIterator) {
|
|
310
|
+
const valueResult = valuesIterator.next();
|
|
311
|
+
if (!valueResult.done) {
|
|
312
|
+
value = valueResult.value;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
inserted.push(this.add(kne, value));
|
|
299
317
|
}
|
|
318
|
+
|
|
300
319
|
return inserted;
|
|
301
320
|
}
|
|
302
321
|
|
|
322
|
+
|
|
303
323
|
/**
|
|
304
324
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
305
325
|
* Space Complexity: O(1)
|
|
@@ -1775,7 +1795,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1775
1795
|
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
1776
1796
|
* pass the given predicate function.
|
|
1777
1797
|
*/
|
|
1778
|
-
filter(predicate:
|
|
1798
|
+
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any) {
|
|
1779
1799
|
const newTree = this.createTree();
|
|
1780
1800
|
let index = 0;
|
|
1781
1801
|
for (const [key, value] of this) {
|
|
@@ -1806,7 +1826,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|
|
1806
1826
|
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
1807
1827
|
* @returns The `map` method is returning a new tree object.
|
|
1808
1828
|
*/
|
|
1809
|
-
map(callback:
|
|
1829
|
+
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any) {
|
|
1810
1830
|
const newTree = this.createTree();
|
|
1811
1831
|
let index = 0;
|
|
1812
1832
|
for (const [key, value] of this) {
|
|
@@ -154,13 +154,16 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
154
154
|
return exemplar instanceof BSTNode;
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
+
|
|
157
158
|
/**
|
|
158
|
-
* The function `exemplarToNode` takes an exemplar and returns a
|
|
159
|
-
*
|
|
160
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N
|
|
161
|
-
* @
|
|
159
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
160
|
+
* otherwise it returns undefined.
|
|
161
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
162
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
163
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
164
|
+
* @returns a node of type N or undefined.
|
|
162
165
|
*/
|
|
163
|
-
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
166
|
+
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
|
164
167
|
let node: N | undefined;
|
|
165
168
|
if (exemplar === null || exemplar === undefined) {
|
|
166
169
|
return;
|
|
@@ -174,7 +177,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
174
177
|
node = this.createNode(key, value);
|
|
175
178
|
}
|
|
176
179
|
} else if (this.isNotNodeInstance(exemplar)) {
|
|
177
|
-
node = this.createNode(exemplar);
|
|
180
|
+
node = this.createNode(exemplar, value);
|
|
178
181
|
} else {
|
|
179
182
|
return;
|
|
180
183
|
}
|
|
@@ -190,14 +193,16 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
190
193
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
191
194
|
* Space Complexity: O(1) - Constant space is used.
|
|
192
195
|
*
|
|
193
|
-
* The `add` function adds a new node to a binary
|
|
194
|
-
*
|
|
195
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can
|
|
196
|
-
* @
|
|
197
|
-
*
|
|
196
|
+
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
197
|
+
* or inserting a new node if the key is unique.
|
|
198
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
199
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
200
|
+
* being added to the binary tree.
|
|
201
|
+
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
202
|
+
* node was not added.
|
|
198
203
|
*/
|
|
199
|
-
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
200
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
204
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
|
205
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
201
206
|
if (newNode === undefined) return;
|
|
202
207
|
|
|
203
208
|
if (this.root === undefined) {
|
|
@@ -251,31 +256,45 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
251
256
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
252
257
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
253
258
|
*
|
|
254
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
255
|
-
*
|
|
256
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
257
|
-
* binary tree.
|
|
258
|
-
* @param [
|
|
259
|
-
*
|
|
259
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
260
|
+
* balancing the tree after each addition.
|
|
261
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
262
|
+
* the binary tree.
|
|
263
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
264
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
265
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
266
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
267
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
268
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
269
|
+
* in the order they appear in the input.
|
|
260
270
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
261
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
271
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
272
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
273
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
265
274
|
*/
|
|
266
275
|
override addMany(
|
|
267
276
|
keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>,
|
|
277
|
+
values?: Iterable<V | undefined>,
|
|
268
278
|
isBalanceAdd = true,
|
|
269
279
|
iterationType = this.iterationType
|
|
270
280
|
): (N | undefined)[] {
|
|
271
|
-
const inserted: (N | undefined)[] = []
|
|
281
|
+
const inserted: (N | undefined)[] = [];
|
|
282
|
+
|
|
283
|
+
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
284
|
+
|
|
285
|
+
if (values) {
|
|
286
|
+
valuesIterator = values[Symbol.iterator]();
|
|
287
|
+
}
|
|
288
|
+
|
|
272
289
|
if (!isBalanceAdd) {
|
|
273
290
|
for (const kve of keysOrNodesOrEntries) {
|
|
274
|
-
const
|
|
291
|
+
const value = valuesIterator?.next().value;
|
|
292
|
+
const nn = this.add(kve, value);
|
|
275
293
|
inserted.push(nn);
|
|
276
294
|
}
|
|
277
295
|
return inserted;
|
|
278
296
|
}
|
|
297
|
+
|
|
279
298
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
280
299
|
|
|
281
300
|
const isRealBTNExemplar = (kve: BTNodeExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
@@ -287,22 +306,20 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
287
306
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
288
307
|
}
|
|
289
308
|
|
|
290
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
291
309
|
let sorted: BTNodePureExemplar<K, V, N>[] = [];
|
|
292
310
|
|
|
293
311
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
294
312
|
let aR: number, bR: number;
|
|
295
|
-
if (this.isEntry(a)) aR = this.extractor(a[0])
|
|
296
|
-
else if (this.isRealNode(a)) aR = this.extractor(a.key)
|
|
313
|
+
if (this.isEntry(a)) aR = this.extractor(a[0]);
|
|
314
|
+
else if (this.isRealNode(a)) aR = this.extractor(a.key);
|
|
297
315
|
else aR = this.extractor(a);
|
|
298
316
|
|
|
299
|
-
if (this.isEntry(b)) bR = this.extractor(b[0])
|
|
300
|
-
else if (this.isRealNode(b)) bR = this.extractor(b.key)
|
|
317
|
+
if (this.isEntry(b)) bR = this.extractor(b[0]);
|
|
318
|
+
else if (this.isRealNode(b)) bR = this.extractor(b.key);
|
|
301
319
|
else bR = this.extractor(b);
|
|
302
320
|
|
|
303
321
|
return aR - bR;
|
|
304
|
-
})
|
|
305
|
-
|
|
322
|
+
});
|
|
306
323
|
|
|
307
324
|
const _dfs = (arr: BTNodePureExemplar<K, V, N>[]) => {
|
|
308
325
|
if (arr.length === 0) return;
|
|
@@ -313,6 +330,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
313
330
|
_dfs(arr.slice(0, mid));
|
|
314
331
|
_dfs(arr.slice(mid + 1));
|
|
315
332
|
};
|
|
333
|
+
|
|
316
334
|
const _iterate = () => {
|
|
317
335
|
const n = sorted.length;
|
|
318
336
|
const stack: [[number, number]] = [[0, n - 1]];
|
|
@@ -330,6 +348,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
330
348
|
}
|
|
331
349
|
}
|
|
332
350
|
};
|
|
351
|
+
|
|
333
352
|
if (iterationType === IterationType.RECURSIVE) {
|
|
334
353
|
_dfs(sorted);
|
|
335
354
|
} else {
|
|
@@ -339,6 +358,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|
|
339
358
|
return inserted;
|
|
340
359
|
}
|
|
341
360
|
|
|
361
|
+
|
|
342
362
|
// /**
|
|
343
363
|
// * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
344
364
|
// * Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
@@ -116,14 +116,14 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
|
-
* The function `exemplarToNode` takes an exemplar and
|
|
120
|
-
*
|
|
121
|
-
* @param
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @returns a
|
|
119
|
+
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
120
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
121
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
122
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
123
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
124
|
+
* @returns a node of type N or undefined.
|
|
125
125
|
*/
|
|
126
|
-
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
126
|
+
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
|
127
127
|
let node: N | undefined;
|
|
128
128
|
|
|
129
129
|
if (exemplar === null || exemplar === undefined) {
|
|
@@ -138,7 +138,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
138
138
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
139
139
|
}
|
|
140
140
|
} else if (this.isNotNodeInstance(exemplar)) {
|
|
141
|
-
node = this.createNode(exemplar,
|
|
141
|
+
node = this.createNode(exemplar, value, RBTNColor.RED);
|
|
142
142
|
} else {
|
|
143
143
|
return;
|
|
144
144
|
}
|
|
@@ -151,13 +151,19 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|
|
151
151
|
*/
|
|
152
152
|
|
|
153
153
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
* `
|
|
154
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
155
|
+
* Space Complexity: O(1)
|
|
156
|
+
*
|
|
157
|
+
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
158
|
+
* color changes to maintain the red-black tree properties.
|
|
159
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
160
|
+
* entry.
|
|
161
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
162
|
+
* being added to the binary search tree.
|
|
163
|
+
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
158
164
|
*/
|
|
159
|
-
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
160
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
165
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
|
166
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
161
167
|
if (newNode === undefined) return;
|
|
162
168
|
|
|
163
169
|
newNode.left = this.Sentinel;
|
|
@@ -85,15 +85,19 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
85
85
|
return exemplar instanceof TreeMultimapNode;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
|
|
88
89
|
/**
|
|
89
90
|
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
90
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`,
|
|
91
|
-
*
|
|
91
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
|
|
92
|
+
* can be one of the following:
|
|
93
|
+
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
94
|
+
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
95
|
+
* it defaults to `undefined`.
|
|
92
96
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
93
|
-
* times the
|
|
94
|
-
* @returns a
|
|
97
|
+
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
98
|
+
* @returns a node of type `N` or `undefined`.
|
|
95
99
|
*/
|
|
96
|
-
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, count = 1): N | undefined {
|
|
100
|
+
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
|
97
101
|
let node: N | undefined;
|
|
98
102
|
if (exemplar === undefined || exemplar === null) {
|
|
99
103
|
return;
|
|
@@ -107,7 +111,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
107
111
|
node = this.createNode(key, value, count);
|
|
108
112
|
}
|
|
109
113
|
} else if (this.isNotNodeInstance(exemplar)) {
|
|
110
|
-
node = this.createNode(exemplar,
|
|
114
|
+
node = this.createNode(exemplar, value, count);
|
|
111
115
|
} else {
|
|
112
116
|
return;
|
|
113
117
|
}
|
|
@@ -123,16 +127,20 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
123
127
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
124
128
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
125
129
|
*
|
|
126
|
-
* The
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* @param [
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* @
|
|
130
|
+
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
131
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
132
|
+
* entry. It represents the key, node, or entry that you want to add to the binary tree.
|
|
133
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
134
|
+
* binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
|
|
135
|
+
* method.
|
|
136
|
+
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
137
|
+
* be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
|
|
138
|
+
* added once. However, you can specify a different value for `count` if you want to add
|
|
139
|
+
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
140
|
+
* was not successful.
|
|
133
141
|
*/
|
|
134
|
-
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, count = 1): N | undefined {
|
|
135
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry, count);
|
|
142
|
+
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
|
143
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
136
144
|
if (newNode === undefined) return;
|
|
137
145
|
|
|
138
146
|
const orgNodeCount = newNode?.count || 0;
|
|
@@ -190,7 +198,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
190
198
|
if (l > r) return;
|
|
191
199
|
const m = l + Math.floor((r - l) / 2);
|
|
192
200
|
const midNode = sorted[m];
|
|
193
|
-
this.add(
|
|
201
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
194
202
|
buildBalanceBST(l, m - 1);
|
|
195
203
|
buildBalanceBST(m + 1, r);
|
|
196
204
|
};
|
|
@@ -206,7 +214,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
206
214
|
if (l <= r) {
|
|
207
215
|
const m = l + Math.floor((r - l) / 2);
|
|
208
216
|
const midNode = sorted[m];
|
|
209
|
-
this.add(
|
|
217
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
210
218
|
stack.push([m + 1, r]);
|
|
211
219
|
stack.push([l, m - 1]);
|
|
212
220
|
}
|
|
@@ -327,7 +335,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|
|
327
335
|
*/
|
|
328
336
|
override clone(): TREE {
|
|
329
337
|
const cloned = this.createTree();
|
|
330
|
-
this.bfs(node => cloned.add(
|
|
338
|
+
this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
331
339
|
return cloned;
|
|
332
340
|
}
|
|
333
341
|
|