linked-list-typed 1.50.9 → 1.51.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/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-tree.js +73 -73
- package/dist/data-structures/binary-tree/bst.js +39 -39
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/rb-tree.js +3 -3
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/package.json +2 -2
- 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 +71 -71
- package/src/data-structures/binary-tree/bst.ts +33 -33
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
|
@@ -194,7 +194,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
194
194
|
* decremented by 1 and
|
|
195
195
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
196
196
|
*/
|
|
197
|
-
delete(identifier, callback = this.
|
|
197
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
198
198
|
var _a;
|
|
199
199
|
const deletedResult = [];
|
|
200
200
|
if (!this.root)
|
|
@@ -108,7 +108,7 @@ export declare class AVLTree<K = any, V = any, NODE extends AVLTreeNode<K, V, NO
|
|
|
108
108
|
* `callback` function.
|
|
109
109
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
110
110
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
111
|
-
* default to the `
|
|
111
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
112
112
|
* parameter of type `NODE
|
|
113
113
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
114
114
|
*/
|
|
@@ -133,11 +133,11 @@ class AVLTree extends bst_1.BST {
|
|
|
133
133
|
* `callback` function.
|
|
134
134
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
135
135
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
136
|
-
* default to the `
|
|
136
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
137
137
|
* parameter of type `NODE
|
|
138
138
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
139
139
|
*/
|
|
140
|
-
delete(identifier, callback = this.
|
|
140
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
141
141
|
if (identifier instanceof AVLTreeNode)
|
|
142
142
|
callback = (node => node);
|
|
143
143
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -576,7 +576,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
576
576
|
* 4. `middleIndex`: The index of the middle character
|
|
577
577
|
*/
|
|
578
578
|
protected _displayAux(node: NODE | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
579
|
-
protected
|
|
579
|
+
protected _DEFAULT_CALLBACK: (node: NODE | null | undefined) => K | undefined;
|
|
580
580
|
/**
|
|
581
581
|
* Swap the data of two nodes in the binary tree.
|
|
582
582
|
* @param {NODE} srcNode - The source node to swap.
|
|
@@ -107,7 +107,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
107
107
|
super();
|
|
108
108
|
this.iterationType = 'ITERATIVE';
|
|
109
109
|
this._extractor = (key) => (typeof key === 'number' ? key : Number(key));
|
|
110
|
-
this.
|
|
110
|
+
this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
111
111
|
if (options) {
|
|
112
112
|
const { iterationType, extractor } = options;
|
|
113
113
|
if (iterationType)
|
|
@@ -217,23 +217,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
217
217
|
* itself if it is not a valid node key.
|
|
218
218
|
*/
|
|
219
219
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
220
|
-
let res;
|
|
221
220
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
222
|
-
|
|
221
|
+
return keyOrNodeOrEntry;
|
|
223
222
|
}
|
|
224
223
|
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
225
224
|
if (keyOrNodeOrEntry[0] === null)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
225
|
+
return null;
|
|
226
|
+
if (keyOrNodeOrEntry[0] === undefined)
|
|
227
|
+
return;
|
|
228
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
229
229
|
}
|
|
230
230
|
else {
|
|
231
231
|
if (keyOrNodeOrEntry === null)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
232
|
+
return null;
|
|
233
|
+
if (keyOrNodeOrEntry === undefined)
|
|
234
|
+
return;
|
|
235
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
235
236
|
}
|
|
236
|
-
return res;
|
|
237
237
|
}
|
|
238
238
|
/**
|
|
239
239
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
@@ -409,14 +409,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
409
409
|
* specific node based on its value or object.
|
|
410
410
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
411
411
|
* identifier of the node to be deleted. It is optional and has a default value of
|
|
412
|
-
* `this.
|
|
412
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
|
|
413
413
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
414
414
|
*/
|
|
415
|
-
delete(identifier, callback = this.
|
|
415
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
416
416
|
const deletedResult = [];
|
|
417
417
|
if (!this.root)
|
|
418
418
|
return deletedResult;
|
|
419
|
-
if ((!callback || callback === this.
|
|
419
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
420
420
|
callback = (node => node);
|
|
421
421
|
const curr = this.getNode(identifier, callback);
|
|
422
422
|
if (!curr)
|
|
@@ -475,7 +475,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
475
475
|
* specific value.
|
|
476
476
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
477
477
|
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
478
|
-
* identifier. If no callback is provided, the `
|
|
478
|
+
* identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
|
|
479
479
|
* default
|
|
480
480
|
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
481
481
|
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
@@ -488,15 +488,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
488
488
|
* traverse the binary tree. It can have two possible values:
|
|
489
489
|
* @returns an array of nodes of type `NODE`.
|
|
490
490
|
*/
|
|
491
|
-
getNodes(identifier, callback = this.
|
|
492
|
-
if ((!callback || callback === this.
|
|
491
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
492
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
493
493
|
callback = (node => node);
|
|
494
494
|
beginRoot = this.ensureNode(beginRoot);
|
|
495
495
|
if (!beginRoot)
|
|
496
496
|
return [];
|
|
497
497
|
const ans = [];
|
|
498
498
|
if (iterationType === 'RECURSIVE') {
|
|
499
|
-
const
|
|
499
|
+
const dfs = (cur) => {
|
|
500
500
|
if (callback(cur) === identifier) {
|
|
501
501
|
ans.push(cur);
|
|
502
502
|
if (onlyOne)
|
|
@@ -504,23 +504,23 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
504
504
|
}
|
|
505
505
|
if (!cur.left && !cur.right)
|
|
506
506
|
return;
|
|
507
|
-
cur.left &&
|
|
508
|
-
cur.right &&
|
|
507
|
+
cur.left && dfs(cur.left);
|
|
508
|
+
cur.right && dfs(cur.right);
|
|
509
509
|
};
|
|
510
|
-
|
|
510
|
+
dfs(beginRoot);
|
|
511
511
|
}
|
|
512
512
|
else {
|
|
513
|
-
const
|
|
514
|
-
while (
|
|
515
|
-
const cur =
|
|
513
|
+
const stack = [beginRoot];
|
|
514
|
+
while (stack.length > 0) {
|
|
515
|
+
const cur = stack.pop();
|
|
516
516
|
if (cur) {
|
|
517
517
|
if (callback(cur) === identifier) {
|
|
518
518
|
ans.push(cur);
|
|
519
519
|
if (onlyOne)
|
|
520
520
|
return ans;
|
|
521
521
|
}
|
|
522
|
-
cur.left &&
|
|
523
|
-
cur.right &&
|
|
522
|
+
cur.left && stack.push(cur.left);
|
|
523
|
+
cur.right && stack.push(cur.right);
|
|
524
524
|
}
|
|
525
525
|
}
|
|
526
526
|
}
|
|
@@ -551,9 +551,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
551
551
|
* nodes are visited during the search.
|
|
552
552
|
* @returns a value of type `NODE | null | undefined`.
|
|
553
553
|
*/
|
|
554
|
-
getNode(identifier, callback = this.
|
|
554
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
555
555
|
var _a;
|
|
556
|
-
if ((!callback || callback === this.
|
|
556
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
557
557
|
callback = (node => node);
|
|
558
558
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
559
559
|
}
|
|
@@ -579,27 +579,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
579
579
|
if (!this.root)
|
|
580
580
|
return undefined;
|
|
581
581
|
if (iterationType === 'RECURSIVE') {
|
|
582
|
-
const
|
|
582
|
+
const dfs = (cur) => {
|
|
583
583
|
if (cur.key === key)
|
|
584
584
|
return cur;
|
|
585
585
|
if (!cur.left && !cur.right)
|
|
586
586
|
return;
|
|
587
587
|
if (cur.left)
|
|
588
|
-
return
|
|
588
|
+
return dfs(cur.left);
|
|
589
589
|
if (cur.right)
|
|
590
|
-
return
|
|
590
|
+
return dfs(cur.right);
|
|
591
591
|
};
|
|
592
|
-
return
|
|
592
|
+
return dfs(this.root);
|
|
593
593
|
}
|
|
594
594
|
else {
|
|
595
|
-
const
|
|
596
|
-
while (
|
|
597
|
-
const cur =
|
|
595
|
+
const stack = [this.root];
|
|
596
|
+
while (stack.length > 0) {
|
|
597
|
+
const cur = stack.pop();
|
|
598
598
|
if (cur) {
|
|
599
599
|
if (cur.key === key)
|
|
600
600
|
return cur;
|
|
601
|
-
cur.left &&
|
|
602
|
-
cur.right &&
|
|
601
|
+
cur.left && stack.push(cur.left);
|
|
602
|
+
cur.right && stack.push(cur.right);
|
|
603
603
|
}
|
|
604
604
|
}
|
|
605
605
|
}
|
|
@@ -630,9 +630,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
630
630
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
631
631
|
* found, `undefined` is returned.
|
|
632
632
|
*/
|
|
633
|
-
get(identifier, callback = this.
|
|
633
|
+
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
634
634
|
var _a, _b;
|
|
635
|
-
if ((!callback || callback === this.
|
|
635
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
636
636
|
callback = (node => node);
|
|
637
637
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
638
638
|
}
|
|
@@ -661,8 +661,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
661
661
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
662
662
|
* @returns a boolean value.
|
|
663
663
|
*/
|
|
664
|
-
has(identifier, callback = this.
|
|
665
|
-
if ((!callback || callback === this.
|
|
664
|
+
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
665
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
666
666
|
callback = (node => node);
|
|
667
667
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
668
668
|
}
|
|
@@ -967,21 +967,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
967
967
|
if (!this.isRealNode(beginRoot))
|
|
968
968
|
return beginRoot;
|
|
969
969
|
if (iterationType === 'RECURSIVE') {
|
|
970
|
-
const
|
|
970
|
+
const dfs = (cur) => {
|
|
971
971
|
if (!this.isRealNode(cur.left))
|
|
972
972
|
return cur;
|
|
973
|
-
return
|
|
973
|
+
return dfs(cur.left);
|
|
974
974
|
};
|
|
975
|
-
return
|
|
975
|
+
return dfs(beginRoot);
|
|
976
976
|
}
|
|
977
977
|
else {
|
|
978
978
|
// Indirect implementation of iteration using tail recursion optimization
|
|
979
|
-
const
|
|
979
|
+
const dfs = (0, utils_1.trampoline)((cur) => {
|
|
980
980
|
if (!this.isRealNode(cur.left))
|
|
981
981
|
return cur;
|
|
982
|
-
return
|
|
982
|
+
return dfs.cont(cur.left);
|
|
983
983
|
});
|
|
984
|
-
return
|
|
984
|
+
return dfs(beginRoot);
|
|
985
985
|
}
|
|
986
986
|
}
|
|
987
987
|
/**
|
|
@@ -1011,21 +1011,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1011
1011
|
if (!beginRoot)
|
|
1012
1012
|
return beginRoot;
|
|
1013
1013
|
if (iterationType === 'RECURSIVE') {
|
|
1014
|
-
const
|
|
1014
|
+
const dfs = (cur) => {
|
|
1015
1015
|
if (!this.isRealNode(cur.right))
|
|
1016
1016
|
return cur;
|
|
1017
|
-
return
|
|
1017
|
+
return dfs(cur.right);
|
|
1018
1018
|
};
|
|
1019
|
-
return
|
|
1019
|
+
return dfs(beginRoot);
|
|
1020
1020
|
}
|
|
1021
1021
|
else {
|
|
1022
1022
|
// Indirect implementation of iteration using tail recursion optimization
|
|
1023
|
-
const
|
|
1023
|
+
const dfs = (0, utils_1.trampoline)((cur) => {
|
|
1024
1024
|
if (!this.isRealNode(cur.right))
|
|
1025
1025
|
return cur;
|
|
1026
|
-
return
|
|
1026
|
+
return dfs.cont(cur.right);
|
|
1027
1027
|
});
|
|
1028
|
-
return
|
|
1028
|
+
return dfs(beginRoot);
|
|
1029
1029
|
}
|
|
1030
1030
|
}
|
|
1031
1031
|
/**
|
|
@@ -1110,65 +1110,65 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1110
1110
|
* `false`, null or undefined
|
|
1111
1111
|
* @returns an array of values that are the return values of the callback function.
|
|
1112
1112
|
*/
|
|
1113
|
-
dfs(callback = this.
|
|
1113
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1114
1114
|
beginRoot = this.ensureNode(beginRoot);
|
|
1115
1115
|
if (!beginRoot)
|
|
1116
1116
|
return [];
|
|
1117
1117
|
const ans = [];
|
|
1118
1118
|
if (iterationType === 'RECURSIVE') {
|
|
1119
|
-
const
|
|
1119
|
+
const dfs = (node) => {
|
|
1120
1120
|
switch (pattern) {
|
|
1121
1121
|
case 'IN':
|
|
1122
1122
|
if (includeNull) {
|
|
1123
1123
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1124
|
-
|
|
1124
|
+
dfs(node.left);
|
|
1125
1125
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1126
1126
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1127
|
-
|
|
1127
|
+
dfs(node.right);
|
|
1128
1128
|
}
|
|
1129
1129
|
else {
|
|
1130
1130
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1131
|
-
|
|
1131
|
+
dfs(node.left);
|
|
1132
1132
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1133
1133
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1134
|
-
|
|
1134
|
+
dfs(node.right);
|
|
1135
1135
|
}
|
|
1136
1136
|
break;
|
|
1137
1137
|
case 'PRE':
|
|
1138
1138
|
if (includeNull) {
|
|
1139
1139
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1140
1140
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1141
|
-
|
|
1141
|
+
dfs(node.left);
|
|
1142
1142
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1143
|
-
|
|
1143
|
+
dfs(node.right);
|
|
1144
1144
|
}
|
|
1145
1145
|
else {
|
|
1146
1146
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1147
1147
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1148
|
-
|
|
1148
|
+
dfs(node.left);
|
|
1149
1149
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1150
|
-
|
|
1150
|
+
dfs(node.right);
|
|
1151
1151
|
}
|
|
1152
1152
|
break;
|
|
1153
1153
|
case 'POST':
|
|
1154
1154
|
if (includeNull) {
|
|
1155
1155
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1156
|
-
|
|
1156
|
+
dfs(node.left);
|
|
1157
1157
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1158
|
-
|
|
1158
|
+
dfs(node.right);
|
|
1159
1159
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1160
1160
|
}
|
|
1161
1161
|
else {
|
|
1162
1162
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1163
|
-
|
|
1163
|
+
dfs(node.left);
|
|
1164
1164
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1165
|
-
|
|
1165
|
+
dfs(node.right);
|
|
1166
1166
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1167
1167
|
}
|
|
1168
1168
|
break;
|
|
1169
1169
|
}
|
|
1170
1170
|
};
|
|
1171
|
-
|
|
1171
|
+
dfs(beginRoot);
|
|
1172
1172
|
}
|
|
1173
1173
|
else {
|
|
1174
1174
|
// 0: visit, 1: print
|
|
@@ -1241,14 +1241,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1241
1241
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1242
1242
|
* the breadth-first traversal of a binary tree.
|
|
1243
1243
|
*/
|
|
1244
|
-
bfs(callback = this.
|
|
1244
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1245
1245
|
beginRoot = this.ensureNode(beginRoot);
|
|
1246
1246
|
if (!beginRoot)
|
|
1247
1247
|
return [];
|
|
1248
1248
|
const ans = [];
|
|
1249
1249
|
if (iterationType === 'RECURSIVE') {
|
|
1250
1250
|
const queue = new queue_1.Queue([beginRoot]);
|
|
1251
|
-
const
|
|
1251
|
+
const dfs = (level) => {
|
|
1252
1252
|
if (queue.size === 0)
|
|
1253
1253
|
return;
|
|
1254
1254
|
const current = queue.shift();
|
|
@@ -1265,9 +1265,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1265
1265
|
if (this.isRealNode(current.right))
|
|
1266
1266
|
queue.push(current.right);
|
|
1267
1267
|
}
|
|
1268
|
-
|
|
1268
|
+
dfs(level + 1);
|
|
1269
1269
|
};
|
|
1270
|
-
|
|
1270
|
+
dfs(0);
|
|
1271
1271
|
}
|
|
1272
1272
|
else {
|
|
1273
1273
|
const queue = new queue_1.Queue([beginRoot]);
|
|
@@ -1318,7 +1318,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1318
1318
|
* be excluded
|
|
1319
1319
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1320
1320
|
*/
|
|
1321
|
-
listLevels(callback = this.
|
|
1321
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1322
1322
|
beginRoot = this.ensureNode(beginRoot);
|
|
1323
1323
|
const levelsNodes = [];
|
|
1324
1324
|
if (!beginRoot)
|
|
@@ -1390,7 +1390,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1390
1390
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1391
1391
|
* by the return type of the `callback` function.
|
|
1392
1392
|
*/
|
|
1393
|
-
morris(callback = this.
|
|
1393
|
+
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1394
1394
|
beginRoot = this.ensureNode(beginRoot);
|
|
1395
1395
|
if (beginRoot === null)
|
|
1396
1396
|
return [];
|
|
@@ -165,19 +165,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
165
165
|
* @returns either a node object (NODE) or undefined.
|
|
166
166
|
*/
|
|
167
167
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
168
|
-
let res;
|
|
169
168
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
170
|
-
|
|
169
|
+
return keyOrNodeOrEntry;
|
|
171
170
|
}
|
|
172
171
|
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
173
|
-
if (keyOrNodeOrEntry[0])
|
|
174
|
-
|
|
172
|
+
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined)
|
|
173
|
+
return;
|
|
174
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
175
175
|
}
|
|
176
176
|
else {
|
|
177
|
-
if (keyOrNodeOrEntry)
|
|
178
|
-
|
|
177
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined)
|
|
178
|
+
return;
|
|
179
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
179
180
|
}
|
|
180
|
-
return res;
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
183
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
@@ -361,33 +361,33 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
361
361
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
362
362
|
*/
|
|
363
363
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
364
|
-
// return this.getNodes(key, this.
|
|
364
|
+
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
365
365
|
if (!this.isRealNode(this.root))
|
|
366
|
-
return
|
|
366
|
+
return;
|
|
367
367
|
if (iterationType === 'RECURSIVE') {
|
|
368
|
-
const
|
|
368
|
+
const dfs = (cur) => {
|
|
369
369
|
if (cur.key === key)
|
|
370
370
|
return cur;
|
|
371
371
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
372
|
return;
|
|
373
|
-
if (this._compare(cur.key, key) === 'GT'
|
|
374
|
-
return
|
|
375
|
-
if (this._compare(cur.key, key) === 'LT'
|
|
376
|
-
return
|
|
373
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
374
|
+
return dfs(cur.left);
|
|
375
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
376
|
+
return dfs(cur.right);
|
|
377
377
|
};
|
|
378
|
-
return
|
|
378
|
+
return dfs(this.root);
|
|
379
379
|
}
|
|
380
380
|
else {
|
|
381
|
-
const
|
|
382
|
-
while (
|
|
383
|
-
const cur =
|
|
381
|
+
const stack = [this.root];
|
|
382
|
+
while (stack.length > 0) {
|
|
383
|
+
const cur = stack.pop();
|
|
384
384
|
if (this.isRealNode(cur)) {
|
|
385
385
|
if (this._compare(cur.key, key) === 'EQ')
|
|
386
386
|
return cur;
|
|
387
|
-
if (this._compare(cur.key, key) === 'GT')
|
|
388
|
-
|
|
389
|
-
if (this._compare(cur.key, key) === 'LT')
|
|
390
|
-
|
|
387
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
388
|
+
stack.push(cur.left);
|
|
389
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
390
|
+
stack.push(cur.right);
|
|
391
391
|
}
|
|
392
392
|
}
|
|
393
393
|
}
|
|
@@ -420,13 +420,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
420
420
|
* performed on the binary tree. It can have two possible values:
|
|
421
421
|
* @returns The method returns an array of nodes (`NODE[]`).
|
|
422
422
|
*/
|
|
423
|
-
getNodes(identifier, callback = this.
|
|
423
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
424
424
|
beginRoot = this.ensureNode(beginRoot);
|
|
425
425
|
if (!beginRoot)
|
|
426
426
|
return [];
|
|
427
427
|
const ans = [];
|
|
428
428
|
if (iterationType === 'RECURSIVE') {
|
|
429
|
-
const
|
|
429
|
+
const dfs = (cur) => {
|
|
430
430
|
const callbackResult = callback(cur);
|
|
431
431
|
if (callbackResult === identifier) {
|
|
432
432
|
ans.push(cur);
|
|
@@ -436,18 +436,18 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
436
436
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
437
437
|
return;
|
|
438
438
|
// TODO potential bug
|
|
439
|
-
if (callback === this.
|
|
439
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
440
440
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
441
|
-
|
|
441
|
+
dfs(cur.left);
|
|
442
442
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
443
|
-
|
|
443
|
+
dfs(cur.right);
|
|
444
444
|
}
|
|
445
445
|
else {
|
|
446
|
-
this.isRealNode(cur.left) &&
|
|
447
|
-
this.isRealNode(cur.right) &&
|
|
446
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
447
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
448
448
|
}
|
|
449
449
|
};
|
|
450
|
-
|
|
450
|
+
dfs(beginRoot);
|
|
451
451
|
}
|
|
452
452
|
else {
|
|
453
453
|
const stack = [beginRoot];
|
|
@@ -461,7 +461,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
461
461
|
return ans;
|
|
462
462
|
}
|
|
463
463
|
// TODO potential bug
|
|
464
|
-
if (callback === this.
|
|
464
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
465
465
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
466
466
|
stack.push(cur.right);
|
|
467
467
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
@@ -505,7 +505,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
505
505
|
* following values:
|
|
506
506
|
* @returns The method is returning an array of the return type of the callback function.
|
|
507
507
|
*/
|
|
508
|
-
dfs(callback = this.
|
|
508
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') {
|
|
509
509
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
510
510
|
}
|
|
511
511
|
/**
|
|
@@ -529,7 +529,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
529
529
|
* nodes are visited.
|
|
530
530
|
* @returns The method is returning an array of the return type of the callback function.
|
|
531
531
|
*/
|
|
532
|
-
bfs(callback = this.
|
|
532
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
533
533
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
534
534
|
}
|
|
535
535
|
/**
|
|
@@ -554,7 +554,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
554
554
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
555
555
|
* function.
|
|
556
556
|
*/
|
|
557
|
-
listLevels(callback = this.
|
|
557
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
558
558
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
559
559
|
}
|
|
560
560
|
/**
|
|
@@ -617,7 +617,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
617
617
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
618
618
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
619
619
|
*/
|
|
620
|
-
lesserOrGreaterTraverse(callback = this.
|
|
620
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) {
|
|
621
621
|
targetNode = this.ensureNode(targetNode);
|
|
622
622
|
const ans = [];
|
|
623
623
|
if (!targetNode)
|
|
@@ -626,16 +626,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
626
626
|
return ans;
|
|
627
627
|
const targetKey = targetNode.key;
|
|
628
628
|
if (iterationType === 'RECURSIVE') {
|
|
629
|
-
const
|
|
629
|
+
const dfs = (cur) => {
|
|
630
630
|
const compared = this._compare(cur.key, targetKey);
|
|
631
631
|
if (compared === lesserOrGreater)
|
|
632
632
|
ans.push(callback(cur));
|
|
633
633
|
if (this.isRealNode(cur.left))
|
|
634
|
-
|
|
634
|
+
dfs(cur.left);
|
|
635
635
|
if (this.isRealNode(cur.right))
|
|
636
|
-
|
|
636
|
+
dfs(cur.right);
|
|
637
637
|
};
|
|
638
|
-
|
|
638
|
+
dfs(this.root);
|
|
639
639
|
return ans;
|
|
640
640
|
}
|
|
641
641
|
else {
|
|
@@ -187,7 +187,7 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
187
187
|
* deleted is not found.
|
|
188
188
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
189
189
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
190
|
-
* `
|
|
190
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
191
191
|
* return the identifier of the node to
|
|
192
192
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
193
193
|
*/
|
|
@@ -190,7 +190,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
190
190
|
* its default value is taken from the `iterationType` property of the class.
|
|
191
191
|
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
192
192
|
*/
|
|
193
|
-
getNode(identifier, callback = this.
|
|
193
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
194
194
|
var _a;
|
|
195
195
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
196
196
|
}
|
|
@@ -261,11 +261,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
261
261
|
* deleted is not found.
|
|
262
262
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
263
263
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
264
|
-
* `
|
|
264
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
265
265
|
* return the identifier of the node to
|
|
266
266
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
267
267
|
*/
|
|
268
|
-
delete(identifier, callback = this.
|
|
268
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
269
269
|
if (identifier === null)
|
|
270
270
|
return [];
|
|
271
271
|
const results = [];
|
|
@@ -145,7 +145,7 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
145
145
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
146
146
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
147
147
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
148
|
-
* identifier for deletion. If no callback is provided, the `
|
|
148
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
149
149
|
* used
|
|
150
150
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
151
151
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -198,7 +198,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
198
198
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
199
199
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
200
200
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
201
|
-
* identifier for deletion. If no callback is provided, the `
|
|
201
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
202
202
|
* used
|
|
203
203
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
204
204
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -206,7 +206,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
206
206
|
* target node will be decremented
|
|
207
207
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
208
208
|
*/
|
|
209
|
-
delete(identifier, callback = this.
|
|
209
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
210
210
|
if (identifier === null)
|
|
211
211
|
return [];
|
|
212
212
|
const results = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.51.1",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.
|
|
69
|
+
"data-structure-typed": "^1.51.0"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -240,7 +240,7 @@ export class AVLTreeMultiMap<
|
|
|
240
240
|
*/
|
|
241
241
|
override delete<C extends BTNCallback<NODE>>(
|
|
242
242
|
identifier: ReturnType<C>,
|
|
243
|
-
callback: C = this.
|
|
243
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
244
244
|
ignoreCount = false
|
|
245
245
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
246
246
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
@@ -164,13 +164,13 @@ export class AVLTree<
|
|
|
164
164
|
* `callback` function.
|
|
165
165
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
166
166
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
167
|
-
* default to the `
|
|
167
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
168
168
|
* parameter of type `NODE
|
|
169
169
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
170
170
|
*/
|
|
171
171
|
override delete<C extends BTNCallback<NODE>>(
|
|
172
172
|
identifier: ReturnType<C>,
|
|
173
|
-
callback: C = this.
|
|
173
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
174
174
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
175
175
|
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
176
176
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -268,17 +268,17 @@ export class BinaryTree<
|
|
|
268
268
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
269
269
|
iterationType: IterationType = 'ITERATIVE'
|
|
270
270
|
): NODE | null | undefined {
|
|
271
|
-
let res: NODE | null | undefined;
|
|
272
271
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
273
|
-
|
|
272
|
+
return keyOrNodeOrEntry;
|
|
274
273
|
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
275
|
-
if (keyOrNodeOrEntry[0] === null)
|
|
276
|
-
|
|
274
|
+
if (keyOrNodeOrEntry[0] === null) return null;
|
|
275
|
+
if (keyOrNodeOrEntry[0] === undefined) return;
|
|
276
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
277
277
|
} else {
|
|
278
|
-
if (keyOrNodeOrEntry === null)
|
|
279
|
-
|
|
278
|
+
if (keyOrNodeOrEntry === null) return null;
|
|
279
|
+
if (keyOrNodeOrEntry === undefined) return;
|
|
280
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
280
281
|
}
|
|
281
|
-
return res;
|
|
282
282
|
}
|
|
283
283
|
|
|
284
284
|
/**
|
|
@@ -486,16 +486,16 @@ export class BinaryTree<
|
|
|
486
486
|
* specific node based on its value or object.
|
|
487
487
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
488
488
|
* identifier of the node to be deleted. It is optional and has a default value of
|
|
489
|
-
* `this.
|
|
489
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
|
|
490
490
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
491
491
|
*/
|
|
492
492
|
delete<C extends BTNCallback<NODE>>(
|
|
493
493
|
identifier: ReturnType<C> | null | undefined,
|
|
494
|
-
callback: C = this.
|
|
494
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
495
495
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
496
496
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
497
497
|
if (!this.root) return deletedResult;
|
|
498
|
-
if ((!callback || callback === this.
|
|
498
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
499
499
|
callback = (node => node) as C;
|
|
500
500
|
|
|
501
501
|
const curr = this.getNode(identifier, callback);
|
|
@@ -579,7 +579,7 @@ export class BinaryTree<
|
|
|
579
579
|
* specific value.
|
|
580
580
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
581
581
|
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
582
|
-
* identifier. If no callback is provided, the `
|
|
582
|
+
* identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
|
|
583
583
|
* default
|
|
584
584
|
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
585
585
|
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
@@ -594,12 +594,12 @@ export class BinaryTree<
|
|
|
594
594
|
*/
|
|
595
595
|
getNodes<C extends BTNCallback<NODE>>(
|
|
596
596
|
identifier: ReturnType<C> | null | undefined,
|
|
597
|
-
callback: C = this.
|
|
597
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
598
598
|
onlyOne = false,
|
|
599
599
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
600
600
|
iterationType: IterationType = this.iterationType
|
|
601
601
|
): NODE[] {
|
|
602
|
-
if ((!callback || callback === this.
|
|
602
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
603
603
|
callback = (node => node) as C;
|
|
604
604
|
beginRoot = this.ensureNode(beginRoot);
|
|
605
605
|
if (!beginRoot) return [];
|
|
@@ -607,28 +607,28 @@ export class BinaryTree<
|
|
|
607
607
|
const ans: NODE[] = [];
|
|
608
608
|
|
|
609
609
|
if (iterationType === 'RECURSIVE') {
|
|
610
|
-
const
|
|
610
|
+
const dfs = (cur: NODE) => {
|
|
611
611
|
if (callback(cur) === identifier) {
|
|
612
612
|
ans.push(cur);
|
|
613
613
|
if (onlyOne) return;
|
|
614
614
|
}
|
|
615
615
|
if (!cur.left && !cur.right) return;
|
|
616
|
-
cur.left &&
|
|
617
|
-
cur.right &&
|
|
616
|
+
cur.left && dfs(cur.left);
|
|
617
|
+
cur.right && dfs(cur.right);
|
|
618
618
|
};
|
|
619
619
|
|
|
620
|
-
|
|
620
|
+
dfs(beginRoot);
|
|
621
621
|
} else {
|
|
622
|
-
const
|
|
623
|
-
while (
|
|
624
|
-
const cur =
|
|
622
|
+
const stack = [beginRoot];
|
|
623
|
+
while (stack.length > 0) {
|
|
624
|
+
const cur = stack.pop();
|
|
625
625
|
if (cur) {
|
|
626
626
|
if (callback(cur) === identifier) {
|
|
627
627
|
ans.push(cur);
|
|
628
628
|
if (onlyOne) return ans;
|
|
629
629
|
}
|
|
630
|
-
cur.left &&
|
|
631
|
-
cur.right &&
|
|
630
|
+
cur.left && stack.push(cur.left);
|
|
631
|
+
cur.right && stack.push(cur.right);
|
|
632
632
|
}
|
|
633
633
|
}
|
|
634
634
|
}
|
|
@@ -685,11 +685,11 @@ export class BinaryTree<
|
|
|
685
685
|
*/
|
|
686
686
|
getNode<C extends BTNCallback<NODE>>(
|
|
687
687
|
identifier: ReturnType<C> | null | undefined,
|
|
688
|
-
callback: C = this.
|
|
688
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
689
689
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
690
690
|
iterationType: IterationType = this.iterationType
|
|
691
691
|
): NODE | null | undefined {
|
|
692
|
-
if ((!callback || callback === this.
|
|
692
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
693
693
|
callback = (node => node) as C;
|
|
694
694
|
|
|
695
695
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
@@ -717,23 +717,23 @@ export class BinaryTree<
|
|
|
717
717
|
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
718
718
|
if (!this.root) return undefined;
|
|
719
719
|
if (iterationType === 'RECURSIVE') {
|
|
720
|
-
const
|
|
720
|
+
const dfs = (cur: NODE): NODE | undefined => {
|
|
721
721
|
if (cur.key === key) return cur;
|
|
722
722
|
|
|
723
723
|
if (!cur.left && !cur.right) return;
|
|
724
|
-
if (cur.left) return
|
|
725
|
-
if (cur.right) return
|
|
724
|
+
if (cur.left) return dfs(cur.left);
|
|
725
|
+
if (cur.right) return dfs(cur.right);
|
|
726
726
|
};
|
|
727
727
|
|
|
728
|
-
return
|
|
728
|
+
return dfs(this.root);
|
|
729
729
|
} else {
|
|
730
|
-
const
|
|
731
|
-
while (
|
|
732
|
-
const cur =
|
|
730
|
+
const stack = [this.root];
|
|
731
|
+
while (stack.length > 0) {
|
|
732
|
+
const cur = stack.pop();
|
|
733
733
|
if (cur) {
|
|
734
734
|
if (cur.key === key) return cur;
|
|
735
|
-
cur.left &&
|
|
736
|
-
cur.right &&
|
|
735
|
+
cur.left && stack.push(cur.left);
|
|
736
|
+
cur.right && stack.push(cur.right);
|
|
737
737
|
}
|
|
738
738
|
}
|
|
739
739
|
}
|
|
@@ -789,11 +789,11 @@ export class BinaryTree<
|
|
|
789
789
|
*/
|
|
790
790
|
override get<C extends BTNCallback<NODE>>(
|
|
791
791
|
identifier: ReturnType<C> | null | undefined,
|
|
792
|
-
callback: C = this.
|
|
792
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
793
793
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
794
794
|
iterationType: IterationType = this.iterationType
|
|
795
795
|
): V | undefined {
|
|
796
|
-
if ((!callback || callback === this.
|
|
796
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
797
797
|
callback = (node => node) as C;
|
|
798
798
|
|
|
799
799
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
@@ -848,11 +848,11 @@ export class BinaryTree<
|
|
|
848
848
|
*/
|
|
849
849
|
override has<C extends BTNCallback<NODE>>(
|
|
850
850
|
identifier: ReturnType<C> | null | undefined,
|
|
851
|
-
callback: C = this.
|
|
851
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
852
852
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
853
853
|
iterationType: IterationType = this.iterationType
|
|
854
854
|
): boolean {
|
|
855
|
-
if ((!callback || callback === this.
|
|
855
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
856
856
|
callback = (node => node) as C;
|
|
857
857
|
|
|
858
858
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
@@ -1184,20 +1184,20 @@ export class BinaryTree<
|
|
|
1184
1184
|
if (!this.isRealNode(beginRoot)) return beginRoot;
|
|
1185
1185
|
|
|
1186
1186
|
if (iterationType === 'RECURSIVE') {
|
|
1187
|
-
const
|
|
1187
|
+
const dfs = (cur: NODE): NODE => {
|
|
1188
1188
|
if (!this.isRealNode(cur.left)) return cur;
|
|
1189
|
-
return
|
|
1189
|
+
return dfs(cur.left);
|
|
1190
1190
|
};
|
|
1191
1191
|
|
|
1192
|
-
return
|
|
1192
|
+
return dfs(beginRoot);
|
|
1193
1193
|
} else {
|
|
1194
1194
|
// Indirect implementation of iteration using tail recursion optimization
|
|
1195
|
-
const
|
|
1195
|
+
const dfs = trampoline((cur: NODE) => {
|
|
1196
1196
|
if (!this.isRealNode(cur.left)) return cur;
|
|
1197
|
-
return
|
|
1197
|
+
return dfs.cont(cur.left);
|
|
1198
1198
|
});
|
|
1199
1199
|
|
|
1200
|
-
return
|
|
1200
|
+
return dfs(beginRoot);
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
1203
|
|
|
@@ -1231,20 +1231,20 @@ export class BinaryTree<
|
|
|
1231
1231
|
if (!beginRoot) return beginRoot;
|
|
1232
1232
|
|
|
1233
1233
|
if (iterationType === 'RECURSIVE') {
|
|
1234
|
-
const
|
|
1234
|
+
const dfs = (cur: NODE): NODE => {
|
|
1235
1235
|
if (!this.isRealNode(cur.right)) return cur;
|
|
1236
|
-
return
|
|
1236
|
+
return dfs(cur.right);
|
|
1237
1237
|
};
|
|
1238
1238
|
|
|
1239
|
-
return
|
|
1239
|
+
return dfs(beginRoot);
|
|
1240
1240
|
} else {
|
|
1241
1241
|
// Indirect implementation of iteration using tail recursion optimization
|
|
1242
|
-
const
|
|
1242
|
+
const dfs = trampoline((cur: NODE) => {
|
|
1243
1243
|
if (!this.isRealNode(cur.right)) return cur;
|
|
1244
|
-
return
|
|
1244
|
+
return dfs.cont(cur.right);
|
|
1245
1245
|
});
|
|
1246
1246
|
|
|
1247
|
-
return
|
|
1247
|
+
return dfs(beginRoot);
|
|
1248
1248
|
}
|
|
1249
1249
|
}
|
|
1250
1250
|
|
|
@@ -1351,7 +1351,7 @@ export class BinaryTree<
|
|
|
1351
1351
|
* @returns an array of values that are the return values of the callback function.
|
|
1352
1352
|
*/
|
|
1353
1353
|
dfs<C extends BTNCallback<NODE | null | undefined>>(
|
|
1354
|
-
callback: C = this.
|
|
1354
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1355
1355
|
pattern: DFSOrderPattern = 'IN',
|
|
1356
1356
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1357
1357
|
iterationType: IterationType = 'ITERATIVE',
|
|
@@ -1361,38 +1361,38 @@ export class BinaryTree<
|
|
|
1361
1361
|
if (!beginRoot) return [];
|
|
1362
1362
|
const ans: ReturnType<C>[] = [];
|
|
1363
1363
|
if (iterationType === 'RECURSIVE') {
|
|
1364
|
-
const
|
|
1364
|
+
const dfs = (node: NODE | null | undefined) => {
|
|
1365
1365
|
switch (pattern) {
|
|
1366
1366
|
case 'IN':
|
|
1367
1367
|
if (includeNull) {
|
|
1368
|
-
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1368
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
|
|
1369
1369
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1370
|
-
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1370
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
|
|
1371
1371
|
} else {
|
|
1372
|
-
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1372
|
+
if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
|
|
1373
1373
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1374
|
-
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1374
|
+
if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
|
|
1375
1375
|
}
|
|
1376
1376
|
break;
|
|
1377
1377
|
case 'PRE':
|
|
1378
1378
|
if (includeNull) {
|
|
1379
1379
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1380
|
-
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1381
|
-
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1380
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
|
|
1381
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
|
|
1382
1382
|
} else {
|
|
1383
1383
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1384
|
-
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1385
|
-
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1384
|
+
if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
|
|
1385
|
+
if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
|
|
1386
1386
|
}
|
|
1387
1387
|
break;
|
|
1388
1388
|
case 'POST':
|
|
1389
1389
|
if (includeNull) {
|
|
1390
|
-
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1391
|
-
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1390
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left)) dfs(node.left);
|
|
1391
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right)) dfs(node.right);
|
|
1392
1392
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1393
1393
|
} else {
|
|
1394
|
-
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1395
|
-
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1394
|
+
if (this.isRealNode(node) && this.isRealNode(node.left)) dfs(node.left);
|
|
1395
|
+
if (this.isRealNode(node) && this.isRealNode(node.right)) dfs(node.right);
|
|
1396
1396
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1397
1397
|
}
|
|
1398
1398
|
|
|
@@ -1400,7 +1400,7 @@ export class BinaryTree<
|
|
|
1400
1400
|
}
|
|
1401
1401
|
};
|
|
1402
1402
|
|
|
1403
|
-
|
|
1403
|
+
dfs(beginRoot);
|
|
1404
1404
|
} else {
|
|
1405
1405
|
// 0: visit, 1: print
|
|
1406
1406
|
const stack: { opt: 0 | 1; node: NODE | null | undefined }[] = [{ opt: 0, node: beginRoot }];
|
|
@@ -1486,7 +1486,7 @@ export class BinaryTree<
|
|
|
1486
1486
|
* the breadth-first traversal of a binary tree.
|
|
1487
1487
|
*/
|
|
1488
1488
|
bfs<C extends BTNCallback<NODE | null>>(
|
|
1489
|
-
callback: C = this.
|
|
1489
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1490
1490
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1491
1491
|
iterationType: IterationType = this.iterationType,
|
|
1492
1492
|
includeNull = false
|
|
@@ -1499,7 +1499,7 @@ export class BinaryTree<
|
|
|
1499
1499
|
if (iterationType === 'RECURSIVE') {
|
|
1500
1500
|
const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
|
|
1501
1501
|
|
|
1502
|
-
const
|
|
1502
|
+
const dfs = (level: number) => {
|
|
1503
1503
|
if (queue.size === 0) return;
|
|
1504
1504
|
|
|
1505
1505
|
const current = queue.shift()!;
|
|
@@ -1513,10 +1513,10 @@ export class BinaryTree<
|
|
|
1513
1513
|
if (this.isRealNode(current.right)) queue.push(current.right);
|
|
1514
1514
|
}
|
|
1515
1515
|
|
|
1516
|
-
|
|
1516
|
+
dfs(level + 1);
|
|
1517
1517
|
};
|
|
1518
1518
|
|
|
1519
|
-
|
|
1519
|
+
dfs(0);
|
|
1520
1520
|
} else {
|
|
1521
1521
|
const queue = new Queue<NODE | null | undefined>([beginRoot]);
|
|
1522
1522
|
while (queue.size > 0) {
|
|
@@ -1580,7 +1580,7 @@ export class BinaryTree<
|
|
|
1580
1580
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1581
1581
|
*/
|
|
1582
1582
|
listLevels<C extends BTNCallback<NODE | null>>(
|
|
1583
|
-
callback: C = this.
|
|
1583
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1584
1584
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
1585
1585
|
iterationType: IterationType = this.iterationType,
|
|
1586
1586
|
includeNull = false
|
|
@@ -1651,7 +1651,7 @@ export class BinaryTree<
|
|
|
1651
1651
|
* by the return type of the `callback` function.
|
|
1652
1652
|
*/
|
|
1653
1653
|
morris<C extends BTNCallback<NODE>>(
|
|
1654
|
-
callback: C = this.
|
|
1654
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
1655
1655
|
pattern: DFSOrderPattern = 'IN',
|
|
1656
1656
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root
|
|
1657
1657
|
): ReturnType<C>[] {
|
|
@@ -1995,7 +1995,7 @@ export class BinaryTree<
|
|
|
1995
1995
|
}
|
|
1996
1996
|
}
|
|
1997
1997
|
|
|
1998
|
-
protected
|
|
1998
|
+
protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
|
|
1999
1999
|
|
|
2000
2000
|
/**
|
|
2001
2001
|
* Swap the data of two nodes in the binary tree.
|
|
@@ -214,15 +214,15 @@ export class BST<
|
|
|
214
214
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
215
215
|
iterationType: IterationType = 'ITERATIVE'
|
|
216
216
|
): NODE | undefined {
|
|
217
|
-
let res: NODE | undefined;
|
|
218
217
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
219
|
-
|
|
218
|
+
return keyOrNodeOrEntry;
|
|
220
219
|
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
221
|
-
if (keyOrNodeOrEntry[0]
|
|
220
|
+
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined) return;
|
|
221
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
222
222
|
} else {
|
|
223
|
-
if (keyOrNodeOrEntry
|
|
223
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) return;
|
|
224
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
224
225
|
}
|
|
225
|
-
return res;
|
|
226
226
|
}
|
|
227
227
|
|
|
228
228
|
/**
|
|
@@ -426,26 +426,26 @@ export class BST<
|
|
|
426
426
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
427
427
|
*/
|
|
428
428
|
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
429
|
-
// return this.getNodes(key, this.
|
|
430
|
-
if (!this.isRealNode(this.root)) return
|
|
429
|
+
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
430
|
+
if (!this.isRealNode(this.root)) return;
|
|
431
431
|
if (iterationType === 'RECURSIVE') {
|
|
432
|
-
const
|
|
432
|
+
const dfs = (cur: NODE): NODE | undefined => {
|
|
433
433
|
if (cur.key === key) return cur;
|
|
434
434
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
435
435
|
|
|
436
|
-
if (this._compare(cur.key, key) === 'GT'
|
|
437
|
-
if (this._compare(cur.key, key) === 'LT'
|
|
436
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') return dfs(cur.left);
|
|
437
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') return dfs(cur.right);
|
|
438
438
|
};
|
|
439
439
|
|
|
440
|
-
return
|
|
440
|
+
return dfs(this.root);
|
|
441
441
|
} else {
|
|
442
|
-
const
|
|
443
|
-
while (
|
|
444
|
-
const cur =
|
|
442
|
+
const stack = [this.root];
|
|
443
|
+
while (stack.length > 0) {
|
|
444
|
+
const cur = stack.pop();
|
|
445
445
|
if (this.isRealNode(cur)) {
|
|
446
446
|
if (this._compare(cur.key, key) === 'EQ') return cur;
|
|
447
|
-
if (this._compare(cur.key, key) === 'GT')
|
|
448
|
-
if (this._compare(cur.key, key) === 'LT')
|
|
447
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') stack.push(cur.left);
|
|
448
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') stack.push(cur.right);
|
|
449
449
|
}
|
|
450
450
|
}
|
|
451
451
|
}
|
|
@@ -481,7 +481,7 @@ export class BST<
|
|
|
481
481
|
*/
|
|
482
482
|
override getNodes<C extends BTNCallback<NODE>>(
|
|
483
483
|
identifier: ReturnType<C> | undefined,
|
|
484
|
-
callback: C = this.
|
|
484
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
485
485
|
onlyOne = false,
|
|
486
486
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
487
487
|
iterationType: IterationType = this.iterationType
|
|
@@ -491,7 +491,7 @@ export class BST<
|
|
|
491
491
|
const ans: NODE[] = [];
|
|
492
492
|
|
|
493
493
|
if (iterationType === 'RECURSIVE') {
|
|
494
|
-
const
|
|
494
|
+
const dfs = (cur: NODE) => {
|
|
495
495
|
const callbackResult = callback(cur);
|
|
496
496
|
if (callbackResult === identifier) {
|
|
497
497
|
ans.push(cur);
|
|
@@ -500,16 +500,16 @@ export class BST<
|
|
|
500
500
|
|
|
501
501
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
502
502
|
// TODO potential bug
|
|
503
|
-
if (callback === this.
|
|
504
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT')
|
|
505
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT')
|
|
503
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
504
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') dfs(cur.left);
|
|
505
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') dfs(cur.right);
|
|
506
506
|
} else {
|
|
507
|
-
this.isRealNode(cur.left) &&
|
|
508
|
-
this.isRealNode(cur.right) &&
|
|
507
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
508
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
509
509
|
}
|
|
510
510
|
};
|
|
511
511
|
|
|
512
|
-
|
|
512
|
+
dfs(beginRoot);
|
|
513
513
|
} else {
|
|
514
514
|
const stack = [beginRoot];
|
|
515
515
|
while (stack.length > 0) {
|
|
@@ -521,7 +521,7 @@ export class BST<
|
|
|
521
521
|
if (onlyOne) return ans;
|
|
522
522
|
}
|
|
523
523
|
// TODO potential bug
|
|
524
|
-
if (callback === this.
|
|
524
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
525
525
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
|
|
526
526
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
|
|
527
527
|
|
|
@@ -568,7 +568,7 @@ export class BST<
|
|
|
568
568
|
* @returns The method is returning an array of the return type of the callback function.
|
|
569
569
|
*/
|
|
570
570
|
override dfs<C extends BTNCallback<NODE>>(
|
|
571
|
-
callback: C = this.
|
|
571
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
572
572
|
pattern: DFSOrderPattern = 'IN',
|
|
573
573
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
574
574
|
iterationType: IterationType = 'ITERATIVE'
|
|
@@ -599,7 +599,7 @@ export class BST<
|
|
|
599
599
|
* @returns The method is returning an array of the return type of the callback function.
|
|
600
600
|
*/
|
|
601
601
|
override bfs<C extends BTNCallback<NODE>>(
|
|
602
|
-
callback: C = this.
|
|
602
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
603
603
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
604
604
|
iterationType: IterationType = this.iterationType
|
|
605
605
|
): ReturnType<C>[] {
|
|
@@ -630,7 +630,7 @@ export class BST<
|
|
|
630
630
|
* function.
|
|
631
631
|
*/
|
|
632
632
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
633
|
-
callback: C = this.
|
|
633
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
634
634
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
635
635
|
iterationType: IterationType = this.iterationType
|
|
636
636
|
): ReturnType<C>[][] {
|
|
@@ -700,7 +700,7 @@ export class BST<
|
|
|
700
700
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
701
701
|
*/
|
|
702
702
|
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
703
|
-
callback: C = this.
|
|
703
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
704
704
|
lesserOrGreater: CP = 'LT',
|
|
705
705
|
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
706
706
|
iterationType: IterationType = this.iterationType
|
|
@@ -713,15 +713,15 @@ export class BST<
|
|
|
713
713
|
const targetKey = targetNode.key;
|
|
714
714
|
|
|
715
715
|
if (iterationType === 'RECURSIVE') {
|
|
716
|
-
const
|
|
716
|
+
const dfs = (cur: NODE) => {
|
|
717
717
|
const compared = this._compare(cur.key, targetKey);
|
|
718
718
|
if (compared === lesserOrGreater) ans.push(callback(cur));
|
|
719
719
|
|
|
720
|
-
if (this.isRealNode(cur.left))
|
|
721
|
-
if (this.isRealNode(cur.right))
|
|
720
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
721
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
722
722
|
};
|
|
723
723
|
|
|
724
|
-
|
|
724
|
+
dfs(this.root);
|
|
725
725
|
return ans;
|
|
726
726
|
} else {
|
|
727
727
|
const queue = new Queue<NODE>([this.root]);
|
|
@@ -231,7 +231,7 @@ export class RedBlackTree<
|
|
|
231
231
|
*/
|
|
232
232
|
override getNode<C extends BTNCallback<NODE>>(
|
|
233
233
|
identifier: ReturnType<C> | undefined,
|
|
234
|
-
callback: C = this.
|
|
234
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
235
235
|
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
236
236
|
iterationType: IterationType = this.iterationType
|
|
237
237
|
): NODE | null | undefined {
|
|
@@ -308,13 +308,13 @@ export class RedBlackTree<
|
|
|
308
308
|
* deleted is not found.
|
|
309
309
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
310
310
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
311
|
-
* `
|
|
311
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
312
312
|
* return the identifier of the node to
|
|
313
313
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
314
314
|
*/
|
|
315
315
|
override delete<C extends BTNCallback<NODE>>(
|
|
316
316
|
identifier: ReturnType<C> | null | undefined,
|
|
317
|
-
callback: C = this.
|
|
317
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
318
318
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
319
319
|
if (identifier === null) return [];
|
|
320
320
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
@@ -242,7 +242,7 @@ export class TreeMultiMap<
|
|
|
242
242
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
243
243
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
244
244
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
245
|
-
* identifier for deletion. If no callback is provided, the `
|
|
245
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
246
246
|
* used
|
|
247
247
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
248
248
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -252,7 +252,7 @@ export class TreeMultiMap<
|
|
|
252
252
|
*/
|
|
253
253
|
override delete<C extends BTNCallback<NODE>>(
|
|
254
254
|
identifier: ReturnType<C> | null | undefined,
|
|
255
|
-
callback: C = this.
|
|
255
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
256
256
|
ignoreCount = false
|
|
257
257
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
258
258
|
if (identifier === null) return [];
|