I've made a binary search tree
struct BTNode
{
int info;
struct BTNode *left,*right;
};
I've wrote a code to insert a node in the tree
void insert(struct BTNode *root,int data)
{
struct BTNode *ptr;
struct BTNode *n=malloc(sizeof(struct BTNode));
n->info=data;
n->left=NULL;
n->right=NULL;
if(root==NULL)
root=n;
else{
ptr=root;
while(ptr!=NULL){
if(data<ptr->info){
if(ptr->left==NULL)
ptr->left=n;
else
ptr=ptr->left;
}
else if(data>ptr->info){
if(ptr->right==NULL)
ptr->right=n;
else
ptr=ptr->right;
}
}
}
}
and a main() function
int main()
{
struct BTNode *root=NULL;
int choice,data;
printf("\n1.Insert 2.Preorder 3.Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\nWrite the data: ");
scanf("%d",data);
insert(root, data);
break;
But when I try to insert a node, my program crashes. Any hints what's the problem?