Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 二叉搜索树构造中的编译错误_C_Pointers_Data Structures_Linked List - Fatal编程技术网

C 二叉搜索树构造中的编译错误

C 二叉搜索树构造中的编译错误,c,pointers,data-structures,linked-list,C,Pointers,Data Structures,Linked List,我正在建立一个二叉搜索树。代码如下: #include<stdio.h> #include<stdlib.h> struct tree_node { int val; struct tree_node *left; struct tree_node *right; }; void insert(struct tree_node **, int); int main(void) { struct tree_node *tree;

我正在建立一个二叉搜索树。代码如下:

#include<stdio.h>
#include<stdlib.h>


struct tree_node
{
    int val;
    struct tree_node *left;
    struct tree_node *right;
};
void insert(struct tree_node **, int);
int main(void)
{
    struct tree_node *tree;

    tree = NULL;
    insert(&tree, 10);
    insert(&tree, 20);
    insert(&tree, 5);
    insert(&tree, 7);

    return 0;   
}

void insert(struct tree_node **tree1, int value)
{
    struct tree_node *temp, *start, *tem;
    tem = NULL;
    temp = (struct tree_node *)malloc(sizeof(struct tree_node) );
    start = *tree1;

    temp->val = value;
    temp->left = NULL;
    temp->right = NULL;

    if(*tree1 == NULL)
        *tree1 = temp;
    else
    {
        while(*tree1 != NULL )
        {
            if(value <= (*tree1)->val)
            {
                *tem = *tree1;
                *tree1 = (*tree1)->left;
            }
            else
            {
                tem = *tree1;
                *tree1 = (*tree1)->right;   
            }

        }
        if((tem->left) ==  *tree1)
            tem->left = temp;
        else
            tem->right = temp;
    }
    *tree1 = start;
}
为什么?

内部
if(value val)

应该是:

tem = *tree1;
您在
else
分支中有正确的版本。

if(value val)

应该是:

tem = *tree1;
您在
else
分支中有正确的版本。

if(value val)

应该是:

tem = *tree1;
您在
else
分支中有正确的版本。

if(value val)

应该是:

tem = *tree1;
您在
else
分支中有正确的版本。

此处:

void insert(struct tree_node **tree1, int value)
是指向指针的指针,并且
tem
是指针
因此,声明:

*tem = *tree1;
应该是:

tem = *tree1;
在这里:

void insert(struct tree_node **tree1, int value)
是指向指针的指针,并且
tem
是指针
因此,声明:

*tem = *tree1;
应该是:

tem = *tree1;
在这里:

void insert(struct tree_node **tree1, int value)
是指向指针的指针,并且
tem
是指针
因此,声明:

*tem = *tree1;
应该是:

tem = *tree1;
在这里:

void insert(struct tree_node **tree1, int value)
是指向指针的指针,并且
tem
是指针
因此,声明:

*tem = *tree1;
应该是:

tem = *tree1;


顺便说一下,您应该为
malloc()
包含
,而不是
#包含
两次
。当我们讨论这个主题时,您应该编写
temp=malloc(sizeof(*temp))你应该为
malloc()
包含
,而不是
。#顺便说一句,包含
两次。当我们讨论这个主题时,您应该编写
temp=malloc(sizeof(*temp))你应该为
malloc()
包含
,而不是
。#顺便说一句,包含
两次。当我们讨论这个主题时,您应该编写
temp=malloc(sizeof(*temp))你应该为
malloc()
包含
,而不是
。#顺便说一句,包含
两次。当我们讨论这个主题时,您应该编写
temp=malloc(sizeof(*temp))嘿,如何打印根值。我正在做printf(“%d”,tree->val);在main()中,但获取运行时错误。@Naman-这是正确的。temp->value将与该语句一起打印。@Yu Hao-我发布了我的,你的已经更新了。确认总是一件好事:)@ryker我写的是main。抱歉,这是tree->val。为什么运行时出错。实际上,我不知道如何使用Debugger调试程序。嘿,如何打印根值。我正在做printf(“%d”,tree->val);在main()中,但获取运行时错误。@Naman-这是正确的。temp->value将与该语句一起打印。@Yu Hao-我发布了我的,你的已经更新了。确认总是一件好事:)@ryker我写的是main。抱歉,这是tree->val。为什么运行时出错。实际上,我不知道如何使用Debugger调试程序。嘿,如何打印根值。我正在做printf(“%d”,tree->val);在main()中,但获取运行时错误。@Naman-这是正确的。temp->value将与该语句一起打印。@Yu Hao-我发布了我的,你的已经更新了。确认总是一件好事:)@ryker我写的是main。抱歉,这是tree->val。为什么运行时出错。实际上,我不知道如何使用Debugger调试程序。嘿,如何打印根值。我正在做printf(“%d”,tree->val);在main()中,但获取运行时错误。@Naman-这是正确的。temp->value将与该语句一起打印。@Yu Hao-我发布了我的,你的已经更新了。确认总是一件好事:)@ryker我写的是main。很抱歉是tree->val。为什么运行时出错。实际上,我不知道如何使用调试器调试程序