I am working through Algorithms in C++ by Robert Sedgewick and came across the following statement:
The height of a binary tree with
Ninternal nodes is at leastlg N
and at mostN-1. The best case occurs in a balanced tree with2^i
internal nodes at every level except possibly the bottom level. If the
height is “h” then we must have2^(h-1) < N+1 <= 2^hsince there are N+1 external nodes.
There wasn’t much explanation surrounding the inequality, so my question is: how did the author deduce the inequality and what is it showing exactly?
Thanks!
The inequality
2^(h-1) < N + 1 <= 2^hdemonstrates that, for a given heighth, there is a range of node quantities that will havehas a minimum height in common. This is indicative of the property: all binary trees containing N nodes will have a height of at least log(N) rounded up to the next integer.For example, a tree with either
4, 5, 6 or 7nodes can have at best a minimum height of3. One less than this range, and you can have a tree of height2; one more and the best you can do is a height of4.If we map out the minimum height for a tree that grows from
3nodes to8nodes using the base 2 logarithms for N and round up, the inequality becomes clear:It might be useful to notice that the range (made up of
N+1different quantities) is directly related to the number of external nodes for a given tree. Take a tree with3nodes and having a height of2:add one node to this tree,
and regardless of where you place it, the height will increase by 1. We can then keep creating leaf nodes without changing the height until the tree contains 7 nodes in total, at which point, any further additions will increase the minimum possible height once more:
Originally, N was equal to
3nodes, which meantN+1 = 4and we saw that there were4quantities that had a common minimum height.If you need more information, I suggest you look up the properties of complete and balanced binary trees.