Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a recursive function to use a stack?

Suppose that I have a tree to traverse using a Depth First Search, and that my algorithm for traversing it looks something like this:

algorithm search(NODE):
  doSomethingWith(NODE)
  for each node CHILD connected to NODE:
    search(CHILD)

Now in many languages there is a maximum depth to recursion, for example if the depth of recursion is over a certain limit, then the procedure will crash with a stack overflow.

How can this function be implemented without the recursion, and instead with a stack? In many cases, there are a lot of local variables; where can they be stored?

like image 974
Ran Avatar asked Aug 02 '10 19:08

Ran


People also ask

How recursive function uses stack?

Recursive functions use something called “the call stack.” When a program calls a function, that function goes on top of the call stack. This is similar to a stack of books. You add things one at a time. Then, when you are ready to take something off, you always take off the top item.

Can recursion be simulated with a stack?

In the recursive case we put entries on the stack to simulate recursion. That is, the next time we pop an entry from the stack, we should find a function call. If that is the base case, then the next time we pop from the stack we should get the value from that function call and resume execution.

What can be used to replace recursion?

Many professional developers probably already know how to replace recursive functions to avoid stack-overflow problems in advance by replacing with iterative function or using stack (heap stack) and while-loop (recursive simulation function).


2 Answers

You change this to use a stack like so:

algorithm search(NODE):
  createStack()
  addNodeToStack(NODE)

  while(stackHasElements)
      NODE = popNodeFromStack()
      doSomethingWith(NODE)
      for each node CHILD connected to NODE:
         addNodeToStack(CHILD)

As for your second question:

In many cases, there are a lot of local variables; where can they be stored?

These really can be kept in the same location as they were originally. If the variables are local to the "doSomethingWith" method, just move them into that, and refactor that into a separate method. The method doesn't need to handle the traversal, only the processing, and can have it's own local variables this way that work in its scope only.

like image 50
Reed Copsey Avatar answered Oct 12 '22 13:10

Reed Copsey


For a slightly different traversal.

push(root)
while not empty:
    node = pop
    doSomethingWith node
    for each node CHILD connected to NODE:
        push(CHILD)

For an identical traversal push the nodes in reverse order.

If you are blowing your stack, this probably won't help, as you'll blow your heap instead

You can avoid pushing all the children if you have a nextChild function

like image 26
deinst Avatar answered Oct 12 '22 13:10

deinst