Binary Search Tree
Binary Search Tree
getch();
}
struct node*insert(struct node*r, int x)
{
if (r == NULL)
{
r = (struct node*)malloc(sizeof(struct node));
r->info = x;
r->left = r->right = NULL;
return r;
}
else if (x < r->info)
r->left = insert(r->left, x);
else if (x > r->info)
r->right = insert(r->right, x);
return r;
getch();
}
void inorder(struct node* r)
{
//
printf("Inorder Traversal: ");
if(r!=NULL)
{
inorder(r->left);
printf("%d ", r->info);
printf("\n");
inorder(r->right);
}
}
void preorder(struct node* r)
{
//printf("Preorder Traversal: ");
if(r!=NULL)
{
printf("%d ", r->info);
printf("\n");
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node* r)
{
//printf("Postorder Traversal: ");
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
printf("\n");
printf("%d ", r->info);
}
}
int main()
{
struct node* root = NULL;
int x, c = 1, z;
int element;
char ch;
printf("\nEnter an element in Binary Search tree\n: ");
scanf("%d", &x);
root = insert(root, x);
printf("\nDo you want to enter another element :y or n");
}
}
return 0;