I am coding for binary search tree . I am sure that this code is good as the compiler does not throw any errors . But at run time i get the SEGMENTATION FAULT , CORE DUMPED error . Can anyone please help me out ?
I am using CYGWIN IN WINDOWS XP.
Code:
#include<stdio.h>
#include<malloc.h>
typedef struct nodetype
{
int data;
struct nodetype *left;
struct nodetype *right;
}node;
void showtree(node *root);
node * insert(node *root,int key);
int main()
{
node *root=NULL;
root=insert(root,5);
root=insert(root,3);
root=insert(root,4);
root=insert(root,3);
root=insert(root,2);
root=insert(root,7);
root=insert(root,6);
root=insert(root,8);
showtree(root);
return 0;
}
node *insert(node *root,int key)
{
if(!root)
{
root=(node*)malloc(sizeof(node));
root->data=key;
root->left=NULL;
root->right=NULL;
}
else if (key<root->data)
{
root->left=insert(root->left,key);
}
else if (key>root->data)
{
root->right=insert(root->right,key);
}
return root;
}
void showtree(node *root)
{
showtree(root->left);
printf("%d/n",root->data);
showtree(root->right);
}