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

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 : () / \ () () / \ / \ () () () () / \ () ()

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;` `}` |

## 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 :**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**