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)
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.
The minimum height is h=ceiling( log(n+1)/log(2) -1) for any binary tree.