Thursday, June 27, 2013

Binary Tree – C++.




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

       class node
      {
           private:
              int data;
              node *left;
              node *right;
              node *root;
            int Ctr;
           public:
              node()
                   {
                   Ctr=0;
                        root=NULL;
                   }

               void preorder(node *);
               void inorder(node *);
               void postorder(node *);
              node *insert(node *root,int val)
                   {
                        if(root==NULL)
                            {
                                root=new node();
                                root->left=root->right=NULL;
                                root->data=val;
                                Ctr++;
                            }
                        else if(Ctr%2==0)
                     {
                                root->left=insert(root->left,val);
                        }
                        else
                        {
                                root->right=insert(root->right,val);
                        }
                        return(root);
                   }
      };
  void node::inorder(node *root)
      {
           if(root!=NULL)
              {
                   inorder(root->left);
                   cout<<root->data;
                 inorder(root->right);
              }
       }

  void node::preorder(node *root)
      {
           if(root!=NULL)
              {
                   cout<<root->data;
                   preorder(root->left);
                   preorder(root->right);
              }
       }

  void node::postorder(node *root)
      {
           if(root!=NULL)
              {
                   postorder(root->left);
                   postorder(root->right);
                   cout<<root->data;
              }
       }

  int main()
      {
           node nD,*tree=NULL;
           int ch,val;
           do
              {
                   cout<<"\n\t\t***Binary Tree.***";
                   cout<<"\n\t1.Insert New Node.";
                   cout<<"\n\t2. PreOrder Traversal.";
                   cout<<"\n\t3. InOrder Traversal.";
                   cout<<"\n\t4. PostOder Traversal.";
                   cout<<"\n\t5. Exit.";
                   cout<<"\n\tChoice ? ";
                   cin>>ch;
                   switch(ch)
                        {
                            case 1:
                                cout<<"\n\tEnter Element : ";
                                cin>>val;
                                tree=nD.insert(tree,val);
                                break;
                            case 2:
                                 cout<<"\n\t **Pre-Order Traversal ** ";
                                 nD.preorder(tree);
                                 break;
                             case 3:
                                 cout<<"\n\t **In-Order Traversal ** ";
                                 nD.inorder(tree);
                                 break;
                             case 4:
                                 cout<<"\n\t **Post-Order Traversal ** ";
                                 nD.postorder(tree);
                                 break;
                             case 5:
                                 exit(0);
                        }
              }while(ch!=5);
           return 0;
      }

No comments: