Sub topic 4
Tree Shapes and Why They Matter
This chapter assumes you can write the Node class and the recursive height function from the earlier chapters. It also assumes you know basic Big-O notation. Two trees can hold the same keys and still behave very differently. The difference is their shape.
The five named shapes
| Shape | Rule | Example property |
|---|---|---|
| Full | Every node has 0 or 2 children. | No node has a single child. |
| Complete | Every level is filled except possibly the last, which fills from the left. | Fits an array with no gaps. |
| Perfect | All internal nodes have 2 children and all leaves share one depth. | Holds exactly 2^(h+1) - 1 nodes. |
| Balanced | The heights of the two subtrees of every node differ by at most 1. | Height stays near log2(n). |
| Degenerate | Every node has at most one child. | Behaves like a linked list. |
The shapes overlap. A perfect tree is both full and complete. A complete tree is always balanced. A full tree can still be badly lopsided.
Why shape matters
Most tree operations follow one path from the root downward, so their cost is O(h), where h is the height. The number of nodes n fixes nothing about h:
- A perfect tree has h = log2(n + 1) - 1, so a path is short.
- A degenerate tree has h = n - 1, so a path can touch every node.
The code below builds both shapes with 15 nodes and compares their heights.
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def height(node):
if node is None:
return -1
return 1 + max(height(node.left), height(node.right))
def perfect(h):
if h < 0:
return None
return Node(h, perfect(h - 1), perfect(h - 1))
def degenerate(n):
root = None
for value in range(n, 0, -1):
root = Node(value, None, root) # each node gets one right child
return root
print(height(perfect(3))) # 3 (15 nodes)
print(height(degenerate(15))) # 14 (15 nodes)Both trees hold 15 nodes. Searching the perfect tree takes at most 4 steps. Searching the degenerate one can take 15.
Shape also affects recursion. Python's default recursion limit is about 1000 frames. A recursive traversal of a degenerate tree with 5000 nodes raises RecursionError, while a balanced tree with the same node count is only about 12 levels deep.
Common errors
- Mixing up full and complete. Full limits the number of children. Complete limits where the gaps may be.
- Mixing up height conventions. This tutorial counts edges, so an empty tree is -1 and a single node is 0.
- Expecting insertion order not to matter. Inserting sorted keys into a plain binary search tree produces a degenerate tree.
Exercise
A binary tree has 7 nodes and is perfect. What are its height (in edges) and the number of leaves? Then, in one sentence, say why a plain binary search tree built from the keys 1, 2, 3, 4, 5, 6, 7 inserted in that order is a poor structure.
Show answer
A perfect tree with 7 nodes has height 2 and 4 leaves, since 2^(2+1) - 1 = 7 and the last level holds 2^2 = 4 nodes. Inserting sorted keys makes every new key a right child, so the tree is degenerate with height 6, and search costs O(n) instead of O(log n).