Height of a complete binary tree

advertisements

How to solve below equation for the height of a full binary tree, which contains n number of nodes?

n=2^(h+1)-1

I got the answer as,

             n = 2^(h+1)-1
n+(-2^(h+1)+1) = 2^(h+1)-1 + (-2^(h+1)+1)
   n-2^(h+1)+1 = 0
             h = ln(n+2)/ln(2)

Is this equation solving is correct? If not, How to get h from n = 2^(h+1)-1 equation?


  1. We use "Complete" for a full binary tree so it is called a Complete Binary Tree instead of Full Binary Tree.
  2. Below is the derivation of h from the formula n=2^(h+1)-1

        n = 2^(h+1)-1
    
        n + 1 = 2^(h+1)
    
    

Taking log base 2 (ln2) of both sides

        ln2(n+1) = ln2(2^(h+1))

        ln2(n+1) = h+1

        ln2(n+1) - 1 = h

or

        h = ln2(n+1) - 1

I hope you get it right. Bingo.

Further I think you're not so familiar with the properties of logarithm. I will try to explain it here for you. ln2(8) is read as log 8 base 2. ln2(8) answers 3. How is it calculated? what's the answer of 2^3? it's 8. So we may say that taking a log is inverse of taking power. we can answer simple log problems like ln3(9) = ? , as 3^2 = 9 so ln3(9) results 2. Another example ln10(100) = ?, we know that 10^2 = 100, so ln10(100) = 2. You need to know the log properties for excelling in the course of Data Structures and Algorithms. It helps a lot.