Adrita

task 4( ds lab 7) 3rd sem

Jul 6th, 2020 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 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. Node* insert_key(Node* x,int key)
  24. {
  25. Node* new_node=create_node(key);
  26. if(x==NULL)
  27. return new_node;
  28. if(key>x->data)
  29. {
  30. x->right=insert_key(x->right,key);
  31. }
  32. else if(key<x->data)
  33. {
  34. x->left=insert_key(x->left,key);
  35. }
  36. return x;
  37. }
  38. Node * findmin(Node* node)
  39. {
  40. struct node* current = node;
  41. while (current && current->left != NULL)
  42. current = current->left;
  43.  
  44. return current;
  45. }
  46. Node * findmax(Node* node)
  47. {
  48. Node* current = node;
  49. while (current && current->right != NULL)
  50. current = current->right;
  51.  
  52. return current;
  53. }
  54. void find_pre_suc(Node* root, Node*& pre, Node*& suc, int val)
  55. {
  56. if (root == NULL)
  57. return ;
  58. if (root->data == val)
  59. {
  60. Node* temp;
  61. if (root->left != NULL)
  62. {
  63. temp = root->left;
  64. pre = findmax(temp) ;
  65. }
  66. if (root->right != NULL)
  67. {
  68. temp = root->right ;
  69. suc = findmin(temp) ;
  70. }
  71. return ;
  72. }
  73. if (root->data>val)
  74. {
  75. suc = root ;
  76. find_pre_suc(root->left, pre, suc, val) ;
  77. }
  78. else
  79. {
  80. pre = root ;
  81. find_pre_suc(root->right, pre, suc, val) ;
  82. }
  83. }
  84.  
  85. bool find_node(Node* node, int val)
  86. {
  87. if (node == NULL)
  88. return false;
  89. if (node->data == val)
  90. return true;
  91. if(find_node(node->left,val))
  92. return true;
  93. else
  94. return find_node(node->right,val);
  95. }
  96.  
  97. int main()
  98. {
  99.  
  100. Node* root=NULL;
  101. while(1)
  102. {
  103. int n;
  104. cin>>n;
  105. y=1;
  106. if(n!=-1)
  107. root=insert_key(root,n);
  108. else
  109. break;
  110. }
  111. int k;
  112. cout<<"Number of test-cases"<<endl;
  113. cin>>k;
  114. for(int j=0; j<k; j++)
  115. {
  116. int p;
  117. cin>>p;
  118. if(find_node(root,p)==false)
  119. cout<<"Reservation not found!"<<endl;
  120. else
  121. {
  122. Node* pre = NULL, *suc = NULL;
  123. find_pre_suc(root,pre,suc,p);
  124. if (pre != NULL)
  125. cout << pre->data <<" ";
  126. else
  127. cout << "NULL" <<" ";
  128. if (suc != NULL)
  129. cout << suc->data <<" ";
  130. else
  131. cout << "NULL" <<" ";
  132. cout<<endl;
  133. }
  134. }
  135. }
  136.  
Add Comment
Please, Sign In to add comment