Friday, June 14, 2013

OPERATIONS ON BINARY TREE

OPERATIONS ON BINARY TREE (C++ CODE):

/* TITLE: TO IMPLEMENT BINARY TREE AND PERFORM OPERATIONS LIKE FINDING HEIGHT AND MIRROR IMAGE


#include<iostream.h>
#include<conio.h>
#define size 10

class tree
{
 private:
 typedef struct bin
 {
  int data;
  struct bin *left;
  struct bin *right;
 }node;

 public:
  node *New,*root;
  node *que[10];
  int front,rear;
  tree();
  void create();
  void insert(node *,node *);
  int height(node *root);
  void leaf(node *root,int *);
  void convert();
  void mirror(node *root);
  void display(node *root);
  void enque(node *temp);
  node *deque();
 };

 tree::tree()
 {
  root=NULL;
  front=rear=-1;
 }

 void tree::create()
 {
  char ans='y';
  do
  {
   New=new node;
   cout<<"Enter the element:\t";
   cin>>New->data;
   New->left=NULL;
   New->right=NULL;

   if(root==NULL)
    root=New;
   else
    insert(root,New);
    cout<<"\nDo u want to enter more elements?(y/n):\t";
    cin>>ans;
   }while(ans=='y'||ans=='Y');
   clrscr();

  }

  void tree::insert(node *root,node *New)
  {
   if(New->data>root->data)
   {
    if(root->right!=NULL)
    insert(root->right,New);
    else
    root->right=New;
   }

   else

    if(New->data<root->data)
    {
     if(root->left!=NULL)
     insert(root->left,New);
     else
     root->left=New;
    }
   }

   int tree::height(node *root)
   {
    int d1,d2;
    if(root==NULL)
    return 0;
    if(root->left==NULL&&root->right==NULL)
    return 0;
    d1=height(root->left);
    d2=height(root->right);
    if(d1>d2)
    return d1+1;
    else
    return d2+1;

   }

   void tree::leaf(node *root,int *count)
   {


    if(root!=NULL)
    {
     if((root->left==NULL)&&(root->right==NULL))
     {
      cout<<" "<<root->data;
      *count=*count+1;
     }
     else
     {
      leaf(root->left,count);
      leaf(root->right,count);
      }
     }
    }

    void tree::convert()
    {
     cout<<"\n Original image is...\n";
     display(root);
     mirror(root);
     cout<<"\nMirror image is created";
     cout<<"\nMirror image is...\n";
     display(root);
    }

    void tree::mirror(node *root)
    {
     node *temp;
     if(root!=NULL)
     {
      mirror(root->left);
      mirror(root->right);
      temp=root->left;
      root->left=root->right;
      root->right=temp;
     }
    }

    void tree::enque(node *temp)
    {
     if(rear==size-1)
     {
      cout<<"Queue is empty\n";
      return;
     }

     rear=rear+1;
     que[rear]=temp;
     }

     node* tree::deque()
     {
      node *temp;
      if(front==rear)
      {
       cout<<"Queue is empty";
       return NULL;
      }
      front++;
      temp=que[front];
      return temp;
      }

      void tree::display(node *root)
      {
       node *temp,*dummy;
       dummy=new node;
       front=rear=-1;
       if(dummy==NULL)
cout<<"Insufficient memory\n";
dummy->left=root;
dummy->right=NULL;
dummy->data=-999;
temp=dummy->left;
enque(temp);
enque(dummy);
temp=deque();
cout<<"\n";
while(front!=rear)
{
if(temp!=dummy)
{
 cout<<" "<<temp->data;
 if(temp->left!=NULL)
 enque(temp->left);
 if(temp->right!=NULL)
 enque(temp->right);
}
else
{
 enque(temp);
 cout<<"\n";
}
temp=deque();

}
       }

      void main()
      {
       int choice;
       tree obj;
       int ht;
       int *count;
       clrscr();
       do
       {
cout<<"\nProgram for implementing simple binary tree";
cout<<"\n1.Create";
cout<<"\n2.Height of binary tree";
cout<<"\n3.Leaf Nodes";
cout<<"\n4.Mirror image";
cout<<"\n5.Level-wise display";
cout<<"\n6.Exit";
cout<<"\n\tEnter your choice:";
cin>>choice;
switch(choice)
{
case 1:obj.create();
break;
case 2:ht=obj.height(obj.root);
cout<<"\The height of the tree is: "<<ht;
break;
case 3:*count=0;
cout<<"\n Leaf nodes are...\n";
obj.leaf(obj.root,count);
cout<<"\nThe total leaf nodes are: "<<*count;
break;
case 4:obj.convert();
break;
case 5:obj.display(obj.root);
getch();
break;

}
}while(choice<=5);
getch();
      }























































































No comments:

Post a Comment

Thanks for your valuable comment