js递归函数算法_如何将递归函数或算法转换为非递归?

js递归函数算法

Any recursive function can be converted to non-recursive function
through use of a stack as explained below.
  • A recursive call is similar to a call to another function.
  • Any call to a function requires that the function has

    storage area where it can store its local variables and actual parameters.

  • Return address must be saved before a call is made to a function.
  • Storage area for local variables, actual parameters and the

    return address can be provided through a stack.

  • In case of a recursive call, the value of local variables,

    parameters and the return address must be saved on the stack.

  • While returning from a nested call, the previous outer call

    must be recalled with resetting all the local variables and operation must

    resume from where it was suspended.

Rules for converting
a recursive algorithm to non-recursive one:

Initialization:
  • Declare stack – It will hold local variables, parameters,

    return address, etc.

  • The first statement after the stack initialization must have

    a label.

Steps required to
replace a recursive call:
  • Push all local variables and parameters into the stack.
  • Push an integer i into the stack, i gives the return

    address.

  • Set the value of formal parameters.
  • Transfer the control to the beginning of the function (i.e.

    first label immediately after initialization

    of stack) using goto. Use of goto

    is not a good practice but here we have no choice.

  • There should always be a label statement immediately

    following the recursive call. This label is the return address.

Steps required at the
end of recursive function:
  • If the stack is empty, then the recursion is finished.
  • Otherwise, pop the stack to restore the values of all local

    variables and parameters called by value.

  • Pop the return address.
Any recursive function can be converted to non-recursive function
through use of a stack as explained below.
  • A recursive call is similar to a call to another function.
  • Any call to a function requires that the function has

    storage area where it can store its local variables and actual parameters.

  • Return address must be saved before a call is made to a function.
  • Storage area for local variables, actual parameters and the

    return address can be provided through a stack.

  • In case of a recursive call, the value of local variables,

    parameters and the return address must be saved on the stack.

  • While returning from a nested call, the previous outer call

    must be recalled with resetting all the local variables and operation must

    resume from where it was suspended.

Rules for converting
a recursive algorithm to non-recursive one:

Initialization:
  • Declare stack – It will hold local variables, parameters,

    return address, etc.

  • The first statement after the stack initialization must have

    a label.

Steps required to
replace a recursive call:
  • Push all local variables and parameters into the stack.
  • Push an integer i into the stack, i gives the return

    address.

  • Set the value of formal parameters.
  • Transfer the control to the beginning of the function (i.e.

    first label immediately after initialization

    of stack ) using goto. Use of goto

    is not a good practice but here we have no choice.

  • There should always be a label statement immediately

    following the recursive call. This label is the return address.

Steps required at the
end of recursive function:
  • If the stack is empty, then the recursion is finished.
  • Otherwise, pop the stack to restore the values of all local

    variables and parameters called by value.

  • Pop the return address.

翻译自: https://www.thecrazyprogrammer.com/2014/02/how-to-convert-recursive-function-or-algorithm-to-non-recursive.html

js递归函数算法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值