6.1Binary Tree
Number of nodes and height
The running time of tree operations usually depends on the height: a search, an insertion or a deletion goes down from the root at most height steps. So this question matters: how short and how tall can a binary tree with n nodes be? This lesson gives the binary tree formulas that exams ask about most, together with where they come from.
- The most nodes a level, and a tree of height h, can hold
- The smallest and the largest height of a tree with n nodes
- How the number of leaves relates to the number of nodes with two children
1Each level holds at most twice the one above
The root is on level 0 and is alone. Since every node has at most two children, level 1 holds at most 2 nodes, level 2 at most 4 and level 3 at most 8. In general level k holds at most 2^k nodes.
In a tree of height h the levels go from 0 to h. If every level is filled to the brim (that is a perfect tree), the node count is 1 + 2 + 4 + ... + 2^h. This sum equals 2^(h+1) - 1. So a binary tree of height h has at most 2^(h+1) - 1 nodes.
The fewest nodes come when each level has a single node, that is when the tree is skewed: at least h + 1 nodes.
2The shortest and the tallest tree with n nodes
Turn the question around: you have n nodes. The tallest tree is the skewed one, with one node per level: the height is at most n - 1.
For the shortest tree you fill the levels as much as you can. With height h at most 2^(h+1) - 1 nodes fit, so n ≤ 2^(h+1) - 1. This gives h ≥ log2(n + 1) - 1. Rounded to a whole number, the smallest height is ⌊log2 n⌋. A complete tree has exactly this height.
| Node count n | Smallest height ⌊log2 n⌋ | Largest height n - 1 |
|---|---|---|
| 1 | 0 | 0 |
| 7 | 2 | 6 |
| 8 | 3 | 7 |
| 15 | 3 | 14 |
| 1000 | 9 | 999 |
| 1 000 000 | 19 | 999 999 |
Look at the last two rows. In a balanced tree with a million nodes you reach any node from the root in at most 19 steps; in a skewed tree it takes 999 999. The first column is why trees are fast, the second is their danger. The Unbalanced trees lesson comes back to this.
3Edges and NULL pointers
Like every tree, a binary tree with n nodes has n - 1 edges: each node except the root is joined to its parent by one edge.
With linked nodes every node carries two pointers (left and right), so there are 2n pointers in all. n - 1 of them are edges and point at a child. The remaining n + 1 pointers are NULL. A tree with 6 nodes has 7 NULL pointers; exams ask this often.
4Leaves and nodes with two children
Split the nodes by their number of children: L leaves (0 children), D1 nodes with one child and D2 nodes with two. Count two things:
- The nodes: n = L + D1 + D2.
- The edges: each edge goes down to a child, so there are D1 + 2·D2 edges. Since there are n - 1 edges, n - 1 = D1 + 2·D2.
Subtract the second equation from the first and you are left with 1 = L - D2. So in every binary tree the number of leaves is one more than the number of nodes with two children: L = D2 + 1. Nodes with one child do not affect this.
In a full tree D1 = 0. If there are i internal nodes, there are i + 1 leaves and 2i + 1 nodes in all. That is why a full binary tree always has an odd number of nodes.
5Worked examples
- What is the most nodes a binary tree of height 4 can have? 2^(4+1) - 1 = 31. The fewest is 4 + 1 = 5.
- What is the smallest height of a binary tree with 100 nodes? 2^6 = 64 ≤ 100 < 128 = 2^7, so ⌊log2 100⌋ = 6.
- A binary tree has 12 leaves. How many nodes have two children? L = D2 + 1, so D2 = 11.
- A full binary tree has 20 internal nodes. How many nodes does it have? 2·20 + 1 = 41, and 21 of them are leaves.
- How many NULL pointers does a linked binary tree with 50 nodes have? n + 1 = 51.
In books that count height in levels the formulas shift by one: the most nodes is 2^h - 1 and the smallest height is ⌈log2(n + 1)⌉. Check the definition in the question: is a one-node tree of height 0 or 1?
One last fact: 3 nodes can form 5 different binary tree shapes; 4 nodes can form 14 and 5 nodes 42. These are the Catalan numbers, and they answer the exam question "how many different binary trees can you draw with n nodes".
6Try it
What is the most nodes a binary tree of height 5 can have?
The answer is B. The most nodes come when every level from 0 to 5 is full: 2^(5+1) - 1 = 63.
Show the answer
The answer is B. The most nodes come when every level from 0 to 5 is full: 2^(5+1) - 1 = 63.
A binary tree has 7 nodes with two children and 4 nodes with one child. How many leaves does it have?
The answer is A. The number of leaves is one more than the number of nodes with two children: 7 + 1 = 8. Nodes with one child play no part in this.
Show the answer
The answer is A. The number of leaves is one more than the number of nodes with two children: 7 + 1 = 8. Nodes with one child play no part in this.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.