Advertisement
Guest User

Untitled

a guest
May 29th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5. int info;
  6. struct node *left;
  7. struct node *right;
  8. };
  9. struct node *create(int val)
  10. {
  11. struct node *newnode=new node;
  12. newnode->info=val;
  13. newnode->left=newnode->right=NULL;
  14. return newnode;
  15. }
  16. void inorder( struct node *root ) {
  17. if (root != NULL) {
  18. inorder(root->left);
  19. cout<<root->info;
  20. inorder(root->right);
  21. }
  22.  
  23. }
  24.  
  25. main()
  26. {
  27. int flag=0,flag1=0;
  28. int val,i=0;
  29. char ch='Y';
  30. struct node *ptr=NULL,*avail,*newlist,*address;
  31. while(ch=='Y'|| ch=='y')
  32. {
  33. cout<<"enter the value to be inserted";cin>>val;
  34. if(ptr==NULL)
  35. {
  36. ptr=create(val);
  37. }
  38. avail=ptr;
  39.  
  40. while(avail!=NULL&&i>0)
  41. {
  42. if(val==avail->info)
  43. break;
  44. flag=flag1=0;
  45. cout<<"loop entered";
  46. if(val < avail->info)
  47. {
  48. address=avail;
  49. avail=avail->left;
  50. flag=1;
  51. }
  52. else if(val>avail->info)
  53. {
  54. address=avail;
  55. avail=avail->right;
  56. flag1=1;
  57. }
  58. }
  59.  
  60. if(flag==1)
  61. {
  62. newlist=create(val);
  63. address->left=newlist;
  64. }
  65. if(flag1==1)
  66. {
  67. newlist=create(val);
  68. address->right=newlist;
  69. }
  70.  
  71. i=1;
  72. cout<<"Press Y or y to enter newnode";cin>>ch;
  73.  
  74. }
  75. inorder(ptr);
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement