forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert min heap to max heap
59 lines (50 loc) · 1.48 KB
/
Convert min heap to max heap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
The problem might look complex at first look. But our final goal is to only build the max heap. The idea is very simple – we simply build Max Heap without caring about the input. We start from the bottom-most and rightmost internal mode of min Heap and heapify all internal modes in the bottom-up way to build the Max heap.
Below is its implementation.
// A C++ program to convert min Heap to max Heap
#include<bits/stdc++.h>
using namespace std;
// to heapify a subtree with root at given index
void MaxHeapify(int arr[], int i, int n)
{
int l = 2*i + 1;
int r = 2*i + 2;
int largest = i;
if (l < n && arr[l] > arr[i])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
swap(arr[i], arr[largest]);
MaxHeapify(arr, largest, n);
}
}
// This function basically builds max heap
void convertMaxHeap(int arr[], int n)
{
// Start from bottommost and rightmost
// internal mode and heapify all internal
// modes in bottom up way
for (int i = (n-2)/2; i >= 0; --i)
MaxHeapify(arr, i, n);
}
// A utility function to print a given array
// of given size
void printArray(int* arr, int size)
{
for (int i = 0; i < size; ++i)
printf("%d ", arr[i]);
}
// Driver program to test above functions
int main()
{
// array representing Min Heap
int arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Min Heap array : ");
printArray(arr, n);
convertMaxHeap(arr, n);
printf("\nMax Heap array : ");
printArray(arr, n);
return 0;
}