Advertisement
konrada111

Karol

Nov 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include "pch.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <time.h>
  6. #include <random>
  7.  
  8. using namespace std;
  9.  
  10. struct Element {
  11.  
  12. Element *left;
  13. Element* right;
  14. int key;
  15. char tab[10];
  16. Element(int k);
  17. };
  18.  
  19. Element::Element(int k) {
  20.  
  21. key = k;
  22. left = NULL;
  23. right = NULL;
  24. sprintf(tab, "%d", key);
  25. }
  26.  
  27.  
  28. bool insert(Element *&root, int k) {
  29.  
  30. Element *previous = NULL;
  31. Element *p = root;
  32.  
  33. while (p != NULL) {
  34. if (p->key == k) {
  35. return false;
  36. }
  37.  
  38. previous = p;
  39. if (p->key < k) {
  40. p = p->right;
  41. }
  42. else {
  43. p = p->left;
  44. }
  45. }
  46.  
  47. Element *nowy = new Element(k);
  48. nowy->key = k;
  49. nowy->left = NULL;
  50. nowy->right = NULL;
  51.  
  52. if (previous == NULL) {
  53. root = nowy;
  54. return true;
  55. }
  56.  
  57. if (previous->key < k) {
  58. previous->right = nowy;
  59. }
  60. else {
  61. previous->left = nowy;
  62. }
  63. return true;
  64. }
  65.  
  66.  
  67. void insert_x(Element *&root, int X) {
  68.  
  69. int k;
  70.  
  71. for (int i = 0; i < X; i++) {
  72.  
  73. k = (rand() % 20000) + (-10000);
  74.  
  75. while (insert(root, k) == false) {
  76.  
  77. k = (rand() % 20000) + (-10000);
  78. }
  79. }
  80. }
  81.  
  82.  
  83.  
  84.  
  85. void rotate_right(Element* root,Element *grandfather, Element* parent, Element *child) {
  86.  
  87.  
  88.  
  89. if (grandfather != NULL) {
  90.  
  91. if (grandfather->right = parent)
  92. grandfather->right = child;
  93. else
  94. grandfather->left = child;
  95. }
  96. else
  97. root = child;
  98.  
  99. Element* tmp = child->right;
  100. child->right = parent;
  101. parent->left = tmp;
  102. return;
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109. void make_backbone(Element* root) {
  110.  
  111. Element* grandfather = NULL;
  112. Element *tmp = root;
  113.  
  114.  
  115. while (tmp != NULL) {
  116. if ((tmp->left) != NULL) {
  117. Element *tmp2 = tmp->left;
  118. rotate_right(root,grandfather, tmp, tmp->left);
  119. tmp = tmp2;
  120. }
  121. else {
  122. grandfather = tmp;
  123. tmp = tmp->right;
  124. }
  125. }
  126. }
  127.  
  128. int maxDepth(Element* root)
  129. {
  130. if (root == NULL)
  131. return 0;
  132. else
  133. {
  134. /* compute the depth of each subtree */
  135. int lDepth = maxDepth(root->left);
  136. int rDepth = maxDepth(root->right);
  137.  
  138. /* use the larger one */
  139. if (lDepth > rDepth)
  140. return(lDepth + 1);
  141. else return(rDepth + 1);
  142. }
  143. }
  144.  
  145.  
  146. void rotate_left(Element*& root, Element*& grandfather, Element*& parent, Element*& child) {
  147.  
  148.  
  149. if (grandfather != NULL) {
  150.  
  151. if (grandfather->left == parent)
  152. grandfather->left = child;
  153. else
  154. grandfather->right = child;
  155. }
  156. else
  157. root = child;
  158. Element* tmp = child->left;
  159. child->left = parent;
  160. parent->right = tmp;
  161. return;
  162. }
  163.  
  164.  
  165. void make_perfect_tree(Element* root,int X) {
  166.  
  167. Element* grandfather = NULL;
  168. Element *tmp = root;
  169. int m = 1;
  170.  
  171. while (m <= X) {
  172. m = 2*m + 1;
  173. }
  174.  
  175. m = m / 2;
  176.  
  177. for (int i = 0; i < (X - m); i++) {
  178.  
  179. Element *tmp2 = tmp->right;
  180.  
  181. if (tmp2 != NULL) {
  182. rotate_left(root, grandfather, tmp, tmp->right);
  183. grandfather = tmp2;
  184. tmp = tmp2->right;
  185. }
  186. }
  187. while (m > 1) {
  188. m = m / 2;
  189. Element* grandfather = NULL;
  190. Element *tmp = root;
  191. for (int i = 0; i < m; i++) {
  192.  
  193. Element *tmp2 = tmp->right;
  194. rotate_left(root, grandfather, tmp, tmp->right);
  195. grandfather = tmp2;
  196. tmp = tmp2->right;
  197. }
  198. }
  199. }
  200.  
  201.  
  202.  
  203. int preorder(Element *root) {
  204.  
  205. int l = 0;
  206.  
  207. if (root == NULL)
  208. return l;
  209. l += preorder(root->left);
  210. l += preorder(root->right);
  211. l++;
  212. return l;
  213. }
  214.  
  215. void DSW(Element* root,int X) {
  216. if (root != NULL) {
  217. make_backbone(root);
  218. make_perfect_tree(root,X);
  219. }
  220. }
  221.  
  222.  
  223.  
  224. int main()
  225. {
  226.  
  227. srand(time(NULL));
  228.  
  229. int X1,X2;
  230.  
  231. Element *root = NULL;
  232.  
  233. clock_t start, stop;
  234.  
  235. start = clock();
  236.  
  237. double czas;
  238.  
  239. FILE* fp = fopen("inlab05.txt", "r");
  240. if (fp == NULL)
  241. return -1;
  242. fscanf(fp, "%d %d", &X1, &X2);
  243. fclose(fp);
  244.  
  245. insert_x(root, X1);
  246. insert(root, 9);
  247. int z = preorder(root);
  248. /*
  249. insert(root, 13);
  250. insert(root, 11);
  251. insert(root, 16);
  252. insert(root, 14);
  253. insert(root, 6);
  254. insert(root, 3);
  255. insert(root, 4);*/
  256. cout << maxDepth(root) << endl;
  257. make_backbone(root);
  258. make_perfect_tree(root, z);
  259. cout << maxDepth(root) << endl;
  260. //make_perfect_tree(root,X1);
  261.  
  262.  
  263. stop = clock();
  264.  
  265. czas = (double)(stop - start) / CLOCKS_PER_SEC;
  266.  
  267. cout << "Czas wykonania programu: " << czas << endl;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement