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 above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the
// minimum element in a
// max heap
int findMinimumElement(int heap[], int n)
{
    int minimumElement = heap[0];
  
    for (int i = 1; i < n; ++i)
        minimumElement = min(minimumElement, heap[i]);
  
    return minimumElement;
}
  
// Driver code
int main()
{
    // Number of nodes
    int n = 10;
    // heap represents the following max heap:
    //         20
    //       /    \
    //      18     10
    //   /    \    /  \
    //   12     9  9   3
    //  /  \   /
    // 5    6 8
    int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
  
    cout << findMinimumElement(heap, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach 
  
public class Improve {
  
    // Function to find the 
    // minimum element in a 
    // max heap 
    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; 
    
      
    // Driver code
    public static void main(String args[])
    {
         // Number of nodes 
        int n = 10
        // heap represents the following max heap: 
        //         20 
        //       /    \ 
        //      18     10 
        //   /    \    /  \ 
        //   12     9  9   3 
        //  /  \   / 
        // 5    6 8 
        int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 }; 
        
        System.out.println(findMinimumElement(heap, n)); 
        
      
    }
    // This Code is contributed by ANKITRAI1
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of above approach
  
# Function to find the minimum 
# element in a max heap
def findMiniumumElement(heap, n):
  
    minimumElement = heap[0]
  
    for i in range(1, n):
        minimumElement = min(minimumElement, heap[i])
  
    return minimumElement
  
# Driver Code
n = 10 # Numbers Of Node
  
# heap represents the following max heap:
#         20
#         / \
#         18 10
#     / \ / \
#     12 9 9 3
#     / \ /
#     5 6 8
#
  
heap = [20, 18, 10, 12, 8
            9, 3, 5, 6, 8]
  
print(findMiniumumElement(heap,n))
  
# This code is contributed by SANKAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using System; 
class GFG 
{
  
// Function to find the 
// minimum element in a 
// max heap 
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; 
  
// Driver code
public static void Main()
{
    // Number of nodes 
    int n = 10; 
      
    // heap represents the following max heap: 
    //             20 
    //           /     \ 
    //          18     10 
    //       /  \    / \ 
    //      12   9  9   3 
    //     /  \ / 
    //    5   6 8 
    int[] heap = { 20, 18, 10, 12, 9, 
                    9, 3, 5, 6, 8 }; 
      
    Console.Write(findMinimumElement(heap, n)); 
}
}
  
// This code is contributed by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach 
  
// Function to find the 
// minimum element in a 
// max heap 
function findMinimumElement(&$heap, $n
    $minimumElement = $heap[0]; 
  
    for ($i = 1; $i < $n; ++$i
        $minimumElement = min($minimumElement
                              $heap[$i]); 
  
    return $minimumElement
  
// Driver code 
  
// Number of nodes 
$n = 10; 
  
// heap represents the following 
// max heap: 
//         20 
//       /    \ 
//     18     10 
//    /  \   /  \ 
// 12   9  9   3 
// / \ / 
// 5 6 8 
$heap = array(20, 18, 10, 12, 9, 
               9, 3, 5, 6, 8 ); 
  
echo findMinimumElement($heap, $n); 
  
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right


Output:

3

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 are 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the
// minimum element in a
// max heap
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;
}
  
// Driver code
int main()
{
    // Number of nodes
    int n = 10;
    // heap represents the following max heap:
    //        20
    //       /    \
    //     18     10
    //   /    \    /  \
    //   12     9  9   3
    //  /  \   /
    // 5    6 8
    int heap[] = { 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
  
    cout << findMinimumElement(heap, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
import java.util.*;
import java.lang.*;
  
class GFG
{
   
// Function to find the
// minimum element in a
// max heap
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;
}
  
// Driver code
public static void main(String args[])
{
    // Number of nodes
    int n = 10;
    // heap represents the following max heap:
    //     20
    //     / \
    //     18     10
    // / \ / \
    // 12     9 9 3
    // / \ /
    // 5 6 8
    int heap[] = new int[]{ 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 };
  
    System.out.println( findMinimumElement(heap, n));
}
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of
# above approach
  
# Function to find the minimum
# element in a max heap
def findMiniumumElement(heap, n):
  
    # // -->Floor Division Arithmetic Operator
    minimumElement = heap[n // 2]
  
    for i in range(1 + n // 2, n):
        minimumElement = min(minimumElement, 
                                    heap[i])
  
    return minimumElement
  
# Driver Code
n = 10 # Numbers Of Node
  
# heap represents the 
# following max heap:
# 20
# / \
# 18 10
# / \ / \
# 12 9 9 3
# / \ \
# 5 6 8
#
  
heap = [20, 18, 10, 12, 8
            9, 3, 5, 6, 8]
  
print(findMiniumumElement(heap, n))
  
# This code is contributed by SANKAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
  
class GFG
{
      
// Function to find the minimum element 
// in a max heap 
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; 
  
// Driver code 
static public void Main ()
{
      
    // Number of nodes 
    int n = 10; 
      
    // heap represents the following
    // max heap: 
    // 20 
    // / \ 
    // 18 10 
    // / \ / \ 
    // 12 9 9 3 
    // / \ / 
    // 5 6 8 
    int []heap = new int[]{ 20, 18, 10, 12, 9,
                              9, 3, 5, 6, 8 }; 
      
    Console.WriteLine(findMinimumElement(heap, n)); 
  
// This code is contributed 
// by ajit 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function to find the
// minimum element in a
// max heap
function findMinimumElement($heap, $n)
{
    $minimumElement = $heap[$n/2];
  
    for ($i = 1 + $n / 2; $i < $n; ++$i)
        $minimumElement = min($minimumElement, $heap[$i]);
  
    return $minimumElement;
}
  
    // Driver code
    // Number of nodes
    $n = 10;
    // heap represents the following max heap:
    //   20
    //   / \
    //   18  10
    // / \ / \
    // 12    9 9 3
    // / \ /
    // 5 6 8
    $heap = Array(20, 18, 10, 12, 9, 9, 3, 5, 6, 8 );
  
    echo(findMinimumElement($heap, $n));
  
//  This code is contributed to Rajput-Ji
?>

chevron_right


Output:

3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.