Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5.  
  6. typedef struct Node
  7. {
  8. int key;
  9. struct Node* left;
  10. struct Node* right;
  11. int height;
  12. } BST;
  13.  
  14.  
  15. int max(int a, int b)
  16. {
  17. return a > b ? a : b;
  18. }
  19.  
  20. int min(int a, int b)
  21. {
  22. return a < b ? a : b;
  23. }
  24.  
  25.  
  26. BST* create()
  27. {
  28. return NULL;
  29. }
  30.  
  31. void release(BST* root)
  32. {
  33. if(root)
  34. {
  35. release(root->left);
  36. release(root->right);
  37.  
  38. free(root);
  39. }
  40. }
  41.  
  42. int getHeight(BST* root)
  43. {
  44. if(!root)
  45. return -1;
  46. else
  47. return root->height;
  48. }
  49.  
  50. BST* rightRotation(BST* k2)
  51. {
  52. printf("\nPerforming right rotation %d\n ", k2->key);
  53.  
  54. BST* k1 = k2->left;
  55. k2->left = k1->right;
  56. k1->right = k2;
  57. k2->height = max(getHeight( k2->left), getHeight(k2->right)) + 1;
  58. k1->height = max(getHeight( k1->left), k2->height) + 1;
  59. return k1; /* nova raiz */
  60. }
  61.  
  62.  
  63. BST* leftRotation(BST* k1)
  64. {
  65. printf("\nPerforming left rotation %d\n ", k1->key);
  66. BST* k2 = k1->right;
  67. k1->right = k2->left;
  68. k2->left = k1;
  69. k1->height = max(getHeight(k1->left), getHeight(k1->right)) + 1;
  70. k2->height = max(getHeight(k2->right), k1->height) + 1;
  71. return k2; /* nova raiz */
  72. }
  73.  
  74.  
  75. BST* leftRightRotation(BST* k3){
  76. k3->left = leftRotation( k3->left );
  77. return rightRotation( k3 );
  78. }
  79.  
  80.  
  81. BST* rightLeftRotation(BST* k1)
  82. {
  83. k1->right = rightRotation( k1->right );
  84. return leftRotation( k1 );
  85. }
  86.  
  87. BST* insert(int value, BST* root)
  88. {
  89. if(!root)
  90. {
  91. root = (BST*)malloc(sizeof(BST));
  92. root->key = value;
  93. root->left = root->right = NULL;
  94.  
  95. root->height = 0;
  96. }
  97. else if(value < root->key)
  98. {
  99. root->left = insert(value, root->left);
  100.  
  101. if(getHeight(root->left) - getHeight(root->right) == 2)
  102. {
  103. if(value < root->left->key)
  104. {
  105. root = rightRotation(root);
  106. }
  107. else
  108. {
  109. root = leftRightRotation(root);
  110. }
  111. }
  112. }
  113. else if(value > root->key)
  114. {
  115. root->right = insert(value, root->right);
  116.  
  117. if(getHeight(root->right) - getHeight(root->left) == 2)
  118. {
  119. if(value > root->right->key)
  120. {
  121. root = leftRotation(root);
  122. }
  123. else
  124. {
  125. root = rightLeftRotation(root);
  126. }
  127. }
  128. }
  129.  
  130. root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
  131. return root;
  132. }
  133.  
  134. // Find a node in a Binary Search Tree
  135. BST* search(int key, BST* root)
  136. {
  137. if(!root || root->key == key)
  138. return root;
  139. else if(key < root->key)
  140. return search(key, root->left);
  141. else
  142. return search(key, root->right);
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. void print(BST* root, int spaces)
  151. {
  152. if(root)
  153. {
  154. print(root->left, spaces + 3);
  155. //printf("%d\t", root->key);
  156. for(int i = 0; i < spaces; i++ )
  157. printf(" ");
  158.  
  159. printf("%d\n", root->key);
  160.  
  161. print(root->right, spaces + 3);
  162. }
  163. }
  164.  
  165. // Q02
  166. BST* copy(BST* root)
  167. {
  168. if(!root)
  169. return root;
  170.  
  171. BST* node = (BST*)malloc(sizeof(BST));
  172. node->height = root->height;
  173. node->key = root->key;
  174.  
  175. node->left = copy(root->left);
  176. node->right = copy(root->right);
  177.  
  178. return node;
  179. }
  180.  
  181.  
  182. // Q03
  183. BST* mirror(BST* root)
  184. {
  185. if(!root)
  186. return root;
  187.  
  188. BST* right = mirror(root->right);
  189. BST* left = mirror(root->left);
  190.  
  191. root->left = right;
  192. root->right = left;
  193.  
  194. return root;
  195. }
  196.  
  197. // Q04
  198. int maximum(BST* root)
  199. {
  200. if(!root)
  201. return INT_MIN;
  202.  
  203. int key = root->key;
  204. int left_key = maximum(root->left);
  205. int right_key = maximum(root->right);
  206.  
  207. int number = max(key, left_key);
  208. number = max(number, right_key);
  209.  
  210. return number;
  211. }
  212.  
  213. // Q05
  214. int minimum(BST* root)
  215. {
  216. if(!root)
  217. return INT_MAX;
  218.  
  219. int key = root->key;
  220. int left_key = minimum(root->left);
  221. int right_key = minimum(root->right);
  222.  
  223. int number = min(key, left_key);
  224. number = min(number, right_key);
  225.  
  226. return number;
  227. }
  228.  
  229. // Q06
  230. int isEqual(BST* t1, BST* t2)
  231. {
  232. if(!t1 && !t2)
  233. return 1;
  234.  
  235. if(t1 && t2 && t1->key == t2->key)
  236. return isEqual(t1->left, t2->left) && isEqual(t1->right, t2->right);
  237. else
  238. return 0;
  239. }
  240.  
  241. BST* removeOdds(BST* root)
  242. {
  243. if(!root)
  244. return root;
  245.  
  246. BST* left = root->left;
  247. BST* right = root->right;
  248.  
  249.  
  250. }
  251.  
  252.  
  253.  
  254. int main(int argv, char* argc[])
  255. {
  256. BST* root = create();
  257.  
  258. root = insert(50, root);
  259. root = insert(1, root);
  260. root = insert(64, root);
  261. root = insert(12, root);
  262. root = insert(18, root);
  263. root = insert(66, root);
  264. root = insert(38, root);
  265. root = insert(95, root);
  266. root = insert(58, root);
  267. root = insert(59, root);
  268.  
  269. //BST* copied = copy(root);
  270. BST* copied = create();
  271. copied = insert(50, copied);
  272. copied = insert(1, copied);
  273. copied = insert(64, copied);
  274.  
  275. print(root, 0);
  276.  
  277. printf("\n\nCOPIED:\n");
  278.  
  279. print(copied, 0);
  280.  
  281. printf("\n\nEquals: %d\n\n", isEqual(root, copied));
  282.  
  283. printf("\n\nMaximum: %d\n\n", maximum(root));
  284. printf("\n\nMinimum: %d\n\n", minimum(root));
  285.  
  286. printf("Mirror: \n");
  287.  
  288. root = mirror(root);
  289.  
  290. print(root, 0);
  291.  
  292. release(copied);
  293. release(root);
  294.  
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement