Related Articles
Minimum element in a max heap
• Last Updated : 11 Dec, 2020

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

 `// C++ implementation of above approach` `#include ` `using` `namespace` `std;`   `// Function to find the` `// minimum element in a` `// max heap` `int` `findMinimumElement(``int` `heap[], ``int` `n)` `{` `    ``int` `minimumElement = heap;`   `    ``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;` `}`

## Java

 `// 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` `}`

## Python3

 `# 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`

## C#

 `// 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;`   `        ``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

 ``

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

 `// C++ implementation of above approach` `#include ` `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;` `}`

## Java

 `// 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)`

## Python3

 `# 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``, ``9``,` `        ``9``, ``3``, ``5``, ``6``, ``8``]`   `print``(findMiniumumElement(heap, n))`   `# This code is contributed by SANKAR`

## C#

 `// 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`

## PHP

 ``

Output:

`3`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :