Wednesday 13 June 2012

Binary Search Tree in C ++


/*
* Performing various operation on Binary search tree
* Author : Ravi Kiran
* Date : 4 April 2011
—————–Steps——————————–
* Create :- Create Binary Search tree
* Perform Various operations on Binary Search tree created
* Display the Binary search tree
*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

void insert(struct node*, int);
int MIN(struct node*);
int MAX(struct node*);
void search(struct node*, int);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);

struct node
{
int data;
node *left;
node *right;
};
struct node*root = NULL;

void main()
{
int a, ele;
char ans;
clrscr();
cout<<"\n\n\nDo you wish to continue?"<<endl;
cin>>ans;
while((ans=='y') || (ans =='Y'))
{
cout<<"What operation do you wish to perform ...?"<<endl;
cout<<"Please enter your choice ..."<<endl;
cout<<"\n\n\n\t\t\t******MAIN MENU******"<<endl;
cout<<"\n\n\n\t\t\t1.INSERT"<<endl;
cout<<"\t\t\t3.SEARCH"<<endl;
cout<<"\t\t\t4.MIN"<<endl;
cout<<"\t\t\t5.MAX"<<endl;
cout<<"\t\t\t6.INORDER TRAVERSING"<<endl;
cout<<"\t\t\t7.POSTORDER TRAVERSING"<<endl;
cout<<"\t\t\t8.PREORDER TRAVERSING"<<endl;
cout<<"\t\t\t9.EXIT"<<endl;
cin>>a;
switch(a)
{
case 1:
cout<<"\nPlease enter the element to be inserted\n";
cin>>ele;

if(root==NULL)
{
root=(struct node*)malloc(sizeof(struct node));
root->data=ele;
root->left=root->right=NULL;
}
else
insert(root,ele);
break;
case 2:
cout<<"\nPlease enter the element to be searched\n";
cin>>ele;
search(root,ele);
break;
case 3:cout<<"The MIN value of the tree is:";
cout<<MIN(root)<<endl;
break;
case 4:cout<<"The MAX value of the tree is:" ;
cout<< MAX(root)<<endl;
break;
case 5:
cout<<"\nPerforming INORDER traversal\n";
inorder(root);
break;
case 6:
cout<<"\nPerforming POSTORDER traversal\n";
postorder(root);
break;
case 7:
cout<<"\nPerforming PREORDER traversal\n";
preorder(root);
break;
case 8:
exit(0);
default: cout<<"\nOops! Wrong choice ....!";
}
}
getch();
}
void insert(struct node* root, int element)
{
if(element<root->data)
{
if(root->left==NULL)
{
struct node* newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=element;
newnode->left=newnode->right=NULL;
root->left=newnode;
}
else
insert(root->left,element)  ;

}
else if(element>root->data)
{
if(root->right==NULL)
{
struct node* newnode;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=element;
newnode->left=newnode->right=NULL;
root->right=newnode;
}
else
insert(root->right,element);
 }
}

void search(struct node *root, int element)
{
if(root->data==element)
cout<<"\t\tFound...! "<<endl<<endl<<endl;
else if(root->left!=NULL)
{
search(root->left,element);
}
else if(root->right!=NULL)
{
search(root->right,element);
}
else
cout<<"Element not found ...!"<<endl;
}

int MIN(struct node *root)
{
int min=root->data;
while(root->left!=NULL)
{
root=root->left;
min=root->data;
}
return(min);

}

int MAX(struct node *root)
{
int max=root->data;
while(root->right!=NULL)
{
root=root->right;
max=root->data;
}
return(max);
}

void inorder(struct node *root)
{
if(root!=NULL)
{
if(root->left!=NULL)
inorder(root->left);
cout<<root->data<<endl;
if(root->right!=NULL)
inorder(root->right);
}
else
cout<<"OOps ! Tree is empty"<<endl;
}

void preorder(struct node* root)
{
if(root!=NULL)
{
cout<<root->data;
if(root->left!=NULL)
inorder(root->left);
if(root->right!=NULL)
inorder(root->right);
}
else
cout<<"OOps ! Tree is empty"<<endl;
}


void postorder(struct node* root)
{
if(root!=NULL)
{
if(root->left!=NULL)
inorder(root->left);
if(root->right!=NULL)
inorder(root->right);
cout<<root->data;
}
else
cout<<"OOps ! Tree is empty"<<endl;
}

No comments:

Post a Comment