Advertisement
Guest User

Untitled

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