undirected-graph-typed 1.52.9 → 1.53.0
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-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +63 -46
- package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/data-structures/binary-tree/binary-tree.js +375 -264
- package/dist/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/data-structures/binary-tree/bst.js +105 -77
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/rb-tree.js +35 -33
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/tree-multi-map.js +58 -48
- package/dist/data-structures/trie/trie.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +59 -53
- package/src/data-structures/binary-tree/avl-tree.ts +31 -34
- package/src/data-structures/binary-tree/binary-tree.ts +439 -359
- package/src/data-structures/binary-tree/bst.ts +142 -112
- package/src/data-structures/binary-tree/rb-tree.ts +37 -41
- package/src/data-structures/binary-tree/tree-multi-map.ts +56 -60
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/interfaces/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
|
@@ -20,7 +20,7 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
23
|
-
* @param {
|
|
23
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
24
24
|
* instance of the `NODE` class or `undefined`.
|
|
25
25
|
*/
|
|
26
26
|
set left(v) {
|
|
@@ -39,7 +39,7 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
41
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
42
|
-
* @param {
|
|
42
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
43
43
|
* `NODE` object or `undefined`.
|
|
44
44
|
*/
|
|
45
45
|
set right(v) {
|
|
@@ -62,13 +62,13 @@ exports.BSTNode = BSTNode;
|
|
|
62
62
|
class BST extends binary_tree_1.BinaryTree {
|
|
63
63
|
/**
|
|
64
64
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
65
|
-
* @param
|
|
65
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
66
66
|
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
67
67
|
* added to the binary search tree during the construction of the object.
|
|
68
68
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
69
69
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
70
70
|
*/
|
|
71
|
-
constructor(
|
|
71
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
72
72
|
super([], options);
|
|
73
73
|
this._root = undefined;
|
|
74
74
|
this._DEFAULT_COMPARATOR = (a, b) => {
|
|
@@ -87,8 +87,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
87
87
|
if (comparator)
|
|
88
88
|
this._comparator = comparator;
|
|
89
89
|
}
|
|
90
|
-
if (
|
|
91
|
-
this.addMany(
|
|
90
|
+
if (keysNodesEntriesOrRaws)
|
|
91
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
94
|
* The function returns the root node of a tree structure.
|
|
@@ -116,20 +116,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
116
116
|
* @returns a new instance of the BST class with the provided options.
|
|
117
117
|
*/
|
|
118
118
|
createTree(options) {
|
|
119
|
-
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
119
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, comparator: this._comparator, toEntryFn: this._toEntryFn }, options));
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
122
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
123
|
-
* @param {
|
|
124
|
-
* type R or
|
|
123
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
124
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
125
125
|
* element.
|
|
126
126
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
127
127
|
* value associated with a key in a key-value pair.
|
|
128
128
|
* @returns either a NODE object or undefined.
|
|
129
129
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
131
|
+
const [node, tValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
132
|
+
if (node === null)
|
|
133
|
+
return [undefined, undefined];
|
|
134
|
+
return [node, tValue !== null && tValue !== void 0 ? tValue : value];
|
|
133
135
|
}
|
|
134
136
|
/**
|
|
135
137
|
* Time Complexity: O(log n)
|
|
@@ -137,8 +139,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
137
139
|
*
|
|
138
140
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
139
141
|
* it doesn't exist.
|
|
140
|
-
* @param {
|
|
141
|
-
* `
|
|
142
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
143
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
|
|
142
144
|
* entry, or raw element that needs to be ensured in the tree.
|
|
143
145
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
144
146
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -146,19 +148,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
146
148
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
147
149
|
* not be ensured.
|
|
148
150
|
*/
|
|
149
|
-
ensureNode(
|
|
151
|
+
ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
|
|
150
152
|
var _a;
|
|
151
|
-
return (_a = super.ensureNode(
|
|
153
|
+
return (_a = super.ensureNode(keyNodeEntryOrRaw, iterationType)) !== null && _a !== void 0 ? _a : undefined;
|
|
152
154
|
}
|
|
153
155
|
/**
|
|
154
156
|
* The function checks if the input is an instance of the BSTNode class.
|
|
155
|
-
* @param {
|
|
156
|
-
* `
|
|
157
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
157
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
158
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
159
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
158
160
|
* an instance of the `BSTNode` class.
|
|
159
161
|
*/
|
|
160
|
-
isNode(
|
|
161
|
-
return
|
|
162
|
+
isNode(keyNodeEntryOrRaw) {
|
|
163
|
+
return keyNodeEntryOrRaw instanceof BSTNode;
|
|
162
164
|
}
|
|
163
165
|
/**
|
|
164
166
|
* The function "override isKey" checks if a key is comparable based on a given comparator.
|
|
@@ -176,18 +178,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
176
178
|
* Space Complexity: O(1)
|
|
177
179
|
*
|
|
178
180
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
179
|
-
* @param {
|
|
180
|
-
* `
|
|
181
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
182
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
181
183
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
182
184
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
183
185
|
* @returns a boolean value.
|
|
184
186
|
*/
|
|
185
|
-
add(
|
|
186
|
-
const newNode = this.
|
|
187
|
+
add(keyNodeEntryOrRaw, value) {
|
|
188
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
187
189
|
if (newNode === undefined)
|
|
188
190
|
return false;
|
|
189
191
|
if (this._root === undefined) {
|
|
190
192
|
this._setRoot(newNode);
|
|
193
|
+
if (this._isMapMode)
|
|
194
|
+
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
191
195
|
this._size++;
|
|
192
196
|
return true;
|
|
193
197
|
}
|
|
@@ -200,6 +204,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
200
204
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
201
205
|
if (current.left === undefined) {
|
|
202
206
|
current.left = newNode;
|
|
207
|
+
if (this._isMapMode)
|
|
208
|
+
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
203
209
|
this._size++;
|
|
204
210
|
return true;
|
|
205
211
|
}
|
|
@@ -208,6 +214,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
208
214
|
else {
|
|
209
215
|
if (current.right === undefined) {
|
|
210
216
|
current.right = newNode;
|
|
217
|
+
if (this._isMapMode)
|
|
218
|
+
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
211
219
|
this._size++;
|
|
212
220
|
return true;
|
|
213
221
|
}
|
|
@@ -222,7 +230,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
230
|
*
|
|
223
231
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
224
232
|
* an array indicating whether each key or node was successfully inserted.
|
|
225
|
-
* @param
|
|
233
|
+
* @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
226
234
|
* elements to be added to the data structure.
|
|
227
235
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
228
236
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -237,14 +245,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
237
245
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
238
246
|
* successfully inserted into the data structure.
|
|
239
247
|
*/
|
|
240
|
-
addMany(
|
|
248
|
+
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
241
249
|
const inserted = [];
|
|
242
250
|
let valuesIterator;
|
|
243
251
|
if (values) {
|
|
244
252
|
valuesIterator = values[Symbol.iterator]();
|
|
245
253
|
}
|
|
246
254
|
if (!isBalanceAdd) {
|
|
247
|
-
for (const kve of
|
|
255
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
248
256
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
249
257
|
inserted.push(this.add(kve, value));
|
|
250
258
|
}
|
|
@@ -252,7 +260,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
252
260
|
}
|
|
253
261
|
const realBTNExemplars = [];
|
|
254
262
|
let i = 0;
|
|
255
|
-
for (const kve of
|
|
263
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
256
264
|
realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i });
|
|
257
265
|
i++;
|
|
258
266
|
}
|
|
@@ -323,33 +331,33 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
323
331
|
* Space Complexity: O(k + log n)
|
|
324
332
|
*
|
|
325
333
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
326
|
-
* given
|
|
327
|
-
* @param {
|
|
334
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
335
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
328
336
|
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
329
|
-
* key, a node, an entry, or a custom
|
|
337
|
+
* key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
|
|
330
338
|
* included in the result.
|
|
331
339
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
332
|
-
* determines whether to return only the first node that matches the
|
|
333
|
-
* that match the
|
|
340
|
+
* determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
|
|
341
|
+
* that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
334
342
|
* and
|
|
335
|
-
* @param {
|
|
343
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
336
344
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
337
|
-
* nodes that match a given
|
|
345
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
338
346
|
* should begin. If not explicitly provided, the default value for `begin
|
|
339
347
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
340
348
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
341
349
|
* have two possible values:
|
|
342
|
-
* @returns The `getNodes` method returns an array of nodes that satisfy the given
|
|
350
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
|
|
343
351
|
*/
|
|
344
|
-
getNodes(
|
|
345
|
-
if (
|
|
352
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
353
|
+
if (keyNodeEntryRawOrPredicate === undefined)
|
|
346
354
|
return [];
|
|
347
|
-
if (
|
|
355
|
+
if (keyNodeEntryRawOrPredicate === null)
|
|
348
356
|
return [];
|
|
349
|
-
|
|
350
|
-
if (!
|
|
357
|
+
startNode = this.ensureNode(startNode);
|
|
358
|
+
if (!startNode)
|
|
351
359
|
return [];
|
|
352
|
-
const callback = this._ensurePredicate(
|
|
360
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
353
361
|
const ans = [];
|
|
354
362
|
if (iterationType === 'RECURSIVE') {
|
|
355
363
|
const dfs = (cur) => {
|
|
@@ -360,10 +368,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
360
368
|
}
|
|
361
369
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
362
370
|
return;
|
|
363
|
-
if (this.
|
|
364
|
-
|
|
371
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
372
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
373
|
+
if (this.isRealNode(cur.left) &&
|
|
374
|
+
benchmarkKey !== null &&
|
|
375
|
+
benchmarkKey !== undefined &&
|
|
376
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
365
377
|
dfs(cur.left);
|
|
366
|
-
if (this.isRealNode(cur.right) &&
|
|
378
|
+
if (this.isRealNode(cur.right) &&
|
|
379
|
+
benchmarkKey !== null &&
|
|
380
|
+
benchmarkKey !== undefined &&
|
|
381
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
367
382
|
dfs(cur.right);
|
|
368
383
|
}
|
|
369
384
|
else {
|
|
@@ -373,10 +388,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
373
388
|
dfs(cur.right);
|
|
374
389
|
}
|
|
375
390
|
};
|
|
376
|
-
dfs(
|
|
391
|
+
dfs(startNode);
|
|
377
392
|
}
|
|
378
393
|
else {
|
|
379
|
-
const stack = [
|
|
394
|
+
const stack = [startNode];
|
|
380
395
|
while (stack.length > 0) {
|
|
381
396
|
const cur = stack.pop();
|
|
382
397
|
if (callback(cur)) {
|
|
@@ -384,10 +399,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
384
399
|
if (onlyOne)
|
|
385
400
|
return ans;
|
|
386
401
|
}
|
|
387
|
-
if (this.
|
|
388
|
-
|
|
402
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
403
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
404
|
+
if (this.isRealNode(cur.right) &&
|
|
405
|
+
benchmarkKey !== null &&
|
|
406
|
+
benchmarkKey !== undefined &&
|
|
407
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
389
408
|
stack.push(cur.right);
|
|
390
|
-
if (this.isRealNode(cur.left) &&
|
|
409
|
+
if (this.isRealNode(cur.left) &&
|
|
410
|
+
benchmarkKey !== null &&
|
|
411
|
+
benchmarkKey !== undefined &&
|
|
412
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
391
413
|
stack.push(cur.left);
|
|
392
414
|
}
|
|
393
415
|
else {
|
|
@@ -404,10 +426,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
404
426
|
* Time Complexity: O(log n)
|
|
405
427
|
* Space Complexity: O(1)
|
|
406
428
|
*
|
|
407
|
-
* This function retrieves a node based on a given
|
|
408
|
-
* @param {
|
|
409
|
-
* parameter can be of type `
|
|
410
|
-
* @param {R |
|
|
429
|
+
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
|
|
430
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
431
|
+
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
|
|
432
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
|
|
411
433
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
412
434
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
413
435
|
* node of the binary search tree.
|
|
@@ -415,14 +437,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
415
437
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
416
438
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
417
439
|
* no value is provided when calling the method.
|
|
418
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`
|
|
419
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
420
|
-
* the specified root node (`
|
|
440
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
|
|
441
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
|
|
442
|
+
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
421
443
|
* returns the first node found or `undefined` if no node is found.
|
|
422
444
|
*/
|
|
423
|
-
getNode(
|
|
445
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
424
446
|
var _a;
|
|
425
|
-
return (_a = this.getNodes(
|
|
447
|
+
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
426
448
|
}
|
|
427
449
|
/**
|
|
428
450
|
* Time Complexity: O(log n)
|
|
@@ -448,11 +470,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
448
470
|
* the callback function.
|
|
449
471
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
450
472
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
451
|
-
* `this.
|
|
473
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
452
474
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
453
475
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
454
476
|
* take one of the following values:
|
|
455
|
-
* @param {
|
|
477
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
456
478
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
457
479
|
* node entry. If not specified, the default value is the root of the tree.
|
|
458
480
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -460,8 +482,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
460
482
|
* following values:
|
|
461
483
|
* @returns The method is returning an array of the return type of the callback function.
|
|
462
484
|
*/
|
|
463
|
-
dfs(callback = this.
|
|
464
|
-
return super.dfs(callback, pattern,
|
|
485
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) {
|
|
486
|
+
return super.dfs(callback, pattern, startNode, iterationType);
|
|
465
487
|
}
|
|
466
488
|
/**
|
|
467
489
|
* Time complexity: O(n)
|
|
@@ -472,7 +494,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
472
494
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
473
495
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
474
496
|
* node being visited, and it can return a value of any type.
|
|
475
|
-
* @param {
|
|
497
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
476
498
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
477
499
|
* object. If no value is provided, the default value is the root of the tree.
|
|
478
500
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -480,8 +502,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
480
502
|
* the following values:
|
|
481
503
|
* @returns an array of the return type of the callback function.
|
|
482
504
|
*/
|
|
483
|
-
bfs(callback = this.
|
|
484
|
-
return super.bfs(callback,
|
|
505
|
+
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
506
|
+
return super.bfs(callback, startNode, iterationType, false);
|
|
485
507
|
}
|
|
486
508
|
/**
|
|
487
509
|
* Time complexity: O(n)
|
|
@@ -490,9 +512,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
490
512
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
491
513
|
* containing the results of the callback function applied to each level of the tree.
|
|
492
514
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
493
|
-
* `
|
|
515
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
494
516
|
* tree during the iteration process.
|
|
495
|
-
* @param {
|
|
517
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
496
518
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
497
519
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
498
520
|
* value is provided, the root of
|
|
@@ -501,8 +523,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
501
523
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
502
524
|
* function.
|
|
503
525
|
*/
|
|
504
|
-
listLevels(callback = this.
|
|
505
|
-
return super.listLevels(callback,
|
|
526
|
+
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
527
|
+
return super.listLevels(callback, startNode, iterationType, false);
|
|
506
528
|
}
|
|
507
529
|
/**
|
|
508
530
|
* Time complexity: O(n)
|
|
@@ -516,7 +538,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
516
538
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
517
539
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
518
540
|
* 0, or 1, where:
|
|
519
|
-
* @param {
|
|
541
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
520
542
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
521
543
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
522
544
|
* `targetNode` is provided,
|
|
@@ -525,7 +547,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
525
547
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
526
548
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
527
549
|
*/
|
|
528
|
-
lesserOrGreaterTraverse(callback = this.
|
|
550
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
529
551
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
530
552
|
const ans = [];
|
|
531
553
|
if (!this._root)
|
|
@@ -577,7 +599,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
577
599
|
*/
|
|
578
600
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
579
601
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
580
|
-
this.
|
|
602
|
+
this._clearNodes();
|
|
581
603
|
if (sorted.length < 1)
|
|
582
604
|
return false;
|
|
583
605
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -586,7 +608,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
586
608
|
return;
|
|
587
609
|
const m = l + Math.floor((r - l) / 2);
|
|
588
610
|
const midNode = sorted[m];
|
|
589
|
-
this.
|
|
611
|
+
if (this._isMapMode)
|
|
612
|
+
this.add(midNode.key);
|
|
613
|
+
else
|
|
614
|
+
this.add([midNode.key, midNode.value]);
|
|
590
615
|
buildBalanceBST(l, m - 1);
|
|
591
616
|
buildBalanceBST(m + 1, r);
|
|
592
617
|
};
|
|
@@ -602,7 +627,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
602
627
|
if (l <= r) {
|
|
603
628
|
const m = l + Math.floor((r - l) / 2);
|
|
604
629
|
const midNode = sorted[m];
|
|
605
|
-
this.
|
|
630
|
+
if (this._isMapMode)
|
|
631
|
+
this.add(midNode.key);
|
|
632
|
+
else
|
|
633
|
+
this.add([midNode.key, midNode.value]);
|
|
606
634
|
stack.push([m + 1, r]);
|
|
607
635
|
stack.push([l, m - 1]);
|
|
608
636
|
}
|
|
@@ -678,7 +706,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
678
706
|
/**
|
|
679
707
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
680
708
|
* root.
|
|
681
|
-
* @param {
|
|
709
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
682
710
|
*/
|
|
683
711
|
_setRoot(v) {
|
|
684
712
|
if (v) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult,
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
2
|
import { BST, BSTNode } from './bst';
|
|
3
3
|
import { IBinaryTree } from '../../interfaces';
|
|
4
4
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -26,10 +26,10 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
26
26
|
*/
|
|
27
27
|
set color(value: RBTNColor);
|
|
28
28
|
}
|
|
29
|
-
export declare class RedBlackTree<K = any, V = any, R =
|
|
29
|
+
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
30
30
|
/**
|
|
31
31
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
32
|
-
* @param
|
|
32
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
33
33
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
34
34
|
* initialize the RBTree with the provided elements.
|
|
35
35
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -37,7 +37,7 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
37
37
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
38
38
|
* depend on the implementation
|
|
39
39
|
*/
|
|
40
|
-
constructor(
|
|
40
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RBTreeOptions<K, V, R>);
|
|
41
41
|
protected _root: NODE | undefined;
|
|
42
42
|
/**
|
|
43
43
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -71,12 +71,12 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
71
71
|
* Space Complexity: O(1)
|
|
72
72
|
*
|
|
73
73
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
74
|
-
* @param {
|
|
75
|
-
* `
|
|
76
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
74
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
75
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
76
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
77
77
|
* an instance of the `RedBlackTreeNode` class.
|
|
78
78
|
*/
|
|
79
|
-
isNode(
|
|
79
|
+
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
80
80
|
/**
|
|
81
81
|
* Time Complexity: O(1)
|
|
82
82
|
* Space Complexity: O(1)
|
|
@@ -91,8 +91,8 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
91
91
|
*
|
|
92
92
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
93
93
|
* added.
|
|
94
|
-
* @param {
|
|
95
|
-
* `
|
|
94
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
95
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
96
96
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
97
97
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
98
98
|
* structure.
|
|
@@ -100,14 +100,14 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
100
100
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
101
101
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
102
102
|
*/
|
|
103
|
-
add(
|
|
103
|
+
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean;
|
|
104
104
|
/**
|
|
105
105
|
* Time Complexity: O(log n)
|
|
106
106
|
* Space Complexity: O(1)
|
|
107
107
|
*
|
|
108
108
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
109
109
|
* a given predicate and maintain the binary search tree properties.
|
|
110
|
-
* @param {
|
|
110
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `keyNodeEntryOrRaw`
|
|
111
111
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
112
112
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
113
113
|
* function that determines which node(s) should be deleted.
|
|
@@ -115,7 +115,7 @@ export declare class RedBlackTree<K = any, V = any, R = BTNEntry<K, V>, NODE ext
|
|
|
115
115
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
116
116
|
* balancing is needed.
|
|
117
117
|
*/
|
|
118
|
-
delete(
|
|
118
|
+
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
119
119
|
/**
|
|
120
120
|
* Time Complexity: O(1)
|
|
121
121
|
* Space Complexity: O(1)
|