c# - Recursion vs loop -


i'm using recursive function in c# asp.net program , throws "stackoverflow exception". exception thrown when run program in iis.

if use loop instead of recursive function throw "stackoverflow exception"?

which use loop or recursion in case of exception?

edit:

after analyzing problem, found exception caused because level of recursion more 1000 , causes stack overflow.

now, i'm lost in converting recursive function iteration because multiple recursion used. i'm posting sample code here reference:

recursivefunction(node n)    {    //some code local variables node.processed=true; if(n.up){    //create sub node node below current 1   if(!subnode.processed)    recursivefunction(subnode); } else{    //create sub node node above current 1  if(!subnode.processed)    recursivefunction(subnode); } return result; } 

note: above sample code may infinite loop because used mention multiple recursions used, actual implementation not infinite loop.

in case, base condition is, if node processed not use recursion , returns result directly.

my question is, if multiple recursions used, how can replace iteration. googled , found many suggestions replacing recursion iteration or stock. don't find replacing multiple recursion iteration.

you can convert recursive function non-recursive managing own stack. this:

void nonrecursivefunction(node n)    {    var stack = new stack<node>();    stack.push(n);     while(stack.any()) {         node = stack.pop();         //some code local variables         node.processed=true;         if(n.up){             //create sub node node below current 1             if(!subnode.processed)                 stack.push(subnode);         } else{             //create sub node node above current 1             if(!subnode.processed)                 stack.push(subnode);         }     }     return result; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -