-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathConvertMinHeapToMax.cpp
75 lines (62 loc) · 1.52 KB
/
ConvertMinHeapToMax.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Converting min Heap to max Heap
#include<bits/stdc++.h>
using namespace std;
// to heapify a subtree
void MaxHeap(int arr[], int x, int num)
{
int left = 2*x + 1;
int right = 2*x + 2;
int greatest = x;
if (left < num && arr[left] > arr[x])
greatest = left;
if (right < num && arr[right] > arr[greatest])
greatest = right;
if (greatest != x)
{
swap(arr[x], arr[greatest]);
MaxHeap(arr, greatest, num);
}
}
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)
MaxHeap(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]);
}
//Main Function
int main()
{
// array with elements of Min Heap Tree
int n;
cout<<"Enter the size of the array:";
cin>>n;
int arr[n];
cout<<"Enter the array elements:";
for(int i=0;i<n;i++){
cin>>arr[i];
}
printf("Min Heap array : ");
printArray(arr, n);
convertMaxHeap(arr, n);
printf("\nMax Heap array : ");
printArray(arr, n);
return 0;
}
/*
OUTPUT:
Enter the size of the array:8
Enter the array elements:10 19 20 23 25 16 26 51
Min Heap array : 10 19 20 23 25 16 26 51
Max Heap array : 51 25 26 23 10 16 20 19
TIME COMPLEXITY: O(N) [N here is number of elements in array]
SPACE COMPLEXITY: O(N)
*/