简体   繁体   English

二叉树递归InOrder方法混淆

[英]Binary Tree Recursive InOrder method confusion

I am working on a Red Black tree for a data structures class. 我正在为数据结构类的红黑树工作。

I have the code: 我有代码:

void printInorder(Node node)    //Input is initially the root node 
{ 
     if (node == null) 
     { 
           return; 
     } 
     printInorder(node.left); 
     System.out.println(node.data); 
     printInorder(node.right); 
} 

Let's use this Binary tree as an example: 我们以这个二叉树为例:

           50 
          /    \ 
       40     60 
      /    \       \ 
     20    45      70 
        /   \      / 
        43  47  65 

The output of the code is correct and is : 20 40 43 45 47 50 60 65 70 代码的输出是正确的,并且是:20 40 43 45 47 50 60 65 70

My understanding of the code is that it would recursively call printInorder(node.left) until it reached 20. 我对代码的理解是它会递归调用printInorder(node.left)直到达到20。

At that point, it would then print "20 ", then it checks printInorder(node.right), sees that it is null and returns back to the printInorder(node.right) statement, at which point it is at the bottom of the method and has no more code to run so it exits. 此时,它将打印“20”,然后它检查printInorder(node.right),看到它为null并返回到printInorder(node.right)语句,此时它位于方法,没有更多的代码可以运行,因此它退出。

The output is correct, but from my understanding of the code it should stop after printing "20 ". 输出是正确的,但根据我对代码的理解,它应该在打印“20”后停止。

Can someone explain the process of this recursive loop, LITERALLY step-by-step for me? 有人可以逐步解释这个递归循环的过程,对我来说是一步一步的吗? Please pretend you are explaining it to someone with a mental impediment. 请假装你正在向有精神障碍的人解释。 Thank you. 谢谢。

There is this joke "To understand recursion one should first understand recursion" 有这个笑话“要理解递归,首先应该了解递归”

First lets look at the function. 首先让我们来看看这个功能。 What does it do? 它有什么作用?

  • Checks if there is a left node and if there is goes there (without printing yet) 检查是否有左节点以及是否有去的地方(尚未打印)

  • If there is no left node prints 如果没有左节点打印

  • checks if there is a right node and goes there. 检查是否有正确的节点然后去那里。

So the execution here. 所以在这里执行。

  1. 1st execution of printInorder(50) Checks if has left - goes to the left node (printing waits) printInorder的第一次执行(50)检查是否已经离开 - 进入左侧节点(打印等待)
  2. 2nd execution now with the left node printInorder(40) Checks if has left node - yes it does! 现在用左节点printInorder(40)执行第二次执行检查是否已经离开节点 - 是的! Go to the left an wait with the print 向左转到打印等待
  3. 3rd execution with left node printInorder(20) Does it have a left node? 使用左节点执行第3次执行printInorder(20)是否有左节点? No! 没有! Calls printInorder(null) and continues the execution. 调用printInorder(null)并继续执行。 Now 20 is printed! 现在打印20! Does it have right node? 它有正确的节点吗? No 没有
  4. We go back! 我们回去吧! to step 2 where we had printinorder(40) but now we are at the point AFTER going to the leftNode. 到第2步我们有printinorder(40),但现在我们在转到leftNode之后。 So we print that 40 and check for right node - voila 45 is found! 所以我们打印那个40并检查正确的节点 - 找到了瞧!
  5. Go to 45 and check if has left node (printing 45 waits). 转到45并检查是否已离开节点(打印45等)。 Left node is 43 左节点是43
  6. Goes to printinorder(43) and since it has no left prints it! 去打印订单(43),因为它没有左打印它!

and so it goes... 所以它......

call printInorder (node 50) // root
  call printInorder (40) // left child of 50
    call printInorder (20) // left child of 40
      call printInorder (null) // left child of 20
      print 20
      call printInorder (null) // right child of 20
    print 40
    call printInorder (45) // right child of 40
      call printInorder (43) // left child of 45
        call printInorder (null) // left child of 43
        print 43
        call printInorder (null) // right child of 43
      print 45
      call printInorder (47) // right child of 45
        call printInorder (null) // left child of 47
        print 47
        call printInorder (null) // right child of 47
print 50
call printInorder (60) // right child of 50
  ...

and so on 等等

My understanding of the code is that it would recursively call printInorder(node.left) until it reached 20. 我对代码的理解是它会递归调用printInorder(node.left)直到达到20。

Correct. 正确。

At that point, it would then print "20 ", then it checks printInorder(node.right), sees that it is null and returns back to the printInorder(node.right) statement ... 此时,它将打印“20”,然后它检查printInorder(node.right),看到它为null并返回到printInorder(node.right)语句...

This is where you have missed a crucial point. 这是你错过关键点的地方。

It doesn't return back to the printInOrder(node.right) it returns back to printInOrder(40){ ... System.out.println(node.data); ...} 它不会返回到printInOrder(node.right)而是返回printInOrder(40){ ... System.out.println(node.data); ...} printInOrder(40){ ... System.out.println(node.data); ...} followed by printInOrder(40){ ... printInOrder(node.right); ...} printInOrder(40){ ... System.out.println(node.data); ...}后跟printInOrder(40){ ... printInOrder(node.right); ...} printInOrder(40){ ... printInOrder(node.right); ...} . printInOrder(40){ ... printInOrder(node.right); ...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM