min-heap-typed 1.52.8 → 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 +22 -22
- 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 +28 -26
- package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/data-structures/binary-tree/binary-tree.js +395 -269
- package/dist/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/data-structures/binary-tree/bst.js +114 -91
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/rb-tree.js +35 -31
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/data-structures/binary-tree/tree-multi-map.js +59 -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 +60 -55
- package/src/data-structures/binary-tree/avl-tree.ts +31 -35
- package/src/data-structures/binary-tree/binary-tree.ts +461 -385
- package/src/data-structures/binary-tree/bst.ts +155 -128
- package/src/data-structures/binary-tree/rb-tree.ts +37 -39
- package/src/data-structures/binary-tree/tree-multi-map.ts +57 -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,32 +245,27 @@ 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
|
-
|
|
250
|
-
inserted.push(nn);
|
|
257
|
+
inserted.push(this.add(kve, value));
|
|
251
258
|
}
|
|
252
259
|
return inserted;
|
|
253
260
|
}
|
|
254
261
|
const realBTNExemplars = [];
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
};
|
|
260
|
-
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
261
|
-
if (isRealBTNExemplar(kve))
|
|
262
|
-
realBTNExemplars.push(kve);
|
|
262
|
+
let i = 0;
|
|
263
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
264
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value, orgIndex: i });
|
|
265
|
+
i++;
|
|
263
266
|
}
|
|
264
267
|
let sorted = [];
|
|
265
|
-
sorted = realBTNExemplars.sort((a, b) => {
|
|
268
|
+
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
266
269
|
let keyA, keyB;
|
|
267
270
|
if (this.isEntry(a))
|
|
268
271
|
keyA = a[0];
|
|
@@ -293,8 +296,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
293
296
|
if (arr.length === 0)
|
|
294
297
|
return;
|
|
295
298
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
296
|
-
const
|
|
297
|
-
inserted.
|
|
299
|
+
const { key, value, orgIndex } = arr[mid];
|
|
300
|
+
inserted[orgIndex] = this.add(key, value);
|
|
298
301
|
_dfs(arr.slice(0, mid));
|
|
299
302
|
_dfs(arr.slice(mid + 1));
|
|
300
303
|
};
|
|
@@ -307,8 +310,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
307
310
|
const [l, r] = popped;
|
|
308
311
|
if (l <= r) {
|
|
309
312
|
const m = l + Math.floor((r - l) / 2);
|
|
310
|
-
const
|
|
311
|
-
inserted.
|
|
313
|
+
const { key, value, orgIndex } = sorted[m];
|
|
314
|
+
inserted[orgIndex] = this.add(key, value);
|
|
312
315
|
stack.push([m + 1, r]);
|
|
313
316
|
stack.push([l, m - 1]);
|
|
314
317
|
}
|
|
@@ -328,33 +331,33 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
328
331
|
* Space Complexity: O(k + log n)
|
|
329
332
|
*
|
|
330
333
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
331
|
-
* given
|
|
332
|
-
* @param {
|
|
334
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
335
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
333
336
|
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
334
|
-
* 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
|
|
335
338
|
* included in the result.
|
|
336
339
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
337
|
-
* determines whether to return only the first node that matches the
|
|
338
|
-
* 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
|
|
339
342
|
* and
|
|
340
|
-
* @param {
|
|
343
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
341
344
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
342
|
-
* nodes that match a given
|
|
345
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
343
346
|
* should begin. If not explicitly provided, the default value for `begin
|
|
344
347
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
345
348
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
346
349
|
* have two possible values:
|
|
347
|
-
* @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.
|
|
348
351
|
*/
|
|
349
|
-
getNodes(
|
|
350
|
-
if (
|
|
352
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
353
|
+
if (keyNodeEntryRawOrPredicate === undefined)
|
|
351
354
|
return [];
|
|
352
|
-
if (
|
|
355
|
+
if (keyNodeEntryRawOrPredicate === null)
|
|
353
356
|
return [];
|
|
354
|
-
|
|
355
|
-
if (!
|
|
357
|
+
startNode = this.ensureNode(startNode);
|
|
358
|
+
if (!startNode)
|
|
356
359
|
return [];
|
|
357
|
-
const callback = this._ensurePredicate(
|
|
360
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
358
361
|
const ans = [];
|
|
359
362
|
if (iterationType === 'RECURSIVE') {
|
|
360
363
|
const dfs = (cur) => {
|
|
@@ -365,10 +368,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
365
368
|
}
|
|
366
369
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
367
370
|
return;
|
|
368
|
-
if (this.
|
|
369
|
-
|
|
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)
|
|
370
377
|
dfs(cur.left);
|
|
371
|
-
if (this.isRealNode(cur.right) &&
|
|
378
|
+
if (this.isRealNode(cur.right) &&
|
|
379
|
+
benchmarkKey !== null &&
|
|
380
|
+
benchmarkKey !== undefined &&
|
|
381
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
372
382
|
dfs(cur.right);
|
|
373
383
|
}
|
|
374
384
|
else {
|
|
@@ -378,10 +388,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
378
388
|
dfs(cur.right);
|
|
379
389
|
}
|
|
380
390
|
};
|
|
381
|
-
dfs(
|
|
391
|
+
dfs(startNode);
|
|
382
392
|
}
|
|
383
393
|
else {
|
|
384
|
-
const stack = [
|
|
394
|
+
const stack = [startNode];
|
|
385
395
|
while (stack.length > 0) {
|
|
386
396
|
const cur = stack.pop();
|
|
387
397
|
if (callback(cur)) {
|
|
@@ -389,10 +399,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
389
399
|
if (onlyOne)
|
|
390
400
|
return ans;
|
|
391
401
|
}
|
|
392
|
-
if (this.
|
|
393
|
-
|
|
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)
|
|
394
408
|
stack.push(cur.right);
|
|
395
|
-
if (this.isRealNode(cur.left) &&
|
|
409
|
+
if (this.isRealNode(cur.left) &&
|
|
410
|
+
benchmarkKey !== null &&
|
|
411
|
+
benchmarkKey !== undefined &&
|
|
412
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
396
413
|
stack.push(cur.left);
|
|
397
414
|
}
|
|
398
415
|
else {
|
|
@@ -409,10 +426,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
409
426
|
* Time Complexity: O(log n)
|
|
410
427
|
* Space Complexity: O(1)
|
|
411
428
|
*
|
|
412
|
-
* This function retrieves a node based on a given
|
|
413
|
-
* @param {
|
|
414
|
-
* parameter can be of type `
|
|
415
|
-
* @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
|
|
416
433
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
417
434
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
418
435
|
* node of the binary search tree.
|
|
@@ -420,14 +437,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
420
437
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
421
438
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
422
439
|
* no value is provided when calling the method.
|
|
423
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`
|
|
424
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
425
|
-
* 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
|
|
426
443
|
* returns the first node found or `undefined` if no node is found.
|
|
427
444
|
*/
|
|
428
|
-
getNode(
|
|
445
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
429
446
|
var _a;
|
|
430
|
-
return (_a = this.getNodes(
|
|
447
|
+
return (_a = this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
431
448
|
}
|
|
432
449
|
/**
|
|
433
450
|
* Time Complexity: O(log n)
|
|
@@ -453,11 +470,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
453
470
|
* the callback function.
|
|
454
471
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
455
472
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
456
|
-
* `this.
|
|
473
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
457
474
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
458
475
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
459
476
|
* take one of the following values:
|
|
460
|
-
* @param {
|
|
477
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
461
478
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
462
479
|
* node entry. If not specified, the default value is the root of the tree.
|
|
463
480
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -465,8 +482,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
465
482
|
* following values:
|
|
466
483
|
* @returns The method is returning an array of the return type of the callback function.
|
|
467
484
|
*/
|
|
468
|
-
dfs(callback = this.
|
|
469
|
-
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);
|
|
470
487
|
}
|
|
471
488
|
/**
|
|
472
489
|
* Time complexity: O(n)
|
|
@@ -477,7 +494,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
477
494
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
478
495
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
479
496
|
* node being visited, and it can return a value of any type.
|
|
480
|
-
* @param {
|
|
497
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
481
498
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
482
499
|
* object. If no value is provided, the default value is the root of the tree.
|
|
483
500
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -485,8 +502,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
485
502
|
* the following values:
|
|
486
503
|
* @returns an array of the return type of the callback function.
|
|
487
504
|
*/
|
|
488
|
-
bfs(callback = this.
|
|
489
|
-
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);
|
|
490
507
|
}
|
|
491
508
|
/**
|
|
492
509
|
* Time complexity: O(n)
|
|
@@ -495,9 +512,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
495
512
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
496
513
|
* containing the results of the callback function applied to each level of the tree.
|
|
497
514
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
498
|
-
* `
|
|
515
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
499
516
|
* tree during the iteration process.
|
|
500
|
-
* @param {
|
|
517
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
501
518
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
502
519
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
503
520
|
* value is provided, the root of
|
|
@@ -506,8 +523,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
506
523
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
507
524
|
* function.
|
|
508
525
|
*/
|
|
509
|
-
listLevels(callback = this.
|
|
510
|
-
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);
|
|
511
528
|
}
|
|
512
529
|
/**
|
|
513
530
|
* Time complexity: O(n)
|
|
@@ -521,7 +538,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
521
538
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
522
539
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
523
540
|
* 0, or 1, where:
|
|
524
|
-
* @param {
|
|
541
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
525
542
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
526
543
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
527
544
|
* `targetNode` is provided,
|
|
@@ -530,7 +547,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
530
547
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
531
548
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
532
549
|
*/
|
|
533
|
-
lesserOrGreaterTraverse(callback = this.
|
|
550
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
534
551
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
535
552
|
const ans = [];
|
|
536
553
|
if (!this._root)
|
|
@@ -582,7 +599,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
582
599
|
*/
|
|
583
600
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
584
601
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
585
|
-
this.
|
|
602
|
+
this._clearNodes();
|
|
586
603
|
if (sorted.length < 1)
|
|
587
604
|
return false;
|
|
588
605
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -591,7 +608,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
591
608
|
return;
|
|
592
609
|
const m = l + Math.floor((r - l) / 2);
|
|
593
610
|
const midNode = sorted[m];
|
|
594
|
-
this.
|
|
611
|
+
if (this._isMapMode)
|
|
612
|
+
this.add(midNode.key);
|
|
613
|
+
else
|
|
614
|
+
this.add([midNode.key, midNode.value]);
|
|
595
615
|
buildBalanceBST(l, m - 1);
|
|
596
616
|
buildBalanceBST(m + 1, r);
|
|
597
617
|
};
|
|
@@ -607,7 +627,10 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
607
627
|
if (l <= r) {
|
|
608
628
|
const m = l + Math.floor((r - l) / 2);
|
|
609
629
|
const midNode = sorted[m];
|
|
610
|
-
this.
|
|
630
|
+
if (this._isMapMode)
|
|
631
|
+
this.add(midNode.key);
|
|
632
|
+
else
|
|
633
|
+
this.add([midNode.key, midNode.value]);
|
|
611
634
|
stack.push([m + 1, r]);
|
|
612
635
|
stack.push([l, m - 1]);
|
|
613
636
|
}
|
|
@@ -683,7 +706,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
683
706
|
/**
|
|
684
707
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
685
708
|
* root.
|
|
686
|
-
* @param {
|
|
709
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
687
710
|
*/
|
|
688
711
|
_setRoot(v) {
|
|
689
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)
|