Blogger news

Followers

Thursday, October 31, 2013
Write a C++ program to create a class called BIN_TREE(Binary Tree) with member function to perform inorder,preorder and postorder traversals.Create a BIN_TREE object and demonstrate the traversals
-----
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
node *llink;
node *rlink;
};
class tree
{
  node *root;
 public:
  tree()
  {
   root=NULL;
   }
   void inorder(node *);
   void preorder(node *);
   void postorder(node *);
   void display(int);
   void create(int);
  };
void tree::display(int choice)
{
  switch(choice)
  {
    case 2:
                if(root==NULL)
                  cout<<"tree is empty"<<endl;
                else
                 {
                   cout<<"preorder traversal is "<<endl;
                   preorder(root);
                 }
                 break;
    case 3:
                if(root==NULL)
                  cout<<"tree is empty"<<endl;
                else
                {
                  cout<<"inorder traversal is"<<endl;
                  inorder(root);
                }
                break;
    case 4:
       if(root==NULL)
                cout<<"tree is empty"<<endl;
       else
       {
                 cout<<"Postorder traversal is"<<endl;
                 postorder(root);
       }
       break;
    }
  }

  void tree::inorder(node *root)
  {
    if(root!=NULL)
    {
      inorder(root->llink);
      cout<<root->info<<endl;
      inorder(root->rlink);
    }
  }

  void tree::preorder(node *root)
  {
    if(root!=NULL)
    {
      cout<<root->info<<endl;
      preorder(root->llink);
      preorder(root->rlink);
    }
  }

  void tree::postorder(node *root)
  {
    if(root!=NULL)
    {
      postorder(root->llink);
      postorder(root->rlink);
      cout<<root->info<<endl;
    }
 }

 void tree::create(int item)
 {
   node *temp,*cur,*prev;
   temp=new node;
   temp->info=item;
   temp->llink=NULL;
   temp->rlink=NULL;

   if(root==NULL)
   {
     root=temp;
     return;
   }
   cur=root;
   prev=NULL;
   while(cur!=NULL)
   {
     prev=cur;
     if(item<cur->info)
       cur=cur->llink;
     else
       cur=cur->rlink;
   }
   if(item<prev->info)
      prev->llink=temp;
   else
      prev->rlink=temp;

 }
 void main()
 {
   tree ob;
   int item,choice;
   clrscr();

     cout<<"1:insert 2:preoder"<<endl;
     cout<<"3:inoder 4:postorder"<<endl;
     cout<<"5: exit \n";
      for(;;)
   {
     cout<<"enter the choice \n";
     cin>>choice;

     switch(choice)
     {
       case 1:
                      cout<<"enter the item";
                      cin>>item;
                      ob.create(item);
                      break;
      case 2:
      case 3:
      case 4:
                     ob.display(choice);
                     break;
      default:exit(0);
   }
 }
}
End programs

<![if !vml]><![endif]><![if !vml]><![endif]>Screenshots:

0 comments: