Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct node
- {
- int info;
- struct node *left;
- struct node *right;
- };
- struct node *create(int val)
- {
- struct node *newnode=new node;
- newnode->info=val;
- newnode->left=newnode->right=NULL;
- return newnode;
- }
- void inorder( struct node *root ) {
- if (root != NULL) {
- inorder(root->left);
- cout<<root->info;
- inorder(root->right);
- }
- }
- main()
- {
- int flag=0,flag1=0;
- int val,i=0;
- char ch='Y';
- struct node *ptr=NULL,*avail,*newlist,*address;
- while(ch=='Y'|| ch=='y')
- {
- cout<<"enter the value to be inserted";cin>>val;
- if(ptr==NULL)
- {
- ptr=create(val);
- }
- avail=ptr;
- while(avail!=NULL&&i>0)
- {
- if(val==avail->info)
- break;
- flag=flag1=0;
- cout<<"loop entered";
- if(val < avail->info)
- {
- address=avail;
- avail=avail->left;
- flag=1;
- }
- else if(val>avail->info)
- {
- address=avail;
- avail=avail->right;
- flag1=1;
- }
- }
- if(flag==1)
- {
- newlist=create(val);
- address->left=newlist;
- }
- if(flag1==1)
- {
- newlist=create(val);
- address->right=newlist;
- }
- i=1;
- cout<<"Press Y or y to enter newnode";cin>>ch;
- }
- inorder(ptr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement