Tree 如何解决创建树的代码中的运行时错误。。。?
/*这是一个使用结构创建树的程序Tree 如何解决创建树的代码中的运行时错误。。。?,tree,runtime-error,Tree,Runtime Error,/*这是一个使用结构创建树的程序 #include<stdio.h> /函数在树中插入元素/ /顺序遍历/ //函数的递归调用// void inorder(struct tree *root){ if(root!=NULL){ inorder(root->left); printf("%d",root->data); inorder(root->right); } } 这可能是
#include<stdio.h>
/函数在树中插入元素/
/顺序遍历/
//函数的递归调用//
void inorder(struct tree *root){
if(root!=NULL){
inorder(root->left);
printf("%d",root->data);
inorder(root->right);
}
}
这可能是因为一棵树包含一棵树,其中包含一个树指针,其中包含一个树指针,等等…(即使您没有初始化或“分配”任何东西,CPP编译器也很奇怪)
如果没有它给您带来的错误,我就无能为力。这可能是因为树包含一棵树,其中包含一个树指针,其中包含一个树指针,等等……(即使您没有初始化或“分配”任何东西,CPP编译器也很奇怪)
如果没有它给你带来的错误,我做不了什么。你能给我一段正确的代码吗?尝试将引用设置为“NULL”,这样编译器就不会初始化它。示例:结构树*left=NULL;如果您向我提供运行时错误,我可能会提供更多帮助。上面的解决方法修复了一个坏编译器的堆栈溢出错误。你能给我一段正确的代码吗?尝试将引用设置为“NULL”,这样编译器就不会初始化它。示例:结构树*left=NULL;如果您向我提供运行时错误,我可能会提供更多帮助。上述解决方法修复了错误编译器的堆栈溢出错误。
void insert(struct tree *bt,int data)
{
if(bt==NULL){
bt=(struct tree*)malloc(sizeof(struct tree));
bt->data=data;
bt->left=NULL;
bt->right=NULL;
}
else if(data<bt->data)
insert(&(bt->left),data);
else if(data>bt->data)
insert(&(bt->right),data);
}
void main(){
struct tree *bt=NULL;
insert(&bt,1);
insert(&bt,6);
insert(&bt,12);
insert(&bt,87);
inorder(&bt);
}
void inorder(struct tree *root){
if(root!=NULL){
inorder(root->left);
printf("%d",root->data);
inorder(root->right);
}
}