Friday, November 4, 2011

Binary Tree

Here the Program given Below Does all of the Following Functions In BINARY TREE:
  1. Inserting a Element
  2. Deleting a Element
  3. Preorder
  4. Inorder
  5. Postorder
  6. Searching an Element
(NOTE: the Inorder, PreOrder,PostOrder are in recursion ONLY!!!)

Thanks to தமிழரசு for the book DATA  STRUCTURES USING C - P.RadhGanesan from where this Program is written

#include<iostream>

#include<stdlib.h>


using namespace std;



struct node

{

int d;

struct node *left;

struct node *right;

};



struct node *root;


void init();

void empty();

int insert(int);

void search(int);

void print(int);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
void remove(int);
 



void init()

{

root=(struct node *)malloc(sizeof(struct node));

root=NULL;

}



int insert(int x)

{

struct node *p,*previous,*current;


p=(struct node *)malloc(sizeof(struct node));



 if (p==NULL)

 {

 cout<<"\nOut of Memory";

 return 0;

 }


p->d=x;

p->left=NULL;

p->right=NULL;


 if(root==NULL)

 {

root=p;

 return 1;

 }
 


previous=NULL;

current=root;



 while (current!=NULL)

 {

 previous=current;

 
if (p->d<current->d)
 
  current=current->left;

  else
 
  current=current->right;

 }



if(p->d<previous->d)
 
previous->left=p;

else
 
previous->right=p;


return 1;


}



void inorder(struct node *p)
{
 if(p!=NULL)
 {
 inorder(p->left);
 cout<<"\t"<<p->d;
 inorder(p->right);
 }
}

void preorder(struct node *p)
{
 if(p!=NULL)
 {
 cout<<"\t"<<p->d;
 preorder(p->left);
 preorder(p->right);
 }
}

void postorder(struct node *p)
{
 if(p!=NULL)
 {
 postorder(p->left);
 postorder(p->right);
 cout<<"\t"<<p->d;
 }
}

void remove(int x)
{
struct node *ptr=root, *parent = NULL, *t1, *t2, *temp;
 while(ptr!=NULL && ptr->d!=x)
 {
 parent=ptr;
 if(x<ptr->d)
  ptr=ptr->left;
 else
  ptr=ptr->right;
 }

 if(ptr==NULL)
 {
 cout<<"\nDelete Element Not found";
 return;
 }
 
 if(ptr->left==NULL)
  t1=ptr->right;
 else if(ptr->right==NULL)
  t1=ptr->left;
 else
 {
 t2=ptr;
 t1=ptr->right;
 temp=t1->left;
 
  while(temp!=NULL)
  {
  t2=t1;
  t1=temp;
  temp=t1->left;
  }
 
  if(t2!=ptr)
  {
  t2->left=t1->right;
  t1->right=ptr->right;
  }

  t1->left=ptr->left;
 }

 if(parent==NULL)
  root=t1;
 else
 {
  if(parent->left==ptr)
  parent->left=t1;
  else
  parent->right=t1;
 }

free(ptr);
}


void print(int x)
{
if(x==1)
 preorder(root);
else if(x==2)
 inorder(root);
else
 postorder(root);
}


void search(int sno)

{

struct node*t;

t=(struct node *)malloc(sizeof(struct node));

t=root;



 while(t!=NULL && t->d !=sno)

 {

if(t->d>sno)
{ t=t->left; }

else
 { t=t->right; }

}



if(t)

cout<<"\nThe Search Element is Present";

else

cout<<"\nElement not Present";

}




int main()

{

int c,no,x;

init();



do

{

cout<<"\n1.Insert";

cout<<"\n2.PreOrder";

cout<<"\n3.InOrder";
cout<<"\n4.PostOrder";
cout<<"\n5.Deletion";
cout<<"\n6.Search";
cout<<"\n7.Exit";

cout<<"\nEnter Your Choice:\t";

cin>>c;



switch(c)

{

case 1: cout<<"\nEnter the Element to Insert:\t";
       
        cin>>no;

        x=insert(no);

        if(x==1)

         cout<<"\nsucessful Insertion";

        else

         cout<<"\nError in insertion";

        break;

case 2:if(root!=NULL)
       {
       cout<<"\nPreOrder is:";
       print(1);
       cout<<"\n";
       }
       else
       cout<<"\nNo Element to Display";
       break;
case 3:if(root!=NULL)
       {
       cout<<"\nInOrder is:";
       print(2);
       cout<<"\n";
       }
       else
       cout<<"\nNo Element to Display";
       break;
case 4:if(root!=NULL)
       {
       cout<<"\nPostOrder is:";
       print(3);
       cout<<"\n";
       }
       else
       cout<<"\nNo Element to Display";
       break;
case 5:cout<<"\nEnter the Element to Delete:";
       cin>>x;
       remove(no);
       break;
case 6: cout<<"\nEnter the Element to Search: \t";

        cin>>no;

        search(no);

        break;

case 7: exit(0); break;

default: cout<<"\nEnter Any No. Between 1-7"; break;

}


}while(c!=7);


return 0;

}



No comments:

Post a Comment