简体   繁体   English

为什么这个名为“showTree”的 function 可以正常工作?

[英]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.

相关问题 即使未使用指向指针的指针作为参数来调用函数,为什么仍能正常工作? - Why does this work even though the function is not called with a pointer to a pointer as a parameter? 如何找到RBM无法正常工作的原因? - How to find why a RBM does not work correctly? 为什么在 C++ 中使用关系运算符制作的模板函数对字符串不能正常工作? - Why does a Template function made using relational operator in C++ not work correctly for strings? 为什么我在 AVR 中的中断不能正常工作? - Why my interrupt in AVR does not work correctly? 为什么从外部访问类的成员会给出准确的结果,而从成员函数访问它却不能正常工作? - Why does accessing members of class from outside gives accurate results while accessing it from member function does not work correctly? “二进制搜索”的 Function 无法正常工作 - Function for "Binary Search" does not work correctly 为什么这个 c++ 程序不是 function 正确? - Why does this c++ program not function correctly? 为什么 copy_if 不能与 STL 向量一起正常工作? - Why does copy_if not work correctly with STL vectors? 为什么反转此功能不起作用 - Why does reverse this function not work 为什么在内联 function 中调用的 function 不需要定义? - Why does a function called inside a inline function not require definition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM