Convert Min Heap to Max Heap
Last Updated :
17 Jan, 2023
Given an array representation of min Heap, convert it to max Heap.
Examples:
Input: arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9}
3
/ \
5 9
/ \ / \
6 8 20 10
/ \ /
12 18 9
Output: arr[] = {20, 18, 10, 12, 9, 9, 3, 5, 6, 8}
20
/ \
18 10
/ \ / \
12 9 9 3
/ \ /
5 6 8
Input: arr[] = {3, 4, 8, 11, 13}
Output: arr[] = {13, 11, 8, 4, 3}
Approach: To solve the problem follow the below idea:
The idea is, simply build Max Heap without caring about the input. Start from the bottom-most and rightmost internal node of Min-Heap and heapify all internal nodes in the bottom-up way to build the Max heap.
Follow the given steps to solve the problem:
- Call the Heapify function from the rightmost internal node of Min-Heap
- Heapify all internal nodes in the bottom-up way to build max heap
- Print the Max-Heap
Algorithm: Here’s an algorithm for converting a min heap to a max heap:
- Start at the last non-leaf node of the heap (i.e., the parent of the last leaf node). For a binary heap, this node is located at the index floor((n – 1)/2), where n is the number of nodes in the heap.
- For each non-leaf node, perform a “heapify” operation to fix the heap property. In a min heap, this operation involves checking whether the value of the node is greater than that of its children, and if so, swapping the node with the smaller of its children. In a max heap, the operation involves checking whether the value of the node is less than that of its children, and if so, swapping the node with the larger of its children.
- Repeat step 2 for each of the non-leaf nodes, working your way up the heap. When you reach the root of the heap, the entire heap should now be a max heap.
Below is the implementation of the above approach:
C
#include <stdio.h>
void swap( int * a, int * b)
{
int temp = *a;
*a = *b;
*b = temp;
}
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);
}
}
void convertMaxHeap( int arr[], int N)
{
for ( int i = (N - 2) / 2; i >= 0; --i)
MaxHeapify(arr, i, N);
}
void printArray( int * arr, int size)
{
for ( int i = 0; i < size; ++i)
printf ( "%d " , arr[i]);
}
int main()
{
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;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
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);
}
}
void convertMaxHeap( int arr[], int N)
{
for ( int i = (N - 2) / 2; i >= 0; --i)
MaxHeapify(arr, i, N);
}
void printArray( int * arr, int size)
{
for ( int i = 0; i < size; ++i)
cout << arr[i] << " " ;
}
int main()
{
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;
}
|
Java
class GFG {
static 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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
MaxHeapify(arr, largest, N);
}
}
static void convertMaxHeap( int arr[], int N)
{
for ( int i = (N - 2 ) / 2 ; i >= 0 ; --i)
MaxHeapify(arr, i, N);
}
static void printArray( int arr[], int size)
{
for ( int i = 0 ; i < size; ++i)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 9 , 6 , 8 , 20 , 10 , 12 , 18 , 9 };
int N = arr.length;
System.out.print( "Min Heap array : " );
printArray(arr, N);
convertMaxHeap(arr, N);
System.out.print( "\nMax Heap array : " );
printArray(arr, N);
}
}
|
Python3
def MaxHeapify(arr, i, N):
l = 2 * i + 1
r = 2 * i + 2
largest = i
if l < N and arr[l] > arr[i]:
largest = l
if r < N and arr[r] > arr[largest]:
largest = r
if largest ! = i:
arr[i], arr[largest] = arr[largest], arr[i]
MaxHeapify(arr, largest, N)
def convertMaxHeap(arr, N):
for i in range ( int ((N - 2 ) / 2 ), - 1 , - 1 ):
MaxHeapify(arr, i, N)
def printArray(arr, size):
for i in range (size):
print (arr[i], end = " " )
print ()
if __name__ = = '__main__' :
arr = [ 3 , 5 , 9 , 6 , 8 , 20 , 10 , 12 , 18 , 9 ]
N = len (arr)
print ( "Min Heap array : " )
printArray(arr, N)
convertMaxHeap(arr, N)
print ( "Max Heap array : " )
printArray(arr, N)
|
C#
using System;
class GFG {
static 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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
MaxHeapify(arr, largest, n);
}
}
static void convertMaxHeap( int [] arr, int n)
{
for ( int i = (n - 2) / 2; i >= 0; --i)
MaxHeapify(arr, i, n);
}
static void printArray( int [] arr, int size)
{
for ( int i = 0; i < size; ++i)
Console.Write(arr[i] + " " );
}
public static void Main()
{
int [] arr = { 3, 5, 9, 6, 8, 20, 10, 12, 18, 9 };
int n = arr.Length;
Console.Write( "Min Heap array : " );
printArray(arr, n);
convertMaxHeap(arr, n);
Console.Write( "\nMax Heap array : " );
printArray(arr, n);
}
}
|
PHP
<?php
function swap(& $a ,& $b )
{
$tmp = $a ;
$a = $b ;
$b = $tmp ;
}
function MaxHeapify(& $arr , $i , $n )
{
$l = 2* $i + 1;
$r = 2* $i + 2;
$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 );
}
}
function convertMaxHeap(& $arr , $n )
{
for ( $i = (int)(( $n -2)/2); $i >= 0; -- $i )
MaxHeapify( $arr , $i , $n );
}
function printArray( $arr , $size )
{
for ( $i = 0; $i < $size ; ++ $i )
print ( $arr [ $i ]. " " );
}
$arr = array (3, 5, 9, 6, 8, 20, 10, 12, 18, 9);
$n = count ( $arr );
print ( "Min Heap array : " );
printArray( $arr , $n );
convertMaxHeap( $arr , $n );
print ( "\nMax Heap array : " );
printArray( $arr , $n );
?>
|
Javascript
<script>
function MaxHeapify(arr , i , n)
{
var l = 2*i + 1;
var r = 2*i + 2;
var largest = i;
if (l < n && arr[l] > arr[i])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
var temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
MaxHeapify(arr, largest, n);
}
}
function convertMaxHeap(arr , n)
{
for (i = (n-2)/2; i >= 0; --i)
MaxHeapify(arr, i, n);
}
function printArray(arr , size)
{
for (i = 0; i < size; ++i)
document.write(arr[i]+ " " );
}
var arr = [3, 5, 9, 6, 8, 20, 10, 12, 18, 9];
var n = arr.length;
document.write( "Min Heap array : " );
printArray(arr, n);
convertMaxHeap(arr, n);
document.write( "<br>Max Heap array : " );
printArray(arr, n);
</script>
|
Output
Min Heap array : 3 5 9 6 8 20 10 12 18 9
Max Heap array : 20 18 10 12 9 9 3 5 6 8
Time Complexity: O(N), for details, please refer: Time Complexity of building a heap
Auxiliary Space: O(N)
Please Login to comment...