Reputation: 1
#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);
}
this is a basic insert and search for binary tree, can someone please help fix this code to get rid off the following error I can't seem to solve. --->incompatible types when assigning to type 'struct node *' from type 'struct node' --->expected '=', ',', ';', 'asm' or 'attribute' before '{' token
Upvotes: 0
Views: 93
Reputation: 409472
With
struct node(*new_node)(int);
you declare the variable new_node
, as a pointer to a function which takes one argument and returns a struct node
.
What you want is to declare (and later define) the function as:
struct node*new_node(int);
Upvotes: 1