Convert min heap to max heap in linear time
Given an array representing a min-heap, convert it into a max-heap. The conversion should be done in-place and in linear time.
The idea is simple and inspired by the Heapsort algorithm. The problem looks complex at first glance, but this problem is no different from building a max-heap from an unsorted array. This is a tricky problem! It doesn’t matter whether the given array is a min-heap or not. We can simply build the max-heap from the array by starting from the last internal mode (rightmost, bottom-most node) of the min-heap and Heapify all internal modes in a bottom-up manner to build the max-heap.
The algorithm can be implemented as follows in C++, Java, and Python:
C++
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <iostream> #include <vector> using namespace std; class MaxHeap { int LEFT(int i); int RIGHT(int i); void heapify(vector<int> &A, int i, int n); public: MaxHeap(vector<int> &A); int pop(vector<int> &A, int n); }; // Return left child of A[i] int MaxHeap::LEFT(int i) { return (2*i + 1); } // Return right child of A[i] int MaxHeap::RIGHT(int i) { return (2*i + 2); } // Recursive function to perform a heapify-down operation. The node at // index `i` and its two direct children violates the heap property void MaxHeap::heapify(vector<int> &A, int i, int n) { // get left and right child of node at index `i` int left = LEFT(i); int right = RIGHT(i); // compare A[i] with its left & right child and store the largest value int largest = i; if (left < n && A[left] > A[i]) { largest = left; } if (right < n && A[right] > A[largest]) { largest = right; } // swap with a child having greater value and call heapify on the child if (largest != i) { swap(A[i], A[largest]); heapify(A, largest, n); } } // A Class Constructor // (performs Build-Heap operation to convert a min-heap into a max-heap) MaxHeap::MaxHeap(vector<int> &A) { int n = A.size(); // call heapify starting from the last internal node all the // way up to the root node int i = (n - 2) / 2; while (i >= 0) { heapify(A, i--, n); } } int main() { /* Input: min-heap 1 3 4 6 10 8 5 */ // input array (representing the min-heap as shown above) vector<int> A = { 1, 3, 4, 6, 10, 8, 5 }; // Convert min-heap into max-heap MaxHeap pq(A); /* Output: max-heap 10 6 8 1 3 4 5 */ cout << "The array representation of max-heap is "; for (int i: A) { cout << i << " "; } return 0; } |
Output:
The array representation of max-heap is 10 6 8 1 3 4 5
The time complexity of the above solution looks like O(n.log(n)) at first look, but it is the same as the time complexity of building a heap, i.e., O(n) for an input containing n
items. It also requires O(n) implicit space for the call stack.
Exercise: Convert max-heap into min-heap in linear time
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)