data-structure-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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +162 -162
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +21 -21
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +63 -46
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +375 -264
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/cjs/data-structures/binary-tree/bst.js +105 -77
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +35 -33
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +58 -48
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js +3 -3
- package/dist/cjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +21 -21
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +63 -45
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +28 -25
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +375 -263
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/mjs/data-structures/binary-tree/bst.js +105 -75
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +35 -32
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +58 -47
- package/dist/mjs/data-structures/trie/trie.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +5 -5
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/umd/data-structure-typed.js +583 -461
- package/dist/umd/data-structure-typed.min.js +5 -5
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- 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
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +113 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +87 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +162 -39
- package/test/unit/data-structures/binary-tree/bst.test.ts +315 -15
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +139 -1
|
@@ -19,7 +19,7 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
22
|
-
* @param {
|
|
22
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
23
23
|
* instance of the `NODE` class or `undefined`.
|
|
24
24
|
*/
|
|
25
25
|
set left(v) {
|
|
@@ -39,7 +39,7 @@ export class BSTNode extends 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) {
|
|
@@ -61,21 +61,21 @@ export class BSTNode extends BinaryTreeNode {
|
|
|
61
61
|
export class BST extends BinaryTree {
|
|
62
62
|
/**
|
|
63
63
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
64
|
-
* @param
|
|
64
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
65
65
|
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
66
66
|
* added to the binary search tree during the construction of the object.
|
|
67
67
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
68
68
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
69
69
|
*/
|
|
70
|
-
constructor(
|
|
70
|
+
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
71
71
|
super([], options);
|
|
72
72
|
if (options) {
|
|
73
73
|
const { comparator } = options;
|
|
74
74
|
if (comparator)
|
|
75
75
|
this._comparator = comparator;
|
|
76
76
|
}
|
|
77
|
-
if (
|
|
78
|
-
this.addMany(
|
|
77
|
+
if (keysNodesEntriesOrRaws)
|
|
78
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
79
79
|
}
|
|
80
80
|
_root = undefined;
|
|
81
81
|
/**
|
|
@@ -106,6 +106,7 @@ export class BST extends BinaryTree {
|
|
|
106
106
|
createTree(options) {
|
|
107
107
|
return new BST([], {
|
|
108
108
|
iterationType: this.iterationType,
|
|
109
|
+
isMapMode: this._isMapMode,
|
|
109
110
|
comparator: this._comparator,
|
|
110
111
|
toEntryFn: this._toEntryFn,
|
|
111
112
|
...options
|
|
@@ -113,15 +114,18 @@ export class BST extends BinaryTree {
|
|
|
113
114
|
}
|
|
114
115
|
/**
|
|
115
116
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
116
|
-
* @param {
|
|
117
|
-
* type R or
|
|
117
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
118
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
118
119
|
* element.
|
|
119
120
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
120
121
|
* value associated with a key in a key-value pair.
|
|
121
122
|
* @returns either a NODE object or undefined.
|
|
122
123
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
124
|
+
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
125
|
+
const [node, tValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
126
|
+
if (node === null)
|
|
127
|
+
return [undefined, undefined];
|
|
128
|
+
return [node, tValue ?? value];
|
|
125
129
|
}
|
|
126
130
|
/**
|
|
127
131
|
* Time Complexity: O(log n)
|
|
@@ -129,8 +133,8 @@ export class BST extends BinaryTree {
|
|
|
129
133
|
*
|
|
130
134
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
131
135
|
* it doesn't exist.
|
|
132
|
-
* @param {
|
|
133
|
-
* `
|
|
136
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
137
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
|
|
134
138
|
* entry, or raw element that needs to be ensured in the tree.
|
|
135
139
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
136
140
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -138,18 +142,18 @@ export class BST extends BinaryTree {
|
|
|
138
142
|
* @returns The method is returning either the node that was ensured or `undefined` if the node could
|
|
139
143
|
* not be ensured.
|
|
140
144
|
*/
|
|
141
|
-
ensureNode(
|
|
142
|
-
return super.ensureNode(
|
|
145
|
+
ensureNode(keyNodeEntryOrRaw, iterationType = this.iterationType) {
|
|
146
|
+
return super.ensureNode(keyNodeEntryOrRaw, iterationType) ?? undefined;
|
|
143
147
|
}
|
|
144
148
|
/**
|
|
145
149
|
* The function checks if the input is an instance of the BSTNode class.
|
|
146
|
-
* @param {
|
|
147
|
-
* `
|
|
148
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
150
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
151
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
152
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
149
153
|
* an instance of the `BSTNode` class.
|
|
150
154
|
*/
|
|
151
|
-
isNode(
|
|
152
|
-
return
|
|
155
|
+
isNode(keyNodeEntryOrRaw) {
|
|
156
|
+
return keyNodeEntryOrRaw instanceof BSTNode;
|
|
153
157
|
}
|
|
154
158
|
/**
|
|
155
159
|
* The function "override isKey" checks if a key is comparable based on a given comparator.
|
|
@@ -167,18 +171,20 @@ export class BST extends BinaryTree {
|
|
|
167
171
|
* Space Complexity: O(1)
|
|
168
172
|
*
|
|
169
173
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
170
|
-
* @param {
|
|
171
|
-
* `
|
|
174
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
175
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
172
176
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
173
177
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
174
178
|
* @returns a boolean value.
|
|
175
179
|
*/
|
|
176
|
-
add(
|
|
177
|
-
const newNode = this.
|
|
180
|
+
add(keyNodeEntryOrRaw, value) {
|
|
181
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
178
182
|
if (newNode === undefined)
|
|
179
183
|
return false;
|
|
180
184
|
if (this._root === undefined) {
|
|
181
185
|
this._setRoot(newNode);
|
|
186
|
+
if (this._isMapMode)
|
|
187
|
+
this._setValue(newNode?.key, newValue);
|
|
182
188
|
this._size++;
|
|
183
189
|
return true;
|
|
184
190
|
}
|
|
@@ -191,6 +197,8 @@ export class BST extends BinaryTree {
|
|
|
191
197
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
192
198
|
if (current.left === undefined) {
|
|
193
199
|
current.left = newNode;
|
|
200
|
+
if (this._isMapMode)
|
|
201
|
+
this._setValue(newNode?.key, newValue);
|
|
194
202
|
this._size++;
|
|
195
203
|
return true;
|
|
196
204
|
}
|
|
@@ -199,6 +207,8 @@ export class BST extends BinaryTree {
|
|
|
199
207
|
else {
|
|
200
208
|
if (current.right === undefined) {
|
|
201
209
|
current.right = newNode;
|
|
210
|
+
if (this._isMapMode)
|
|
211
|
+
this._setValue(newNode?.key, newValue);
|
|
202
212
|
this._size++;
|
|
203
213
|
return true;
|
|
204
214
|
}
|
|
@@ -213,7 +223,7 @@ export class BST extends BinaryTree {
|
|
|
213
223
|
*
|
|
214
224
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
215
225
|
* an array indicating whether each key or node was successfully inserted.
|
|
216
|
-
* @param
|
|
226
|
+
* @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
217
227
|
* elements to be added to the data structure.
|
|
218
228
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
219
229
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -228,14 +238,14 @@ export class BST extends BinaryTree {
|
|
|
228
238
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
229
239
|
* successfully inserted into the data structure.
|
|
230
240
|
*/
|
|
231
|
-
addMany(
|
|
241
|
+
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
232
242
|
const inserted = [];
|
|
233
243
|
let valuesIterator;
|
|
234
244
|
if (values) {
|
|
235
245
|
valuesIterator = values[Symbol.iterator]();
|
|
236
246
|
}
|
|
237
247
|
if (!isBalanceAdd) {
|
|
238
|
-
for (const kve of
|
|
248
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
239
249
|
const value = valuesIterator?.next().value;
|
|
240
250
|
inserted.push(this.add(kve, value));
|
|
241
251
|
}
|
|
@@ -243,7 +253,7 @@ export class BST extends BinaryTree {
|
|
|
243
253
|
}
|
|
244
254
|
const realBTNExemplars = [];
|
|
245
255
|
let i = 0;
|
|
246
|
-
for (const kve of
|
|
256
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
247
257
|
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
|
|
248
258
|
i++;
|
|
249
259
|
}
|
|
@@ -314,33 +324,33 @@ export class BST extends BinaryTree {
|
|
|
314
324
|
* Space Complexity: O(k + log n)
|
|
315
325
|
*
|
|
316
326
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
317
|
-
* given
|
|
318
|
-
* @param {
|
|
327
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
328
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
319
329
|
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
320
|
-
* key, a node, an entry, or a custom
|
|
330
|
+
* key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
|
|
321
331
|
* included in the result.
|
|
322
332
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
323
|
-
* determines whether to return only the first node that matches the
|
|
324
|
-
* that match the
|
|
333
|
+
* determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
|
|
334
|
+
* that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
325
335
|
* and
|
|
326
|
-
* @param {
|
|
336
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
327
337
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
328
|
-
* nodes that match a given
|
|
338
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
329
339
|
* should begin. If not explicitly provided, the default value for `begin
|
|
330
340
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
331
341
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
332
342
|
* have two possible values:
|
|
333
|
-
* @returns The `getNodes` method returns an array of nodes that satisfy the given
|
|
343
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
|
|
334
344
|
*/
|
|
335
|
-
getNodes(
|
|
336
|
-
if (
|
|
345
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
346
|
+
if (keyNodeEntryRawOrPredicate === undefined)
|
|
337
347
|
return [];
|
|
338
|
-
if (
|
|
348
|
+
if (keyNodeEntryRawOrPredicate === null)
|
|
339
349
|
return [];
|
|
340
|
-
|
|
341
|
-
if (!
|
|
350
|
+
startNode = this.ensureNode(startNode);
|
|
351
|
+
if (!startNode)
|
|
342
352
|
return [];
|
|
343
|
-
const callback = this._ensurePredicate(
|
|
353
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
344
354
|
const ans = [];
|
|
345
355
|
if (iterationType === 'RECURSIVE') {
|
|
346
356
|
const dfs = (cur) => {
|
|
@@ -351,10 +361,17 @@ export class BST extends BinaryTree {
|
|
|
351
361
|
}
|
|
352
362
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
353
363
|
return;
|
|
354
|
-
if (this.
|
|
355
|
-
|
|
364
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
365
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
366
|
+
if (this.isRealNode(cur.left) &&
|
|
367
|
+
benchmarkKey !== null &&
|
|
368
|
+
benchmarkKey !== undefined &&
|
|
369
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
356
370
|
dfs(cur.left);
|
|
357
|
-
if (this.isRealNode(cur.right) &&
|
|
371
|
+
if (this.isRealNode(cur.right) &&
|
|
372
|
+
benchmarkKey !== null &&
|
|
373
|
+
benchmarkKey !== undefined &&
|
|
374
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
358
375
|
dfs(cur.right);
|
|
359
376
|
}
|
|
360
377
|
else {
|
|
@@ -364,10 +381,10 @@ export class BST extends BinaryTree {
|
|
|
364
381
|
dfs(cur.right);
|
|
365
382
|
}
|
|
366
383
|
};
|
|
367
|
-
dfs(
|
|
384
|
+
dfs(startNode);
|
|
368
385
|
}
|
|
369
386
|
else {
|
|
370
|
-
const stack = [
|
|
387
|
+
const stack = [startNode];
|
|
371
388
|
while (stack.length > 0) {
|
|
372
389
|
const cur = stack.pop();
|
|
373
390
|
if (callback(cur)) {
|
|
@@ -375,10 +392,17 @@ export class BST extends BinaryTree {
|
|
|
375
392
|
if (onlyOne)
|
|
376
393
|
return ans;
|
|
377
394
|
}
|
|
378
|
-
if (this.
|
|
379
|
-
|
|
395
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
396
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
397
|
+
if (this.isRealNode(cur.right) &&
|
|
398
|
+
benchmarkKey !== null &&
|
|
399
|
+
benchmarkKey !== undefined &&
|
|
400
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
380
401
|
stack.push(cur.right);
|
|
381
|
-
if (this.isRealNode(cur.left) &&
|
|
402
|
+
if (this.isRealNode(cur.left) &&
|
|
403
|
+
benchmarkKey !== null &&
|
|
404
|
+
benchmarkKey !== undefined &&
|
|
405
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
382
406
|
stack.push(cur.left);
|
|
383
407
|
}
|
|
384
408
|
else {
|
|
@@ -395,10 +419,10 @@ export class BST extends BinaryTree {
|
|
|
395
419
|
* Time Complexity: O(log n)
|
|
396
420
|
* Space Complexity: O(1)
|
|
397
421
|
*
|
|
398
|
-
* This function retrieves a node based on a given
|
|
399
|
-
* @param {
|
|
400
|
-
* parameter can be of type `
|
|
401
|
-
* @param {R |
|
|
422
|
+
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
|
|
423
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
424
|
+
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
|
|
425
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
|
|
402
426
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
403
427
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
404
428
|
* node of the binary search tree.
|
|
@@ -406,13 +430,13 @@ export class BST extends BinaryTree {
|
|
|
406
430
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
407
431
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
408
432
|
* no value is provided when calling the method.
|
|
409
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`
|
|
410
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
411
|
-
* the specified root node (`
|
|
433
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
|
|
434
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
|
|
435
|
+
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
412
436
|
* returns the first node found or `undefined` if no node is found.
|
|
413
437
|
*/
|
|
414
|
-
getNode(
|
|
415
|
-
return this.getNodes(
|
|
438
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
439
|
+
return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined;
|
|
416
440
|
}
|
|
417
441
|
/**
|
|
418
442
|
* Time Complexity: O(log n)
|
|
@@ -438,11 +462,11 @@ export class BST extends BinaryTree {
|
|
|
438
462
|
* the callback function.
|
|
439
463
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
440
464
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
441
|
-
* `this.
|
|
465
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
442
466
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
443
467
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
444
468
|
* take one of the following values:
|
|
445
|
-
* @param {
|
|
469
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
446
470
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
447
471
|
* node entry. If not specified, the default value is the root of the tree.
|
|
448
472
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -450,8 +474,8 @@ export class BST extends BinaryTree {
|
|
|
450
474
|
* following values:
|
|
451
475
|
* @returns The method is returning an array of the return type of the callback function.
|
|
452
476
|
*/
|
|
453
|
-
dfs(callback = this.
|
|
454
|
-
return super.dfs(callback, pattern,
|
|
477
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) {
|
|
478
|
+
return super.dfs(callback, pattern, startNode, iterationType);
|
|
455
479
|
}
|
|
456
480
|
/**
|
|
457
481
|
* Time complexity: O(n)
|
|
@@ -462,7 +486,7 @@ export class BST extends BinaryTree {
|
|
|
462
486
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
463
487
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
464
488
|
* node being visited, and it can return a value of any type.
|
|
465
|
-
* @param {
|
|
489
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
466
490
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
467
491
|
* object. If no value is provided, the default value is the root of the tree.
|
|
468
492
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -470,8 +494,8 @@ export class BST extends BinaryTree {
|
|
|
470
494
|
* the following values:
|
|
471
495
|
* @returns an array of the return type of the callback function.
|
|
472
496
|
*/
|
|
473
|
-
bfs(callback = this.
|
|
474
|
-
return super.bfs(callback,
|
|
497
|
+
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
498
|
+
return super.bfs(callback, startNode, iterationType, false);
|
|
475
499
|
}
|
|
476
500
|
/**
|
|
477
501
|
* Time complexity: O(n)
|
|
@@ -480,9 +504,9 @@ export class BST extends BinaryTree {
|
|
|
480
504
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
481
505
|
* containing the results of the callback function applied to each level of the tree.
|
|
482
506
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
483
|
-
* `
|
|
507
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
484
508
|
* tree during the iteration process.
|
|
485
|
-
* @param {
|
|
509
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
486
510
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
487
511
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
488
512
|
* value is provided, the root of
|
|
@@ -491,8 +515,8 @@ export class BST extends BinaryTree {
|
|
|
491
515
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
492
516
|
* function.
|
|
493
517
|
*/
|
|
494
|
-
listLevels(callback = this.
|
|
495
|
-
return super.listLevels(callback,
|
|
518
|
+
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
519
|
+
return super.listLevels(callback, startNode, iterationType, false);
|
|
496
520
|
}
|
|
497
521
|
/**
|
|
498
522
|
* Time complexity: O(n)
|
|
@@ -506,7 +530,7 @@ export class BST extends BinaryTree {
|
|
|
506
530
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
507
531
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
508
532
|
* 0, or 1, where:
|
|
509
|
-
* @param {
|
|
533
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
510
534
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
511
535
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
512
536
|
* `targetNode` is provided,
|
|
@@ -515,7 +539,7 @@ export class BST extends BinaryTree {
|
|
|
515
539
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
516
540
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
517
541
|
*/
|
|
518
|
-
lesserOrGreaterTraverse(callback = this.
|
|
542
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
519
543
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
520
544
|
const ans = [];
|
|
521
545
|
if (!this._root)
|
|
@@ -567,7 +591,7 @@ export class BST extends BinaryTree {
|
|
|
567
591
|
*/
|
|
568
592
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
569
593
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
570
|
-
this.
|
|
594
|
+
this._clearNodes();
|
|
571
595
|
if (sorted.length < 1)
|
|
572
596
|
return false;
|
|
573
597
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -576,7 +600,10 @@ export class BST extends BinaryTree {
|
|
|
576
600
|
return;
|
|
577
601
|
const m = l + Math.floor((r - l) / 2);
|
|
578
602
|
const midNode = sorted[m];
|
|
579
|
-
this.
|
|
603
|
+
if (this._isMapMode)
|
|
604
|
+
this.add(midNode.key);
|
|
605
|
+
else
|
|
606
|
+
this.add([midNode.key, midNode.value]);
|
|
580
607
|
buildBalanceBST(l, m - 1);
|
|
581
608
|
buildBalanceBST(m + 1, r);
|
|
582
609
|
};
|
|
@@ -592,7 +619,10 @@ export class BST extends BinaryTree {
|
|
|
592
619
|
if (l <= r) {
|
|
593
620
|
const m = l + Math.floor((r - l) / 2);
|
|
594
621
|
const midNode = sorted[m];
|
|
595
|
-
this.
|
|
622
|
+
if (this._isMapMode)
|
|
623
|
+
this.add(midNode.key);
|
|
624
|
+
else
|
|
625
|
+
this.add([midNode.key, midNode.value]);
|
|
596
626
|
stack.push([m + 1, r]);
|
|
597
627
|
stack.push([l, m - 1]);
|
|
598
628
|
}
|
|
@@ -679,7 +709,7 @@ export class BST extends BinaryTree {
|
|
|
679
709
|
/**
|
|
680
710
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
681
711
|
* root.
|
|
682
|
-
* @param {
|
|
712
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
683
713
|
*/
|
|
684
714
|
_setRoot(v) {
|
|
685
715
|
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)
|