Minimum element in a max heap - GeeksforGeeks

10 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to find the// minimum element in a// max heapint findMinimumElement(int heap[], int n){    int minimumElement = heap[0];     for (int i = 1; i < n; ++i)        minimumElement = min(minimumElement, heap[i]);     return minimumElement;} // Driver codeint 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;}
// 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}
# 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 Coden = 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
// C# implementation of above approachusing 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
<?php// PHP implementation of above approach // Function to find the// minimum element in a// max heapfunction 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?>
<script>     // Javascript implementation of above approach         // Function to find the    // minimum element in a    // max heap    function findMinimumElement(heap, n)    {        let minimumElement = heap[0];          for (let i = 1; i < n; ++i)            minimumElement = Math.min(minimumElement, heap[i]);          return minimumElement;    }         // Number of nodes    let n = 10;     // heap represents the following max heap:    //             20    //           /     \    //          18     10    //       /  \    / \    //      12   9  9   3    //     /  \ /    //    5   6 8    let heap = [ 20, 18, 10, 12, 9, 9, 3, 5, 6, 8 ];     document.write(findMinimumElement(heap, n));     </script>
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 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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to find the// minimum element in a// max heapint 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 codeint 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;}
// Java implementation of above approachimport 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)
# 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 Coden = 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, 9,        9, 3, 5, 6, 8] print(findMiniumumElement(heap, n)) # This code is contributed by SANKAR
// C# implementation of above approachusing 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
<?php// PHP implementation of above approach // Function to find the// minimum element in a// max heapfunction 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?>
<script>    // Javascript implementation of above approach         // Function to find the minimum element    // in a max heap    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;    }         // Number of nodes    let n = 10;     // heap represents the following    // max heap:    // 20    // / \    // 18 10    // / \ / \    // 12 9 9 3    // / \ /    // 5 6 8    let heap = [ 20, 18, 10, 12, 9, 9,  3,  5,  6,  8 ];     document.write(findMinimumElement(heap, n));     </script>
3
 
Shivi_Aggarwal
ankthon
ukasp
Akanksha_Rai
vaibhav29498
jit_t
sankarguru63
Rajput-Ji
nemanjaboskovic17
suresh07
divyeshrabadiya07
Arrays
Heap
Technical Scripter
Arrays
Heap
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Huffman Coding | Greedy Algo-3
K'th Smallest/Largest Element in Unsorted Array | Set 1
k largest(or smallest) elements in an array
Building Heap from Array
Insertion and Deletion in Heaps