i want to make binary tree program in c, but i am getting segmentation fault error.. my code is this...
Code:
#include <stdio.h>#include <stdlib.h>
typedef struct node {
int data;
struct node* left;
struct node* right;
}node;
int n;
int main()
{
node *root, *first, *current, *temp; int num,i;
printf("How many no.\n");
scanf("%d", &n);
first = root;
printf("Enter no.");
scanf("%d", &root->data);
for(i=1;i<n;i++) {
current=first;
temp = (node*)malloc(sizeof(node));
printf("Enter no.");
scanf("%d", &num);
temp->data=num;
if(num<current->data) {
current->left=temp;
}
else current->right=temp;
first=current;
}
return 0;
}
i am getting error at this line
Code:
temp = (node*)malloc(sizeof(node));
can anyone tell me what is wrong. and how can i write crud operation on binary tree