Open In App

How to check if a given array represents a Binary Heap?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
29 Likes
Like
Report

Given an array, how to check if the given array represents a Binary Max-Heap.
Examples: 

Input:  arr[] = {90, 15, 10, 7, 12, 2} 
Output: True
The given array represents below tree
       90
     /    \
   15      10
  /  \     /
 7    12  2 
The tree follows max-heap property as every
node is greater than all of its descendants.

Input:  arr[] = {9, 15, 10, 7, 12, 11} 
Output: False
The given array represents below tree
       9
     /    \
   15      10
  /  \     /
 7    12  11
The tree doesn't follows max-heap property 9 is 
smaller than 15 and 10, and 10 is smaller than 11. 

A Simple Solution is to first check root if it's greater than all of its descendants. Then check for children of the root. Time complexity of this solution is O(n2)

An Efficient Solution is to compare root only with its children (not all descendants), if root is greater than its children and the same is true for all nodes, then tree is max-heap (This conclusion is based on transitive property of > operator, i.e., if x > y and y > z, then x > z).
The last internal node is present at index (n-2)/2 assuming that indexing begins with 0.

Below is the implementation of this solution. 

C++
// C program to check whether a given array
// represents a max-heap or not
#include <limits.h>
#include <stdio.h>

// Returns true if arr[i..n-1] represents a
// max-heap
bool isHeap(int arr[], int i, int n)
{
    // If (2 * i) + 1 >= n, then leaf node, so return true
    if (i >= (n - 1) / 2)
        return true;

    // If an internal node and is 
    // greater than its children,
    // and same is recursively 
    // true for the children
    if (arr[i] >= arr[2 * i + 1] && 
        arr[i] >= arr[2 * i + 2]
        && isHeap(arr, 2 * i + 1, n)
        && isHeap(arr, 2 * i + 2, n))
        return true;

    return false;
}

// Driver program
int main()
{
    int arr[] = { 90, 15, 10, 7, 12, 2, 7, 3 };
    int n = sizeof(arr) / sizeof(int) - 1;

    isHeap(arr, 0, n) ? printf("Yes") : printf("No");

    return 0;
}
Java Python3 C# PHP JavaScript

Output
Yes

Time complexity: O(n)
Auxiliary Space: O(h), Here h is the height of the given tree and the extra space is used due to the recursion call stack.

An Iterative Solution is to traverse all internal nodes and check id the node is greater than its children or not. 

C++
// C program to check whether a given array
// represents a max-heap or not
#include <stdio.h>
#include <limits.h>

// Returns true if arr[i..n-1] represents a
// max-heap
bool isHeap(int arr[],  int n)
{
    // Start from root and go till the last internal
    // node
    for (int i=0; i<=(n-2)/2; i++)
    {
        // If left child is greater, return false
        if (arr[2*i +1] > arr[i])
                return false;

        // If right child is greater, return false
        if (2*i+2 < n && arr[2*i+2] > arr[i])
                return false;
    }
    return true;
}

// Driver program
int main()
{
    int arr[] = {90, 15, 10, 7, 12, 2, 7, 3};
    int n = sizeof(arr) / sizeof(int);

    isHeap(arr, n)? printf("Yes"): printf("No");

    return 0;
}
Java Python3 C# PHP JavaScript

Output
Yes

Time complexity: O(n), Where n is the total number of elements in the given array.
Auxiliary Space: O(1), As constant extra space is used.

Thanks to Himanshu for suggesting this solution.


Does array represent Heap | DSA Problem
Article Tags :

Similar Reads