Minimum element in a max heap
Given a max heap, find the minimum element present in the heap.
Examples:
Input : 100
/ \
75 50
/ \ / \
55 10 2 40
Output : 2
Input : 20
/ \
4 18
Output : 4
Brute force approach:
We can check all the nodes in the max heap to get the minimum element. Note that this approach works on any binary tree and does not makes use of any property of the max heap. It has a time and space complexity of O(n). Since max heap is a complete binary tree, we generally use arrays to store them, so we can check all the nodes by simply traversing the array. If the heap is stored using pointers, then we can use recursion to check all the nodes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumElement( int heap[], int n)
{
int minimumElement = heap[0];
for ( int i = 1; i < n; ++i)
minimumElement = min(minimumElement, heap[i]);
return minimumElement;
}
int main()
{
int n = 10;
int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
cout << findMinimumElement(heap, n);
return 0;
}
|
Java
public class Improve {
static int findMinimumElement( int heap[], int n)
{
int minimumElement = heap[ 0 ];
for ( int i = 1 ; i < n; ++i)
minimumElement
= Math.min(minimumElement, heap[i]);
return minimumElement;
}
public static void main(String args[])
{
int n = 10 ;
int heap[] = { 20 , 18 , 10 , 12 , 9 , 9 , 3 , 5 , 6 , 8 };
System.out.println(findMinimumElement(heap, n));
}
}
|
Python3
def findMiniumumElement(heap, n):
minimumElement = heap[ 0 ]
for i in range ( 1 , n):
minimumElement = min (minimumElement, heap[i])
return minimumElement
n = 10
heap = [ 20 , 18 , 10 , 12 , 8 ,
9 , 3 , 5 , 6 , 8 ]
print (findMiniumumElement(heap, n))
|
C#
using System;
class GFG {
static int findMinimumElement( int [] heap, int n)
{
int minimumElement = heap[0];
for ( int i = 1; i < n; ++i)
minimumElement
= Math.Min(minimumElement, heap[i]);
return minimumElement;
}
public static void Main()
{
int n = 10;
int [] heap = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
Console.Write(findMinimumElement(heap, n));
}
}
|
PHP
<?php
function findMinimumElement(& $heap , $n )
{
$minimumElement = $heap [0];
for ( $i = 1; $i < $n ; ++ $i )
$minimumElement = min( $minimumElement ,
$heap [ $i ]);
return $minimumElement ;
}
$n = 10;
$heap = array (20, 18, 10, 12, 9,
9, 3, 5, 6, 8 );
echo findMinimumElement( $heap , $n );
?>
|
Javascript
<script>
function findMinimumElement(heap, n)
{
let minimumElement = heap[0];
for (let i = 1; i < n; ++i)
minimumElement = Math.min(minimumElement, heap[i]);
return minimumElement;
}
let n = 10;
let heap = [ 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 ];
document.write(findMinimumElement(heap, n));
</script>
|
Efficient approach:
The max heap property requires that the parent node be greater than its child node(s). Due to this, we can conclude that a non-leaf node cannot be the minimum element as its child node has a lower value. So we can narrow down our search space to only leaf nodes. In a max heap having n elements, there is ceil(n/2) leaf nodes. The time and space complexity remains O(n) as a constant factor of 1/2 does not affect the asymptotic complexity.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumElement( int heap[], int n)
{
int minimumElement = heap[n / 2];
for ( int i = 1 + n / 2; i < n; ++i)
minimumElement = min(minimumElement, heap[i]);
return minimumElement;
}
int main()
{
int n = 10;
int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
cout << findMinimumElement(heap, n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int findMinimumElement( int heap[], int n)
{
int minimumElement = heap[n / 2 ];
for ( int i = 1 + n / 2 ; i < n; ++i)
minimumElement
= Math.min(minimumElement, heap[i]);
return minimumElement;
}
public static void main(String args[])
{
int n = 10 ;
int heap[] = new int [] { 20 , 18 , 10 , 12 , 9 ,
9 , 3 , 5 , 6 , 8 };
System.out.println(findMinimumElement(heap, n));
}
}
|
Python3
def findMiniumumElement(heap, n):
minimumElement = heap[n / / 2 ]
for i in range ( 1 + n / / 2 , n):
minimumElement = min (minimumElement,
heap[i])
return minimumElement
n = 10
heap = [ 20 , 18 , 10 , 12 , 9 ,
9 , 3 , 5 , 6 , 8 ]
print (findMiniumumElement(heap, n))
|
C#
using System;
class GFG {
static int findMinimumElement( int [] heap, int n)
{
int minimumElement = heap[n / 2];
for ( int i = 1 + n / 2; i < n; ++i)
minimumElement
= Math.Min(minimumElement, heap[i]);
return minimumElement;
}
static public void Main()
{
int n = 10;
int [] heap = new int [] { 20, 18, 10, 12, 9,
9, 3, 5, 6, 8 };
Console.WriteLine(findMinimumElement(heap, n));
}
}
|
PHP
<?php
function findMinimumElement( $heap , $n )
{
$minimumElement = $heap [ $n /2];
for ( $i = 1 + $n / 2; $i < $n ; ++ $i )
$minimumElement = min( $minimumElement , $heap [ $i ]);
return $minimumElement ;
}
$n = 10;
$heap = Array(20, 18, 10, 12, 9, 9, 3, 5, 6, 8 );
echo (findMinimumElement( $heap , $n ));
?>
|
Javascript
<script>
function findMinimumElement(heap, n)
{
let minimumElement = heap[parseInt(n / 2, 10)];
for (let i = 1 + parseInt(n / 2, 10); i < n; ++i)
minimumElement
= Math.min(minimumElement, heap[i]);
return minimumElement;
}
let n = 10;
let heap = [ 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 ];
document.write(findMinimumElement(heap, n));
</script>
|
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...