Open In App

Height of a complete binary tree (or Heap) with N nodes

Last Updated : 09 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Consider a Binary Heap of size N. We need to find the height of it.

Examples:  

Input : N = 6
Output : 2
        ()
      /    \
     ()     ()
    /  \    /
  ()    () ()

Input : N = 9
Output : 3
        ()
      /    \
     ()     ()
    /  \    /  \
  ()    () ()   ()
 / \
()  ()
Recommended Practice

Let the size of the heap be N and the height be h. If we take a few examples, we can notice that the value of h in a complete binary tree is floor(log2N). 

Examples:  

 N    h
---------
 1    0
 2    1
 3    1
 4    2
 5    2
 .....
 .....

Implementation: 

C++




// CPP program to find height of complete
// binary tree from total nodes.
#include <bits/stdc++.h>
using namespace std;
 
int height(int N)
{
    return floor(log2(N));
}
 
// driver node
int main()
{
    int N = 2;
    cout << height(N);
    return 0;
}


Java




// Java program to find height
// of complete binary tree
// from total nodes.
import java.lang.*;
 
class GFG {
     
    // Function to calculate height
    static int height(int N)
    {
        return (int)Math.ceil(Math.log(N +
                    1) / Math.log(2)) - 1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(height(N));
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


Python 3




# Python 3 program to find
# height of complete binary
# tree from total nodes.
import math
def height(N):
    return math.ceil(math.log2(N + 1)) - 1
 
# driver node
N = 6
print(height(N))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# program to find height
// of complete binary tree
// from total nodes.
using System;
 
class GFG {
    static int height(int N)
    {
        return (int)Math.Ceiling(Math.Log(N
                   + 1) / Math.Log(2)) - 1;
    }
 
    // Driver node
    public static void Main()
    {
        int N = 6;
        Console.Write(height(N));
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal


PHP




<?php
// PHP program to find height
// of complete binary tree
// from total nodes.
 
function height($N)
{
    return ceil(log($N + 1, 2)) - 1;
}
 
// Driver Code
$N = 6;
echo height($N);
 
// This code is contributed by aj_36
?>


Javascript




<script>
 
    // Javascript program to find height
    // of complete binary tree
    // from total nodes.
     
    function height(N)
    {
        return Math.ceil(Math.log(N + 1) / Math.log(2)) - 1;
    }
     
      let N = 6;
      document.write(height(N));
         
</script>


Output

1

Time Complexity: O(1), Since performing constant operations.
Auxiliary Space: O(1), Since constant extra space is used.



Previous Article
Next Article

Similar Reads

Difference between Binary Heap, Binomial Heap and Fibonacci Heap
Binary Heap:A Binary Heap is a Binary Tree with following properties. It’s a complete binary tree i.e., all levels are completely filled except possibly the last level and the last level has all keys as left as possible. This property of Binary Heap makes them suitable to be stored in an array. A Binary Heap is either Min Heap or Max Heap. In a Min
2 min read
Print the nodes of the Binary Tree whose height is a Prime number
Given a binary tree, our task is to print the nodes whose height is a prime number starting from the root node. Examples: Input: 1 / \ 2 3 / \ 4 5 Output: 4 5 Explanation: For this tree: Height of Node 1 - 0, Height of Node 2 - 1, Height of Node 3 - 1, Height of Node 4 - 2, Height of Node 5 - 2. Hence, the nodes whose height is a prime number are 4
8 min read
Find height of a special binary tree whose leaf nodes are connected
Given a special binary tree whose leaf nodes are connected to form a circular doubly linked list, find its height. For example, 1 / \ 2 3 / \ 4 5 / 6 In the above binary tree, 6, 5 and 3 are leaf nodes and they form a circular doubly linked list. Here, the left pointer of leaf node will act as a previous pointer of circular doubly linked list and i
9 min read
Relationship between number of nodes and height of binary tree
Prerequisite - Binary Tree Data Structure In this article, we will discuss various cases for relationship between number of nodes and height of binary tree. Before understanding this article, you should have basic idea about binary trees and their properties. The height of the binary tree is the longest path from root node to any leaf node in the t
3 min read
Tournament Tree (Winner Tree) and Binary Heap
Given a team of N players. How many minimum games are required to find the second-best player? We can use adversary arguments based on the tournament tree (Binary Heap). A Tournament tree is a form of min (max) heap which is a complete binary tree. Every external node represents a player and the internal node represents the winner. In a tournament
6 min read
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 :
6 min read
Convert Min Heap to Max Heap
Given an array representation of min Heap, convert it to max Heap. Examples: Input: arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9} 3 / \ 5 9 / \ / \ 6 8 20 10 / \ /12 18 9 Output: arr[] = {20, 18, 10, 12, 9, 9, 3, 5, 6, 8} 20 / \ 18 10 / \ / \ 12 9 9 3 / \ /5 6 8 Input: arr[] = {3, 4, 8, 11, 13}Output: arr[] = {13, 11, 8, 4, 3} Approach: To solve the p
10 min read
Heap Sort for decreasing order using min heap
Given an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1} Output : arr[] = {10, 5, 3, 1} Input : arr[] = {1, 50, 100, 25} Output : arr[] = {100, 50, 25, 1} Prerequisite: Heap sort using min heap. Algorithm : Build a min heap from the input data. At this point, the smallest item is stored
13 min read
When building a Heap, is the structure of Heap unique?
What is Heap? A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap). Properties of Heap: Structural Property: This property states that it should be A Complete Binary Tree. For
4 min read
Difference between Min Heap and Max Heap
A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Since a heap is a complete binary tree, a heap with N nodes has log N height. It is useful to remove the highest or lowest priority element. It is typically represented as an array. There are two types of Heaps in the data structure. Min-HeapIn a Min-Heap the
3 min read
Article Tags :
Practice Tags :