Advertisement
kunalpanchal

Tree

Oct 31st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7.        int data;
  8.        node *left;
  9.        node *right;
  10.        };
  11.        node  *head,*n1;
  12.  
  13. void create(node *root ,node *n1)
  14. {
  15.                                                          if(head==NULL)
  16.                                                          {
  17.                                                             head=new node;
  18.                                                             head->data=n1->data;
  19.                                                             head->left=NULL;
  20.                                                             head->right=NULL;
  21.                                                             cout<<"root added";
  22.                                                             return;
  23.                                                          }
  24.                                                    
  25.      if((n1->data)<= (root->data))
  26.      {      
  27.                         if(root->left!=NULL)
  28.                          {
  29.                             create(root->left,n1);
  30.                          }
  31.                          else
  32.                         {
  33.                             //n1->data=ite;
  34.                             root->left=n1;
  35.                             root->left->left=NULL;
  36.                             root->left->right=NULL;
  37.                             cout<<"left node added";
  38.                             return;                    
  39.                         }
  40.     }
  41.     else
  42.     {               if(root->right!=NULL)
  43.                      {
  44.                          create(root->right,n1);
  45.                      }
  46.                      else
  47.                      {
  48.                       //n1->data=ite;
  49.                       root->right=n1;
  50.                       root->right->left=NULL;
  51.                       root->right->right=NULL;
  52.                        cout<<"right node added";
  53.                       return;
  54.                      }
  55.     }          
  56.                                                                              
  57. }
  58.    
  59. void post(node *n1)
  60. {
  61.      if(n1!=NULL)
  62.      {            
  63.                  post(n1->left);
  64.                  post(n1->right);
  65.                  cout<<"\t"<<n1->data;
  66.      }
  67. }
  68.  
  69. int main()
  70. {   int it;
  71.     n1= new node;
  72.    
  73.     char y;
  74.     do
  75.     {
  76.         cout<<"Enter r";
  77.     cin>>n1->data;
  78.    create(head,n1);
  79.      cout<<"\nenter more?(Y/N)";
  80.         cin>>y;
  81. }
  82.    while(y=='y');
  83.      post(head);
  84.    getch();
  85.    return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement