Perfect Binary Tree

  • Explain why the height of a complete binary tree is O(lgN)\Omicron(\lg N).

A perfect binary tree is a binary tree in which all interior nodes have two children, and all leaves have the same depth or same level

The height of a perfect binary tree is O(lgn)\Omicron(\lg n), where nn is the number of nodes.

You can verify this by looking at the example above: a perfect binary tree has 11 node at level (depth) 00, 22 nodes at level 11, 44 nodes at level 22, and so on. Thus, it will have 2d2^d nodes at level dd. Adding these quantities, the total number of nodes nn for a full binary tree with depth dd is:

n=20+21+22++2d=2d+11 n = 2^0 + 2^1 + 2^2 + \dots + 2^d = 2^{d+1} − 1

For example, the full binary tree of depth 22 above has 231=72^3 – 1 = 7 nodes.

Now, considering the formula above for the number of nodes in a full binary search tree:

n=2d+11 n = 2^{d+1} − 1

Solving for dd, we get:

n+1=2d+1 n+1 = 2^{d+1}

lg(n+1)=lg(2d+1) \lg(n+1) = \lg(2^{d+1})

lg(n+1)=(d+1)lg(2) \lg(n+1) = (d+1)\lg(2)

lg(n+1)=(d+1) \lg(n+1) = (d+1)

d=lg(n+1)1 d = \lg(n+1) - 1

We know the height of the tree is the depth of the deepest node. So the height of a perfect binary tree is at most lg(n+1)1\lg(n+1) - 1 or O(lgn)\Omicron(\lg n).

Resources