Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7. double key;
  8. struct Node *left;
  9. struct Node *right;
  10. int height;
  11. };
  12.  
  13. int max(int a, int b);
  14.  
  15. int height(struct Node *N)
  16. {
  17. if (N == NULL)
  18. return 0;
  19. return N->height;
  20. }
  21.  
  22. int max(int a, int b)
  23. {
  24. return (a > b)? a : b;
  25. }
  26.  
  27. struct Node* newNode(double key)
  28. {
  29. struct Node* node = new Node;
  30. node->key = key;
  31. node->left = NULL;
  32. node->right = NULL;
  33. node->height = 1;
  34. return(node);
  35. }
  36.  
  37. struct Node *rightRotate(Node *y)
  38. {
  39. struct Node *x = y->left;
  40. struct Node *T2 = x->right;
  41.  
  42. x->right = y;
  43. y->left = T2;
  44.  
  45. y->height = max(height(y->left), height(y->right))+1;
  46. x->height = max(height(x->left), height(x->right))+1;
  47.  
  48. return x;
  49. }
  50.  
  51. struct Node *leftRotate(struct Node *x)
  52. {
  53. struct Node *y = x->right;
  54. struct Node *T2 = y->left;
  55.  
  56. y->left = x;
  57. x->right = T2;
  58.  
  59. x->height = max(height(x->left), height(x->right))+1;
  60. y->height = max(height(y->left), height(y->right))+1;
  61.  
  62. return y;
  63. }
  64.  
  65. int getBalance(struct Node *N)
  66. {
  67. if (N == NULL)
  68. return 0;
  69. return height(N->left) - height(N->right);
  70. }
  71. bool containsRecursive(Node *current, double key)
  72. {
  73. if (current == NULL)
  74. {
  75. return false;
  76. }
  77.  
  78. if (current->key == key)
  79. {
  80. return true;
  81. }
  82.  
  83. if (key < current->key)
  84. {
  85. return containsRecursive(current->left, key);
  86. }
  87. else
  88. {
  89. return containsRecursive(current->right, key);
  90. }
  91. }
  92. bool contains(Node* root, double key)
  93. {
  94. if (root == NULL)
  95. {
  96. return false;
  97. }
  98.  
  99. return containsRecursive(root, key);
  100. }
  101. Node* add(Node* node, double key)
  102. {
  103. if (node == NULL)
  104. return newNode(key);
  105.  
  106. if (key < node->key)
  107. node->left = add(node->left, key);
  108. else if (key > node->key)
  109. node->right = add(node->right, key);
  110. else
  111. return node;
  112.  
  113. node->height = 1 + max(height(node->left),
  114. height(node->right));
  115.  
  116. int balance = getBalance(node);
  117. if (balance > 1 && key < node->left->key)
  118. return rightRotate(node);
  119. if (balance < -1 && key > node->right->key)
  120. return leftRotate(node);
  121. if (balance > 1 && key > node->left->key)
  122. {
  123. node->left = leftRotate(node->left);
  124. return rightRotate(node);
  125. }
  126.  
  127. if (balance < -1 && key < node->right->key)
  128. {
  129. node->right = rightRotate(node->right);
  130. return leftRotate(node);
  131. }
  132.  
  133. return node;
  134. }
  135.  
  136. Node * minkeyNode(Node* node)
  137. {
  138. Node* current = node;
  139. while (current->left != NULL)
  140. current = current->left;
  141.  
  142. return current;
  143. }
  144.  
  145. Node* remove(Node* root, double key)
  146. {
  147. if (root == NULL)
  148. return root;
  149.  
  150. if ( key < root->key )
  151. root->left = remove(root->left, key);
  152.  
  153.  
  154. else if( key > root->key )
  155. root->right = remove(root->right, key);
  156.  
  157. else
  158. {
  159. if( (root->left == NULL) || (root->right == NULL) )
  160. {
  161. Node *temp = root->left ? root->left :
  162. root->right;
  163. if (temp == NULL)
  164. {
  165. temp = root;
  166. root = NULL;
  167. }
  168. else
  169. *root = *temp;
  170. free(temp);
  171. }
  172. else
  173. {
  174. Node* temp = minkeyNode(root->right);
  175.  
  176. root->key = temp->key;
  177.  
  178. root->right = remove(root->right, temp->key);
  179. }
  180. }
  181.  
  182.  
  183. if (root == NULL)
  184. return root;
  185.  
  186. root->height = 1 + max(height(root->left),
  187. height(root->right));
  188.  
  189.  
  190. int balance = getBalance(root);
  191.  
  192. if (balance > 1 && getBalance(root->left) >= 0)
  193. return rightRotate(root);
  194.  
  195.  
  196. if (balance > 1 && getBalance(root->left) < 0)
  197. {
  198. root->left = leftRotate(root->left);
  199. return rightRotate(root);
  200. }
  201.  
  202. if (balance < -1 && getBalance(root->right) <= 0)
  203. return leftRotate(root);
  204.  
  205. if (balance < -1 && getBalance(root->right) > 0)
  206. {
  207. root->right = rightRotate(root->right);
  208. return leftRotate(root);
  209. }
  210.  
  211. return root;
  212. }
  213. void printRecursive(Node *current)
  214. {
  215. if (current == NULL)
  216. {
  217. return;
  218. }
  219.  
  220. printRecursive(current->left);
  221. cout << fixed << setprecision(6) << current->key * 1.0 << " ";
  222. printRecursive(current->right);
  223. }
  224.  
  225. int main()
  226. {
  227. Node* root = NULL;
  228. string operation;
  229. double number;
  230. int N;
  231.  
  232. cin >> N;
  233. cout << fixed;
  234.  
  235. for (size_t i = 0; i < N; i++)
  236. {
  237. cin >> operation;
  238. if (operation != "print")
  239. {
  240. cin >> number;
  241. }
  242.  
  243. if (operation == "add")
  244. {
  245. if(contains(root,number)) cout << number << " already added\n";
  246. else root = add(root, number);
  247. }
  248. else if (operation == "remove")
  249. {
  250. if(!contains(root,number)) cout << number << " not found to remove\n";
  251. else root = remove(root, number);
  252. }
  253. else if (operation == "contains")
  254. {
  255. if (contains(root, number))
  256. {
  257. cout << "yes" << endl;
  258. }
  259. else
  260. {
  261. cout << "no" << endl;
  262. }
  263. }
  264. else if (operation == "print")
  265. {
  266. printRecursive(root);
  267. }
  268. }
  269.  
  270. return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement