data-structure-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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +24 -384
- 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 +64 -47
- 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 +29 -27
- 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 +376 -265
- 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 +108 -78
- 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 +42 -36
- 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 +59 -49
- 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 +64 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +29 -26
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +376 -264
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/mjs/data-structures/binary-tree/bst.js +108 -76
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +42 -35
- 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 +59 -48
- 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 +596 -468
- 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 +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
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +9 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +115 -3
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +109 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +183 -46
- package/test/unit/data-structures/binary-tree/bst.test.ts +340 -21
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +19 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +141 -3
- package/test/utils/array.ts +15 -12
|
@@ -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
|
/**
|
|
@@ -94,7 +94,7 @@ export class BST extends BinaryTree {
|
|
|
94
94
|
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
|
|
95
95
|
*/
|
|
96
96
|
createNode(key, value) {
|
|
97
|
-
return new BSTNode(key, value);
|
|
97
|
+
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
100
|
* The function creates a new binary search tree with the specified options.
|
|
@@ -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, entryValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
126
|
+
if (node === null)
|
|
127
|
+
return [undefined, undefined];
|
|
128
|
+
return [node, value ?? entryValue];
|
|
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
|
}
|
|
@@ -186,11 +192,15 @@ export class BST extends BinaryTree {
|
|
|
186
192
|
while (current !== undefined) {
|
|
187
193
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
188
194
|
this._replaceNode(current, newNode);
|
|
195
|
+
if (this._isMapMode)
|
|
196
|
+
this._setValue(current.key, newValue);
|
|
189
197
|
return true;
|
|
190
198
|
}
|
|
191
199
|
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
192
200
|
if (current.left === undefined) {
|
|
193
201
|
current.left = newNode;
|
|
202
|
+
if (this._isMapMode)
|
|
203
|
+
this._setValue(newNode?.key, newValue);
|
|
194
204
|
this._size++;
|
|
195
205
|
return true;
|
|
196
206
|
}
|
|
@@ -199,6 +209,8 @@ export class BST extends BinaryTree {
|
|
|
199
209
|
else {
|
|
200
210
|
if (current.right === undefined) {
|
|
201
211
|
current.right = newNode;
|
|
212
|
+
if (this._isMapMode)
|
|
213
|
+
this._setValue(newNode?.key, newValue);
|
|
202
214
|
this._size++;
|
|
203
215
|
return true;
|
|
204
216
|
}
|
|
@@ -213,7 +225,7 @@ export class BST extends BinaryTree {
|
|
|
213
225
|
*
|
|
214
226
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
215
227
|
* an array indicating whether each key or node was successfully inserted.
|
|
216
|
-
* @param
|
|
228
|
+
* @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
217
229
|
* elements to be added to the data structure.
|
|
218
230
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
219
231
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -228,14 +240,14 @@ export class BST extends BinaryTree {
|
|
|
228
240
|
* @returns The function `addMany` returns an array of booleans indicating whether each element was
|
|
229
241
|
* successfully inserted into the data structure.
|
|
230
242
|
*/
|
|
231
|
-
addMany(
|
|
243
|
+
addMany(keysNodesEntriesOrRaws, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
232
244
|
const inserted = [];
|
|
233
245
|
let valuesIterator;
|
|
234
246
|
if (values) {
|
|
235
247
|
valuesIterator = values[Symbol.iterator]();
|
|
236
248
|
}
|
|
237
249
|
if (!isBalanceAdd) {
|
|
238
|
-
for (const kve of
|
|
250
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
239
251
|
const value = valuesIterator?.next().value;
|
|
240
252
|
inserted.push(this.add(kve, value));
|
|
241
253
|
}
|
|
@@ -243,7 +255,7 @@ export class BST extends BinaryTree {
|
|
|
243
255
|
}
|
|
244
256
|
const realBTNExemplars = [];
|
|
245
257
|
let i = 0;
|
|
246
|
-
for (const kve of
|
|
258
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
247
259
|
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
|
|
248
260
|
i++;
|
|
249
261
|
}
|
|
@@ -314,33 +326,33 @@ export class BST extends BinaryTree {
|
|
|
314
326
|
* Space Complexity: O(k + log n)
|
|
315
327
|
*
|
|
316
328
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
317
|
-
* given
|
|
318
|
-
* @param {
|
|
329
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
330
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
319
331
|
* 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
|
|
332
|
+
* key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
|
|
321
333
|
* included in the result.
|
|
322
334
|
* @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
|
|
335
|
+
* determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
|
|
336
|
+
* that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
325
337
|
* and
|
|
326
|
-
* @param {
|
|
338
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
327
339
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
328
|
-
* nodes that match a given
|
|
340
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
329
341
|
* should begin. If not explicitly provided, the default value for `begin
|
|
330
342
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
331
343
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
332
344
|
* have two possible values:
|
|
333
|
-
* @returns The `getNodes` method returns an array of nodes that satisfy the given
|
|
345
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
|
|
334
346
|
*/
|
|
335
|
-
getNodes(
|
|
336
|
-
if (
|
|
347
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
348
|
+
if (keyNodeEntryRawOrPredicate === undefined)
|
|
337
349
|
return [];
|
|
338
|
-
if (
|
|
350
|
+
if (keyNodeEntryRawOrPredicate === null)
|
|
339
351
|
return [];
|
|
340
|
-
|
|
341
|
-
if (!
|
|
352
|
+
startNode = this.ensureNode(startNode);
|
|
353
|
+
if (!startNode)
|
|
342
354
|
return [];
|
|
343
|
-
const callback = this._ensurePredicate(
|
|
355
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
344
356
|
const ans = [];
|
|
345
357
|
if (iterationType === 'RECURSIVE') {
|
|
346
358
|
const dfs = (cur) => {
|
|
@@ -351,10 +363,17 @@ export class BST extends BinaryTree {
|
|
|
351
363
|
}
|
|
352
364
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
353
365
|
return;
|
|
354
|
-
if (this.
|
|
355
|
-
|
|
366
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
367
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
368
|
+
if (this.isRealNode(cur.left) &&
|
|
369
|
+
benchmarkKey !== null &&
|
|
370
|
+
benchmarkKey !== undefined &&
|
|
371
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
356
372
|
dfs(cur.left);
|
|
357
|
-
if (this.isRealNode(cur.right) &&
|
|
373
|
+
if (this.isRealNode(cur.right) &&
|
|
374
|
+
benchmarkKey !== null &&
|
|
375
|
+
benchmarkKey !== undefined &&
|
|
376
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
358
377
|
dfs(cur.right);
|
|
359
378
|
}
|
|
360
379
|
else {
|
|
@@ -364,10 +383,10 @@ export class BST extends BinaryTree {
|
|
|
364
383
|
dfs(cur.right);
|
|
365
384
|
}
|
|
366
385
|
};
|
|
367
|
-
dfs(
|
|
386
|
+
dfs(startNode);
|
|
368
387
|
}
|
|
369
388
|
else {
|
|
370
|
-
const stack = [
|
|
389
|
+
const stack = [startNode];
|
|
371
390
|
while (stack.length > 0) {
|
|
372
391
|
const cur = stack.pop();
|
|
373
392
|
if (callback(cur)) {
|
|
@@ -375,10 +394,17 @@ export class BST extends BinaryTree {
|
|
|
375
394
|
if (onlyOne)
|
|
376
395
|
return ans;
|
|
377
396
|
}
|
|
378
|
-
if (this.
|
|
379
|
-
|
|
397
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
398
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
399
|
+
if (this.isRealNode(cur.right) &&
|
|
400
|
+
benchmarkKey !== null &&
|
|
401
|
+
benchmarkKey !== undefined &&
|
|
402
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
380
403
|
stack.push(cur.right);
|
|
381
|
-
if (this.isRealNode(cur.left) &&
|
|
404
|
+
if (this.isRealNode(cur.left) &&
|
|
405
|
+
benchmarkKey !== null &&
|
|
406
|
+
benchmarkKey !== undefined &&
|
|
407
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
382
408
|
stack.push(cur.left);
|
|
383
409
|
}
|
|
384
410
|
else {
|
|
@@ -395,10 +421,10 @@ export class BST extends BinaryTree {
|
|
|
395
421
|
* Time Complexity: O(log n)
|
|
396
422
|
* Space Complexity: O(1)
|
|
397
423
|
*
|
|
398
|
-
* This function retrieves a node based on a given
|
|
399
|
-
* @param {
|
|
400
|
-
* parameter can be of type `
|
|
401
|
-
* @param {R |
|
|
424
|
+
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
|
|
425
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
426
|
+
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
|
|
427
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
|
|
402
428
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
403
429
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
404
430
|
* node of the binary search tree.
|
|
@@ -406,13 +432,13 @@ export class BST extends BinaryTree {
|
|
|
406
432
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
407
433
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
408
434
|
* 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 (`
|
|
435
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
|
|
436
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
|
|
437
|
+
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
412
438
|
* returns the first node found or `undefined` if no node is found.
|
|
413
439
|
*/
|
|
414
|
-
getNode(
|
|
415
|
-
return this.getNodes(
|
|
440
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
441
|
+
return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined;
|
|
416
442
|
}
|
|
417
443
|
/**
|
|
418
444
|
* Time Complexity: O(log n)
|
|
@@ -438,11 +464,11 @@ export class BST extends BinaryTree {
|
|
|
438
464
|
* the callback function.
|
|
439
465
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
440
466
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
441
|
-
* `this.
|
|
467
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
442
468
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
443
469
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
444
470
|
* take one of the following values:
|
|
445
|
-
* @param {
|
|
471
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
446
472
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
447
473
|
* node entry. If not specified, the default value is the root of the tree.
|
|
448
474
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -450,8 +476,8 @@ export class BST extends BinaryTree {
|
|
|
450
476
|
* following values:
|
|
451
477
|
* @returns The method is returning an array of the return type of the callback function.
|
|
452
478
|
*/
|
|
453
|
-
dfs(callback = this.
|
|
454
|
-
return super.dfs(callback, pattern,
|
|
479
|
+
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) {
|
|
480
|
+
return super.dfs(callback, pattern, startNode, iterationType);
|
|
455
481
|
}
|
|
456
482
|
/**
|
|
457
483
|
* Time complexity: O(n)
|
|
@@ -462,7 +488,7 @@ export class BST extends BinaryTree {
|
|
|
462
488
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
463
489
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
464
490
|
* node being visited, and it can return a value of any type.
|
|
465
|
-
* @param {
|
|
491
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
466
492
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
467
493
|
* object. If no value is provided, the default value is the root of the tree.
|
|
468
494
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -470,8 +496,8 @@ export class BST extends BinaryTree {
|
|
|
470
496
|
* the following values:
|
|
471
497
|
* @returns an array of the return type of the callback function.
|
|
472
498
|
*/
|
|
473
|
-
bfs(callback = this.
|
|
474
|
-
return super.bfs(callback,
|
|
499
|
+
bfs(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
500
|
+
return super.bfs(callback, startNode, iterationType, false);
|
|
475
501
|
}
|
|
476
502
|
/**
|
|
477
503
|
* Time complexity: O(n)
|
|
@@ -480,9 +506,9 @@ export class BST extends BinaryTree {
|
|
|
480
506
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
481
507
|
* containing the results of the callback function applied to each level of the tree.
|
|
482
508
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
483
|
-
* `
|
|
509
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
484
510
|
* tree during the iteration process.
|
|
485
|
-
* @param {
|
|
511
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
486
512
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
487
513
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
488
514
|
* value is provided, the root of
|
|
@@ -491,8 +517,8 @@ export class BST extends BinaryTree {
|
|
|
491
517
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
492
518
|
* function.
|
|
493
519
|
*/
|
|
494
|
-
listLevels(callback = this.
|
|
495
|
-
return super.listLevels(callback,
|
|
520
|
+
listLevels(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
521
|
+
return super.listLevels(callback, startNode, iterationType, false);
|
|
496
522
|
}
|
|
497
523
|
/**
|
|
498
524
|
* Time complexity: O(n)
|
|
@@ -506,7 +532,7 @@ export class BST extends BinaryTree {
|
|
|
506
532
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
507
533
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
508
534
|
* 0, or 1, where:
|
|
509
|
-
* @param {
|
|
535
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
510
536
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
511
537
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
512
538
|
* `targetNode` is provided,
|
|
@@ -515,7 +541,7 @@ export class BST extends BinaryTree {
|
|
|
515
541
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
516
542
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
517
543
|
*/
|
|
518
|
-
lesserOrGreaterTraverse(callback = this.
|
|
544
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
519
545
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
520
546
|
const ans = [];
|
|
521
547
|
if (!this._root)
|
|
@@ -567,7 +593,7 @@ export class BST extends BinaryTree {
|
|
|
567
593
|
*/
|
|
568
594
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
569
595
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
570
|
-
this.
|
|
596
|
+
this._clearNodes();
|
|
571
597
|
if (sorted.length < 1)
|
|
572
598
|
return false;
|
|
573
599
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -576,7 +602,10 @@ export class BST extends BinaryTree {
|
|
|
576
602
|
return;
|
|
577
603
|
const m = l + Math.floor((r - l) / 2);
|
|
578
604
|
const midNode = sorted[m];
|
|
579
|
-
this.
|
|
605
|
+
if (this._isMapMode)
|
|
606
|
+
this.add(midNode.key);
|
|
607
|
+
else
|
|
608
|
+
this.add([midNode.key, midNode.value]);
|
|
580
609
|
buildBalanceBST(l, m - 1);
|
|
581
610
|
buildBalanceBST(m + 1, r);
|
|
582
611
|
};
|
|
@@ -592,7 +621,10 @@ export class BST extends BinaryTree {
|
|
|
592
621
|
if (l <= r) {
|
|
593
622
|
const m = l + Math.floor((r - l) / 2);
|
|
594
623
|
const midNode = sorted[m];
|
|
595
|
-
this.
|
|
624
|
+
if (this._isMapMode)
|
|
625
|
+
this.add(midNode.key);
|
|
626
|
+
else
|
|
627
|
+
this.add([midNode.key, midNode.value]);
|
|
596
628
|
stack.push([m + 1, r]);
|
|
597
629
|
stack.push([l, m - 1]);
|
|
598
630
|
}
|
|
@@ -679,7 +711,7 @@ export class BST extends BinaryTree {
|
|
|
679
711
|
/**
|
|
680
712
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
681
713
|
* root.
|
|
682
|
-
* @param {
|
|
714
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
683
715
|
*/
|
|
684
716
|
_setRoot(v) {
|
|
685
717
|
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)
|