C 中的递归和树搜索?

发布于 2024-10-01 01:14:39 字数 617 浏览 4 评论 0原文

对树和递归函数有点陌生......

我知道如何创建堆栈以及如何创建递归函数的基础知识。

我正在进行预排序遍历搜索,当搜索的值与树中节点的值匹配时,该搜索应该返回该节点的地址。

我在返回部分遇到问题...我尝试阅读调用堆栈上的一些内容...但我不明白如何实现它。它已经存在还是我必须制作这个堆栈?如果我必须制作这个堆栈,我该如何制作它?我读到它需要与树的高度成正比...是找到树的高度以创建另一个执行此操作的函数的最佳方法吗?

这是我迄今为止编写的一些代码: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽心 2024-10-08 01:14:39

首先,参见 递归

现在,您想要返回对 SearchTree() 的递归调用返回的内容,因为您正在使用递归将问题委托给您自己的两个实例,因此如果您没有在上游获取返回值,没有什么是不可能的:

NodePtr SearchTree(int v, Tree t)
{
    printf(" %i \n", t->value);
    if (t->value == v) {
        return t;
    } else {
        if (t->Left != NULL) {
            NodePtr left = SearchTree(v, t->Left);
            if (left != NULL) {
                return left;
            }
        }
        if (t->Right != NULL) {
            return SearchTree(v, t->Right);
        }
    }

    return NULL;
}

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:

NodePtr SearchTree(int v, Tree t)
{
    printf(" %i \n", t->value);
    if (t->value == v) {
        return t;
    } else {
        if (t->Left != NULL) {
            NodePtr left = SearchTree(v, t->Left);
            if (left != NULL) {
                return left;
            }
        }
        if (t->Right != NULL) {
            return SearchTree(v, t->Right);
        }
    }

    return NULL;
}
我是男神闪亮亮 2024-10-08 01:14:39

您无需担心调用堆栈;它的工作原理被 C 编译器隐藏,除非您正在搜索非常深的树,否则它不会打扰您。

您的算法或多或少是正确的,但在访问另一个子树之前,您需要检查递归调用的返回值是否为空。像这样的东西:

    if(T->Left != NULL) { NodePtr temp=SearchTree(value, T->Left); 
                          if (temp !=NULL)  return temp; 
                        } 
    if(T->Right != NULL) return SearchTree(value, T->Right); 

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:

    if(T->Left != NULL) { NodePtr temp=SearchTree(value, T->Left); 
                          if (temp !=NULL)  return temp; 
                        } 
    if(T->Right != NULL) return SearchTree(value, T->Right); 
恬淡成诗 2024-10-08 01:14:39

您不需要自己创建调用堆栈。它是一种数据结构,由执行程序的运行时环境维护,例如操作系统或 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.

够运 2024-10-08 01:14:39

Ira Baxter 的答案和 Frédéric Hamidi 的答案中的所有特殊外壳都表明设计不太正确:

NodePtr SearchTree(int v, NodePtr T)
{
    if (T == NULL || T->value == v)
        return T;
    NodePtr R = SearchTree(v, T->Left);
    if (R == NULL)
        R = SearchTree(v, T->Right);
    return R;
}

我恭敬地认为,这是更简单的。当然,在处理 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:

NodePtr SearchTree(int v, NodePtr T)
{
    if (T == NULL || T->value == v)
        return T;
    NodePtr R = SearchTree(v, T->Left);
    if (R == NULL)
        R = SearchTree(v, T->Right);
    return R;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文