Maximum element in min heap
Last Updated :
15 Mar, 2023
Given a min heap, find the maximum element present in the heap.
Examples:
Input : 10
/ \
25 23
/ \ / \
45 30 50 40
Output : 50
Input : 20
/ \
40 28
Output : 40
Brute force approach:
We can check all the nodes in the min-heap to get the maximum element. Note that this approach works on any binary tree and does not makes use of any property of the min-heap. It has a time and space complexity of O(n). Since min-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.
Algorithm:
Step 1: Create a function named "findMaximumElement" which takes the heap array and the number of nodes n as input parameter with the int return type.
Step 2: Create a variable named "maximumElement" and initialize it with the first element of the heap array.
Step 3: Start a for loop and traverse over the elements of the heap array starting from the second element.
Step 4: In each iteration, compare the current element with the "maximumElement" and update it if the current element is greater.
Step 5: After the loop completes, return the "maximumElement".
Below is the implementation of above approach:
C++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// maximum element in a
// min heap
int findMaximumElement(int heap[], int n)
{
int maximumElement = heap[0];
for (int i = 1; i < n; ++i)
maximumElement = max(maximumElement, heap[i]);
return maximumElement;
}
// Driver code
int main()
{
// Number of nodes
int n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
// 63 65 81
int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 };
cout << findMaximumElement(heap, n);
return 0;
}
Java
// Java implementation of above approach
class GFG {
// Function to find the maximum element
// in a min heap
static int findMaximumElement(int[] heap, int n) {
int maximumElement = heap[0];
for (int i = 1; i < n; ++i) {
maximumElement = Math.max(maximumElement,
heap[i]);
}
return maximumElement;
}
// Driver code
public static void main(String[] args) {
// Number of nodes
int n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
//63 65 81
int[] heap = {10, 25, 23, 45, 50,
30, 35, 63, 65, 81};
System.out.print(findMaximumElement(heap, n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of above approach
# Function to find the maximum element
# in a min heap
def findMaximumElement(heap, n):
maximumElement = heap[0];
for i in range(1, n):
maximumElement = max(maximumElement, heap[i]);
return maximumElement;
# Driver code
if __name__ == '__main__':
# Number of nodes
n = 10;
# heap represents the following min heap:
# 10
# / \
# 25 23
# / \ / \
# 45 50 30 35
# / \ /
#63 65 81
heap = [ 10, 25, 23, 45, 50,
30, 35, 63, 65, 81 ];
print(findMaximumElement(heap, n));
# This code is contributed by Princi Singh
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to find the maximum element
// in a min heap
static int findMaximumElement(int[] heap, int n)
{
int maximumElement = heap[0];
for (int i = 1; i < n; ++i)
maximumElement = Math.Max(maximumElement,
heap[i]);
return maximumElement;
}
// Driver code
public static void Main()
{
// Number of nodes
int n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
//63 65 81
int[] heap = { 10, 25, 23, 45, 50,
30, 35, 63, 65, 81 };
Console.Write(findMaximumElement(heap, n));
}
}
// This code is contributed by Akanksha Rai
JavaScript
<script>
// JavaScript implementation of above approach
// Function to find the maximum element
// in a min heap
function findMaximumElement(heap , n) {
var maximumElement = heap[0];
for (i = 1; i < n; ++i) {
maximumElement = Math.max(maximumElement,
heap[i]);
}
return maximumElement;
}
// Driver code
// Number of nodes
var n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
//63 65 81
var heap = [10, 25, 23, 45, 50,
30, 35, 63, 65, 81];
document.write(findMaximumElement(heap, n));
// This code contributed by aashish1995
</script>
Efficient approach:
The min heap property requires that the parent node be lesser than its child node(s). Due to this, we can conclude that a non-leaf node cannot be the maximum element as its child node has a higher value. So we can narrow down our search space to only leaf nodes. In a min 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++14
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// maximumelement in a
// max heap
int findMaximumElement(int heap[], int n)
{
int maximumElement = heap[n / 2];
for (int i = 1 + n / 2; i < n; ++i)
maximumElement = max(maximumElement, heap[i]);
return maximumElement;
}
// Driver code
int main()
{
// Number of nodes
int n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
//63 65 81
int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 };
cout << findMaximumElement(heap, n);
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// Function to find the
// maximumelement in a
// max heap
static int findMaximumElement(int heap[], int n)
{
int maximumElement = heap[n / 2];
for (int i = 1 + n / 2; i < n; ++i)
maximumElement = Math.max(maximumElement, heap[i]);
return maximumElement;
}
// Driver code
public static void main(String args[])
{
// Number of nodes
int n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
//63 65 81
int heap[] = { 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 };
System.out.println(findMaximumElement(heap, n));
}
}
Python 3
# Python 3 implementation of
# above approach
# Function to find the maximum
# element in a max heap
def findMaximumElement(heap, n):
maximumElement = heap[n // 2]
for i in range(1 + n // 2, n):
maximumElement = max(maximumElement,
heap[i])
return maximumElement
# Driver Code
n = 10 # Numbers Of Node
# heap represents the following min heap:
# 10
# / \
# 25 23
# / \ / \
# 45 50 30 35
# / \ /
# 63 65 81
heap = [10, 25, 23, 45, 50,
30, 35, 63, 65, 81]
print(findMaximumElement(heap, n))
# This code is contributed by Yogesh Joshi
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to find the
// maximumelement in a
// max heap
static int findMaximumElement(int[] heap,
int n)
{
int maximumElement = heap[n / 2];
for (int i = 1 + n / 2; i < n; ++i)
maximumElement = Math.Max(maximumElement,
heap[i]);
return maximumElement;
}
// Driver code
public static void Main()
{
// Number of nodes
int n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
//63 65 81
int[] heap = { 10, 25, 23, 45, 50,
30, 35, 63, 65, 81 };
Console.WriteLine(findMaximumElement(heap, n));
}
}
// This code is contributed
// by Akanksha Rai
JavaScript
<script>
// javascript implementation of above approach
// Function to find the
// maximumelement in a
// max heap
function findMaximumElement(heap , n) {
var maximumElement = heap[n / 2];
for (i = 1 + n / 2; i < n; ++i)
maximumElement = Math.max(maximumElement, heap[i]);
return maximumElement;
}
// Driver code
// Number of nodes
var n = 10;
// heap represents the following min heap:
// 10
// / \
// 25 23
// / \ / \
// 45 50 30 35
// / \ /
// 63 65 81
var heap = [ 10, 25, 23, 45, 50, 30, 35, 63, 65, 81 ];
document.write(findMaximumElement(heap, n));
// This code contributed by aashish1995
</script>
Similar Reads
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 a
9 min read
Kâth Least Element in a Min-Heap Given a min-heap of size n, find the kth least element in the min-heap. Examples: Input : {10, 50, 40, 75, 60, 65, 45} k = 4 Output : 50 Input : {10, 50, 40, 75, 60, 65, 45} k = 2 Output : 40 Naive approach: We can extract the minimum element from the min-heap k times and the last element extracted
15+ min read
K-th Greatest Element in a Max-Heap Given a max-heap of size n, find the kth greatest element in the max-heap. Examples: Input : maxHeap = {20, 15, 18, 8, 10, 5, 17} k = 4 Output : 15 Input : maxHeap = {100, 50, 80, 10, 25, 20, 75} k = 2 Output : 80 Naive approach: We can extract the maximum element from the max-heap k times and the l
15+ min read
Max Heap meaning in DSA A max heap is a complete binary tree in which every parent node has a value greater than or equal to its children nodes. Example of Max HeapProperties of a Max Heap :A max heap is a complete binary tree, meaning that all levels of the tree are completely filled, except possibly the last level which
3 min read
Min Heap meaning in DSA A min heap is a binary tree-based data structure where the value of each node is less than or equal to its child nodes. In other words, the root node is always the minimum element in the heap. Here is an example of the min heap tree: Example of min HeapArray representation of the above min heapChara
3 min read