关于递归的问题

发布于 2024-09-07 22:26:13 字数 244 浏览 4 评论 0原文

请建议我解决以下问题

考虑一个函数

function recurse(a):

  for child in a.childs:

      recurse(child)

现在我想执行一些代码,比如说

print "Program ends here"

程序完成递归时,那么我如何知道递归何时结束?

谢谢

Please suggest me the solution to the following problem

consider a function

function recurse(a):

  for child in a.childs:

      recurse(child)

Now I want to execute some code lets say

print "Program ends here"

when the program is done with recursion,so how can I know when the recursion will end?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光①夏 2024-09-14 22:26:13

到目前为止提出的各种答案,总结起来就是“在recurse之外进行”,都很好。但是,如果您热衷于内部递归,那也不难(只是效率稍低):

function recurse(a, _toplevel=True):
  for child in a.childs:
      recurse(child, False)
  if _toplevel:
      print "Recursion done!"

名称中的前导_ _toplevel 参数表示它是私有的,因此调用者知道传递它。

类似的解决方案涉及跟踪递归的级别(不仅仅是它是否是顶级,而是一般来说“你有多深”):

function recurse(a, _level=0):
  for child in a.childs:
      recurse(child, _level + 1)
  if _level == 0:
      print "Recursion done!"

虽然在其他情况下很珍贵(例如打印出嵌套结构 )缩进)这里不需要它(与我给出的基于 _toplevel 布尔值的第一个解决方案相比,还有另一个边际效率损失)。

The various answers proposed so far, which sum up to "do it outside of recurse", are fine. But if you're keen to do it inside recurse, that's not hard either (just marginally less efficient):

function recurse(a, _toplevel=True):
  for child in a.childs:
      recurse(child, False)
  if _toplevel:
      print "Recursion done!"

The leading _ in the name of the _toplevel argument indicates it's private, so callers know not to pass it.

Similar solutions involve keeping track of the level of recursion (not just whether it's top level or not, but "how deep you are" in general):

function recurse(a, _level=0):
  for child in a.childs:
      recurse(child, _level + 1)
  if _level == 0:
      print "Recursion done!"

While precious in other cases (e.g. printing out nested structures with indents) it's not needed here (and there's another marginal loss of efficiency compared with the first solution I gave, based on the _toplevel boolean).

难如初 2024-09-14 22:26:13

你的意思是这样吗?

recurse(something)
print "Program ends here"

递归函数完成后,程序将继续执行下一条语句,就像任何其他函数一样。该函数是递归的并不重要。

Do you mean like this?

recurse(something)
print "Program ends here"

When the recursive function is done, the program will continue with the next statement, just like with any other function. That the function was recursive doesn't matter.

情仇皆在手 2024-09-14 22:26:13

您可以使用默认参数:

function recurse(a, top=True):
    for child in a.childs:
        recurse(child, False)
    if top: print("Program ends here.")

recurse(a)

You could use a default parameter:

function recurse(a, top=True):
    for child in a.childs:
        recurse(child, False)
    if top: print("Program ends here.")

recurse(a)
一世旳自豪 2024-09-14 22:26:13

我可能误解了你的问题,但递归将在最高级别完成时完成。

包装 recurse() 的新函数可以执行此操作:

function do_recursion(a):
    res = recurse(a)
    print "Program ends here"
    return res

I may be misunderstanding your question, but the recursion will be done when the highest level completes.

A new function that wraps recurse() could do this:

function do_recursion(a):
    res = recurse(a)
    print "Program ends here"
    return res
冧九 2024-09-14 22:26:13

将 print 语句放在第一次调用 recurse 之后。

def main():
    a = NodeOfSomeSort()
    recurse(a)
    print "Recursion done!"        

Put the print statement after the first call to recurse.

def main():
    a = NodeOfSomeSort()
    recurse(a)
    print "Recursion done!"        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文