[英]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. 所以在这里执行。
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.