Adrita

task 1( ds lab 7) 3rd sem

Jul 6th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int y;
  4. struct node
  5. {
  6. int data;
  7. struct node* left;
  8. struct node* right;
  9. };
  10. typedef struct node Node;
  11. Node* create_node(int item)
  12. {
  13. Node* new_node=new Node();
  14. if(new_node==NULL)
  15. cout<<"Error"<<endl;
  16. else
  17. {
  18. new_node->data=item;
  19. new_node->left=new_node->right=NULL;
  20. }
  21. return new_node;
  22. }
  23. void inorder(Node* x)
  24. {
  25. if(x!=NULL)
  26. {
  27. inorder(x->left);
  28. cout<<x->data<<" ";
  29. inorder(x->right);
  30. }
  31. }
  32. Node* insert_key(Node* x,int key)
  33. {
  34. Node* new_node=create_node(key);
  35. if(x==NULL)
  36. return new_node;
  37. if(key>x->data+3)
  38. {
  39. x->right=insert_key(x->right,key);
  40. }
  41. else if(key<x->data-3)
  42. {
  43. x->left=insert_key(x->left,key);
  44. }
  45. else
  46. y=0;
  47. return x;
  48. }
  49. int main()
  50. {
  51. Node* root=NULL;
  52. while(1)
  53. {
  54. int n;
  55. cin>>n;
  56. y=1;
  57. if(n!=-1)
  58. {
  59. root=insert_key(root,n);
  60. inorder(root);
  61. if(y==0)
  62. cout<<"(Reservation failed)"<<endl;
  63. else
  64. cout<<endl;
  65. }
  66. else
  67. break;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment