data-structure-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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +151 -151
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +22 -22
- 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 +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 +240 -141
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +395 -269
- 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 +114 -91
- 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 -31
- 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 +22 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +59 -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 +22 -22
- 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 +28 -25
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +240 -141
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +395 -268
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/mjs/data-structures/binary-tree/bst.js +114 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +35 -30
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +22 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +59 -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 +617 -482
- 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 -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
- package/test/integration/bst.test.ts +2 -2
- 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 +107 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +169 -26
- package/test/unit/data-structures/binary-tree/bst.test.ts +326 -21
- 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,32 +238,27 @@ 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
|
-
|
|
241
|
-
inserted.push(nn);
|
|
250
|
+
inserted.push(this.add(kve, value));
|
|
242
251
|
}
|
|
243
252
|
return inserted;
|
|
244
253
|
}
|
|
245
254
|
const realBTNExemplars = [];
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
};
|
|
251
|
-
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
252
|
-
if (isRealBTNExemplar(kve))
|
|
253
|
-
realBTNExemplars.push(kve);
|
|
255
|
+
let i = 0;
|
|
256
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
257
|
+
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
|
|
258
|
+
i++;
|
|
254
259
|
}
|
|
255
260
|
let sorted = [];
|
|
256
|
-
sorted = realBTNExemplars.sort((a, b) => {
|
|
261
|
+
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
257
262
|
let keyA, keyB;
|
|
258
263
|
if (this.isEntry(a))
|
|
259
264
|
keyA = a[0];
|
|
@@ -284,8 +289,8 @@ export class BST extends BinaryTree {
|
|
|
284
289
|
if (arr.length === 0)
|
|
285
290
|
return;
|
|
286
291
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
287
|
-
const
|
|
288
|
-
inserted.
|
|
292
|
+
const { key, value, orgIndex } = arr[mid];
|
|
293
|
+
inserted[orgIndex] = this.add(key, value);
|
|
289
294
|
_dfs(arr.slice(0, mid));
|
|
290
295
|
_dfs(arr.slice(mid + 1));
|
|
291
296
|
};
|
|
@@ -298,8 +303,8 @@ export class BST extends BinaryTree {
|
|
|
298
303
|
const [l, r] = popped;
|
|
299
304
|
if (l <= r) {
|
|
300
305
|
const m = l + Math.floor((r - l) / 2);
|
|
301
|
-
const
|
|
302
|
-
inserted.
|
|
306
|
+
const { key, value, orgIndex } = sorted[m];
|
|
307
|
+
inserted[orgIndex] = this.add(key, value);
|
|
303
308
|
stack.push([m + 1, r]);
|
|
304
309
|
stack.push([l, m - 1]);
|
|
305
310
|
}
|
|
@@ -319,33 +324,33 @@ export class BST extends BinaryTree {
|
|
|
319
324
|
* Space Complexity: O(k + log n)
|
|
320
325
|
*
|
|
321
326
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
322
|
-
* given
|
|
323
|
-
* @param {
|
|
327
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
328
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
324
329
|
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
325
|
-
* 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
|
|
326
331
|
* included in the result.
|
|
327
332
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
328
|
-
* determines whether to return only the first node that matches the
|
|
329
|
-
* 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
|
|
330
335
|
* and
|
|
331
|
-
* @param {
|
|
336
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
332
337
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
333
|
-
* nodes that match a given
|
|
338
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
334
339
|
* should begin. If not explicitly provided, the default value for `begin
|
|
335
340
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
336
341
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
337
342
|
* have two possible values:
|
|
338
|
-
* @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.
|
|
339
344
|
*/
|
|
340
|
-
getNodes(
|
|
341
|
-
if (
|
|
345
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
346
|
+
if (keyNodeEntryRawOrPredicate === undefined)
|
|
342
347
|
return [];
|
|
343
|
-
if (
|
|
348
|
+
if (keyNodeEntryRawOrPredicate === null)
|
|
344
349
|
return [];
|
|
345
|
-
|
|
346
|
-
if (!
|
|
350
|
+
startNode = this.ensureNode(startNode);
|
|
351
|
+
if (!startNode)
|
|
347
352
|
return [];
|
|
348
|
-
const callback = this._ensurePredicate(
|
|
353
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
349
354
|
const ans = [];
|
|
350
355
|
if (iterationType === 'RECURSIVE') {
|
|
351
356
|
const dfs = (cur) => {
|
|
@@ -356,10 +361,17 @@ export class BST extends BinaryTree {
|
|
|
356
361
|
}
|
|
357
362
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
358
363
|
return;
|
|
359
|
-
if (this.
|
|
360
|
-
|
|
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)
|
|
361
370
|
dfs(cur.left);
|
|
362
|
-
if (this.isRealNode(cur.right) &&
|
|
371
|
+
if (this.isRealNode(cur.right) &&
|
|
372
|
+
benchmarkKey !== null &&
|
|
373
|
+
benchmarkKey !== undefined &&
|
|
374
|
+
this.comparator(cur.key, benchmarkKey) < 0)
|
|
363
375
|
dfs(cur.right);
|
|
364
376
|
}
|
|
365
377
|
else {
|
|
@@ -369,10 +381,10 @@ export class BST extends BinaryTree {
|
|
|
369
381
|
dfs(cur.right);
|
|
370
382
|
}
|
|
371
383
|
};
|
|
372
|
-
dfs(
|
|
384
|
+
dfs(startNode);
|
|
373
385
|
}
|
|
374
386
|
else {
|
|
375
|
-
const stack = [
|
|
387
|
+
const stack = [startNode];
|
|
376
388
|
while (stack.length > 0) {
|
|
377
389
|
const cur = stack.pop();
|
|
378
390
|
if (callback(cur)) {
|
|
@@ -380,10 +392,17 @@ export class BST extends BinaryTree {
|
|
|
380
392
|
if (onlyOne)
|
|
381
393
|
return ans;
|
|
382
394
|
}
|
|
383
|
-
if (this.
|
|
384
|
-
|
|
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)
|
|
385
401
|
stack.push(cur.right);
|
|
386
|
-
if (this.isRealNode(cur.left) &&
|
|
402
|
+
if (this.isRealNode(cur.left) &&
|
|
403
|
+
benchmarkKey !== null &&
|
|
404
|
+
benchmarkKey !== undefined &&
|
|
405
|
+
this.comparator(cur.key, benchmarkKey) > 0)
|
|
387
406
|
stack.push(cur.left);
|
|
388
407
|
}
|
|
389
408
|
else {
|
|
@@ -400,10 +419,10 @@ export class BST extends BinaryTree {
|
|
|
400
419
|
* Time Complexity: O(log n)
|
|
401
420
|
* Space Complexity: O(1)
|
|
402
421
|
*
|
|
403
|
-
* This function retrieves a node based on a given
|
|
404
|
-
* @param {
|
|
405
|
-
* parameter can be of type `
|
|
406
|
-
* @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
|
|
407
426
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
408
427
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
409
428
|
* node of the binary search tree.
|
|
@@ -411,13 +430,13 @@ export class BST extends BinaryTree {
|
|
|
411
430
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
412
431
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
413
432
|
* no value is provided when calling the method.
|
|
414
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`
|
|
415
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
416
|
-
* 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
|
|
417
436
|
* returns the first node found or `undefined` if no node is found.
|
|
418
437
|
*/
|
|
419
|
-
getNode(
|
|
420
|
-
return this.getNodes(
|
|
438
|
+
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
439
|
+
return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined;
|
|
421
440
|
}
|
|
422
441
|
/**
|
|
423
442
|
* Time Complexity: O(log n)
|
|
@@ -443,11 +462,11 @@ export class BST extends BinaryTree {
|
|
|
443
462
|
* the callback function.
|
|
444
463
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
445
464
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
446
|
-
* `this.
|
|
465
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
447
466
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
448
467
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
449
468
|
* take one of the following values:
|
|
450
|
-
* @param {
|
|
469
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
451
470
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
452
471
|
* node entry. If not specified, the default value is the root of the tree.
|
|
453
472
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -455,8 +474,8 @@ export class BST extends BinaryTree {
|
|
|
455
474
|
* following values:
|
|
456
475
|
* @returns The method is returning an array of the return type of the callback function.
|
|
457
476
|
*/
|
|
458
|
-
dfs(callback = this.
|
|
459
|
-
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);
|
|
460
479
|
}
|
|
461
480
|
/**
|
|
462
481
|
* Time complexity: O(n)
|
|
@@ -467,7 +486,7 @@ export class BST extends BinaryTree {
|
|
|
467
486
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
468
487
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
469
488
|
* node being visited, and it can return a value of any type.
|
|
470
|
-
* @param {
|
|
489
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
471
490
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
472
491
|
* object. If no value is provided, the default value is the root of the tree.
|
|
473
492
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -475,8 +494,8 @@ export class BST extends BinaryTree {
|
|
|
475
494
|
* the following values:
|
|
476
495
|
* @returns an array of the return type of the callback function.
|
|
477
496
|
*/
|
|
478
|
-
bfs(callback = this.
|
|
479
|
-
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);
|
|
480
499
|
}
|
|
481
500
|
/**
|
|
482
501
|
* Time complexity: O(n)
|
|
@@ -485,9 +504,9 @@ export class BST extends BinaryTree {
|
|
|
485
504
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
486
505
|
* containing the results of the callback function applied to each level of the tree.
|
|
487
506
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
488
|
-
* `
|
|
507
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
489
508
|
* tree during the iteration process.
|
|
490
|
-
* @param {
|
|
509
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
491
510
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
492
511
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
493
512
|
* value is provided, the root of
|
|
@@ -496,8 +515,8 @@ export class BST extends BinaryTree {
|
|
|
496
515
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
497
516
|
* function.
|
|
498
517
|
*/
|
|
499
|
-
listLevels(callback = this.
|
|
500
|
-
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);
|
|
501
520
|
}
|
|
502
521
|
/**
|
|
503
522
|
* Time complexity: O(n)
|
|
@@ -511,7 +530,7 @@ export class BST extends BinaryTree {
|
|
|
511
530
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
512
531
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
513
532
|
* 0, or 1, where:
|
|
514
|
-
* @param {
|
|
533
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
515
534
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
516
535
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
517
536
|
* `targetNode` is provided,
|
|
@@ -520,7 +539,7 @@ export class BST extends BinaryTree {
|
|
|
520
539
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
521
540
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
522
541
|
*/
|
|
523
|
-
lesserOrGreaterTraverse(callback = this.
|
|
542
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_NODE_CALLBACK, lesserOrGreater = -1, targetNode = this._root, iterationType = this.iterationType) {
|
|
524
543
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
525
544
|
const ans = [];
|
|
526
545
|
if (!this._root)
|
|
@@ -572,7 +591,7 @@ export class BST extends BinaryTree {
|
|
|
572
591
|
*/
|
|
573
592
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
574
593
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
575
|
-
this.
|
|
594
|
+
this._clearNodes();
|
|
576
595
|
if (sorted.length < 1)
|
|
577
596
|
return false;
|
|
578
597
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -581,7 +600,10 @@ export class BST extends BinaryTree {
|
|
|
581
600
|
return;
|
|
582
601
|
const m = l + Math.floor((r - l) / 2);
|
|
583
602
|
const midNode = sorted[m];
|
|
584
|
-
this.
|
|
603
|
+
if (this._isMapMode)
|
|
604
|
+
this.add(midNode.key);
|
|
605
|
+
else
|
|
606
|
+
this.add([midNode.key, midNode.value]);
|
|
585
607
|
buildBalanceBST(l, m - 1);
|
|
586
608
|
buildBalanceBST(m + 1, r);
|
|
587
609
|
};
|
|
@@ -597,7 +619,10 @@ export class BST extends BinaryTree {
|
|
|
597
619
|
if (l <= r) {
|
|
598
620
|
const m = l + Math.floor((r - l) / 2);
|
|
599
621
|
const midNode = sorted[m];
|
|
600
|
-
this.
|
|
622
|
+
if (this._isMapMode)
|
|
623
|
+
this.add(midNode.key);
|
|
624
|
+
else
|
|
625
|
+
this.add([midNode.key, midNode.value]);
|
|
601
626
|
stack.push([m + 1, r]);
|
|
602
627
|
stack.push([l, m - 1]);
|
|
603
628
|
}
|
|
@@ -684,7 +709,7 @@ export class BST extends BinaryTree {
|
|
|
684
709
|
/**
|
|
685
710
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
686
711
|
* root.
|
|
687
|
-
* @param {
|
|
712
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
688
713
|
*/
|
|
689
714
|
_setRoot(v) {
|
|
690
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)
|