Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- node *left;
- node *right;
- };
- node* temp1=NULL;
- void printPre(node* root)
- {
- if(root==NULL)
- return;
- cout<<root->data<<" ";
- if(root->left !=NULL)
- {
- printPre(root->left);
- }
- if(root->right !=NULL)
- {
- printPre(root->right);
- }
- }
- void printIn(node* root)
- {
- if(root==NULL)
- return;
- if(root->left !=NULL)
- {
- printIn(root->left);
- }
- cout<<root->data<<" ";
- if(root->right !=NULL)
- {
- printIn(root->right);
- }
- }
- void printPost(node* root)
- {
- if(root==NULL)
- return;
- if(root->left !=NULL)
- {
- printPost(root->left);
- }
- if(root->right !=NULL)
- {
- printPost(root->right);
- }
- cout<<root->data<<" ";
- }
- node* bt(int y)
- {
- struct node *temp=new node();
- temp->data=y;
- temp->left=NULL;
- temp->right=NULL;
- if(temp1==NULL)
- {
- temp1=temp;
- }
- else
- {
- struct node *temp2=new node();
- temp2=temp1;
- while(true)
- {
- if(temp2->data>=y)
- {
- if(temp2->left==NULL)
- {
- temp2->left=temp;
- break;
- }
- else
- {
- temp2=temp2->left;
- }
- }
- else
- {
- if(temp2->right==NULL)
- {
- temp2->right=temp;
- break;
- }
- else
- {
- temp2=temp2->right;
- }
- }
- }
- }
- }
- int main()
- {
- int i,N,k,C,n=1;
- cin>>C;
- for(int f=0; f<C; ++f)
- {
- temp1=NULL;
- cin>>N;
- for(i=0; i<N; ++i)
- {
- cin>>k;
- bt(k);
- }
- printf("Case %d:\n",n);
- cout<<"Pre.:"<<" ";
- printPre(temp1);
- cout<<"\n";
- cout<<"In..:"<<" ";
- printIn(temp1);
- cout<<"\n";
- cout<<"Post:"<<" ";
- printPost(temp1);
- cout<<"\n";
- n++;
- cout<<"\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement