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。为什么运行时出错。实际上,我不知道如何使用调试器调试程序