Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- using namespace std;
- struct node
- {
- int data;
- node *left;
- node *right;
- };
- node *head,*n1;
- void create(node *root ,node *n1)
- {
- if(head==NULL)
- {
- head=new node;
- head->data=n1->data;
- head->left=NULL;
- head->right=NULL;
- cout<<"root added";
- return;
- }
- if((n1->data)<= (root->data))
- {
- if(root->left!=NULL)
- {
- create(root->left,n1);
- }
- else
- {
- //n1->data=ite;
- root->left=n1;
- root->left->left=NULL;
- root->left->right=NULL;
- cout<<"left node added";
- return;
- }
- }
- else
- { if(root->right!=NULL)
- {
- create(root->right,n1);
- }
- else
- {
- //n1->data=ite;
- root->right=n1;
- root->right->left=NULL;
- root->right->right=NULL;
- cout<<"right node added";
- return;
- }
- }
- }
- void post(node *n1)
- {
- if(n1!=NULL)
- {
- post(n1->left);
- post(n1->right);
- cout<<"\t"<<n1->data;
- }
- }
- int main()
- { int it;
- n1= new node;
- char y;
- do
- {
- cout<<"Enter r";
- cin>>n1->data;
- create(head,n1);
- cout<<"\nenter more?(Y/N)";
- cin>>y;
- }
- while(y=='y');
- post(head);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement