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
|
@@ -7740,7 +7740,8 @@ var dataStructureTyped = (() => {
|
|
|
7740
7740
|
__publicField(this, "_extractor", (key) => typeof key === "number" ? key : Number(key));
|
|
7741
7741
|
__publicField(this, "_root");
|
|
7742
7742
|
__publicField(this, "_size");
|
|
7743
|
-
__publicField(this, "
|
|
7743
|
+
__publicField(this, "_NIL", new BinaryTreeNode(NaN));
|
|
7744
|
+
__publicField(this, "_DEFAULT_CALLBACK", (node) => node ? node.key : void 0);
|
|
7744
7745
|
if (options) {
|
|
7745
7746
|
const { iterationType, extractor } = options;
|
|
7746
7747
|
if (iterationType)
|
|
@@ -7774,6 +7775,13 @@ var dataStructureTyped = (() => {
|
|
|
7774
7775
|
get size() {
|
|
7775
7776
|
return this._size;
|
|
7776
7777
|
}
|
|
7778
|
+
/**
|
|
7779
|
+
* The function returns the value of the _NIL property.
|
|
7780
|
+
* @returns The method is returning the value of the `_NIL` property.
|
|
7781
|
+
*/
|
|
7782
|
+
get NIL() {
|
|
7783
|
+
return this._NIL;
|
|
7784
|
+
}
|
|
7777
7785
|
/**
|
|
7778
7786
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
7779
7787
|
* @param {K} key - The key for the new node.
|
|
@@ -7844,21 +7852,29 @@ var dataStructureTyped = (() => {
|
|
|
7844
7852
|
* itself if it is not a valid node key.
|
|
7845
7853
|
*/
|
|
7846
7854
|
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE") {
|
|
7847
|
-
let res;
|
|
7848
7855
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
7849
|
-
|
|
7856
|
+
return keyOrNodeOrEntry;
|
|
7850
7857
|
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
7851
7858
|
if (keyOrNodeOrEntry[0] === null)
|
|
7852
|
-
|
|
7853
|
-
|
|
7854
|
-
|
|
7859
|
+
return null;
|
|
7860
|
+
if (keyOrNodeOrEntry[0] === void 0)
|
|
7861
|
+
return;
|
|
7862
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
7855
7863
|
} else {
|
|
7856
7864
|
if (keyOrNodeOrEntry === null)
|
|
7857
|
-
|
|
7858
|
-
|
|
7859
|
-
|
|
7865
|
+
return null;
|
|
7866
|
+
if (keyOrNodeOrEntry === void 0)
|
|
7867
|
+
return;
|
|
7868
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
7860
7869
|
}
|
|
7861
|
-
|
|
7870
|
+
}
|
|
7871
|
+
/**
|
|
7872
|
+
* The function checks if a given node is a real node or null.
|
|
7873
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
7874
|
+
* @returns a boolean value.
|
|
7875
|
+
*/
|
|
7876
|
+
isNodeOrNull(node) {
|
|
7877
|
+
return this.isRealNode(node) || node === null;
|
|
7862
7878
|
}
|
|
7863
7879
|
/**
|
|
7864
7880
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
@@ -7868,15 +7884,6 @@ var dataStructureTyped = (() => {
|
|
|
7868
7884
|
isNode(keyOrNodeOrEntry) {
|
|
7869
7885
|
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
7870
7886
|
}
|
|
7871
|
-
/**
|
|
7872
|
-
* The function checks if a given value is an entry in a binary tree node.
|
|
7873
|
-
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
7874
|
-
* two type parameters V and NODE, representing the value and node type respectively.
|
|
7875
|
-
* @returns a boolean value.
|
|
7876
|
-
*/
|
|
7877
|
-
isEntry(keyOrNodeOrEntry) {
|
|
7878
|
-
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
7879
|
-
}
|
|
7880
7887
|
/**
|
|
7881
7888
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
7882
7889
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -7884,7 +7891,9 @@ var dataStructureTyped = (() => {
|
|
|
7884
7891
|
* @returns a boolean value.
|
|
7885
7892
|
*/
|
|
7886
7893
|
isRealNode(node) {
|
|
7887
|
-
|
|
7894
|
+
if (!this.isNode(node))
|
|
7895
|
+
return false;
|
|
7896
|
+
return node !== this.NIL;
|
|
7888
7897
|
}
|
|
7889
7898
|
/**
|
|
7890
7899
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
@@ -7892,15 +7901,16 @@ var dataStructureTyped = (() => {
|
|
|
7892
7901
|
* @returns a boolean value.
|
|
7893
7902
|
*/
|
|
7894
7903
|
isNIL(node) {
|
|
7895
|
-
return node
|
|
7904
|
+
return node === this.NIL;
|
|
7896
7905
|
}
|
|
7897
7906
|
/**
|
|
7898
|
-
* The function checks if a given
|
|
7899
|
-
* @param
|
|
7907
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
7908
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
7909
|
+
* two type parameters V and NODE, representing the value and node type respectively.
|
|
7900
7910
|
* @returns a boolean value.
|
|
7901
7911
|
*/
|
|
7902
|
-
|
|
7903
|
-
return
|
|
7912
|
+
isEntry(keyOrNodeOrEntry) {
|
|
7913
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
7904
7914
|
}
|
|
7905
7915
|
/**
|
|
7906
7916
|
* Time Complexity O(n)
|
|
@@ -8027,14 +8037,14 @@ var dataStructureTyped = (() => {
|
|
|
8027
8037
|
* specific node based on its value or object.
|
|
8028
8038
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
8029
8039
|
* identifier of the node to be deleted. It is optional and has a default value of
|
|
8030
|
-
* `this.
|
|
8040
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
|
|
8031
8041
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
8032
8042
|
*/
|
|
8033
|
-
delete(identifier, callback = this.
|
|
8043
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
8034
8044
|
const deletedResult = [];
|
|
8035
8045
|
if (!this.root)
|
|
8036
8046
|
return deletedResult;
|
|
8037
|
-
if ((!callback || callback === this.
|
|
8047
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
8038
8048
|
callback = (node) => node;
|
|
8039
8049
|
const curr = this.getNode(identifier, callback);
|
|
8040
8050
|
if (!curr)
|
|
@@ -8089,7 +8099,7 @@ var dataStructureTyped = (() => {
|
|
|
8089
8099
|
* specific value.
|
|
8090
8100
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
8091
8101
|
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
8092
|
-
* identifier. If no callback is provided, the `
|
|
8102
|
+
* identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
|
|
8093
8103
|
* default
|
|
8094
8104
|
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
8095
8105
|
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
@@ -8102,38 +8112,38 @@ var dataStructureTyped = (() => {
|
|
|
8102
8112
|
* traverse the binary tree. It can have two possible values:
|
|
8103
8113
|
* @returns an array of nodes of type `NODE`.
|
|
8104
8114
|
*/
|
|
8105
|
-
getNodes(identifier, callback = this.
|
|
8106
|
-
if ((!callback || callback === this.
|
|
8115
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8116
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
8107
8117
|
callback = (node) => node;
|
|
8108
8118
|
beginRoot = this.ensureNode(beginRoot);
|
|
8109
8119
|
if (!beginRoot)
|
|
8110
8120
|
return [];
|
|
8111
8121
|
const ans = [];
|
|
8112
8122
|
if (iterationType === "RECURSIVE") {
|
|
8113
|
-
const
|
|
8123
|
+
const dfs = (cur) => {
|
|
8114
8124
|
if (callback(cur) === identifier) {
|
|
8115
8125
|
ans.push(cur);
|
|
8116
8126
|
if (onlyOne)
|
|
8117
8127
|
return;
|
|
8118
8128
|
}
|
|
8119
|
-
if (!cur.left && !cur.right)
|
|
8129
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
8120
8130
|
return;
|
|
8121
|
-
cur.left &&
|
|
8122
|
-
cur.right &&
|
|
8131
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
8132
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
8123
8133
|
};
|
|
8124
|
-
|
|
8134
|
+
dfs(beginRoot);
|
|
8125
8135
|
} else {
|
|
8126
|
-
const
|
|
8127
|
-
while (
|
|
8128
|
-
const cur =
|
|
8129
|
-
if (cur) {
|
|
8136
|
+
const stack = [beginRoot];
|
|
8137
|
+
while (stack.length > 0) {
|
|
8138
|
+
const cur = stack.pop();
|
|
8139
|
+
if (this.isRealNode(cur)) {
|
|
8130
8140
|
if (callback(cur) === identifier) {
|
|
8131
8141
|
ans.push(cur);
|
|
8132
8142
|
if (onlyOne)
|
|
8133
8143
|
return ans;
|
|
8134
8144
|
}
|
|
8135
|
-
cur.left &&
|
|
8136
|
-
cur.right &&
|
|
8145
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
8146
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
8137
8147
|
}
|
|
8138
8148
|
}
|
|
8139
8149
|
}
|
|
@@ -8164,10 +8174,8 @@ var dataStructureTyped = (() => {
|
|
|
8164
8174
|
* nodes are visited during the search.
|
|
8165
8175
|
* @returns a value of type `NODE | null | undefined`.
|
|
8166
8176
|
*/
|
|
8167
|
-
getNode(identifier, callback = this.
|
|
8177
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8168
8178
|
var _a;
|
|
8169
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
8170
|
-
callback = (node) => node;
|
|
8171
8179
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : null;
|
|
8172
8180
|
}
|
|
8173
8181
|
/**
|
|
@@ -8192,26 +8200,26 @@ var dataStructureTyped = (() => {
|
|
|
8192
8200
|
if (!this.root)
|
|
8193
8201
|
return void 0;
|
|
8194
8202
|
if (iterationType === "RECURSIVE") {
|
|
8195
|
-
const
|
|
8203
|
+
const dfs = (cur) => {
|
|
8196
8204
|
if (cur.key === key)
|
|
8197
8205
|
return cur;
|
|
8198
8206
|
if (!cur.left && !cur.right)
|
|
8199
8207
|
return;
|
|
8200
8208
|
if (cur.left)
|
|
8201
|
-
return
|
|
8209
|
+
return dfs(cur.left);
|
|
8202
8210
|
if (cur.right)
|
|
8203
|
-
return
|
|
8211
|
+
return dfs(cur.right);
|
|
8204
8212
|
};
|
|
8205
|
-
return
|
|
8213
|
+
return dfs(this.root);
|
|
8206
8214
|
} else {
|
|
8207
|
-
const
|
|
8208
|
-
while (
|
|
8209
|
-
const cur =
|
|
8215
|
+
const stack = [this.root];
|
|
8216
|
+
while (stack.length > 0) {
|
|
8217
|
+
const cur = stack.pop();
|
|
8210
8218
|
if (cur) {
|
|
8211
8219
|
if (cur.key === key)
|
|
8212
8220
|
return cur;
|
|
8213
|
-
cur.left &&
|
|
8214
|
-
cur.right &&
|
|
8221
|
+
cur.left && stack.push(cur.left);
|
|
8222
|
+
cur.right && stack.push(cur.right);
|
|
8215
8223
|
}
|
|
8216
8224
|
}
|
|
8217
8225
|
}
|
|
@@ -8242,10 +8250,8 @@ var dataStructureTyped = (() => {
|
|
|
8242
8250
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
8243
8251
|
* found, `undefined` is returned.
|
|
8244
8252
|
*/
|
|
8245
|
-
get(identifier, callback = this.
|
|
8253
|
+
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8246
8254
|
var _a, _b;
|
|
8247
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
8248
|
-
callback = (node) => node;
|
|
8249
8255
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) == null ? void 0 : _a.value) != null ? _b : void 0;
|
|
8250
8256
|
}
|
|
8251
8257
|
/**
|
|
@@ -8273,8 +8279,8 @@ var dataStructureTyped = (() => {
|
|
|
8273
8279
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
8274
8280
|
* @returns a boolean value.
|
|
8275
8281
|
*/
|
|
8276
|
-
has(identifier, callback = this.
|
|
8277
|
-
if ((!callback || callback === this.
|
|
8282
|
+
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
8283
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
8278
8284
|
callback = (node) => node;
|
|
8279
8285
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
8280
8286
|
}
|
|
@@ -8569,19 +8575,19 @@ var dataStructureTyped = (() => {
|
|
|
8569
8575
|
if (!this.isRealNode(beginRoot))
|
|
8570
8576
|
return beginRoot;
|
|
8571
8577
|
if (iterationType === "RECURSIVE") {
|
|
8572
|
-
const
|
|
8578
|
+
const dfs = (cur) => {
|
|
8573
8579
|
if (!this.isRealNode(cur.left))
|
|
8574
8580
|
return cur;
|
|
8575
|
-
return
|
|
8581
|
+
return dfs(cur.left);
|
|
8576
8582
|
};
|
|
8577
|
-
return
|
|
8583
|
+
return dfs(beginRoot);
|
|
8578
8584
|
} else {
|
|
8579
|
-
const
|
|
8585
|
+
const dfs = trampoline((cur) => {
|
|
8580
8586
|
if (!this.isRealNode(cur.left))
|
|
8581
8587
|
return cur;
|
|
8582
|
-
return
|
|
8588
|
+
return dfs.cont(cur.left);
|
|
8583
8589
|
});
|
|
8584
|
-
return
|
|
8590
|
+
return dfs(beginRoot);
|
|
8585
8591
|
}
|
|
8586
8592
|
}
|
|
8587
8593
|
/**
|
|
@@ -8610,19 +8616,19 @@ var dataStructureTyped = (() => {
|
|
|
8610
8616
|
if (!beginRoot)
|
|
8611
8617
|
return beginRoot;
|
|
8612
8618
|
if (iterationType === "RECURSIVE") {
|
|
8613
|
-
const
|
|
8619
|
+
const dfs = (cur) => {
|
|
8614
8620
|
if (!this.isRealNode(cur.right))
|
|
8615
8621
|
return cur;
|
|
8616
|
-
return
|
|
8622
|
+
return dfs(cur.right);
|
|
8617
8623
|
};
|
|
8618
|
-
return
|
|
8624
|
+
return dfs(beginRoot);
|
|
8619
8625
|
} else {
|
|
8620
|
-
const
|
|
8626
|
+
const dfs = trampoline((cur) => {
|
|
8621
8627
|
if (!this.isRealNode(cur.right))
|
|
8622
8628
|
return cur;
|
|
8623
|
-
return
|
|
8629
|
+
return dfs.cont(cur.right);
|
|
8624
8630
|
});
|
|
8625
|
-
return
|
|
8631
|
+
return dfs(beginRoot);
|
|
8626
8632
|
}
|
|
8627
8633
|
}
|
|
8628
8634
|
/**
|
|
@@ -8706,62 +8712,62 @@ var dataStructureTyped = (() => {
|
|
|
8706
8712
|
* `false`, null or undefined
|
|
8707
8713
|
* @returns an array of values that are the return values of the callback function.
|
|
8708
8714
|
*/
|
|
8709
|
-
dfs(callback = this.
|
|
8715
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root, iterationType = "ITERATIVE", includeNull = false) {
|
|
8710
8716
|
beginRoot = this.ensureNode(beginRoot);
|
|
8711
8717
|
if (!beginRoot)
|
|
8712
8718
|
return [];
|
|
8713
8719
|
const ans = [];
|
|
8714
8720
|
if (iterationType === "RECURSIVE") {
|
|
8715
|
-
const
|
|
8721
|
+
const dfs = (node) => {
|
|
8716
8722
|
switch (pattern) {
|
|
8717
8723
|
case "IN":
|
|
8718
8724
|
if (includeNull) {
|
|
8719
8725
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
8720
|
-
|
|
8726
|
+
dfs(node.left);
|
|
8721
8727
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
8722
8728
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
8723
|
-
|
|
8729
|
+
dfs(node.right);
|
|
8724
8730
|
} else {
|
|
8725
8731
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
8726
|
-
|
|
8732
|
+
dfs(node.left);
|
|
8727
8733
|
this.isRealNode(node) && ans.push(callback(node));
|
|
8728
8734
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
8729
|
-
|
|
8735
|
+
dfs(node.right);
|
|
8730
8736
|
}
|
|
8731
8737
|
break;
|
|
8732
8738
|
case "PRE":
|
|
8733
8739
|
if (includeNull) {
|
|
8734
8740
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
8735
8741
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
8736
|
-
|
|
8742
|
+
dfs(node.left);
|
|
8737
8743
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
8738
|
-
|
|
8744
|
+
dfs(node.right);
|
|
8739
8745
|
} else {
|
|
8740
8746
|
this.isRealNode(node) && ans.push(callback(node));
|
|
8741
8747
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
8742
|
-
|
|
8748
|
+
dfs(node.left);
|
|
8743
8749
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
8744
|
-
|
|
8750
|
+
dfs(node.right);
|
|
8745
8751
|
}
|
|
8746
8752
|
break;
|
|
8747
8753
|
case "POST":
|
|
8748
8754
|
if (includeNull) {
|
|
8749
8755
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
8750
|
-
|
|
8756
|
+
dfs(node.left);
|
|
8751
8757
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
8752
|
-
|
|
8758
|
+
dfs(node.right);
|
|
8753
8759
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
8754
8760
|
} else {
|
|
8755
8761
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
8756
|
-
|
|
8762
|
+
dfs(node.left);
|
|
8757
8763
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
8758
|
-
|
|
8764
|
+
dfs(node.right);
|
|
8759
8765
|
this.isRealNode(node) && ans.push(callback(node));
|
|
8760
8766
|
}
|
|
8761
8767
|
break;
|
|
8762
8768
|
}
|
|
8763
8769
|
};
|
|
8764
|
-
|
|
8770
|
+
dfs(beginRoot);
|
|
8765
8771
|
} else {
|
|
8766
8772
|
const stack = [{ opt: 0, node: beginRoot }];
|
|
8767
8773
|
while (stack.length > 0) {
|
|
@@ -8830,14 +8836,14 @@ var dataStructureTyped = (() => {
|
|
|
8830
8836
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
8831
8837
|
* the breadth-first traversal of a binary tree.
|
|
8832
8838
|
*/
|
|
8833
|
-
bfs(callback = this.
|
|
8839
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
8834
8840
|
beginRoot = this.ensureNode(beginRoot);
|
|
8835
8841
|
if (!beginRoot)
|
|
8836
8842
|
return [];
|
|
8837
8843
|
const ans = [];
|
|
8838
8844
|
if (iterationType === "RECURSIVE") {
|
|
8839
8845
|
const queue = new Queue([beginRoot]);
|
|
8840
|
-
const
|
|
8846
|
+
const dfs = (level) => {
|
|
8841
8847
|
if (queue.size === 0)
|
|
8842
8848
|
return;
|
|
8843
8849
|
const current = queue.shift();
|
|
@@ -8853,9 +8859,9 @@ var dataStructureTyped = (() => {
|
|
|
8853
8859
|
if (this.isRealNode(current.right))
|
|
8854
8860
|
queue.push(current.right);
|
|
8855
8861
|
}
|
|
8856
|
-
|
|
8862
|
+
dfs(level + 1);
|
|
8857
8863
|
};
|
|
8858
|
-
|
|
8864
|
+
dfs(0);
|
|
8859
8865
|
} else {
|
|
8860
8866
|
const queue = new Queue([beginRoot]);
|
|
8861
8867
|
while (queue.size > 0) {
|
|
@@ -8904,7 +8910,7 @@ var dataStructureTyped = (() => {
|
|
|
8904
8910
|
* be excluded
|
|
8905
8911
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
8906
8912
|
*/
|
|
8907
|
-
listLevels(callback = this.
|
|
8913
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
8908
8914
|
beginRoot = this.ensureNode(beginRoot);
|
|
8909
8915
|
const levelsNodes = [];
|
|
8910
8916
|
if (!beginRoot)
|
|
@@ -8973,7 +8979,7 @@ var dataStructureTyped = (() => {
|
|
|
8973
8979
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
8974
8980
|
* by the return type of the `callback` function.
|
|
8975
8981
|
*/
|
|
8976
|
-
morris(callback = this.
|
|
8982
|
+
morris(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root) {
|
|
8977
8983
|
beginRoot = this.ensureNode(beginRoot);
|
|
8978
8984
|
if (beginRoot === null)
|
|
8979
8985
|
return [];
|
|
@@ -9487,17 +9493,17 @@ var dataStructureTyped = (() => {
|
|
|
9487
9493
|
* @returns either a node object (NODE) or undefined.
|
|
9488
9494
|
*/
|
|
9489
9495
|
ensureNode(keyOrNodeOrEntry, iterationType = "ITERATIVE") {
|
|
9490
|
-
let res;
|
|
9491
9496
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
9492
|
-
|
|
9497
|
+
return keyOrNodeOrEntry;
|
|
9493
9498
|
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
9494
|
-
if (keyOrNodeOrEntry[0])
|
|
9495
|
-
|
|
9499
|
+
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === void 0)
|
|
9500
|
+
return;
|
|
9501
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
9496
9502
|
} else {
|
|
9497
|
-
if (keyOrNodeOrEntry)
|
|
9498
|
-
|
|
9503
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === void 0)
|
|
9504
|
+
return;
|
|
9505
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
9499
9506
|
}
|
|
9500
|
-
return res;
|
|
9501
9507
|
}
|
|
9502
9508
|
/**
|
|
9503
9509
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
@@ -9672,30 +9678,30 @@ var dataStructureTyped = (() => {
|
|
|
9672
9678
|
*/
|
|
9673
9679
|
getNodeByKey(key, iterationType = "ITERATIVE") {
|
|
9674
9680
|
if (!this.isRealNode(this.root))
|
|
9675
|
-
return
|
|
9681
|
+
return;
|
|
9676
9682
|
if (iterationType === "RECURSIVE") {
|
|
9677
|
-
const
|
|
9683
|
+
const dfs = (cur) => {
|
|
9678
9684
|
if (cur.key === key)
|
|
9679
9685
|
return cur;
|
|
9680
9686
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
9681
9687
|
return;
|
|
9682
|
-
if (this._compare(cur.key, key) === "GT"
|
|
9683
|
-
return
|
|
9684
|
-
if (this._compare(cur.key, key) === "LT"
|
|
9685
|
-
return
|
|
9688
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === "GT")
|
|
9689
|
+
return dfs(cur.left);
|
|
9690
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === "LT")
|
|
9691
|
+
return dfs(cur.right);
|
|
9686
9692
|
};
|
|
9687
|
-
return
|
|
9693
|
+
return dfs(this.root);
|
|
9688
9694
|
} else {
|
|
9689
|
-
const
|
|
9690
|
-
while (
|
|
9691
|
-
const cur =
|
|
9695
|
+
const stack = [this.root];
|
|
9696
|
+
while (stack.length > 0) {
|
|
9697
|
+
const cur = stack.pop();
|
|
9692
9698
|
if (this.isRealNode(cur)) {
|
|
9693
9699
|
if (this._compare(cur.key, key) === "EQ")
|
|
9694
9700
|
return cur;
|
|
9695
|
-
if (this._compare(cur.key, key) === "GT")
|
|
9696
|
-
|
|
9697
|
-
if (this._compare(cur.key, key) === "LT")
|
|
9698
|
-
|
|
9701
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === "GT")
|
|
9702
|
+
stack.push(cur.left);
|
|
9703
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === "LT")
|
|
9704
|
+
stack.push(cur.right);
|
|
9699
9705
|
}
|
|
9700
9706
|
}
|
|
9701
9707
|
}
|
|
@@ -9728,13 +9734,13 @@ var dataStructureTyped = (() => {
|
|
|
9728
9734
|
* performed on the binary tree. It can have two possible values:
|
|
9729
9735
|
* @returns The method returns an array of nodes (`NODE[]`).
|
|
9730
9736
|
*/
|
|
9731
|
-
getNodes(identifier, callback = this.
|
|
9737
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9732
9738
|
beginRoot = this.ensureNode(beginRoot);
|
|
9733
9739
|
if (!beginRoot)
|
|
9734
9740
|
return [];
|
|
9735
9741
|
const ans = [];
|
|
9736
9742
|
if (iterationType === "RECURSIVE") {
|
|
9737
|
-
const
|
|
9743
|
+
const dfs = (cur) => {
|
|
9738
9744
|
const callbackResult = callback(cur);
|
|
9739
9745
|
if (callbackResult === identifier) {
|
|
9740
9746
|
ans.push(cur);
|
|
@@ -9743,17 +9749,17 @@ var dataStructureTyped = (() => {
|
|
|
9743
9749
|
}
|
|
9744
9750
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
9745
9751
|
return;
|
|
9746
|
-
if (callback === this.
|
|
9752
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
9747
9753
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === "GT")
|
|
9748
|
-
|
|
9754
|
+
dfs(cur.left);
|
|
9749
9755
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === "LT")
|
|
9750
|
-
|
|
9756
|
+
dfs(cur.right);
|
|
9751
9757
|
} else {
|
|
9752
|
-
this.isRealNode(cur.left) &&
|
|
9753
|
-
this.isRealNode(cur.right) &&
|
|
9758
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
9759
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
9754
9760
|
}
|
|
9755
9761
|
};
|
|
9756
|
-
|
|
9762
|
+
dfs(beginRoot);
|
|
9757
9763
|
} else {
|
|
9758
9764
|
const stack = [beginRoot];
|
|
9759
9765
|
while (stack.length > 0) {
|
|
@@ -9765,7 +9771,7 @@ var dataStructureTyped = (() => {
|
|
|
9765
9771
|
if (onlyOne)
|
|
9766
9772
|
return ans;
|
|
9767
9773
|
}
|
|
9768
|
-
if (callback === this.
|
|
9774
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
9769
9775
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === "LT")
|
|
9770
9776
|
stack.push(cur.right);
|
|
9771
9777
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === "GT")
|
|
@@ -9779,6 +9785,35 @@ var dataStructureTyped = (() => {
|
|
|
9779
9785
|
}
|
|
9780
9786
|
return ans;
|
|
9781
9787
|
}
|
|
9788
|
+
/**
|
|
9789
|
+
* Time Complexity: O(log n)
|
|
9790
|
+
* Space Complexity: O(1)
|
|
9791
|
+
*/
|
|
9792
|
+
/**
|
|
9793
|
+
* Time Complexity: O(log n)
|
|
9794
|
+
* Space Complexity: O(1)
|
|
9795
|
+
*
|
|
9796
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
9797
|
+
* callback function.
|
|
9798
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
9799
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
9800
|
+
* with the type of nodes in the tree.
|
|
9801
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
9802
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
9803
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
9804
|
+
* `identifier` parameter.
|
|
9805
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
9806
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
9807
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
9808
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
9809
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
9810
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
9811
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
9812
|
+
*/
|
|
9813
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9814
|
+
var _a;
|
|
9815
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
9816
|
+
}
|
|
9782
9817
|
/**
|
|
9783
9818
|
* Time complexity: O(n)
|
|
9784
9819
|
* Space complexity: O(n)
|
|
@@ -9802,7 +9837,7 @@ var dataStructureTyped = (() => {
|
|
|
9802
9837
|
* following values:
|
|
9803
9838
|
* @returns The method is returning an array of the return type of the callback function.
|
|
9804
9839
|
*/
|
|
9805
|
-
dfs(callback = this.
|
|
9840
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = "IN", beginRoot = this.root, iterationType = "ITERATIVE") {
|
|
9806
9841
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
9807
9842
|
}
|
|
9808
9843
|
/**
|
|
@@ -9826,7 +9861,7 @@ var dataStructureTyped = (() => {
|
|
|
9826
9861
|
* nodes are visited.
|
|
9827
9862
|
* @returns The method is returning an array of the return type of the callback function.
|
|
9828
9863
|
*/
|
|
9829
|
-
bfs(callback = this.
|
|
9864
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9830
9865
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
9831
9866
|
}
|
|
9832
9867
|
/**
|
|
@@ -9851,7 +9886,7 @@ var dataStructureTyped = (() => {
|
|
|
9851
9886
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
9852
9887
|
* function.
|
|
9853
9888
|
*/
|
|
9854
|
-
listLevels(callback = this.
|
|
9889
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
9855
9890
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
9856
9891
|
}
|
|
9857
9892
|
/**
|
|
@@ -9911,7 +9946,7 @@ var dataStructureTyped = (() => {
|
|
|
9911
9946
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
9912
9947
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
9913
9948
|
*/
|
|
9914
|
-
lesserOrGreaterTraverse(callback = this.
|
|
9949
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = "LT", targetNode = this.root, iterationType = this.iterationType) {
|
|
9915
9950
|
targetNode = this.ensureNode(targetNode);
|
|
9916
9951
|
const ans = [];
|
|
9917
9952
|
if (!targetNode)
|
|
@@ -9920,16 +9955,16 @@ var dataStructureTyped = (() => {
|
|
|
9920
9955
|
return ans;
|
|
9921
9956
|
const targetKey = targetNode.key;
|
|
9922
9957
|
if (iterationType === "RECURSIVE") {
|
|
9923
|
-
const
|
|
9958
|
+
const dfs = (cur) => {
|
|
9924
9959
|
const compared = this._compare(cur.key, targetKey);
|
|
9925
9960
|
if (compared === lesserOrGreater)
|
|
9926
9961
|
ans.push(callback(cur));
|
|
9927
9962
|
if (this.isRealNode(cur.left))
|
|
9928
|
-
|
|
9963
|
+
dfs(cur.left);
|
|
9929
9964
|
if (this.isRealNode(cur.right))
|
|
9930
|
-
|
|
9965
|
+
dfs(cur.right);
|
|
9931
9966
|
};
|
|
9932
|
-
|
|
9967
|
+
dfs(this.root);
|
|
9933
9968
|
return ans;
|
|
9934
9969
|
} else {
|
|
9935
9970
|
const queue = new Queue([this.root]);
|
|
@@ -10801,11 +10836,11 @@ var dataStructureTyped = (() => {
|
|
|
10801
10836
|
* `callback` function.
|
|
10802
10837
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
10803
10838
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
10804
|
-
* default to the `
|
|
10839
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
10805
10840
|
* parameter of type `NODE
|
|
10806
10841
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
10807
10842
|
*/
|
|
10808
|
-
delete(identifier, callback = this.
|
|
10843
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
10809
10844
|
if (identifier instanceof AVLTreeNode)
|
|
10810
10845
|
callback = (node) => node;
|
|
10811
10846
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -11178,20 +11213,12 @@ var dataStructureTyped = (() => {
|
|
|
11178
11213
|
*/
|
|
11179
11214
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
11180
11215
|
super([], options);
|
|
11181
|
-
__publicField(this, "_SENTINEL", new RedBlackTreeNode(NaN));
|
|
11182
11216
|
__publicField(this, "_root");
|
|
11183
|
-
this._root = this.
|
|
11217
|
+
this._root = this.NIL;
|
|
11184
11218
|
if (keysOrNodesOrEntries) {
|
|
11185
11219
|
this.addMany(keysOrNodesOrEntries);
|
|
11186
11220
|
}
|
|
11187
11221
|
}
|
|
11188
|
-
/**
|
|
11189
|
-
* The function returns the value of the _SENTINEL property.
|
|
11190
|
-
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
11191
|
-
*/
|
|
11192
|
-
get SENTINEL() {
|
|
11193
|
-
return this._SENTINEL;
|
|
11194
|
-
}
|
|
11195
11222
|
/**
|
|
11196
11223
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
11197
11224
|
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
@@ -11276,53 +11303,6 @@ var dataStructureTyped = (() => {
|
|
|
11276
11303
|
isNode(keyOrNodeOrEntry) {
|
|
11277
11304
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
11278
11305
|
}
|
|
11279
|
-
/**
|
|
11280
|
-
* Time Complexity: O(1)
|
|
11281
|
-
* Space Complexity: O(1)
|
|
11282
|
-
*/
|
|
11283
|
-
/**
|
|
11284
|
-
* Time Complexity: O(1)
|
|
11285
|
-
* Space Complexity: O(1)
|
|
11286
|
-
*
|
|
11287
|
-
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
11288
|
-
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
11289
|
-
* it can either be of type `NODE` or `undefined`.
|
|
11290
|
-
* @returns a boolean value.
|
|
11291
|
-
*/
|
|
11292
|
-
isRealNode(node) {
|
|
11293
|
-
if (node === this.SENTINEL || node === void 0)
|
|
11294
|
-
return false;
|
|
11295
|
-
return node instanceof RedBlackTreeNode;
|
|
11296
|
-
}
|
|
11297
|
-
/**
|
|
11298
|
-
* Time Complexity: O(log n)
|
|
11299
|
-
* Space Complexity: O(1)
|
|
11300
|
-
*/
|
|
11301
|
-
/**
|
|
11302
|
-
* Time Complexity: O(log n)
|
|
11303
|
-
* Space Complexity: O(1)
|
|
11304
|
-
*
|
|
11305
|
-
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
11306
|
-
* callback function.
|
|
11307
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
11308
|
-
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
11309
|
-
* with the type of nodes in the tree.
|
|
11310
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
11311
|
-
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
11312
|
-
* function should take a node as its parameter and return a value that can be compared to the
|
|
11313
|
-
* `identifier` parameter.
|
|
11314
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
11315
|
-
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
11316
|
-
* using the `ensureNode` method. If it is not provided, the `root`
|
|
11317
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
11318
|
-
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
11319
|
-
* its default value is taken from the `iterationType` property of the class.
|
|
11320
|
-
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
11321
|
-
*/
|
|
11322
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
11323
|
-
var _a;
|
|
11324
|
-
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) != null ? _a : void 0;
|
|
11325
|
-
}
|
|
11326
11306
|
/**
|
|
11327
11307
|
* Time Complexity: O(1)
|
|
11328
11308
|
* Space Complexity: O(1)
|
|
@@ -11336,7 +11316,7 @@ var dataStructureTyped = (() => {
|
|
|
11336
11316
|
*/
|
|
11337
11317
|
clear() {
|
|
11338
11318
|
super.clear();
|
|
11339
|
-
this._root = this.
|
|
11319
|
+
this._root = this.NIL;
|
|
11340
11320
|
}
|
|
11341
11321
|
/**
|
|
11342
11322
|
* Time Complexity: O(log n)
|
|
@@ -11387,11 +11367,11 @@ var dataStructureTyped = (() => {
|
|
|
11387
11367
|
* deleted is not found.
|
|
11388
11368
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
11389
11369
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
11390
|
-
* `
|
|
11370
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
11391
11371
|
* return the identifier of the node to
|
|
11392
11372
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
11393
11373
|
*/
|
|
11394
|
-
delete(identifier, callback = this.
|
|
11374
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
11395
11375
|
if (identifier === null)
|
|
11396
11376
|
return [];
|
|
11397
11377
|
const results = [];
|
|
@@ -11491,9 +11471,9 @@ var dataStructureTyped = (() => {
|
|
|
11491
11471
|
while (this.isRealNode(current)) {
|
|
11492
11472
|
parent = current;
|
|
11493
11473
|
if (node.key < current.key) {
|
|
11494
|
-
current = (_a = current.left) != null ? _a : this.
|
|
11474
|
+
current = (_a = current.left) != null ? _a : this.NIL;
|
|
11495
11475
|
} else if (node.key > current.key) {
|
|
11496
|
-
current = (_b = current.right) != null ? _b : this.
|
|
11476
|
+
current = (_b = current.right) != null ? _b : this.NIL;
|
|
11497
11477
|
} else {
|
|
11498
11478
|
this._replaceNode(current, node);
|
|
11499
11479
|
return "UPDATED";
|
|
@@ -11507,8 +11487,8 @@ var dataStructureTyped = (() => {
|
|
|
11507
11487
|
} else {
|
|
11508
11488
|
parent.right = node;
|
|
11509
11489
|
}
|
|
11510
|
-
node.left = this.
|
|
11511
|
-
node.right = this.
|
|
11490
|
+
node.left = this.NIL;
|
|
11491
|
+
node.right = this.NIL;
|
|
11512
11492
|
node.color = "RED";
|
|
11513
11493
|
this._insertFixup(node);
|
|
11514
11494
|
return "CREATED";
|
|
@@ -11927,7 +11907,7 @@ var dataStructureTyped = (() => {
|
|
|
11927
11907
|
* decremented by 1 and
|
|
11928
11908
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
11929
11909
|
*/
|
|
11930
|
-
delete(identifier, callback = this.
|
|
11910
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
11931
11911
|
var _a;
|
|
11932
11912
|
const deletedResult = [];
|
|
11933
11913
|
if (!this.root)
|
|
@@ -12295,7 +12275,7 @@ var dataStructureTyped = (() => {
|
|
|
12295
12275
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
12296
12276
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
12297
12277
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
12298
|
-
* identifier for deletion. If no callback is provided, the `
|
|
12278
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
12299
12279
|
* used
|
|
12300
12280
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
12301
12281
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -12303,7 +12283,7 @@ var dataStructureTyped = (() => {
|
|
|
12303
12283
|
* target node will be decremented
|
|
12304
12284
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
12305
12285
|
*/
|
|
12306
|
-
delete(identifier, callback = this.
|
|
12286
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
12307
12287
|
if (identifier === null)
|
|
12308
12288
|
return [];
|
|
12309
12289
|
const results = [];
|