Advertisement
SIKER_98

#als#kopiec

Jun 5th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. struct kopiec {
  9. int wartosc;
  10. kopiec *left, *right, *up;
  11. };
  12.  
  13. struct kolejka {
  14. bool direction;
  15. kolejka *next, *prev;
  16. };
  17.  
  18. int index = 0;
  19.  
  20. ///////////////////////////
  21.  
  22. void naKopiec(kopiec *root, int wartosc);
  23.  
  24. void showTree(kopiec *root);
  25.  
  26. void usuwanie(kopiec *root);
  27.  
  28. //////////////////////////
  29.  
  30. int main() {
  31.  
  32. kopiec *root = new kopiec;
  33. int wartosc;
  34. int opcja=-2;
  35.  
  36. while(opcja!=-1){
  37. cin >> opcja;
  38. switch(opcja){
  39. case -1:
  40. break;
  41. case 0:
  42. cin >> wartosc;
  43. naKopiec(root, wartosc);
  44. break;
  45. case 1:
  46. usuwanie(root);
  47. break;
  48. case 2:
  49. showTree(root);
  50. break;
  51. }
  52. }
  53.  
  54. return 0;
  55. }
  56.  
  57. /// DODAWANIE
  58.  
  59. void balans(kopiec *nowy) {
  60. if (nowy->up == NULL)return;
  61. else if (nowy->wartosc < nowy->up->wartosc) return;
  62. else if (nowy->wartosc > nowy->up->wartosc) {
  63. int value = nowy->up->wartosc;
  64. nowy->up->wartosc = nowy->wartosc;
  65. nowy->wartosc = value;
  66. balans(nowy->up);
  67. }
  68.  
  69. }
  70.  
  71. void naKopiec(kopiec *root, int wartosc) {
  72. if (root->wartosc == NULL) {
  73. root->wartosc = wartosc;
  74. root->right = NULL;
  75. root->left = NULL;
  76. root->up = NULL;
  77. index++;
  78. } else {
  79. index++;
  80. kolejka *stos = new kolejka;
  81. kolejka *tmp1 = new kolejka;
  82. tmp1 = stos;
  83. int indexCopy = index;
  84. while (indexCopy != 1) {
  85. kolejka *kierunek = new kolejka;
  86. tmp1->next = kierunek;
  87. kierunek->prev = tmp1;
  88. if (indexCopy % 2 == 0) kierunek->direction = 0;
  89. else kierunek->direction = 1;
  90. indexCopy = indexCopy / 2;
  91. tmp1 = tmp1->next;
  92. }
  93. kopiec *nowy = new kopiec;
  94. kopiec *tmp2 = root;
  95. nowy->wartosc = wartosc;
  96. nowy->left = NULL;
  97. nowy->right = NULL;
  98. while (tmp1 != stos) {
  99. if (tmp1->direction == 0) {
  100. if (tmp2->left == NULL) {
  101. tmp2->left = nowy;
  102. nowy->up = tmp2;
  103. } else {
  104. tmp2 = tmp2->left;
  105. tmp1 = tmp1->prev;
  106. }
  107. } else if (tmp1->direction == 1) {
  108. if (tmp2->right == NULL) {
  109. tmp2->right = nowy;
  110. nowy->up = tmp2;
  111. } else {
  112. tmp2 = tmp2->right;
  113. tmp1 = tmp1->prev;
  114. }
  115. }
  116. }
  117.  
  118. balans(nowy);
  119. }
  120.  
  121. }
  122.  
  123. /// WYSWIETLANIE
  124.  
  125. void showTree(kopiec *root) {
  126. kopiec *tmp = root;
  127.  
  128. cout << root->wartosc << " ";
  129.  
  130. if (tmp->left != NULL) showTree(tmp->left);
  131.  
  132. if (tmp->right != NULL) showTree(tmp->right);
  133. }
  134.  
  135. /// USUWANIE
  136.  
  137. void balans2(kopiec *root) {
  138. if (root->wartosc > root->left->wartosc && root->wartosc > root->right->wartosc) return;
  139. else if (root->wartosc > root->left->wartosc && root->wartosc < root->right->wartosc){
  140. int value = root->wartosc;
  141. root->wartosc = root->right->wartosc;
  142. root->right->wartosc = value;
  143. balans2(root->right);
  144. } else{
  145. int value = root->wartosc;
  146. root->wartosc = root->left->wartosc;
  147. root->left->wartosc = value;
  148. balans2(root->left);
  149. }
  150. }
  151.  
  152. void usuwanie(kopiec *root) {
  153.  
  154. kolejka *stos = new kolejka;
  155. kolejka *tmp1 = new kolejka;
  156. tmp1 = stos;
  157. int indexCopy = index;
  158. while (indexCopy != 1) {
  159. kolejka *kierunek = new kolejka;
  160. tmp1->next = kierunek;
  161. kierunek->prev = tmp1;
  162. if (indexCopy % 2 == 0) kierunek->direction = 0;
  163. else kierunek->direction = 1;
  164. indexCopy = indexCopy / 2;
  165. tmp1 = tmp1->next;
  166. }
  167. ///
  168. kopiec *tmp2 = root;
  169.  
  170. while (tmp1 != stos) {
  171. if (tmp1->direction == 0) {
  172. tmp2 = tmp2->left;
  173. tmp1 = tmp1->prev;
  174. } else if (tmp1->direction == 1) {
  175. tmp2 = tmp2->right;
  176. tmp1 = tmp1->prev;
  177. }
  178. }
  179. int value = root->wartosc;
  180. root->wartosc = tmp2->wartosc;
  181. tmp2->wartosc = value;
  182. tmp2 = tmp2->up;
  183. if (tmp2->right->wartosc == value) {
  184. delete (tmp2->right);
  185. tmp2->right = NULL;
  186. } else {
  187. delete (tmp2->left);
  188. tmp2->left = NULL;
  189. }
  190.  
  191. balans2(root);
  192.  
  193.  
  194.  
  195. ////
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement