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:

Give an insertion order:

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\) ________
50 30 70 20 40 80 35 10 5

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: ________

Operation Avg Case Worst Case
find \(\Theta(\log n)\) \(\Theta(n)\)
insert \(\Theta(\log n)\) \(\Theta(n)\)
delete \(\Theta(\log n)\) \(\Theta(n)\)

Comparing Dictionary Implementations

Structure Insert Find Delete Sorted Iterate
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?

Tree A 40 30 50 Tree B 30 40 50

 

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

50 30 80 20 40 60 90 85 99 95

Rotations on a BST

70 40 80 20 50 60

Rotations on a BST

70 40 80 20 50 60

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:

  1. Rotation
  2. Maintaining height
  3. Detecting imbalance

AVL Tree Explorer

Balanced Trees in Practice

Language Balanced Tree
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

  1. BST operations are O(height) — height matters!

  2. Unbalanced BSTs can have O(n) height — bad for sorted input

  3. Rotations reshape BSTs while preserving order

  4. AVL trees use rotations to maintain \(|b| \leq 1\) for every node

  5. Diagnosing: check child’s balance factor to pick the rotation

  6. Guarantee: O(log n) height, so O(log n) operations

Practice

  1. Is this tree AVL-balanced? (Check every node’s balance factor)
        50
       /  \
      30   70
     /    /  \
    20   60   90
   /
  10
  1. If not, which rotation(s) would fix it?

  2. 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!)