Balanced Trees
Week 9, Wednesday
March 4, 2026
Recap: BSTs
Insert and Find running time are O(______)
Recap: Building a BST
Insert: 10, 13, 25, 38, 40, 51, 84
Build another “bad” tree:
How many bad trees are there?
BST: Height and Data
BST operations depend on the height (\(h\)) of the tree.
But we want to analyze in terms of \(n\) (the amount of data).
So we need relationships between \(h\) and \(n\):
- \(h \geq\) ________
- \(h \leq\) ________
Average Case
The height of a BST depends on the order data is inserted.
How many different insertion orders for \(n\) keys? _____
Average height, over all arrangements of \(n\) keys: ________
| find |
\(\Theta(\log n)\) |
\(\Theta(n)\) |
| insert |
\(\Theta(\log n)\) |
\(\Theta(n)\) |
| delete |
\(\Theta(\log n)\) |
\(\Theta(n)\) |
Comparing Dictionary Implementations
| Unsorted list |
O(1) |
O(n) |
O(n) |
O(n log n) |
| Sorted list |
O(n) |
O(log n) |
O(n) |
O(n) |
| Hash table |
O(1) avg |
O(1) avg |
O(1) avg |
Not possible |
| BST |
O(____) |
O(____) |
O(____) |
O(n) |
Why Trees Instead of Hash Tables?
Hash tables give O(1) expected time. Why use trees?
Trees can do things hash tables can’t:
- Sorted iteration: Traverse keys in order
- Range queries: Find all keys between A and B
- Predecessor/successor: Find the next-smallest key
- No hash function needed: Works with any comparable type
Which Tree Makes You Happiest?
The height balance of a tree \(T\) is: \(b = \text{height}(T_R) - \text{height}(T_L)\)
A tree \(T\) is height balanced if:
Rotations on a BST
Rotations on a BST
Rotations on a BST
Operations on BSTs — Rotations
Rotation properties:
- There are 4 kinds: left, right, left-right, right-left
- Local operations (subtrees not affected)
- Constant time (\(O(1)\))
- BST property maintained
AVL Trees
GOAL: use rotations to maintain balance of BSTs.
These trees have a special name: AVL Trees
Named after Adelson-Velsky and Landis (1962).
AVL Property: For every node, \(|b| \leq 1\).
Three issues to consider in the implementation:
- Rotation
- Maintaining height
- Detecting imbalance
AVL Tree Explorer
Balanced Trees in Practice
| C++ |
std::map, std::set (Red-Black) |
| Java |
TreeMap, TreeSet (Red-Black) |
| Python |
sortedcontainers.SortedDict (3rd party) |
| Databases |
B-trees, B+ trees |
Python’s built-in dict uses hash tables, not trees.
Summary: Balanced Trees
BST operations are O(height) — height matters!
Unbalanced BSTs can have O(n) height — bad for sorted input
Rotations reshape BSTs while preserving order
AVL trees use rotations to maintain \(|b| \leq 1\) for every node
Diagnosing: check child’s balance factor to pick the rotation
Guarantee: O(log n) height, so O(log n) operations
Practice
- Is this tree AVL-balanced? (Check every node’s balance factor)
50
/ \
30 70
/ / \
20 60 90
/
10
If not, which rotation(s) would fix it?
After inserting 5, 3, 7, 2, 4, 6, 8 into an empty AVL tree, what does it look like?
(Hint: Use the visualization tool to check!)