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)
Height of the tree is the number of edges in the tree from the root to the deepest node, Height of the empty tree is 0.
In summary, in a complete binary tree with n nodes: the height is h = log2(n + 1), i.e. h is O(log n) • the number of leaves is lh = (n + 1)/2, i.e. roughly half of the nodes are at the leaves.
To calculate the height of the tree recursively, we need to find the height of it's left subtree and right subtree recursively and add 1 to them (height between the topmost node and its children).
Height of a node K (of a Binary Tree) = Number of edges in the longest path connecting K to any leaf node.
If you have N elements, the minimum height of a binary tree will be log2(N)+1.
For a full binary tree, the maximum height will be N/2.
For a non-full binary tree, the maximum height will be N.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With