[英]Why does this function called: “showTree” work correctly?
This is the function that I don't understand:这是我不明白的function:
void showTree (node * tree, int cont) {
if (tree == NULL) {
return;
}
else {
showTree (tree-> right, cont + 1);
for (int i = 0; i <cont; i ++) {
cout << " ";
}
cout << tree-> data << endl;
showTree (tree-> left, cont + 1);
}
}
I do not understand:我不明白:
How do you get to the for loop if that function is always called recursively?如果 function 总是被递归调用,你如何进入 for 循环?
How are tree items displayed if that function is always called recursively?如果 function 总是被递归调用,树项目如何显示?
If someone could explain to me how this feature works I would really appreciate it.如果有人可以向我解释此功能是如何工作的,我将不胜感激。
How do you get to the for loop if that function is always called recursively?
如果 function 总是被递归调用,你如何进入 for 循环?
The function returns immediately if tree == NULL
, so it may continue onto the for
loop.如果
tree == NULL
,则 function 立即返回,因此它可能会继续进入for
循环。
How are tree items displayed if that function is always called recursively?
如果 function 总是被递归调用,树项目如何显示?
The output is produced by the for
loop and the next statement, so same reasoning. output 是由
for
循环和下一条语句产生的,所以同理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.