priority-queue-typed 1.52.9 → 1.53.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +64 -47
- package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/data-structures/binary-tree/avl-tree.js +29 -27
- package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/data-structures/binary-tree/binary-tree.js +376 -265
- package/dist/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/data-structures/binary-tree/bst.js +108 -78
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/rb-tree.js +42 -36
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/tree-multi-map.js +59 -49
- 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 +60 -54
- package/src/data-structures/binary-tree/avl-tree.ts +32 -35
- package/src/data-structures/binary-tree/binary-tree.ts +440 -360
- package/src/data-structures/binary-tree/bst.ts +144 -113
- package/src/data-structures/binary-tree/rb-tree.ts +44 -43
- package/src/data-structures/binary-tree/tree-multi-map.ts +57 -61
- 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 +13 -14
- package/src/types/data-structures/binary-tree/bst.ts +3 -3
|
@@ -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.
|
|
@@ -106,7 +106,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
106
106
|
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
|
|
107
107
|
*/
|
|
108
108
|
createNode(key, value) {
|
|
109
|
-
return new BSTNode(key, value);
|
|
109
|
+
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* The function creates a new binary search tree with the specified options.
|
|
@@ -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, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
132
|
+
if (node === null)
|
|
133
|
+
return [undefined, undefined];
|
|
134
|
+
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
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
|
}
|
|
@@ -195,11 +199,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
195
199
|
while (current !== undefined) {
|
|
196
200
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
197
201
|
this._replaceNode(current, newNode);
|
|
202
|
+
if (this._isMapMode)
|
|
203
|
+
this._setValue(current.key, newValue);
|
|
198
204
|
return true;
|
|
199
205
|
}
|
|
200
206
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
201
207
|
if (current.left === undefined) {
|
|
202
208
|
current.left = newNode;
|
|
209
|
+
if (this._isMapMode)
|
|
210
|
+
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
203
211
|
this._size++;
|
|
204
212
|
return true;
|
|
205
213
|
}
|
|
@@ -208,6 +216,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
208
216
|
else {
|
|
209
217
|
if (current.right === undefined) {
|
|
210
218
|
current.right = newNode;
|
|
219
|
+
if (this._isMapMode)
|
|
220
|
+
this._setValue(newNode === null || newNode === void 0 ? void 0 : newNode.key, newValue);
|
|
211
221
|
this._size++;
|
|
212
222
|
return true;
|
|
213
223
|
}
|
|
@@ -222,7 +232,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
222
232
|
*
|
|
223
233
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
224
234
|
* an array indicating whether each key or node was successfully inserted.
|
|
225
|
-
* @param
|
|
235
|
+
* @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
226
236
|
* elements to be added to the data structure.
|
|
227
237
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
228
238
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -237,14 +247,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
237
247
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
238
248
|
* successfully inserted into the data structure.
|
|
239
249
|
*/
|
|
240
|
-
addMany(
|
|
250
|
+
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
241
251
|
const inserted = [];
|
|
242
252
|
let valuesIterator;
|
|
243
253
|
if (values) {
|
|
244
254
|
valuesIterator = values[Symbol.iterator]();
|
|
245
255
|
}
|
|
246
256
|
if (!isBalanceAdd) {
|
|
247
|
-
for (const kve of
|
|
257
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
248
258
|
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
249
259
|
inserted.push(this.add(kve, value));
|
|
250
260
|
}
|
|
@@ -252,7 +262,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
252
262
|
}
|
|
253
263
|
const realBTNExemplars = [];
|
|
254
264
|
let i = 0;
|
|
255
|
-
for (const kve of
|
|
265
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
256
266
|
realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i });
|
|
257
267
|
i++;
|
|
258
268
|
}
|
|
@@ -323,33 +333,33 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
323
333
|
* Space Complexity: O(k + log n)
|
|
324
334
|
*
|
|
325
335
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
326
|
-
* given
|
|
327
|
-
* @param {
|
|
336
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
337
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
328
338
|
* 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
|
|
339
|
+
* key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
|
|
330
340
|
* included in the result.
|
|
331
341
|
* @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
|
|
342
|
+
* determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
|
|
343
|
+
* that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
334
344
|
* and
|
|
335
|
-
* @param {
|
|
345
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
336
346
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
337
|
-
* nodes that match a given
|
|
347
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
338
348
|
* should begin. If not explicitly provided, the default value for `begin
|
|
339
349
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
340
350
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
341
351
|
* have two possible values:
|
|
342
|
-
* @returns The `getNodes` method returns an array of nodes that satisfy the given
|
|
352
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
|
|
343
353
|
*/
|
|
344
|
-
getNodes(
|
|
345
|
-
if (
|
|
354
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
355
|
+
if (keyNodeEntryRawOrPredicate === undefined)
|
|
346
356
|
return [];
|
|
347
|
-
if (
|
|
357
|
+
if (keyNodeEntryRawOrPredicate === null)
|
|
348
358
|
return [];
|
|
349
|
-
|
|
350
|
-
if (!
|
|
359
|
+
startNode = this.ensureNode(startNode);
|
|
360
|
+
if (!startNode)
|
|
351
361
|
return [];
|
|
352
|
-
const callback = this._ensurePredicate(
|
|
362
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
353
363
|
const ans = [];
|
|
354
364
|
if (iterationType === 'RECURSIVE') {
|
|
355
365
|
const dfs = (cur) => {
|
|
@@ -360,10 +370,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
360
370
|
}
|
|
361
371
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
362
372
|
return;
|
|
363
|
-
if (this.
|
|
364
|
-
|
|
373
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
374
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
375
|
+
if (this.isRealNode(cur.left) &&
|
|
376
|
+
benchmarkKey !== null &&
|
|
377
|
+
benchmarkKey !== undefined &&
|
|
378
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
365
379
|
dfs(cur.left);
|
|
366
|
-
if (this.isRealNode(cur.right) &&
|
|
380
|
+
if (this.isRealNode(cur.right) &&
|
|
381
|
+
benchmarkKey !== null &&
|
|
382
|
+
benchmarkKey !== undefined &&
|
|
383
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
367
384
|
dfs(cur.right);
|
|
368
385
|
}
|
|
369
386
|
else {
|
|
@@ -373,10 +390,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
373
390
|
dfs(cur.right);
|
|
374
391
|
}
|
|
375
392
|
};
|
|
376
|
-
dfs(
|
|
393
|
+
dfs(startNode);
|
|
377
394
|
}
|
|
378
395
|
else {
|
|
379
|
-
const stack = [
|
|
396
|
+
const stack = [startNode];
|
|
380
397
|
while (stack.length > 0) {
|
|
381
398
|
const cur = stack.pop();
|
|
382
399
|
if (callback(cur)) {
|
|
@@ -384,10 +401,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
384
401
|
if (onlyOne)
|
|
385
402
|
return ans;
|
|
386
403
|
}
|
|
387
|
-
if (this.
|
|
388
|
-
|
|
404
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
405
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
406
|
+
if (this.isRealNode(cur.right) &&
|
|
407
|
+
benchmarkKey !== null &&
|
|
408
|
+
benchmarkKey !== undefined &&
|
|
409
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
389
410
|
stack.push(cur.right);
|
|
390
|
-
if (this.isRealNode(cur.left) &&
|
|
411
|
+
if (this.isRealNode(cur.left) &&
|
|
412
|
+
benchmarkKey !== null &&
|
|
413
|
+
benchmarkKey !== undefined &&
|
|
414
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
391
415
|
stack.push(cur.left);
|
|
392
416
|
}
|
|
393
417
|
else {
|
|
@@ -404,10 +428,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
404
428
|
* Time Complexity: O(log n)
|
|
405
429
|
* Space Complexity: O(1)
|
|
406
430
|
*
|
|
407
|
-
* This function retrieves a node based on a given
|
|
408
|
-
* @param {
|
|
409
|
-
* parameter can be of type `
|
|
410
|
-
* @param {R |
|
|
431
|
+
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
|
|
432
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
433
|
+
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
|
|
434
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
|
|
411
435
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
412
436
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
413
437
|
* node of the binary search tree.
|
|
@@ -415,14 +439,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
415
439
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
416
440
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
417
441
|
* 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 (`
|
|
442
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
|
|
443
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
|
|
444
|
+
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
421
445
|
* returns the first node found or `undefined` if no node is found.
|
|
422
446
|
*/
|
|
423
|
-
getNode(
|
|
447
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
424
448
|
var _a;
|
|
425
|
-
return (_a = this.getNodes(
|
|
449
|
+
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
426
450
|
}
|
|
427
451
|
/**
|
|
428
452
|
* Time Complexity: O(log n)
|
|
@@ -448,11 +472,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
448
472
|
* the callback function.
|
|
449
473
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
450
474
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
451
|
-
* `this.
|
|
475
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
452
476
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
453
477
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
454
478
|
* take one of the following values:
|
|
455
|
-
* @param {
|
|
479
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
456
480
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
457
481
|
* node entry. If not specified, the default value is the root of the tree.
|
|
458
482
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -460,8 +484,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
460
484
|
* following values:
|
|
461
485
|
* @returns The method is returning an array of the return type of the callback function.
|
|
462
486
|
*/
|
|
463
|
-
dfs(callback = this.
|
|
464
|
-
return super.dfs(callback, pattern,
|
|
487
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) {
|
|
488
|
+
return super.dfs(callback, pattern, startNode, iterationType);
|
|
465
489
|
}
|
|
466
490
|
/**
|
|
467
491
|
* Time complexity: O(n)
|
|
@@ -472,7 +496,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
472
496
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
473
497
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
474
498
|
* node being visited, and it can return a value of any type.
|
|
475
|
-
* @param {
|
|
499
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
476
500
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
477
501
|
* object. If no value is provided, the default value is the root of the tree.
|
|
478
502
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -480,8 +504,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
480
504
|
* the following values:
|
|
481
505
|
* @returns an array of the return type of the callback function.
|
|
482
506
|
*/
|
|
483
|
-
bfs(callback = this.
|
|
484
|
-
return super.bfs(callback,
|
|
507
|
+
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
508
|
+
return super.bfs(callback, startNode, iterationType, false);
|
|
485
509
|
}
|
|
486
510
|
/**
|
|
487
511
|
* Time complexity: O(n)
|
|
@@ -490,9 +514,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
490
514
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
491
515
|
* containing the results of the callback function applied to each level of the tree.
|
|
492
516
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
493
|
-
* `
|
|
517
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
494
518
|
* tree during the iteration process.
|
|
495
|
-
* @param {
|
|
519
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
496
520
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
497
521
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
498
522
|
* value is provided, the root of
|
|
@@ -501,8 +525,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
501
525
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
502
526
|
* function.
|
|
503
527
|
*/
|
|
504
|
-
listLevels(callback = this.
|
|
505
|
-
return super.listLevels(callback,
|
|
528
|
+
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
529
|
+
return super.listLevels(callback, startNode, iterationType, false);
|
|
506
530
|
}
|
|
507
531
|
/**
|
|
508
532
|
* Time complexity: O(n)
|
|
@@ -516,7 +540,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
516
540
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
517
541
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
518
542
|
* 0, or 1, where:
|
|
519
|
-
* @param {
|
|
543
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
520
544
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
521
545
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
522
546
|
* `targetNode` is provided,
|
|
@@ -525,7 +549,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
525
549
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
526
550
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
527
551
|
*/
|
|
528
|
-
lesserOrGreaterTraverse(callback = this.
|
|
552
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
529
553
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
530
554
|
const ans = [];
|
|
531
555
|
if (!this._root)
|
|
@@ -577,7 +601,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
577
601
|
*/
|
|
578
602
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
579
603
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
580
|
-
this.
|
|
604
|
+
this._clearNodes();
|
|
581
605
|
if (sorted.length < 1)
|
|
582
606
|
return false;
|
|
583
607
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -586,7 +610,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
586
610
|
return;
|
|
587
611
|
const m = l + Math.floor((r - l) / 2);
|
|
588
612
|
const midNode = sorted[m];
|
|
589
|
-
this.
|
|
613
|
+
if (this._isMapMode)
|
|
614
|
+
this.add(midNode.key);
|
|
615
|
+
else
|
|
616
|
+
this.add([midNode.key, midNode.value]);
|
|
590
617
|
buildBalanceBST(l, m - 1);
|
|
591
618
|
buildBalanceBST(m + 1, r);
|
|
592
619
|
};
|
|
@@ -602,7 +629,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
602
629
|
if (l <= r) {
|
|
603
630
|
const m = l + Math.floor((r - l) / 2);
|
|
604
631
|
const midNode = sorted[m];
|
|
605
|
-
this.
|
|
632
|
+
if (this._isMapMode)
|
|
633
|
+
this.add(midNode.key);
|
|
634
|
+
else
|
|
635
|
+
this.add([midNode.key, midNode.value]);
|
|
606
636
|
stack.push([m + 1, r]);
|
|
607
637
|
stack.push([l, m - 1]);
|
|
608
638
|
}
|
|
@@ -678,7 +708,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
678
708
|
/**
|
|
679
709
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
680
710
|
* root.
|
|
681
|
-
* @param {
|
|
711
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
682
712
|
*/
|
|
683
713
|
_setRoot(v) {
|
|
684
714
|
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)
|