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

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

**Examples :**

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

Let the size of heap be **N** and height be **h**

If we take few examples, we can notice that the value of h in a complete binary tree is ceil(log_{2}(N+1)) – 1.

Examples :

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

## 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` `ceil` `(log2(N + 1)) - 1; ` `} ` ` ` `// driver node ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` `cout << height(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Relationship between number of nodes and height of binary tree
- Print the nodes of the Binary Tree whose height is a Prime number
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Print path from root to all nodes in a Complete Binary Tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Find height of a special binary tree whose leaf nodes are connected
- Print all the leaf nodes of Binary Heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Tournament Tree (Winner Tree) and Binary Heap
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Traversal of tree with k jumps allowed between nodes of same height
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Check if a given Binary Tree is Heap
- Height of binary tree considering even level leaves only
- How to determine if a binary tree is height-balanced?
- Linked complete binary tree & its creation
- Sideways traversal of a Complete Binary Tree
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Iterative Method to find Height of Binary Tree
- Find the largest Complete Subtree in a given Binary Tree

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.