【发布时间】:2021-02-07 11:11:44
【问题描述】:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node(*new_node)(int);
struct node(*insert)(struct node*,int);
int search(struct node*, int);
int main()
{
struct node* root;
while(1)
{
int choice,data;
printf("Enter Operation Choice To Perform\n1.Insert\n3.Search\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter data");
scanf("%d",&data);
root=insert(root, data);
break;
case 3: printf("Enter Data To Search\n");
scanf("%d",&data);
search(root,data);
break;
case 5: exit(0);
break;
default:printf("INVALID INPUT");
}
}
}
struct node(*new_node)(int data)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=data;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}
struct node (*insert)(struct node* root,int data)
{
if(root==NULL)
root=new_node(data);
else if(data<=root->data)
root->left=insert(root->left,data);
else
root->right=insert(root->right,data);
return root;
}
int search(struct node* root, int data)
{
if(root==NULL) return 0;
else if(root->data==data) return 1;
else if(data<=root->data) return search(root->left,data);
else return search(root->right,data);
}
这是一个基本的二叉树插入和搜索,有人可以帮助修复此代码以摆脱以下我似乎无法解决的错误。 --->从类型'struct node'分配给类型'struct node *'时不兼容的类型 --->在 '{' 标记
之前应有 '='、','、';'、'asm' 或 'attribute'【问题讨论】:
-
为什么你的函数名有括号??
标签: c algorithm binary-tree