[英]Binary search tree and structure
#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 '
和
struct node(*new_node)(int);
您将变量new_node
声明为指向 function 的指针,它接受一个参数并返回一个struct node
。
您想要的是将 function 声明(然后定义)为:
struct node*new_node(int);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.