Sub topic 17
Why Height Matters: Balanced and Skewed Trees
This chapter assumes you can read the Node class and the recursive height and depths functions from earlier chapters. It also assumes you use the edge-counting convention, where a leaf has height 0.
Height sets the cost of a walk
Many tree operations follow one path from the root downward. Searching for a value, inserting a node, and deleting a node all work this way. Each step moves down one edge, so the work done is at most the number of edges on the longest root-to-leaf path. That number is the height of the tree.
So the cost of these operations is O(h), where h is the tree height. The number of nodes, n, matters only through how it shapes h.
Two shapes with the same node count
Take n = 7 nodes. A balanced tree spreads them across levels. A skewed tree puts each node under the previous one, so every node has one child, like a linked list.
Both trees hold seven nodes, but their heights differ.
| Shape | Nodes (n) | Height (h) | Worst-case walk |
|---|---|---|---|
| Balanced binary | 7 | 2 | 2 edges |
| Skewed | 7 | 6 | 6 edges |
How height grows with n
In a skewed tree, h = n - 1, so the cost grows linearly with n. In a perfectly balanced binary tree, each level doubles the node count, so n = 2^(h+1) - 1. Solving for h gives h = log2(n + 1) - 1, which grows logarithmically.
The difference becomes large quickly. With 1,000,000 nodes, a balanced binary tree has a height of about 19. A skewed tree has a height of 999,999.
You can check this with the height function from earlier:
class Node:
def __init__(self, value, children=None):
self.value = value
self.children = children or []
def height(node):
return 1 + max((height(child) for child in node.children), default=-1)
def skewed(n):
# Build a chain of n nodes, each the only child of the one above.
node = Node(n)
for value in range(n - 1, 0, -1):
node = Node(value, [node])
return node
def balanced(levels):
# Build a perfect binary tree with the given number of levels.
if levels == 0:
return None
child = balanced(levels - 1)
return Node(levels, [child, child] if child else [])
print(height(skewed(7))) # 6
print(height(balanced(3))) # 2The balanced builder reuses one child object for both slots. That is fine here, because height only reads the tree.
Failure modes
- Recursion limits. The recursive
heightgoes as deep as the tree. On a skewed tree with a very large n, Python raisesRecursionErrorat the default limit of about 1,000 frames. Use an iterative traversal for such inputs. - Hidden skew. Inserting already sorted values into a plain binary search tree produces a skewed tree. Self-balancing trees such as AVL and red-black trees rotate nodes on insertion to keep h near log n.
Exercise
A binary search tree holds 15 nodes. What is its height if it is perfectly balanced, and what is it if it is completely skewed? Use edge counting.
Show answer
Balanced: 15 = 2^(h+1) - 1 gives h = 3. Skewed: h = n - 1 = 14. A search can take up to 3 steps in the first tree and 14 in the second.