Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. // Primer.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. struct tree
  10. {
  11. int info;
  12. tree*l, *r;
  13. };
  14. tree*create(tree*, int);
  15. void print(tree*);
  16. void delTree(tree*&);
  17. tree*task(tree*);
  18. tree*balance(tree*, tree*);
  19. int*zapis(tree*, int*, int&);
  20. tree*create2(int, int, int*);
  21. void count(tree*, int&);
  22. void shell(int*, int);
  23. int _tmain(int argc, _TCHAR* argv[])
  24. {
  25. int c = 0, el;
  26. tree*root = NULL;
  27. do
  28. {
  29. cout << "Vvedite element ";
  30. cin >> el;
  31. root = create(root, el);
  32. cout << "Vvedite 0 dlya zaversheniya ";
  33. cin >> c;
  34. } while (c != 0);
  35. cout << "Ishodnoe derevo ";
  36. print(root);
  37. cout << endl;
  38. root = task(root);
  39. cout << "Novoe derevo ";
  40. print(root);
  41. cout << endl;
  42. char ch = 'n';
  43. cout << "Vediye (y) dlya balansirovki i (n) inache ";
  44. cin >> ch;
  45. tree*broot = NULL;
  46. if (ch == 'y')
  47. {
  48. broot = balance(broot, root);
  49. cout << "Balance tree" << endl;
  50. print(broot);
  51. cout << endl;
  52. delTree(broot);
  53. }
  54. delTree(root);
  55. system("pause");
  56. return 0;
  57. }
  58. tree*create(tree*root, int el)
  59. {
  60. tree*tmp, *prev = NULL;
  61. tree*t = new tree;
  62. if (root == NULL)
  63. {
  64. t->info = el;
  65. t->r = t->l = NULL;
  66. return t;
  67. }
  68. else
  69. {
  70. tmp = root;
  71. while (tmp != NULL)
  72. {
  73. prev = tmp;
  74. if (el>tmp->info)
  75. tmp = tmp->r;
  76. else tmp = tmp->l;
  77. }
  78. if (el > prev->info)
  79. {
  80. t->info = el;
  81. t->r = t->l = NULL;
  82. prev->r = t;
  83. }
  84. else
  85. {
  86. t->info = el;
  87. t->r = t->l = NULL;
  88. prev->l = t;
  89. }
  90. }
  91. return root;
  92. }
  93. void print(tree*root)
  94. {
  95. if (root == NULL)
  96. return;
  97. print(root->l);
  98. cout << root->info << " ";
  99. print(root->r);
  100. }
  101. void delTree(tree*&root)
  102. {
  103. if (root != NULL)
  104. {
  105. delTree(root->l);
  106. delTree(root->r);
  107. delete root;
  108. root = NULL;
  109. }
  110. }
  111. tree*task(tree*root)
  112. {
  113. tree*t, *prev=NULL, *tmp;
  114. if (root->info == 5)
  115. {
  116. delTree(root->r);
  117. root->r = NULL;
  118. return root;
  119. }
  120. else
  121. {
  122. if (root->info>5)
  123. {
  124. delTree(root->r);
  125. t = root->l;
  126. delete root;
  127. root = NULL;
  128. root = task(t);
  129. return root;
  130. }
  131. if (root->info<5)
  132. {
  133. t = root;
  134. while (t->info<5 && t != NULL)
  135. {
  136. prev = t;
  137. t = t->r;
  138. }
  139. tmp = task(t);
  140. prev->r = tmp;
  141. return root;
  142. }
  143. }
  144. }
  145. void count(tree*root, int&n)
  146. {
  147. if (root == NULL)
  148. return;
  149. count(root->l, n);
  150. n++;
  151. count(root->r, n);
  152. }
  153. tree*balance(tree*broot, tree*root)
  154. {
  155. int n = 0;
  156. count(root, n);
  157. int*arr = new int[n];
  158. int i = 0;
  159. zapis(root, arr, i);
  160. shell(arr, n);
  161. broot = create2(0, n - 1, arr);
  162. return broot;
  163. }
  164. int*zapis(tree*root, int*arr, int&i)
  165. {
  166. if (root != NULL)
  167. {
  168. zapis(root->l, arr, i);
  169. arr[i] = root->info;
  170. i++;
  171. zapis(root->r, arr, i);
  172. return arr;
  173. }
  174. }
  175. tree*create2(int left, int right, int*arr)
  176. {
  177. if (left>right)
  178. return NULL;
  179. int m = (left + right) / 2;
  180. tree*t = new tree;
  181. t->info = arr[m];
  182. t->l = create2(left, m - 1, arr);
  183. t->r = create2(m + 1, right, arr);
  184. return t;
  185. }
  186. void shell(int*arr, int n)
  187. {
  188. int tmp, j;
  189. for (int step = n / 2; step>0; step /= 2)
  190. {
  191. for (int i = step; i<n; i++)
  192. {
  193. tmp = arr[i];
  194. j = i - step;
  195. while (j >= 0 && tmp<arr[j])
  196. {
  197. arr[j + step] = arr[j];
  198. j -= step;
  199. }
  200. arr[j + step] = tmp;
  201. }
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement