Vla_DOS

lr 5 Піраміди

Jun 15th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class Heap
  5. {
  6. private:
  7.  
  8. static void swap(int& left, int& right)
  9. {
  10. int temp = left;
  11. left = right;
  12. right = temp;
  13. }
  14.  
  15. static int min_compare(int* arr, int left, int right, int root, int size)
  16. {
  17. int location = -1;
  18. if (left < size && arr[left] < arr[root])
  19. {
  20. if (right < size && arr[right] < arr[left])
  21. {
  22. swap(arr[right], arr[root]);
  23. location = right;
  24. }
  25. else
  26. {
  27. swap(arr[left], arr[root]);
  28. location = left;
  29. }
  30. }
  31. else if (right < size && arr[right] < arr[root])
  32. {
  33. swap(arr[right], arr[root]);
  34. location = right;
  35. }
  36. return location;
  37. }
  38.  
  39. static int max_compare(int* arr, int left, int right, int root, int size)
  40. {
  41. int location = -1;
  42. if (left < size && arr[left] > arr[root])
  43. {
  44. if (right < size && arr[right] > arr[left])
  45. {
  46. swap(arr[right], arr[root]);
  47. location = right;
  48. }
  49. else
  50. {
  51. swap(arr[left], arr[root]);
  52. location = left;
  53. }
  54. }
  55. else
  56. {
  57. if (right < size && arr[right] > arr[root])
  58. {
  59. swap(arr[right], arr[root]);
  60. location = right;
  61. }
  62. }
  63. return location;
  64. }
  65.  
  66. static void min_heap(int* arr, int size, int root)
  67. {
  68. int left = 2 * root + 1;
  69. int right = 2 * root + 2;
  70. int step = min_compare(arr, left, right, root, size);
  71. if (step != -1)
  72. {
  73. min_heap(arr, size, step);
  74. }
  75. }
  76.  
  77. static void max_heap(int* arr, int size, int root)
  78. {
  79. int left = 2 * root + 1;
  80. int right = 2 * root + 2;
  81. int next_step = max_compare(arr, left, right, root, size);
  82. if (next_step != -1)
  83. {
  84. max_heap(arr, size, next_step);
  85. }
  86. }
  87.  
  88. public:
  89.  
  90. static void get_max_heap(int* arr, int size)
  91. {
  92. int i = (size / 2) - 1;
  93. while (i >= 0)
  94. {
  95. max_heap(arr, size, i);
  96. i--;
  97. }
  98. }
  99.  
  100. static void get_min_heap(int* arr, int size)
  101. {
  102. int i = (size / 2) - 1;
  103. while (i >= 0)
  104. {
  105. min_heap(arr, size, i);
  106. i--;
  107. }
  108. }
  109.  
  110. static void output(int* arr, int size)
  111. {
  112. int border = 1;
  113. int counter = 0;
  114. for (int i = 0; i < size; i++)
  115. {
  116. counter++;
  117. cout << arr[i] << " ";
  118. if (border == counter)
  119. {
  120. std::cout << std::endl;
  121. border *= 2;
  122. counter = 0;
  123. }
  124. }
  125. }
  126.  
  127. };
  128.  
  129. void output_arr(int* arr, int size)
  130. {
  131. std::cout << "Start array: ";
  132. for (int i = 0; i < size; i++)
  133. {
  134. cout << arr[i] << " ";
  135. }
  136. cout << std::endl;
  137. }
  138.  
  139.  
  140. int main()
  141. {
  142. int arr1[] = { 58, 8, 18, 54, 84, 92, 62, 17, 69, 96, 39, 41, 99, 10, 85, 71, 64, 77, 60, 19, 28, 50, 46, 48, 98, 40 };
  143. int arr2[] = { 58, 8, 18, 54, 84, 92, 62, 17, 69, 96, 39, 41, 99, 10, 85, 71, 64, 77, 60, 19, 28, 50, 46, 48, 98, 40 };
  144. int size1 = sizeof(arr1) / sizeof(arr1[0]);
  145. int size2 = sizeof(arr2) / sizeof(arr2[0]);
  146.  
  147. output_arr(arr1, size1);
  148. Heap::get_max_heap(arr1, size1);
  149. cout << "Max heap:" << endl;
  150. Heap::output(arr1, size1);
  151.  
  152. cout << endl;
  153. cout << endl;
  154.  
  155. output_arr(arr2, size2);
  156. Heap::get_min_heap(arr2, size2);
  157. cout << "Min heap:" << endl;
  158. Heap::output(arr2, size2);
  159. return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment