data-structure-typed 1.51.0 → 1.51.2
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 +37 -1
- package/benchmark/report.json +390 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
- 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 +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +17 -11
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -39
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +8 -63
- 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 +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +17 -11
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +67 -39
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +8 -62
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/umd/data-structure-typed.js +178 -198
- package/dist/umd/data-structure-typed.min.js +2 -2
- 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 +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/bst.ts +69 -34
- package/src/data-structures/binary-tree/rb-tree.ts +8 -74
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +40 -40
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +1 -0
- package/test/utils/big-o.ts +12 -0
|
@@ -143,6 +143,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
143
143
|
get size() {
|
|
144
144
|
return this._size;
|
|
145
145
|
}
|
|
146
|
+
_NIL = new BinaryTreeNode(NaN);
|
|
147
|
+
/**
|
|
148
|
+
* The function returns the value of the _NIL property.
|
|
149
|
+
* @returns The method is returning the value of the `_NIL` property.
|
|
150
|
+
*/
|
|
151
|
+
get NIL() {
|
|
152
|
+
return this._NIL;
|
|
153
|
+
}
|
|
146
154
|
/**
|
|
147
155
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
148
156
|
* @param {K} key - The key for the new node.
|
|
@@ -219,23 +227,31 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
219
227
|
* itself if it is not a valid node key.
|
|
220
228
|
*/
|
|
221
229
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
222
|
-
let res;
|
|
223
230
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
224
|
-
|
|
231
|
+
return keyOrNodeOrEntry;
|
|
225
232
|
}
|
|
226
233
|
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
227
234
|
if (keyOrNodeOrEntry[0] === null)
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
235
|
+
return null;
|
|
236
|
+
if (keyOrNodeOrEntry[0] === undefined)
|
|
237
|
+
return;
|
|
238
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
231
239
|
}
|
|
232
240
|
else {
|
|
233
241
|
if (keyOrNodeOrEntry === null)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
242
|
+
return null;
|
|
243
|
+
if (keyOrNodeOrEntry === undefined)
|
|
244
|
+
return;
|
|
245
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
237
246
|
}
|
|
238
|
-
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* The function checks if a given node is a real node or null.
|
|
250
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
251
|
+
* @returns a boolean value.
|
|
252
|
+
*/
|
|
253
|
+
isNodeOrNull(node) {
|
|
254
|
+
return this.isRealNode(node) || node === null;
|
|
239
255
|
}
|
|
240
256
|
/**
|
|
241
257
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
@@ -245,15 +261,6 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
245
261
|
isNode(keyOrNodeOrEntry) {
|
|
246
262
|
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
247
263
|
}
|
|
248
|
-
/**
|
|
249
|
-
* The function checks if a given value is an entry in a binary tree node.
|
|
250
|
-
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
251
|
-
* two type parameters V and NODE, representing the value and node type respectively.
|
|
252
|
-
* @returns a boolean value.
|
|
253
|
-
*/
|
|
254
|
-
isEntry(keyOrNodeOrEntry) {
|
|
255
|
-
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
256
|
-
}
|
|
257
264
|
/**
|
|
258
265
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
259
266
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -261,7 +268,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
261
268
|
* @returns a boolean value.
|
|
262
269
|
*/
|
|
263
270
|
isRealNode(node) {
|
|
264
|
-
|
|
271
|
+
if (!this.isNode(node))
|
|
272
|
+
return false;
|
|
273
|
+
return node !== this.NIL;
|
|
265
274
|
}
|
|
266
275
|
/**
|
|
267
276
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
@@ -269,15 +278,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
269
278
|
* @returns a boolean value.
|
|
270
279
|
*/
|
|
271
280
|
isNIL(node) {
|
|
272
|
-
return node
|
|
281
|
+
return node === this.NIL;
|
|
273
282
|
}
|
|
274
283
|
/**
|
|
275
|
-
* The function checks if a given
|
|
276
|
-
* @param
|
|
284
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
285
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
286
|
+
* two type parameters V and NODE, representing the value and node type respectively.
|
|
277
287
|
* @returns a boolean value.
|
|
278
288
|
*/
|
|
279
|
-
|
|
280
|
-
return
|
|
289
|
+
isEntry(keyOrNodeOrEntry) {
|
|
290
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
281
291
|
}
|
|
282
292
|
/**
|
|
283
293
|
* Time Complexity O(n)
|
|
@@ -411,14 +421,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
411
421
|
* specific node based on its value or object.
|
|
412
422
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
413
423
|
* identifier of the node to be deleted. It is optional and has a default value of
|
|
414
|
-
* `this.
|
|
424
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
|
|
415
425
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
416
426
|
*/
|
|
417
|
-
delete(identifier, callback = this.
|
|
427
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
418
428
|
const deletedResult = [];
|
|
419
429
|
if (!this.root)
|
|
420
430
|
return deletedResult;
|
|
421
|
-
if ((!callback || callback === this.
|
|
431
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
422
432
|
callback = (node => node);
|
|
423
433
|
const curr = this.getNode(identifier, callback);
|
|
424
434
|
if (!curr)
|
|
@@ -477,7 +487,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
477
487
|
* specific value.
|
|
478
488
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
479
489
|
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
480
|
-
* identifier. If no callback is provided, the `
|
|
490
|
+
* identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
|
|
481
491
|
* default
|
|
482
492
|
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
483
493
|
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
@@ -490,39 +500,39 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
490
500
|
* traverse the binary tree. It can have two possible values:
|
|
491
501
|
* @returns an array of nodes of type `NODE`.
|
|
492
502
|
*/
|
|
493
|
-
getNodes(identifier, callback = this.
|
|
494
|
-
if ((!callback || callback === this.
|
|
503
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
504
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
495
505
|
callback = (node => node);
|
|
496
506
|
beginRoot = this.ensureNode(beginRoot);
|
|
497
507
|
if (!beginRoot)
|
|
498
508
|
return [];
|
|
499
509
|
const ans = [];
|
|
500
510
|
if (iterationType === 'RECURSIVE') {
|
|
501
|
-
const
|
|
511
|
+
const dfs = (cur) => {
|
|
502
512
|
if (callback(cur) === identifier) {
|
|
503
513
|
ans.push(cur);
|
|
504
514
|
if (onlyOne)
|
|
505
515
|
return;
|
|
506
516
|
}
|
|
507
|
-
if (!cur.left && !cur.right)
|
|
517
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
508
518
|
return;
|
|
509
|
-
cur.left &&
|
|
510
|
-
cur.right &&
|
|
519
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
520
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
511
521
|
};
|
|
512
|
-
|
|
522
|
+
dfs(beginRoot);
|
|
513
523
|
}
|
|
514
524
|
else {
|
|
515
|
-
const
|
|
516
|
-
while (
|
|
517
|
-
const cur =
|
|
518
|
-
if (cur) {
|
|
525
|
+
const stack = [beginRoot];
|
|
526
|
+
while (stack.length > 0) {
|
|
527
|
+
const cur = stack.pop();
|
|
528
|
+
if (this.isRealNode(cur)) {
|
|
519
529
|
if (callback(cur) === identifier) {
|
|
520
530
|
ans.push(cur);
|
|
521
531
|
if (onlyOne)
|
|
522
532
|
return ans;
|
|
523
533
|
}
|
|
524
|
-
cur.left &&
|
|
525
|
-
cur.right &&
|
|
534
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
535
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
526
536
|
}
|
|
527
537
|
}
|
|
528
538
|
}
|
|
@@ -553,9 +563,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
553
563
|
* nodes are visited during the search.
|
|
554
564
|
* @returns a value of type `NODE | null | undefined`.
|
|
555
565
|
*/
|
|
556
|
-
getNode(identifier, callback = this.
|
|
557
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
558
|
-
callback = (node => node);
|
|
566
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
559
567
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
560
568
|
}
|
|
561
569
|
/**
|
|
@@ -580,27 +588,27 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
580
588
|
if (!this.root)
|
|
581
589
|
return undefined;
|
|
582
590
|
if (iterationType === 'RECURSIVE') {
|
|
583
|
-
const
|
|
591
|
+
const dfs = (cur) => {
|
|
584
592
|
if (cur.key === key)
|
|
585
593
|
return cur;
|
|
586
594
|
if (!cur.left && !cur.right)
|
|
587
595
|
return;
|
|
588
596
|
if (cur.left)
|
|
589
|
-
return
|
|
597
|
+
return dfs(cur.left);
|
|
590
598
|
if (cur.right)
|
|
591
|
-
return
|
|
599
|
+
return dfs(cur.right);
|
|
592
600
|
};
|
|
593
|
-
return
|
|
601
|
+
return dfs(this.root);
|
|
594
602
|
}
|
|
595
603
|
else {
|
|
596
|
-
const
|
|
597
|
-
while (
|
|
598
|
-
const cur =
|
|
604
|
+
const stack = [this.root];
|
|
605
|
+
while (stack.length > 0) {
|
|
606
|
+
const cur = stack.pop();
|
|
599
607
|
if (cur) {
|
|
600
608
|
if (cur.key === key)
|
|
601
609
|
return cur;
|
|
602
|
-
cur.left &&
|
|
603
|
-
cur.right &&
|
|
610
|
+
cur.left && stack.push(cur.left);
|
|
611
|
+
cur.right && stack.push(cur.right);
|
|
604
612
|
}
|
|
605
613
|
}
|
|
606
614
|
}
|
|
@@ -631,9 +639,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
631
639
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
632
640
|
* found, `undefined` is returned.
|
|
633
641
|
*/
|
|
634
|
-
get(identifier, callback = this.
|
|
635
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
636
|
-
callback = (node => node);
|
|
642
|
+
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
637
643
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
638
644
|
}
|
|
639
645
|
/**
|
|
@@ -661,8 +667,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
661
667
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
662
668
|
* @returns a boolean value.
|
|
663
669
|
*/
|
|
664
|
-
has(identifier, callback = this.
|
|
665
|
-
if ((!callback || callback === this.
|
|
670
|
+
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
671
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
666
672
|
callback = (node => node);
|
|
667
673
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
668
674
|
}
|
|
@@ -966,21 +972,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
966
972
|
if (!this.isRealNode(beginRoot))
|
|
967
973
|
return beginRoot;
|
|
968
974
|
if (iterationType === 'RECURSIVE') {
|
|
969
|
-
const
|
|
975
|
+
const dfs = (cur) => {
|
|
970
976
|
if (!this.isRealNode(cur.left))
|
|
971
977
|
return cur;
|
|
972
|
-
return
|
|
978
|
+
return dfs(cur.left);
|
|
973
979
|
};
|
|
974
|
-
return
|
|
980
|
+
return dfs(beginRoot);
|
|
975
981
|
}
|
|
976
982
|
else {
|
|
977
983
|
// Indirect implementation of iteration using tail recursion optimization
|
|
978
|
-
const
|
|
984
|
+
const dfs = trampoline((cur) => {
|
|
979
985
|
if (!this.isRealNode(cur.left))
|
|
980
986
|
return cur;
|
|
981
|
-
return
|
|
987
|
+
return dfs.cont(cur.left);
|
|
982
988
|
});
|
|
983
|
-
return
|
|
989
|
+
return dfs(beginRoot);
|
|
984
990
|
}
|
|
985
991
|
}
|
|
986
992
|
/**
|
|
@@ -1010,21 +1016,21 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1010
1016
|
if (!beginRoot)
|
|
1011
1017
|
return beginRoot;
|
|
1012
1018
|
if (iterationType === 'RECURSIVE') {
|
|
1013
|
-
const
|
|
1019
|
+
const dfs = (cur) => {
|
|
1014
1020
|
if (!this.isRealNode(cur.right))
|
|
1015
1021
|
return cur;
|
|
1016
|
-
return
|
|
1022
|
+
return dfs(cur.right);
|
|
1017
1023
|
};
|
|
1018
|
-
return
|
|
1024
|
+
return dfs(beginRoot);
|
|
1019
1025
|
}
|
|
1020
1026
|
else {
|
|
1021
1027
|
// Indirect implementation of iteration using tail recursion optimization
|
|
1022
|
-
const
|
|
1028
|
+
const dfs = trampoline((cur) => {
|
|
1023
1029
|
if (!this.isRealNode(cur.right))
|
|
1024
1030
|
return cur;
|
|
1025
|
-
return
|
|
1031
|
+
return dfs.cont(cur.right);
|
|
1026
1032
|
});
|
|
1027
|
-
return
|
|
1033
|
+
return dfs(beginRoot);
|
|
1028
1034
|
}
|
|
1029
1035
|
}
|
|
1030
1036
|
/**
|
|
@@ -1109,65 +1115,65 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1109
1115
|
* `false`, null or undefined
|
|
1110
1116
|
* @returns an array of values that are the return values of the callback function.
|
|
1111
1117
|
*/
|
|
1112
|
-
dfs(callback = this.
|
|
1118
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1113
1119
|
beginRoot = this.ensureNode(beginRoot);
|
|
1114
1120
|
if (!beginRoot)
|
|
1115
1121
|
return [];
|
|
1116
1122
|
const ans = [];
|
|
1117
1123
|
if (iterationType === 'RECURSIVE') {
|
|
1118
|
-
const
|
|
1124
|
+
const dfs = (node) => {
|
|
1119
1125
|
switch (pattern) {
|
|
1120
1126
|
case 'IN':
|
|
1121
1127
|
if (includeNull) {
|
|
1122
1128
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1123
|
-
|
|
1129
|
+
dfs(node.left);
|
|
1124
1130
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1125
1131
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1126
|
-
|
|
1132
|
+
dfs(node.right);
|
|
1127
1133
|
}
|
|
1128
1134
|
else {
|
|
1129
1135
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1130
|
-
|
|
1136
|
+
dfs(node.left);
|
|
1131
1137
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1132
1138
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1133
|
-
|
|
1139
|
+
dfs(node.right);
|
|
1134
1140
|
}
|
|
1135
1141
|
break;
|
|
1136
1142
|
case 'PRE':
|
|
1137
1143
|
if (includeNull) {
|
|
1138
1144
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1139
1145
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1140
|
-
|
|
1146
|
+
dfs(node.left);
|
|
1141
1147
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1142
|
-
|
|
1148
|
+
dfs(node.right);
|
|
1143
1149
|
}
|
|
1144
1150
|
else {
|
|
1145
1151
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1146
1152
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1147
|
-
|
|
1153
|
+
dfs(node.left);
|
|
1148
1154
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1149
|
-
|
|
1155
|
+
dfs(node.right);
|
|
1150
1156
|
}
|
|
1151
1157
|
break;
|
|
1152
1158
|
case 'POST':
|
|
1153
1159
|
if (includeNull) {
|
|
1154
1160
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1155
|
-
|
|
1161
|
+
dfs(node.left);
|
|
1156
1162
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1157
|
-
|
|
1163
|
+
dfs(node.right);
|
|
1158
1164
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1159
1165
|
}
|
|
1160
1166
|
else {
|
|
1161
1167
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1162
|
-
|
|
1168
|
+
dfs(node.left);
|
|
1163
1169
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1164
|
-
|
|
1170
|
+
dfs(node.right);
|
|
1165
1171
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1166
1172
|
}
|
|
1167
1173
|
break;
|
|
1168
1174
|
}
|
|
1169
1175
|
};
|
|
1170
|
-
|
|
1176
|
+
dfs(beginRoot);
|
|
1171
1177
|
}
|
|
1172
1178
|
else {
|
|
1173
1179
|
// 0: visit, 1: print
|
|
@@ -1240,14 +1246,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1240
1246
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1241
1247
|
* the breadth-first traversal of a binary tree.
|
|
1242
1248
|
*/
|
|
1243
|
-
bfs(callback = this.
|
|
1249
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1244
1250
|
beginRoot = this.ensureNode(beginRoot);
|
|
1245
1251
|
if (!beginRoot)
|
|
1246
1252
|
return [];
|
|
1247
1253
|
const ans = [];
|
|
1248
1254
|
if (iterationType === 'RECURSIVE') {
|
|
1249
1255
|
const queue = new Queue([beginRoot]);
|
|
1250
|
-
const
|
|
1256
|
+
const dfs = (level) => {
|
|
1251
1257
|
if (queue.size === 0)
|
|
1252
1258
|
return;
|
|
1253
1259
|
const current = queue.shift();
|
|
@@ -1264,9 +1270,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1264
1270
|
if (this.isRealNode(current.right))
|
|
1265
1271
|
queue.push(current.right);
|
|
1266
1272
|
}
|
|
1267
|
-
|
|
1273
|
+
dfs(level + 1);
|
|
1268
1274
|
};
|
|
1269
|
-
|
|
1275
|
+
dfs(0);
|
|
1270
1276
|
}
|
|
1271
1277
|
else {
|
|
1272
1278
|
const queue = new Queue([beginRoot]);
|
|
@@ -1317,7 +1323,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1317
1323
|
* be excluded
|
|
1318
1324
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1319
1325
|
*/
|
|
1320
|
-
listLevels(callback = this.
|
|
1326
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1321
1327
|
beginRoot = this.ensureNode(beginRoot);
|
|
1322
1328
|
const levelsNodes = [];
|
|
1323
1329
|
if (!beginRoot)
|
|
@@ -1389,7 +1395,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1389
1395
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1390
1396
|
* by the return type of the `callback` function.
|
|
1391
1397
|
*/
|
|
1392
|
-
morris(callback = this.
|
|
1398
|
+
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1393
1399
|
beginRoot = this.ensureNode(beginRoot);
|
|
1394
1400
|
if (beginRoot === null)
|
|
1395
1401
|
return [];
|
|
@@ -1700,7 +1706,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1700
1706
|
];
|
|
1701
1707
|
}
|
|
1702
1708
|
}
|
|
1703
|
-
|
|
1709
|
+
_DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
1704
1710
|
/**
|
|
1705
1711
|
* Swap the data of two nodes in the binary tree.
|
|
1706
1712
|
* @param {NODE} srcNode - The source node to swap.
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
9
|
+
import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
|
|
@@ -209,6 +209,32 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
209
209
|
* @returns The method returns an array of nodes (`NODE[]`).
|
|
210
210
|
*/
|
|
211
211
|
getNodes<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE[];
|
|
212
|
+
/**
|
|
213
|
+
* Time Complexity: O(log n)
|
|
214
|
+
* Space Complexity: O(1)
|
|
215
|
+
*/
|
|
216
|
+
/**
|
|
217
|
+
* Time Complexity: O(log n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
219
|
+
*
|
|
220
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
221
|
+
* callback function.
|
|
222
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
223
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
224
|
+
* with the type of nodes in the tree.
|
|
225
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
226
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
227
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
228
|
+
* `identifier` parameter.
|
|
229
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
230
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
231
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
232
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
233
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
234
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
235
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
236
|
+
*/
|
|
237
|
+
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | undefined;
|
|
212
238
|
/**
|
|
213
239
|
* Time complexity: O(n)
|
|
214
240
|
* Space complexity: O(n)
|