C语言中的二叉树崩溃
我开始在我的C类中学习二叉树。我理解二叉树的概念,但现在我试图对它的工作原理有更深入的了解。我试图建立一个简单的二叉树,根据用户输入的内容改变大小。每当我运行程序时,它在第一次输入后崩溃。有人能帮我理解为什么吗C语言中的二叉树崩溃,c,function,crash,binary-tree,dynamic-memory-allocation,C,Function,Crash,Binary Tree,Dynamic Memory Allocation,我开始在我的C类中学习二叉树。我理解二叉树的概念,但现在我试图对它的工作原理有更深入的了解。我试图建立一个简单的二叉树,根据用户输入的内容改变大小。每当我运行程序时,它在第一次输入后崩溃。有人能帮我理解为什么吗 #include <stdio.h> #include <stdlib.h> typedef struct node { int value; struct node *left; struct node *right; }node;
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int value;
struct node *left;
struct node *right;
}node;
void traverse(node *root);
void addnode (node *root, int nUser);
int checknode (node *root, int nUser);
int main (void)
{
int nUser;
node *root;
root=(node *)malloc(sizeof(node));
root->value=10;
do
{
printf("Please enter any integer, enter 0 when done\n\n\n"); /*Program crashes when I enter the first value, unsure why*/
scanf("%d",&nUser);
if(!(checknode(root,nUser)))/*check node runs through the binary tree to find the data if it exists, returns 1 if exists, 0 if it doesn't*/
addnode(root,nUser); /*add node runs through the binary tree, when it finds a place where the number will fit, that is a null pointer where the number can be placed it will create a new node with the value and null pointers on right and left*/
}while(nUser);
traverse(root);/*should traverse the tree and print out values*/
return(0);
}
void traverse(node *root)
{
printf("%d/n",root->value);
if(root->left)
{
traverse(root->left);
}
if(root->right)
{
traverse(root->right);
}
}
void addnode (node *root, int nUser)
{
if(root->value<nUser)
if(root->right)
{
addnode(root->right,nUser);
}
else
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->value=nUser;
root->right=temp;
}
if(root->value>nUser)
if(root->left)
{
addnode(root->left,nUser);
}
else
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->value=nUser;
root->left=temp;
}
}
int checknode (node *root, int nUser)
{
if(!(root->value==nUser))
{
if(root->value<nUser)
if(root->right)
{
checknode(root->right,nUser);
}
else
{
return(0);
}
if(root->value>nUser)
if(root->left)
{
checknode(root->left,nUser);
}
else
{
return(0);
}
}
else
{
return (1);
}
}
#包括
#包括
类型定义结构节点
{
int值;
结构节点*左;
结构节点*右;
}节点;
无效遍历(节点*根);
void addnode(节点*根,int用户);
int checknode(节点*根,int用户);
内部主(空)
{
int用户;
节点*根;
root=(node*)malloc(sizeof(node));
根->值=10;
做
{
printf(“请输入任何整数,完成后输入0\n\n”);/*当我输入第一个值时,程序崩溃,不确定原因*/
scanf(“%d”,用户(&N);
如果(!(checknode(root,用户)))/*check node在二叉树中运行以查找数据(如果存在),则返回1,如果不存在,则返回0*/
addnode(root,user);/*add node在二叉树中运行,当它找到一个适合数字的位置时,即一个可以放置数字的空指针,它将创建一个新节点,其值和空指针位于左右两侧*/
}而(1),;
遍历(根);/*应遍历树并打印出值*/
返回(0);
}
无效遍历(节点*根)
{
printf(“%d/n”,根->值);
如果(根->左)
{
遍历(根->左);
}
如果(根->右)
{
遍历(根->右);
}
}
void addnode(节点*根,int用户)
{
if(root->valueright)
{
addnode(root->right,用户);
}
其他的
{
节点*温度;
temp=(node*)malloc(sizeof(node));
温度->值=用户;
根->右=温度;
}
如果(根->值>用户)
如果(根->左)
{
addnode(root->left,用户);
}
其他的
{
节点*温度;
temp=(node*)malloc(sizeof(node));
温度->值=用户;
根->左=温度;
}
}
int checknode(节点*根,int用户)
{
如果(!(根->值==用户))
{
if(root->valueright)
{
选中节点(根->右,用户);
}
其他的
{
返回(0);
}
如果(根->值>用户)
如果(根->左)
{
选中节点(根->左,用户);
}
其他的
{
返回(0);
}
}
其他的
{
申报表(1);
}
}
改为:
root=(node *)calloc(1, sizeof(node));
您没有将
root->left
和root->right
清除为NULL,因此您使用的是未初始化的指针。您应该将所有malloc
更改为calloc
,或者您需要在每次malloc
之后将左指针和右指针清除为NULL。我认为问题在于,当您将root传递到addnode
时,root的左指针和右指针未初始化。malloc
返回的缓冲区不保证为空
在循环开始之前,尝试将root->right
和root->left
初始化为NULL
root=(node *)calloc(1, sizeof(node));