How can I perform a value-based search on a complex tree structure in TypeScript without encountering the maximum stack call exceeded error? I am attempting to navigate through an expandable tree using TypeScript, and I will provide the code snippet below for reference:
tree= [
{
"size": -1320,
"offset": 0,
"value": "E",
"subNodes": [
{
"size": 48,
"offset": 0,
"value": "D"
},
{
"size": 48,
"offset": 0,
"value": "D"
},
{
"size": 48,
"offset": 0,
"value": "D"
}
],
{
"size": -1320,
"offset": 0,
"value": "E",
"subNodes": [
{
"size": 48,
"offset": 0,
"value": "D"
},
{
"size": 48,
"offset": 0,
"value": "D",
"subNodes": [
{
"size": 48,
"offset": 0,
"value": "D"
},
{
"size": 48,
"offset": 0,
"value": "D"]
}]]
I keep hitting the maximum call stack limit! The recursive function might be causing this issue. Is there a way to search based on just the letters of the value? Below is my current implementation:
searchTree(protcolTree: Array<SessionTree>, searchString: string): Array<SessionTree> {
if (this.protocolTree[0].value === searchString) {
this.protocolTree = protcolTree;
} else if (this.protocolTree.length) {
let result: Array<SessionTree> = null;
for (let i = 0; result === null && i < this.protocolTree.length; i++) {
result = this.searchTree(this.protocolTree[i].subNodes, searchString);
}
this.protocolTree = result;
return result;
}
return this.protocolTree;
}