## Saturday, January 28, 2012

### C PROGRAM TO IMPLEMENT BINARY SEARCH TREE

ALGORITHM TO IMPLEMENT BINARY SEARCH TREE

INSERTION
1.  t = newnode
2.  t   info = n
3.  t   left = t   right = NULL
4.  If (root = NULL)
root = t
return
5.  ptr = root
6.  Repeat step 7 until ptr = NULL
7.  If (ptr   info > n)
If (ptr   left = NULL)
Ptr  left = t
Return
Else
Ptr = ptr   left
Else
If (ptr  right = NULL)
Ptr  right = t
Return
Else
Ptr = ptr   right

DELETION
1.  If (root = NULL)
Print “Empty tree “
Return
2.  ptr = root, par = NULL
3.  Repeat step 4 & 5 until (ptr  info = n or ptr = NULL)
4.  par = ptr
5.  If (ptr  info > n)
ptr = ptr  left

Else
Ptr = ptr  right
6.  If ptr = NULL
print “ no. not present”

//BST
#include<stdio.h>
#include<conio.h>
struct rec
{

long num;
struct rec *left;
struct rec *right;
};
struct rec *insert(struct rec *tree,long num);
struct rec *copy(struct rec *tree);
void inorder(struct rec *tree);
main()
{
int choice;
long digit;
do
{
choice=select();
switch(choice)
{
case 1:puts("Enter integers:To quit enter 0");
scanf("%ld",&digit);
while(digit!=0)
{
tree=insert(tree,digit);
scanf("%ld",&digit);
}continue;
case 2: copy(tree);continue;
case 3: puts("Inorder traversing TREE");
inorder(tree);continue;
case 4: puts("END");exit(0);
}
}while(choice!=4);
}
int select()
{
int selection;
do
{
puts("Enter 1: Insert a node in the BST");
puts("Enter 2: Copy a tree to another BST");
puts("Enter 3: Display(inorder)the BST");
puts("Enter 4: END");
scanf("%d",&selection);
if((selection<1)||(selection>4))
{puts("Wrong choice: Try again");
getchar();
}
}while((selection<1)||(selection>4));
return selection;
}
struct rec *insert(struct rec *tree,long digit)
{
if(tree==NULL)
{
tree=(struct rec *)malloc(sizeof(struct rec));
tree->left=tree->right=NULL;
tree->num=digit;
}

else
if(digit<tree->num)
tree->left=insert(tree->left,digit);
else if(digit>tree->num)
tree->right=insert(tree->right,digit);
else if(digit==tree->num)
{puts("Duplicate nodes: program exited");exit(0);
}
return(tree);
}
struct rec *copy(struct rec *tree)
{
second=(struct rec *)malloc(sizeof(struct rec));
if(tree!=NULL)
{
second->num=tree->num;
if(tree->left!=NULL)
{
second->left->num=tree->left->num;
copy(tree->right);
}
if(tree->right!=NULL)
{
second->right->num=tree->num;
copy(tree->left);
}
}