Binary Search tree can be defined as a class of binary trees, in which the nodes are arranged in a specific order. This is also called ordered binary tree
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left,*right;
}bTree;
bTree *Tree;
void create(bTree**);
void insert(bTree**,int);
void delete(bTree**,int);
void inorder(bTree**);
void main()
{
int op,n;
while(1)
{
printf("\n1:CREATE\n 2.insert\n 3.delete\n 4.exit");
printf("\n Enter an option\n");
scanf("%d",&op);
switch(op)
{
case 1:create(&Tree);
break;
case 2: printf("\nEnter an element");
scanf("%d",&n);
insert(&Tree,n);
break;
case 3:printf("\nEnter an element to delete");
scanf("%d",&n);
delete(&Tree,n);
break;
case 4:inorder(&Tree);
break;
case 5:exit(0);
break;
}
}
}
void create(bTree **tree)
{
*tree=NULL;
}
void insert(bTree **tree,int n)
{
bTree *temp;
if(*tree==NULL)
{
temp=((bTree*)malloc(sizeof(bTree)));
temp->data = n;
temp->left = NULL;
temp->right = NULL;
*tree=temp;
}
else if((*tree)->data<n)
{
insert(&((*tree)->right),n);
}
else if((*tree)->data>n)
{
insert(&((*tree)->left),n);
}
else
printf("\nDuplicates are not allowed");
}
void delete(bTree **tree,int n)
{
bTree *temp;
if(*tree==NULL)
printf("No node is created yet");
else if((*tree)->data>n)
{
delete(&(*tree)->left,n);
}
else if((*tree)->data<n)
{
delete(&(*tree)->right,n);
}
else{
if((*tree)->left==NULL)
{
*tree=(*tree)->right;
}
else if((*tree)->right==NULL)
{
*tree=(*tree)->left;
}
else{
temp=(*tree)->left;
*tree = (*tree)->right;
while((*tree)->left !=NULL)
*tree = (*tree)->left;
(*tree)->left = temp;
free(temp);
printf("%d element deleted",n);
}
}
}
void inorder(bTree **tree)
{
if(*tree!=NULL)
{
inorder(&((*tree)->left));
printf("%3d",(*tree)->data);
inorder(&((*tree)->right));
}
}
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left,*right;
}bTree;
bTree *Tree;
void create(bTree**);
void insert(bTree**,int);
void delete(bTree**,int);
void inorder(bTree**);
void main()
{
int op,n;
while(1)
{
printf("\n1:CREATE\n 2.insert\n 3.delete\n 4.exit");
printf("\n Enter an option\n");
scanf("%d",&op);
switch(op)
{
case 1:create(&Tree);
break;
case 2: printf("\nEnter an element");
scanf("%d",&n);
insert(&Tree,n);
break;
case 3:printf("\nEnter an element to delete");
scanf("%d",&n);
delete(&Tree,n);
break;
case 4:inorder(&Tree);
break;
case 5:exit(0);
break;
}
}
}
void create(bTree **tree)
{
*tree=NULL;
}
void insert(bTree **tree,int n)
{
bTree *temp;
if(*tree==NULL)
{
temp=((bTree*)malloc(sizeof(bTree)));
temp->data = n;
temp->left = NULL;
temp->right = NULL;
*tree=temp;
}
else if((*tree)->data<n)
{
insert(&((*tree)->right),n);
}
else if((*tree)->data>n)
{
insert(&((*tree)->left),n);
}
else
printf("\nDuplicates are not allowed");
}
void delete(bTree **tree,int n)
{
bTree *temp;
if(*tree==NULL)
printf("No node is created yet");
else if((*tree)->data>n)
{
delete(&(*tree)->left,n);
}
else if((*tree)->data<n)
{
delete(&(*tree)->right,n);
}
else{
if((*tree)->left==NULL)
{
*tree=(*tree)->right;
}
else if((*tree)->right==NULL)
{
*tree=(*tree)->left;
}
else{
temp=(*tree)->left;
*tree = (*tree)->right;
while((*tree)->left !=NULL)
*tree = (*tree)->left;
(*tree)->left = temp;
free(temp);
printf("%d element deleted",n);
}
}
}
void inorder(bTree **tree)
{
if(*tree!=NULL)
{
inorder(&((*tree)->left));
printf("%3d",(*tree)->data);
inorder(&((*tree)->right));
}
}