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++
// 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; } |
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[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 // 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 ?> |
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 <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; } |
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, 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[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 |
3
Recommended Posts:
- K’th Least Element in a Min-Heap
- Maximum element in min heap
- K-th Greatest Element in a Max-Heap
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Heap Sort for decreasing order using min heap
- Convert min Heap to max Heap
- Remove exactly one element from the array such that max - min is minimum
- Minimum sum of differences with an element in an array
- Minimum element of each row and each column in a matrix
- Sum of minimum element of all sub-sequences of a sorted array
- Sum of minimum element of all subarrays of a sorted array
- Sum of minimum absolute difference of each array element
- Minimum array element changes to make its elements 1 to N
- Array element with minimum sum of absolute differences
- Sum and Product of minimum and maximum element of an Array
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.



