Skip to content
Related Articles

Related Articles

Check if Binary Heap is completely filled

Improve Article
Save Article
  • Last Updated : 07 Oct, 2022
Improve Article
Save Article

Given an integer N which denotes the number of elements in a binary heap. Check if the binary Heap is completely filled or not.

Examples:

Input: 7
Output: YES
Explanation: At height = 0, no. of elements = 1
At height = 1, no. of elements = 2
At height = 2, no. of elements = 4 
Last level is completely filled because at level 2 we need 22 = 4 
elements to fill completely and 4 elements are here.

Input: 16
Output: NO
Explanation: At height=0, no. of elements=1
At height = 1, no. of elements = 2
At height = 2, no. of elements = 4
At height = 3, no. of elements = 8
At height = 4, no. of elements = 1 
Last level is not completely filled because at level 4 
we need 24 = 16 elements to fill completely but here is only 1 element

Input: 31
Output: YES

Approach: The idea here is 

In a binary heap, a parent has 2 children. For each level h, number of elements are 2h

Follow the steps below to solve the given problem:

  • First find the number of nodes till height h = 20 + 21 + 22 + . . . + 2h.
  • Above written series is a geometric progression, so using sum of GP = ( a( rk-1 )/(r-1) ), here a = 20 = 1, r = 2, k = h.
  • Therefore sum = 1 * (2h – 1) / (2 – 1) = 2h – 1.
  • Height of a binary tree = log2N+ 1.
  • Now check if the sum is equal to N or not.

Below is the implementation of the above approach: 

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether
// it is completely filled or not
string isCompletelyFilled(int n)
{
    // Height of the heap
    int height = log2(n) + 1;
 
    // Sum of number of elements
    int sum = pow(2, height) - 1;
 
    // If sum equals to n print yes
    if (sum == n)
        return "Yes";
    return "No";
}
 
// Driver code
int main()
{
    int N = 7;
 
    // Function call
    cout << isCompletelyFilled(N);
    return 0;
}


Java




// JAVA program of above approach.
import java.util.*;
class GFG
{
 
// Function to check whether
// it is completely filled or not
static String isCompletelyFilled(int n)
{
    // Height of the heap
    int height =(int)(Math.log(n) / Math.log(2)) + 1;
 
    // Sum of number of elements
    int sum = (int)Math.pow(2, height) - 1;
 
    // If sum equals to n print yes
    if (sum == n)
        return "Yes";
    return "No";
}
 
// Driver code
public static void main(String[] args)
{
    int N = 7;
 
    // Function call
    System.out.print(isCompletelyFilled(N));
}
}
 
// This code is contributed by sanjoy_62.


Python3




# Python code to implement the approach
import math
 
# Function to check whether
# it is completely filled or not
def isCompletelyFilled(n) :
     
    # Height of the heap
    height = int(math.log2(n)) + 1
 
    # Sum of number of elements
    sum = pow(2, height) - 1
 
    # If sum equals to n print yes
    if (sum == n) :
        return "Yes"
    return "No"
 
 
# Driver code
 
N = 7
 
# Function call
print(isCompletelyFilled(N))
 
# This code is contributed by splevel62.


C#




// C# program of above approach.
 
using System;
 
public class GFG {
 
    // Function to check whether
    // it is completely filled or not
    static String isCompletelyFilled(int n)
    {
        // Height of the heap
        int height = (int)(Math.Log(n) / Math.Log(2)) + 1;
 
        // Sum of number of elements
        int sum = (int)Math.Pow(2, height) - 1;
 
        // If sum equals to n print yes
        if (sum == n)
            return "Yes";
        return "No";
    }
 
    static public void Main()
    {
 
        // Code
        int N = 7;
 
        // Function call
        Console.Write(isCompletelyFilled(N));
    }
}
 
// This code is contributed by lokeshmvs21.


Javascript




<script>
    // JavaScript code for the above approach
 
// Function to check whether
// it is completely filled or not
function isCompletelyFilled(n)
{
    // Height of the heap
    let height = Math.floor(Math.log(n) / Math.log(2)) + 1;
  
    // Sum of number of elements
    let sum = Math.pow(2, height) - 1;
  
    // If sum equals to n print yes
    if (sum == n)
        return "Yes";
    return "No";
}
 
    // Driver code
    let N = 7;
  
    // Function call
    document.write(isCompletelyFilled(N));
     
    // This code is contributed by code_hunt.
</script>


Output

Yes

Time Complexity: O(logN), time taken by pow is logN
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!