如何用堆栈模拟递归? [英] How can you emulate recursion with a stack?

查看:31
本文介绍了如何用堆栈模拟递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说任何递归算法都可以通过使用堆栈来表达.最近,我一直在一个可用调用堆栈大小小得令人望而却步的环境中编写程序.

I've heard that any recursive algorithm can always be expressed by using a stack. Recently, I've been working on programs in an environment with a prohibitively small available call stack size.

我需要进行一些深度递归,所以我想知道如何重新设计任何递归算法以使用显式堆栈.

I need to do some deep recursion, so I was wondering how you could rework any recursive algorithm to use an explicit stack.

例如,假设我有一个这样的递归函数

For example, let's suppose I have a recursive function like this

function f(n, i) {
  if n <= i return n
  if n % i = 0 return f(n / i, i)
  return f(n, i + 1)
}

我怎么能用堆栈来写它呢?我可以遵循一个简单的过程将任何递归函数转换为基于堆栈的函数吗?

how could I write it with a stack instead? Is there a simple process I can follow to convert any recursive function into a stack-based one?

推荐答案

如果你了解一个函数调用如何影响进程堆栈,你就可以了解如何自己去做了.

If you understand how a function call affects the process stack, you can understand how to do it yourself.

当你调用一个函数时,一些数据被写入堆栈,包括参数.该函数读取这些参数,对它们执行任何操作并将结果放在堆栈中.你可以做同样的事情.特别是您的示例不需要堆栈,因此如果我将其转换为使用堆栈的示例,它可能看起来有点傻,所以我将给您斐波那契示例:

When you call a function, some data are written on the stack including the arguments. The function reads these arguments, does whatever with them and places the result on the stack. You can do the exact same thing. Your example in particular doesn't need a stack so if I convert that to one that uses stack it may look a bit silly, so I'm going to give you the fibonacci example:

fib(n)
    if n < 2 return n
    return fib(n-1) + fib(n-2)

function fib(n, i)
    stack.empty()
    stack.push(<is_arg, n>)
    while (!stack.size() > 2 || stack.top().is_arg)
        <isarg, argn> = stack.pop()
        if (isarg)
            if (argn < 2)
                stack.push(<is_result, argn>)
            else
                stack.push(<is_arg, argn-1>)
                stack.push(<is_arg, argn-2>)
        else
            <isarg_prev, argn_prev> = stack.pop()
            if (isarg_prev)
                stack.push(<is_result, argn>)
                stack.push(<is_arg, argn_prev>)
            else
                stack.push(<is_result, argn+argn_prev>)
     return stack.top().argn

说明:每次从堆栈中取出一个项目时,您需要检查它是否需要展开.如果是,则将适当的参数压入堆栈,如果不是,则让它与之前的结果合并.在斐波那契的情况下,一旦 fib(n-2) 被计算(并且在栈顶可用),n-1 被检索(一个在栈顶之后)),将fib(n-2)的结果压入其下,然后将fib(n-1)展开计算.如果栈顶的两个元素都是结果,当然,你只需将它们相加并入栈即可.

Explanation: every time you take an item from the stack, you need to check whether it needs to be expanded or not. If so, push appropriate arguments on the stack, if not, let it merge with previous results. In the case of fibonacci, once fib(n-2) is computed (and is available at top of stack), n-1 is retrieved (one after top of stack), result of fib(n-2) is pushed under it, and then fib(n-1) is expanded and computed. If the top two elements of the stack were both results, of course, you just add them and push to stack.

如果您想查看自己的函数的外观,请看这里:

If you'd like to see how your own function would look like, here it is:

function f(n, i)
    stack.empty()
    stack.push(n)
    stack.push(i)
    while (!stack.is_empty())
        argi = stack.pop()
        argn = stack.pop()
        if argn <= argi
            result = argn
        else if n % i = 0
            stack.push(n / i)
            stack.push(i)
        else
            stack.push(n)
            stack.push(i + 1)
    return result

这篇关于如何用堆栈模拟递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆