Exercises — BST — worst case O(n) — motivation for balancing
Before we begin, one shared picture of the two extreme shapes we keep referring to:

Left = a stick (every node has one child, ). Right = a bushy perfect tree (every level full, ). Everything is a story about which wall your tree leans on.
Level 1 — Recognition
Recall Solution L1.1
WHAT: each of these operations walks one path from the root down. WHY: at every node you compare and go either left or right — never both — so you visit at most nodes ( edges). Answer: .
Recall Solution L1.2
Lower wall = perfectly packed (bushy); upper wall = a stick.
Recall Solution L1.3
A linked list — see Linked List — O(n) search. Search cost , because you must potentially walk every node one after another.
Level 2 — Application
Recall Solution L2.1
WHAT: each key is larger than everything already inserted, so every comparison sends it right.
10
\
20
\
30
\
40
WHY it looks like this: → right; → right; → right. Never branches → a stick. , so .
Recall Solution L2.2
Step by step:
- → root.
- → left of .
- → right of .
- : → left to ; → right of .
- : → right to ; → right of .
30
/ \
10 40
\ \
20 50
Longest root-to-leaf path: (or ), edges → .
Recall Solution L2.3
Any fully sorted order (increasing or decreasing ) makes a stick. Maximum height .
Level 3 — Analysis
Recall Solution L3.1
WHY this sum: level holds at most nodes; a perfect tree hits that max on every level. Levels : (Geometric series with ratio 2 — see Geometric series.) Check: , so . ✓
Recall Solution L3.2
- Worst (stick): edges → up to visits.
- Best (bushy): → about visits. The gap: roughly faster when balanced. This factor of ~ is exactly what balancing buys you.
Recall Solution L3.3
WHAT: sum the per-level maxima. WHY the bound flips into a height bound: solve for : So no matter how bushy you pack, you cannot get below . That is the unbeatable floor — the lower wall.
Level 4 — Synthesis
Recall Solution L4.1
(a) Plain BST: every key goes right → stick → . (b) AVL tree: after each insert it checks a balance factor and performs rotations to keep left/right heights within 1, giving . See AVL Trees — rotations and balance factor. One-sentence difference: the AVL restructures on the fly so sorted input can never grow a stick, whereas the plain BST silently accepts the stick.
Recall Solution L4.2
The half-truth: random insertion order gives expected height — often fine in practice. Why it fails as a guarantee:
- You often can't shuffle — data arrives streaming (logs, time series, auto-increment IDs) and must be searchable immediately.
- An adversary (or just unlucky already-sorted data) can still hand you the worst case; expectation ≠ guarantee. Big-O worst case (see Big-O notation) stays . Self-balancing structures give a hard ceiling for every input, which shuffling cannot.
Recall Solution L4.3
- BST wins:
insert/deletein — a sorted array needs to shift elements. - Array wins: raw random-access/cache locality and zero pointer overhead — contiguous memory, index access. Both share the idea of halving the search space; they differ in how flexibly you can mutate.
Level 5 — Mastery
Recall Solution L5.1
Existence of the tall tree: insert ascending → stick → . (Constructive, always works.) Existence of the short tree: insert keys by repeatedly choosing the median of the remaining range as the next key (median-first / recursive-midpoint order). The first key is the overall median → root splits keys into two equal halves; recurse on each half. Each level fills before the next, so height . What this proves: the same key set spans the entire height range purely by order. Therefore a plain BST's performance is a property of input order, not the keys — so it can offer no worst-case guarantee, which is the whole motivation for self-balancing.
Recall Solution L5.2
WHAT: in a stick , searching for key visits nodes (you walk from down to ). Sum: Big-O: . Contrast: in a balanced tree each search is , so the total is — a full order-of-magnitude cheaper for building an index of all keys.
Recall Solution L5.3
Minimum possible: . AVL ceiling: , so . Gap meaning: AVL never lets the tree exceed ~ the perfect height. So even in its worst case an AVL is at most ~ visits vs the perfect — still tiny, and lightyears better than the of a stick. The constant is the small price for a hard guarantee. See Red-Black Trees for a looser () but cheaper-to-maintain alternative.
Score Yourself
Recall How to read your result
- Missed L1/L2 → reread Tree height and depth and the parent's height-bound section.
- Missed L3 → drill the Geometric series sum until it's reflex.
- Missed L4/L5 → the concept (order controls shape controls cost controls the need for balancing) hasn't clicked yet — reread the parent's "WHY this motivates balancing".
Connections
- Binary Search Tree — definition and property
- Tree height and depth
- Binary Search — divide and conquer
- AVL Trees — rotations and balance factor
- Red-Black Trees
- Linked List — O(n) search
- Big-O notation
- Geometric series