Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap

Given the level order traversal of a Complete Binary Tree, determine whether the Binary Tree is a valid Min-Heap

Examples:

Input : level = [10, 15, 14, 25, 30]
Output : True
The tree of the given level order traversal is
                     10
                    /  \
                   15   14
                  /  \
                 25   30
We see that each parent has a value less than
its child, and hence satisfies the min-heap 
property
 
Input : level = [30, 56, 22, 49, 30, 51, 2, 67]
Output : False
The tree of the given level order traversal is
                         30
                      /      \
                    56         22
                 /      \     /   \
               49        30  51    2
              /
             67
We observe that at level 0, 30 > 22, and hence
min-heap property is not satisfied



We need to check whether each non-leaf node (parent) satisfies the heap property. For this, we check whether each parent (at index i) is smaller than its children (at indices 2*i+1 and 2*i+2, if the parent has two children). If only one child, we only check the parent against index 2*i+1.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if a given tree is
// Binary Heap or not
#include <bits/stdc++.h>
using namespace std;
  
// Returns true if given level order traversal
// is Min Heap.
bool isMinHeap(int level[], int n)
{
    // First non leaf node is at index (n/2-1).
    // Check whether each parent is greater than child
    for (int i=(n/2-1) ; i>=0 ; i--)
    {
        // Left child will be at index 2*i+1
        // Right child will be at index 2*i+2
        if (level[i] > level[2 * i + 1])
            return false;
  
        if (2*i + 2 < n)
        {
            // If parent is greater than right child
            if (level[i] > level[2 * i + 2])
                return false;
        }
    }
    return true;
}
  
// Driver code
int main()
{
    int level[] = {10, 15, 14, 25, 30};
    int n = sizeof(level)/sizeof(level[0]);
    if  (isMinHeap(level, n))
        cout << "True";
    else
        cout << "False";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if a given tree is
// Binary Heap or not
import java.io.*;
import java.util.*;
  
public class detheap
{
    // Returns true if given level order traversal
    // is Min Heap.
    static boolean isMinHeap(int []level)
    {
        int n = level.length - 1;
  
        // First non leaf node is at index (n/2-1).
        // Check whether each parent is greater than child
        for (int i=(n/2-1) ; i>=0 ; i--)
        {
            // Left child will be at index 2*i+1
            // Right child will be at index 2*i+2
            if (level[i] > level[2 * i + 1])
                return false;
  
            if (2*i + 2 < n)
            {
                // If parent is greater than right child
                if (level[i] > level[2 * i + 2])
                   return false;
            }
        }
        return true;
    }
  
    // Driver code
    public static void main(String[] args) 
                              throws IOException
    {
        // Level order traversal
        int[] level = new int[]{10, 15, 14, 25, 30};
  
        if  (isMinHeap(level))
            System.out.println("True");
        else
            System.out.println("False");
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if a given tree 
// is Binary Heap or not 
using System;
  
class GFG
{
// Returns true if given level 
// order traversal is Min Heap. 
public static bool isMinHeap(int[] level)
{
    int n = level.Length - 1;
  
    // First non leaf node is at
    // index (n/2-1). Check whether 
    // each parent is greater than child 
    for (int i = (n / 2 - 1) ; i >= 0 ; i--)
    {
        // Left child will be at index 2*i+1 
        // Right child will be at index 2*i+2 
        if (level[i] > level[2 * i + 1])
        {
            return false;
        }
  
        if (2 * i + 2 < n)
        {
            // If parent is greater than right child 
            if (level[i] > level[2 * i + 2])
            {
            return false;
            }
        }
    }
    return true;
}
  
// Driver code 
public static void Main(string[] args)
{
    // Level order traversal 
    int[] level = new int[]{10, 15, 14, 25, 30};
  
    if (isMinHeap(level))
    {
        Console.WriteLine("True");
    }
    else
    {
        Console.WriteLine("False");
    }
}
}
  
// This code is contributed by Shrikant13

chevron_right


PHP

= 0; $i–)
{
// Left child will be at index 2*i+1
// Right child will be at index 2*i+2
if ($level[$i] > $level[2 * $i + 1])
return false;

if (2 * $i + 2 < $n) { // If parent is greater than right child if ($level[$i] > $level[2 * $i + 2])
return false;
}
}
return true;
}

// Driver code
$level = array(10, 15, 14, 25, 30);
$n = sizeof($level);
if (isMinHeap($level, $n))
echo “True”;
else
echo “False”;

// This code is contributed
// by Akanksha Rai

Output:

True

These algorithms run with worse case O(n) complexity

This article is contributed by Deepak Srivatsav. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : shrikanth13, Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.