Sub topic 3
Node, Leaf, and Height Formulas
Assume you know basic recursion, object references, arrays, linked lists, and Big-O notation. This chapter uses a binary tree, where each node has at most two children.
Core vocabulary
A node stores a value and references to zero or more children. The root is the node at the top. A node directly connected above another node is its parent. The node below it is its child.
A node with no children is a leaf. A node with at least one child is an internal node. The degree of a node is its number of children. Therefore, the degree of a node in a binary tree is 0, 1, or 2.
The depth of a node is the number of edges from the root to that node. The root has depth 0. The height of a node is the number of edges on the longest downward path from that node to a leaf. A leaf has height 0. The height of the tree is the height of its root.
A level is often counted as depth plus 1. Thus, the root is at level 1 but depth 0. Always check which convention a question uses.
Counting nodes by height
Let the tree height be h, measured in edges.
At depth d, a binary tree can have at most 2^d nodes. Adding every level from depth 0 through depth h gives the maximum total:
1 + 2 + 4 + ... + 2^h = 2^(h + 1) - 1
Therefore:
- Maximum nodes for height
h:2^(h + 1) - 1 - Minimum nodes for height
h:h + 1 - Maximum height for
nnodes:n - 1 - Minimum possible height for
nnodes:ceil(log2(n + 1)) - 1
The minimum-height case is as full as possible. The maximum-height case is a chain in which every node has only one child.
| Quantity | Minimum or maximum | Formula |
|---|---|---|
| Nodes at depth `d` | Maximum | `2^d` |
| Total nodes at height `h` | Maximum | `2^(h + 1) - 1` |
| Total nodes at height `h` | Minimum | `h + 1` |
| Height with `n` nodes | Maximum | `n - 1` |
Leaf formulas
Let n0, n1, and n2 denote the number of nodes with zero, one, and two children.
Every nonempty binary tree satisfies:
n0 = n2 + 1
To see why, count edges. A tree with n nodes has n - 1 edges. Counting edges by children gives:
n1 + 2n2 = n - 1
Since n = n0 + n1 + n2, simplifying produces n0 = n2 + 1.
A full binary tree is one in which every internal node has exactly two children. It has no nodes with one child, so n1 = 0. If it has l leaves:
- Total nodes:
2l - 1 - Internal nodes:
l - 1 - Leaves:
(n + 1) / 2
A perfect binary tree is full and has every leaf at the same depth. For height h, it has 2^h leaves and 2^(h + 1) - 1 total nodes.
Worked calculation
Suppose a binary tree has height 4.
Its minimum number of nodes is 4 + 1 = 5. Its maximum number is 2^5 - 1 = 31. Thus, a tree of height 4 can contain from 5 through 31 nodes.
If the tree is perfect, it has 2^4 = 16 leaves and 31 total nodes. If it is a chain, it has only one leaf and 5 total nodes.
Exercise
A binary tree has 10 nodes. What are its minimum possible height and maximum possible height when height is measured in edges?
Show answer
The minimum height is ceil(log2(11)) - 1 = 3, because height 2 supports at most 7 nodes. The maximum height is 10 - 1 = 9.