递归节点

发布于 2024-12-09 05:41:54 字数 1210 浏览 0 评论 0原文

我试图在我的课程中了解如何阅读下面名为 printBackward 的函数。当我输入 printBackward(node1) 时,我的输出是 3,2,1,这就是它应该做的事情,这是怎么回事?我只是不明白为什么会这样。请参阅下面我如何解释它,请告诉我我在哪里看到错误...

 class Node:
    def __init__(self, cargo = None, next = None): # optional parameters. cargo and the link(next) are set to None.
        self.cargo = cargo
        self.next = next

    def __str__(self):
        return str(self.cargo)


node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3

# Exercise
def printList(node):

    print "[",
    while node:
        print node,
        node = node.next
        if node != None:
            print ",",
    print "]",
    print


def printBackward(list):
    if list == None: return
    head = list      
    tail = list.next 
    printBackward(tail) 
    print head,

所以让我们说... printBackward(node1) 首先,if list 应该是由于node1包含对node2的引用,因此被忽略,因此我们移动到head = list,即node1。 tail = list.next 我将其视为node1.next = node2,因此tail = node2。然后我们到达printBackward(tail),即node2。到那时,会发生什么?我们要重新做一遍吗?我看到这会上升到node3,此时将返回 None 。我们什么时候到达打印头???我们在到达打印头之前就进行了递归调用,?请教育我,因为我正在尝试理解课程中提供给我的示例。谢谢!

I'm trying to understand within my lesson how to read the below function called printBackward. How is it that when I type printBackward(node1) and my output is 3,2,1 which is what it's suppose to do. I just don't understand why that is. See below how I interpret it and please school me on where I saw it wrong...

 class Node:
    def __init__(self, cargo = None, next = None): # optional parameters. cargo and the link(next) are set to None.
        self.cargo = cargo
        self.next = next

    def __str__(self):
        return str(self.cargo)


node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3

# Exercise
def printList(node):

    print "[",
    while node:
        print node,
        node = node.next
        if node != None:
            print ",",
    print "]",
    print


def printBackward(list):
    if list == None: return
    head = list      
    tail = list.next 
    printBackward(tail) 
    print head,

So let's say... printBackward(node1) at first, if list should be ignored since node1 contains a reference to node2 so we move to head = list which is node1. tail = list.next which I see as node1.next = node2 so tail = node2. Then we get to printBackward(tail)which is node2. At that point, what happens? Do we do this all over again? I see this going up to node3 which at that point will return None. When do we get to print head,??? We make a recursive call before even getting to the print head,? Please educate me as I'm trying to understand the examples that are given to me within my lesson. Thanks!

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

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

发布评论

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

评论(2

沫雨熙 2024-12-16 05:41:54

您对调用 printBackward(node3) 之前发生的所有事情都是正确的。发生的情况是,每次进行递归 printBackward() 调用时,都会深入调用堆栈。直到递归停止调用 printBackward() 然后展开时,最终的 print 才会真正被调用。当它每次返回时,print 就会被调用,这就是为什么你会得到向后的顺序。 print 发生在调用堆栈展开期间。
当您到达 node3 时,tail 变为 None,并且下一次调用 printBackwards() 会返回正确的值离开,打印开始。

printBackward(node1)
    printBackward(node2)
        printBackward(node3)
            printBackward(None)
        print node3
    print node2
print node1

还有一点需要注意的是,您正在隐藏一些内置的 python 名称(listnext)。

You are correct about everything that is happening leading up to when printBackward(node3) is called. What is going on is every time you get to the recursive printBackward() call, you go deeper into the call stack. The final print doesn't actually get called until the recursion stops calling printBackward(), and then unwinds. As it returns each time, THEN the print is called, which is why you get the backwards order. The prints happen during the unwinding of the call stack.
When you get to node3, tail becomes None, and that next call to printBackwards() is what returns right away, and the printing begins.

printBackward(node1)
    printBackward(node2)
        printBackward(node3)
            printBackward(None)
        print node3
    print node2
print node1

Also a small note, you are shadowing a few built-in python names (list and next).

无名指的心愿 2024-12-16 05:41:54

递归就是调用函数本身。那么让我们看看 printBackward 函数的调用顺序。

printBackward(node1)
 |
 +-> printBackward(node2)
      |
      +-> printBackward(node3)
           |
           +-> printBackward(None)
          print node3
     print node2
print node1

如您所见, printBackward1 是用 node1 作为参数调用的。在打印node1之前,它将控制流交给printBackward(node2)。当 printBackward(node2) 完成时,它会打印 node1。

Recursion is calling function itself. So let's see printBackward function's calling order.

printBackward(node1)
 |
 +-> printBackward(node2)
      |
      +-> printBackward(node3)
           |
           +-> printBackward(None)
          print node3
     print node2
print node1

As you can see, printBackward1 is called with node1 as argument. Before it prints node1, it gives the control flow to printBackward(node2). And when printBackward(node2) is finished, it prints node1.

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