Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Heap
- {
- private:
- static void swap(int& left, int& right)
- {
- int temp = left;
- left = right;
- right = temp;
- }
- static int min_compare(int* arr, int left, int right, int root, int size)
- {
- int location = -1;
- if (left < size && arr[left] < arr[root])
- {
- if (right < size && arr[right] < arr[left])
- {
- swap(arr[right], arr[root]);
- location = right;
- }
- else
- {
- swap(arr[left], arr[root]);
- location = left;
- }
- }
- else if (right < size && arr[right] < arr[root])
- {
- swap(arr[right], arr[root]);
- location = right;
- }
- return location;
- }
- static int max_compare(int* arr, int left, int right, int root, int size)
- {
- int location = -1;
- if (left < size && arr[left] > arr[root])
- {
- if (right < size && arr[right] > arr[left])
- {
- swap(arr[right], arr[root]);
- location = right;
- }
- else
- {
- swap(arr[left], arr[root]);
- location = left;
- }
- }
- else
- {
- if (right < size && arr[right] > arr[root])
- {
- swap(arr[right], arr[root]);
- location = right;
- }
- }
- return location;
- }
- static void min_heap(int* arr, int size, int root)
- {
- int left = 2 * root + 1;
- int right = 2 * root + 2;
- int step = min_compare(arr, left, right, root, size);
- if (step != -1)
- {
- min_heap(arr, size, step);
- }
- }
- static void max_heap(int* arr, int size, int root)
- {
- int left = 2 * root + 1;
- int right = 2 * root + 2;
- int next_step = max_compare(arr, left, right, root, size);
- if (next_step != -1)
- {
- max_heap(arr, size, next_step);
- }
- }
- public:
- static void get_max_heap(int* arr, int size)
- {
- int i = (size / 2) - 1;
- while (i >= 0)
- {
- max_heap(arr, size, i);
- i--;
- }
- }
- static void get_min_heap(int* arr, int size)
- {
- int i = (size / 2) - 1;
- while (i >= 0)
- {
- min_heap(arr, size, i);
- i--;
- }
- }
- static void output(int* arr, int size)
- {
- int border = 1;
- int counter = 0;
- for (int i = 0; i < size; i++)
- {
- counter++;
- cout << arr[i] << " ";
- if (border == counter)
- {
- std::cout << std::endl;
- border *= 2;
- counter = 0;
- }
- }
- }
- };
- void output_arr(int* arr, int size)
- {
- std::cout << "Start array: ";
- for (int i = 0; i < size; i++)
- {
- cout << arr[i] << " ";
- }
- cout << std::endl;
- }
- int main()
- {
- 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 };
- 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 };
- int size1 = sizeof(arr1) / sizeof(arr1[0]);
- int size2 = sizeof(arr2) / sizeof(arr2[0]);
- output_arr(arr1, size1);
- Heap::get_max_heap(arr1, size1);
- cout << "Max heap:" << endl;
- Heap::output(arr1, size1);
- cout << endl;
- cout << endl;
- output_arr(arr2, size2);
- Heap::get_min_heap(arr2, size2);
- cout << "Min heap:" << endl;
- Heap::output(arr2, size2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment