C 中的递归和树搜索?
对树和递归函数有点陌生......
我知道如何创建堆栈以及如何创建递归函数的基础知识。
我正在进行预排序遍历搜索,当搜索的值与树中节点的值匹配时,该搜索应该返回该节点的地址。
我在返回部分遇到问题...我尝试阅读调用堆栈上的一些内容...但我不明白如何实现它。它已经存在还是我必须制作这个堆栈?如果我必须制作这个堆栈,我该如何制作它?我读到它需要与树的高度成正比...是找到树的高度以创建另一个执行此操作的函数的最佳方法吗?
这是我迄今为止编写的一些代码: Tree和NodePtr是指向节点的指针......
NodePtr SearchTree(int v, Tree T)
{
//printf(" %i \n", T->value);
if(T->value == v)
{
return T;
}
else
{
if(T->Left != NULL) SearchTree(value, T->Left);
if(T->Right != NULL) SearchTree(value, T->Right);
}
return NULL;
}
Kind of new to trees and recursive functions....
I know the basics of how to create a stack and how to create recursive functions.
I am making a pre-ordered traversal search that should return the address of a node in a tree when the value being searched for matches the value of that node.
I'm having trouble with the return part...I tried reading some things on call stacks...but I don't understand how to implement it. Is it already there or do I have to make this stack? How do I go about making this stack if I have to make it? I read it needs to be proportional to the height of the tree... Is the best way to find the height of the tree to make another function that does this?
Here is some code I've written so far:
Tree and NodePtr is a pointer to a node...
NodePtr SearchTree(int v, Tree T)
{
//printf(" %i \n", T->value);
if(T->value == v)
{
return T;
}
else
{
if(T->Left != NULL) SearchTree(value, T->Left);
if(T->Right != NULL) SearchTree(value, T->Right);
}
return NULL;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,参见 递归。
现在,您想要返回对
SearchTree()
的递归调用返回的内容,因为您正在使用递归将问题委托给您自己的两个实例,因此如果您没有在上游获取返回值,没有什么是不可能的:First of all, see recursion.
Now, you want to return what the recursive calls to
SearchTree()
return, because you're using recursion to delegate the problem into two instances of yourself, so if you don't get your return value upstream, nothing can't possibly work:您无需担心调用堆栈;它的工作原理被 C 编译器隐藏,除非您正在搜索非常深的树,否则它不会打扰您。
您的算法或多或少是正确的,但在访问另一个子树之前,您需要检查递归调用的返回值是否为空。像这样的东西:
You don't need to worry about the call stack; how it works is hidden by the C compiler, and unless you are searching incredibly deep trees it won't bother you.
Your algorithm is more or less correct, but you need to check the returned value from a recursive call to see if it is null, before you visit the other subtree. Something like:
您不需要自己创建调用堆栈。它是一种数据结构,由执行程序的运行时环境维护,例如操作系统或 Java 虚拟机(如果您使用 Java)。每当调用函数时,其参数和局部变量都会被压入堆栈。当函数存在时,它们会被弹出。
作为一名程序员,您通常不必担心这一点。了解调用堆栈是什么有助于准确理解递归函数调用(或任何函数调用)期间发生的情况,或者了解局部变量来自哪里或函数退出后它们会发生什么。
You do not need to create the call stack yourself. It is a data structure that is maintained by the runtime environment in which your program is executed, such as your operating system, or the java virtual machine, if you are using Java. Whenever a function is called its arguments and its local variables are pushed onto the stack. When the function exists, they are popped off.
You, as a programmer, generally do not have to worry about it. It helps to know what the call stack is to understand exactly what happens during recursive function calls (or any function calls), or to understand where you local variables come from or what happens to them after your function exits.
Ira Baxter 的答案和 Frédéric Hamidi 的答案中的所有特殊外壳都表明设计不太正确:
我恭敬地认为,这是更简单的。当然,在处理 NULL 叶节点时,它会多进行一次函数调用,但分散的“if null”测试较少。
另外,我认为函数的参数(原版中名义上是 Tree)与 NodePtr 的类型相同,因此我选择仅使用 NodePtr 而不是 Tree。树由作为树的根节点的 NodePtr 表示。
All the special casing in Ira Baxter's answer and Frédéric Hamidi's answer suggest the design is not quite right:
This is, I respectfully submit, simpler. Granted, it makes one more function call when dealing with a NULL leaf node, but there are fewer 'if null' tests scattered around.
Also, I think that the argument to the function (nominally a Tree in the original) is the same type as a NodePtr, so I've opted to use just NodePtr and not Tree too. A tree is represented by the NodePtr that is the root node of the tree.