【发布时间】:2020-12-31 22:23:07
【问题描述】:
我已经检查了百万次的代码但是这个错误不会出现请帮助,它是用于创建二叉搜索树的程序... 我正在使用 VS Code 进行编译
typedef struct node node;
node *newNode(int key){
node *temp=(node *)malloc(sizeof(node));
temp->info=key;
temp->left=temp->right=NULL;
return temp; }
void inorder(node *root){
if(root!=NULL){
inorder(root->left);
printf("%d\t",root->info);
inorder(root->right); } }
node *Insert(node *root,int key){
if(root=NULL){ return newNode(key);}
if(key<root->info){
root->left = Insert(root->left,key); }
else if(key>root->info){
root->right = Insert(root->right,key); }
return root;
}
int main(){
int n,v;
node *root=NULL;
printf("enter the no of values you wanna enter");
scanf("%d",&n);
int i = 0;
while(i<=n){
scanf("%d\n",&v);
root=Insert(root,v);
i++;
}
inorder(root);
return 0;
}
【问题讨论】:
-
那是什么:
if(root=NULL)?我认为应该是`if(root==NULL)`。 -
您的代码格式不一致且到处都是。如果您希望其他程序员阅读您的代码,请采用更传统的方式。我懒得破译这个并计算所有的大括号......
-
"assignment inside if" 通常会被警告捕获。在运行任何东西之前,您应该在启用警告的情况下进行编译并修复所有警告。
标签: c binary-search-tree insertion