I'm using a recursive function in my c# asp.net program and it throws "StackOverflow Exception". This exception is thrown when I run my program in IIS.
If I use a loop instead of the recursive function will it throw "StackOverflow Exception"?
Which is good to use a loop or recursion in case of this exception?
Edit:
After analyzing the problem, I found the exception is caused because the level of recursion is more than 1000 and it causes the stack to overflow.
Now, I'm completely lost in converting my recursive function to an iteration because multiple recursion is used. I'm posting a sample code here for reference:
RecursiveFunction(Node n) {
//Some Code for local variables
node.processed=true;
if(n.up){
//Create a sub node for node below the current one
if(!subnode.processed)
RecursiveFunction(subnode);
}
else{
//Create a sub node for node above current one
if(!subnode.processed)
RecursiveFunction(subnode);
}
return result;
}
Note: The above sample code may be an infinite loop because I just used it to mention multiple recursions are used, its actual implementation is not an infinite loop.
In this case, the base condition is, if a node is processed already it will not use recursion and returns the result directly.
My question is, if multiple recursions are used, how can I replace it with an iteration. I googled and found many suggestions for replacing a recursion with iteration or stock. But I don't find anything about replacing a multiple recursion with iteration.