Friday, 7 December 2012

Binary tree operations using cpp program

Program illustrates the implementation of various operations on a binary tree implemented using an array



#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>

const int MAX=100;

class SeqTree
{ private:
          char *tree;
 public:
          SeqTree(void);
          int IsNull(void);
          void create(char,char,char);
          void preorder(int);
          void inorder(int);
          void postorder(int);
          void levelorder(void);
          int height(void);
          int nodes(void);

};

SeqTree::SeqTree()
{        tree=new char[MAX];
          for(int i=0;i<MAX;i++)
                   tree[i]=NULL;
}

int SeqTree::IsNull(void)
{        if(tree[0]==NULL)

                   return 1;
          else
                   return 0;
}

void SeqTree::create(char ele,char par,char lr)
{        int flag=0;
          if(this->IsNull())
          {        tree[0]=ele;
                    return;
          }
          for(int i=0;i<MAX;i++)
                    if(par==tree[i])
                   {        flag=1;
                             if((lr=='l')||(lr=='L'))
                             {        if((2*i+1)>MAX-1)
{        cout<<endl<<"ArrayOverflow. Can't add the node"<<endl;
                                                return;
                                      }
                             if(tree[2*i+1]!=NULL)
{        cout<<"The node "<<par<<" already has a left child. Insertion of"  <<ele<<" is aborted";
                                       return;
                             }
                             tree[2*i+1]=ele;
                   }
                   else
                   {        if((2*i+2)>MAX-1)
{        cout<<endl<<"Array Overflow. Can't add the node"<<endl;
                                       return;
                             }
                    if(tree[2*i+2]!=NULL)
{       cout<<"The node "<<par  <<" already has a right child. Insertion of"  <<ele<<" is aborted";
                             return;
                    }
                    tree[2*i+2]=ele;
                   }
          }
          if(!flag)
          cout<<endl<<"The parent node"<<par<<" is not found in the tree"<<endl;
return;
}

void SeqTree::preorder(int i)
{        if((tree[i]!=NULL)&&(i<MAX))
          {        cout<<tree[i]<<" ";
                   preorder(2*i+1);
                   preorder(2*i+2);
          }                          
          return;
}

void SeqTree::inorder(int i)
{        if((tree[i]!=NULL)&&(i<MAX))
          {        inorder(2*i+1);
                   cout<<tree[i]<<" ";
                   inorder(2*i+2);
          }
          return;
}

void SeqTree::postorder(int i)
{        if((tree[i]!=NULL)&&(i<MAX))
{        postorder(2*i+1);
                   postorder(2*i+2);
                   cout<<tree[i]<<" ";
          }
          return;
}
void SeqTree::levelorder(void)
{        for(int i=0;i<MAX;i++)
          if(tree[i]!=NULL)
                   cout<<tree[i]<<" ";
          return;
}
int SeqTree::nodes(void)
{        int count=0;
          for(int i=0;i<MAX;i++)
                   if(tree[i]!=NULL)count++;
          return count;
}
int SeqTree::height(void)
{        int height=0;
          for(int i=0;i<MAX;i++)
                   if(tree[i]!=NULL)height=i+1;
          return(int(log10(height)/log10(2)));
}
void main()
{        int c;
          SeqTree t;
          while(1)
          {        clrscr();
                   cout<<"\nPerform the Following Operations on a Tree Data Structure";
                   cout<<"\n---------------------------------------------------------"<<endl;
                   cout<<"\n1.Input nodes to Tree";
                   cout<<"\n2.Preorder Traversal";
                   cout<<"\n3.Inorder Traversal";
                   cout<<"\n4.Postorder Traversal";
                   cout<<"\n5.Levelorder Traversal";
                   cout<<"\n6.Height of Tree";
                   cout<<"\n7.Nodes in the Tree";
                   cout<<"\n8.Exit from Program";
                   do
                   {        cout<<"\n\nSelect your Choice(1:8):";
                             cin>>c;
                             cout<<" ";
                   }
                   while((c<1)||(c>8));
          if(c==8)exit(1);
          clrscr();
          switch(c)
          { case 1:
                   clrscr();
                   char element;
                   char parent=NULL;
                   char lr=NULL,rep;
                    do
                   {        cout<<"\nEnter the element to insert:";
                             cin>>element;
                             if(t.IsNull())
cout<<endl<<element<<" is the parent node"<<endl;
                             else
                                      {        cout<<"Enter the parent of "<<element<<" in the tree: \n";
                                      cin>>parent;
                                      do
{        cout<<"Insert as Left child or Right child(L/R):";
                                                cin>>lr;
                                                if((lr!='l')&&(lr!='L')&&(lr!='r')&&(lr!='R'))
                                                cout<<"Invalid Data.Please re-enter"<<endl;
                                      }
                                      while((lr!='l')&&(lr!='L')&&(lr!='r')&&(lr!='R'));
                             }
                             t.create(element,parent,lr);
                    cout<<endl<<"\nAdd More(Y/N):";
                   cin>>rep;
                   }
                   while((rep=='y')||(rep=='Y'));
                   break;
      case 2:
                   clrscr();
                   cout<<"The result of preorder traversal is"<<endl;
                   t.preorder(0);
                   break;
      case 3:
                   clrscr();
                   cout<<"The result of inorder traversal is  "<<endl;
                   t.inorder(0);
                   break;
      case 4:
                   clrscr();
                   cout<<"The result of postorder traversal is"<<endl;
                   t.postorder(0);
                   break;
      case 5:
                   clrscr();
                   cout<<"The result of levelorder traversal is"<<endl;
                   t.levelorder();
                   break;
      case 6:
                   clrscr();
                   cout<<"The Height of the tree is"<<t.height();
                   t.height();
                   break;
      case 7:
                   clrscr();
                   cout<<"The number of elements in the tree is"<<t.nodes();
                   t.nodes();
                   break;
          }
          cout<<endl<<"Press Any Key to Continue...";
          getch();
    }
}

No comments:

Post a Comment