Binary Tree Height

后端 未结 8 2030
迷失自我
迷失自我 2020-12-29 12:34

I need a general formula to calculate the minimum height of the binary tree and the maximum height of the binary tree. (not the binary search tree)

相关标签:
8条回答
  • 2020-12-29 13:41

    N - number of nodes.
    H - height of the binary tree.

    Complete Binary Tree:
    Then, with H height N would lie between:

    2^H <= N <= (2^(H+1) - 1)
    

    Thus, solving this inequality; we get :

    H <= lg(N)  and  H >= (lg(N+1) - 1)
    

    Hence we finally get:

    H = floor( lg(N) ) = ceil( (lg(N+1) - 1) )   //as H is integer
    

    (lg : log base 2)

    Binary Tree (not necessarily complete):

    Max height = N;  
    
    Min Height = floor( lg(N) ) = ceil( (lg(N+1) - 1) )
    

    We get minimum height when binary tree is complete.

    0 讨论(0)
  • 2020-12-29 13:41

    The minimum height is h=ceiling( log(n+1)/log(2) -1) for any binary tree.

    0 讨论(0)
提交回复
热议问题