Sub topic 3
Node, leaf, and height formulas
This chapter assumes you know the vocabulary from the first two chapters (root, leaf, depth, height, level, and the full, complete, perfect, and skewed forms) and can do basic exponent and logarithm arithmetic. The formulas here let you answer many GATE questions with a few lines of algebra. They include the height of a tree with n nodes, the number of leaves in a tree with a given number of two-child nodes, and the maximum number of nodes at a level.
Fix the conventions first
Most wrong answers on these questions come from using a different convention than the question uses. This chapter uses the following.
- The depth of the root is 0, and the level of the root is 0.
- The height of a tree is the number of edges on the longest root-to-leaf path. A single node has height 0.
- The empty tree has height -1. This makes the recursive formula
height = 1 + max(left, right)work for leaves. - n is the total number of nodes. n0, n1, and n2 are the numbers of nodes with 0, 1, and 2 children.
Nodes per level and per height
A binary tree has at most 2 children per node, so each level can hold at most twice as many nodes as the one above it.
- The maximum number of nodes at level i is 2^i.
- The maximum number of nodes in a tree of height h is the sum 2^0 + 2^1 + ... + 2^h = 2^(h+1) - 1.
- The minimum number of nodes in a tree of height h is h + 1. This is a skewed tree, where each level holds one node.
Combining the last two facts gives the range for n:
h + 1 ≤ n ≤ 2^(h+1) - 1
Height bounds from the node count
Read the inequality the other way to bound h when n is known.
- From n ≤ 2^(h+1) - 1, we get h ≥ ceil(log2(n + 1)) - 1. This equals floor(log2 n) for every n ≥ 1.
- From h + 1 ≤ n, we get h ≤ n - 1.
So for any binary tree with n ≥ 1 nodes:
floor(log2 n) ≤ h ≤ n - 1
For n = 1000, 2^9 = 512 and 2^10 = 1024, so the minimum height is 9 and the maximum is 999.
| Quantity | Formula (edge-counted height h) |
|---|---|
| Max nodes at level i | 2^i |
| Max nodes with height h | 2^(h+1) - 1 |
| Min nodes with height h | h + 1 |
| Min height with n nodes | floor(log2 n) |
| Max height with n nodes | n - 1 |
| Edges in any tree with n nodes | n - 1 |
The last row holds for every tree, not only binary ones. Each node except the root has exactly one parent edge.
The leaf relation: n0 = n2 + 1
In any non-empty binary tree, the number of leaves is one more than the number of nodes with two children. The proof counts edges in two ways.
- Counting by nodes, the number of edges is n - 1 = n0 + n1 + n2 - 1.
- Counting by parents, each node with k children contributes k edges, so the number of edges is n1 + 2·n2.
- Setting these equal gives n0 + n1 + n2 - 1 = n1 + 2·n2. Cancelling n1 and n2 leaves n0 = n2 + 1.
Nodes with one child (n1) do not appear in the result. That is why GATE questions give you n1 as a distraction.
Here is a worked example. A binary tree has 5 nodes with one child and 4 nodes with two children. Then n0 = 4 + 1 = 5, and the total is n = 5 + 5 + 4 = 14.
In the diagram, A and B have two children, C has one, and D, E, F have none. So n2 = 2 and n0 = 3, which matches n2 + 1. The tree has 6 nodes and 5 edges, which matches n - 1.
Full binary trees
A full binary tree has nodes with either 0 or 2 children, so n1 = 0. The relation n0 = n2 + 1 then gives:
- n = n0 + n2 = 2·n2 + 1, so n is always odd.
- With L leaves, n = 2L - 1 and the number of internal nodes is L - 1.
- The tallest full tree with n nodes has height (n - 1)/2, because each level below the root adds a leaf and an internal node.
- A full tree of height h has at least 2h + 1 nodes.
For example, a full binary tree with 20 leaves has 19 internal nodes and 39 nodes in total.
Perfect and complete binary trees
A perfect binary tree of height h has every level full:
- n = 2^(h+1) - 1
- leaves = 2^h
- internal nodes = 2^h - 1
A complete binary tree fills every level except possibly the last, and fills the last level from the left. Its height is fixed by n alone:
- h = floor(log2 n)
- A complete tree of height h has between 2^h and 2^(h+1) - 1 nodes.
- The number of leaves is ceil(n / 2), and the number of internal nodes is floor(n / 2).
You can check the leaf count with the array numbering from the representation chapter. Nodes are numbered 1 to n, and node i has children 2i and 2i + 1. Node i has a child only if 2i ≤ n, so the internal nodes are exactly 1 through floor(n / 2).
The same facts show that complete trees are as short as a binary tree can be. A complete tree has the minimum height floor(log2 n) for its n.
Trees with more than two children
Some questions replace "binary" with "k-ary" or "every internal node has 3 children." The edge-counting argument extends directly.
Take a tree where every internal node has exactly k children. Call the number of internal nodes i and the number of leaves L.
- Counting edges by parent gives k·i edges. Counting by nodes gives n - 1 = i + L - 1 edges.
- Setting these equal gives k·i = i + L - 1, so L = (k - 1)·i + 1.
- The total is n = k·i + 1.
For k = 2 this is L = i + 1, the binary result. For a tree with every internal node having 3 children and 10 internal nodes, L = 2·10 + 1 = 21 and n = 31.
The maximum number of nodes in a k-ary tree of height h is (k^(h+1) - 1) / (k - 1). This is a geometric series.
Checking the formulas in code
This script builds random binary trees by attaching each new node to a random empty child slot. It then checks the leaf relation and the height bounds for every size from 1 to 200.
import random
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def random_tree(n):
root = Node(0)
free = [(root, "left"), (root, "right")]
for key in range(1, n):
parent, side = free.pop(random.randrange(len(free)))
child = Node(key)
setattr(parent, side, child)
free.append((child, "left"))
free.append((child, "right"))
return root
def height(node):
if node is None:
return -1
return 1 + max(height(node.left), height(node.right))
def count_by_children(node, counts):
if node is None:
return
k = (node.left is not None) + (node.right is not None)
counts[k] += 1
count_by_children(node.left, counts)
count_by_children(node.right, counts)
for n in range(1, 201):
for _ in range(20):
root = random_tree(n)
counts = [0, 0, 0]
count_by_children(root, counts)
n0, n1, n2 = counts
assert n0 == n2 + 1
assert n0 + n1 + n2 == n
h = height(root)
assert n.bit_length() - 1 <= h <= n - 1
print("all checks passed")n.bit_length() - 1 computes floor(log2 n) exactly for integers, which avoids floating-point error from math.log2. The recursion depth stays below Python's default limit because the random trees are short on average.
Common errors
- Using n0 = n2 + 1 for a tree that is not binary. The relation needs every node to have at most 2 children.
- Forgetting that the maximum height is n - 1, not n. A skewed tree with 5 nodes has height 4 in the edge convention.
- Applying "n is odd" to trees that are not full. A complete tree can have any n.
- Mixing level and height. Level is measured from the root down, height from a node to its deepest leaf. In a perfect tree the leaves are at level h.
- Treating the number of edges as the number of nodes in a path-length question. A path with k edges visits k + 1 nodes.
Exercise
A binary tree has 30 nodes with two children and 12 nodes with one child. How many nodes does it have in total, and what is the minimum possible height of any binary tree with that many nodes?
Show answer
n0 = n2 + 1 = 31, so n = 31 + 12 + 30 = 73. The minimum height is floor(log2 73) = 6, since 2^6 = 64 ≤ 73 < 128.
Exercise
A tree in which every internal node has exactly 4 children has 25 leaves. How many internal nodes does it have?
Show answer
With k = 4, L = (k - 1)·i + 1, so 25 = 3i + 1 and i = 8. The tree has 8 internal nodes and 33 nodes in total.
Exercise
A complete binary tree has 100 nodes. Give its height, its number of leaves, and its number of internal nodes.
Show answer
Height = floor(log2 100) = 6 because 64 ≤ 100 < 128. Leaves = ceil(100 / 2) = 50. Internal nodes = floor(100 / 2) = 50.
Exercise
What is the maximum number of nodes in a binary tree of height 4 if height counts nodes, so a single node has height 1?
Show answer
Under the node-counting convention, height 4 means 4 levels, so the maximum is 2^4 - 1 = 15. Under the edge convention the same formula would give 2^5 - 1 = 31 for height 4, so the convention changes the answer.