[英]Program crashes when inserting a node in a binary tree
I created the following library to insert,delete,search and print nodes in a binary tree. 我创建了以下库来插入,删除,搜索和打印二叉树中的节点。
#include <stdlib.h>
struct NODE
{
int code;
char subject[20];
struct NODE *left;
struct NODE *right;
};
void InOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
printf("%d %s\n",R->code,R->subject);
InOrder(R->right);
}
void PreOrder(struct NODE *R)
{
if (R==NULL)
return;
printf("%d %s\n",R->code,R->subject);
InOrder(R->left);
InOrder(R->right);
}
void PostOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
InOrder(R->right);
printf("%d %s\n",R->code,R->subject);
}
struct NODE *Search(struct NODE *R,int CODE,struct NODE **father)
{
if(R==NULL)
return NULL;
if(R->code==CODE)
{
*father=R;
return R;
}
if (CODE<R->code)
return Search(R->left,CODE,father);
else
return Search(R->right,CODE,father);
}
struct NODE * CreateNode(struct NODE T)
{
struct NODE *tmp;
tmp=(struct NODE *)malloc(sizeof(T));
*tmp=T;
tmp->left=tmp->right=NULL;
return tmp;
}
int Insert(struct NODE **R,struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur=Search(*R,ND.code,&fath);
if (cur)
return 0;
cur=CreateNode(ND);
if(fath==NULL)
*R=cur;
else
if(fath->code>ND.code)
fath->left=cur;
else
fath->right=cur;
return 1;
}
struct NODE *MinOfMax (struct NODE *ND)
{
struct NODE *tmp;
if (ND==NULL)
return NULL;
if(ND->right==NULL)
return NULL;
tmp=ND->right;
while(tmp->left!=NULL)
tmp=tmp->left;
return tmp;
}
struct NODE* Delete(struct NODE *R, int code)
{
if (R==NULL)
return R;
if (code<R->code)
R->left=Delete(R->left,code);
else if (code>R->code)
R->right=Delete(R->right,code);
else
{
if (R->left==NULL)
{
struct NODE *temp=R->right;
free(R);
return temp;
}
else if (R->right==NULL)
{
struct NODE *temp=R->left;
free(R);
return temp;
}
struct NODE *temp=MinOfMax(R->right);
R->code=temp->code;
R->right=Delete(R->right,temp->code);
}
return R;
}
When i try to insert a node in the binary tree,the program crashes.Here is my main: 当我尝试在二叉树中插入一个节点时,程序崩溃。这是我的主要:
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE tmp;
Insert(&root,tmp);
return 0;
}
I tried to assign static values (for example code=100 and subject="Physics") but still the program crashes.Should i malloc something,change anything in my header file or do something entirely different?I'm stuck here for hours without finding any solution.Most insert functions out there assume that i only have one integer as data in the node,but i need to pass the entire node. 我试图分配静态值(例如代码= 100和subject =“物理”)但仍然程序崩溃。我应该malloc什么,改变我的头文件中的任何东西或做一些完全不同的事情?我被困在这里几个小时没有找到任何解决方案。那里的大多数插入函数假设我只有一个整数作为节点中的数据,但我需要传递整个节点。
Your tmp
node, which is going to be the newly inserted node is used uninitialized in your main()
. 您的
tmp
节点(将成为新插入的节点)在main()
未初始化使用。 Your compiler could have warned you for this, if you had used -Wall
flag. 如果您使用了
-Wall
标志,那么您的编译器可能已经警告过您。
So let's take a look in your insert function: 那么让我们来看看你的插入函数:
int Insert(struct NODE **R, struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur = Search(*R, ND.code, &fath); // ND.code is junk, since ND is uninitialized
...
return 1;
}
which likely causes the segmentation fault. 这可能会导致分段错误。
root
is too, you could initialize it to NULL
in main()
. root
也是,你可以在main()
中将它初始化为NULL
。
Not the cause of your problem, but Do I cast the result of malloc? 不是你的问题的原因,但我是否施放了malloc的结果? No.
没有。
Your code basically does nothing. 您的代码基本上什么都不做 It seems you copy-pasted it from somewhere.
看来你是从某个地方复制粘贴的。 I tried to figure it out and here's a code example.
我试图搞清楚,这是一个代码示例。 Basically you've to initializate a new node in the main when you try to insert it.
基本上,当您尝试插入主节点时,您必须初始化主节点。 Note that's just an example, i didn't a full test.
请注意,这只是一个例子,我没有完整的测试。
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE *tmp = malloc(sizeof(struct NODE));
tmp->code = 1; /*Just a number*/
strcpy(tmp->subject,"prova"); /*Put something in it*/
Insert(&root,*tmp); /* Try to insert it*/
PreOrder(root); /*Try to see if it has been inserted*/
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.