3.4.4 · D3Trees

Worked examples — Binary Search Tree (BST) — BST property, insert, search, delete (3 cases)

3,400 words15 min readBack to topic

Before any code, one reminder of the terms and one convention we use everywhere:


The scenario matrix

Every problem in BST-land falls into one of these cells. Our examples below are labelled with the cell they cover, and together they hit all of them.

# Cell (scenario class) What makes it special Covered by
A Search — key present falls onto a node Ex 1
B Search — key absent "falls off" to None Ex 1
C Insert — normal build new keys become leaves Ex 2
D Insert — degenerate (sorted input) tree becomes a stick, height Ex 3
E Insert — duplicate key must be ignored / not double-added Ex 2
F Delete — Case 1 (leaf) 0 children Ex 4
G Delete — Case 2 (one child) splice the single child up Ex 5
H Delete — Case 3 (two children) replace with in-order successor Ex 6
I Delete — Case 3 where successor has a right child successor is not a leaf Ex 7
J Degenerate / empty tree edge inputs None root, delete-missing key Ex 8
K Validation — sneaky invalid tree passes local check, fails global Ex 9
L Word problem (real-world) model a scenario as a BST Ex 10
M Exam twist — reconstruct from traversals reason backwards Ex 11

We will use one shared tree for many examples so you can watch it change. Here it is:

Figure — Binary Search Tree (BST) — BST property, insert, search, delete (3 cases)

In-order walk of this tree: — sorted ✓, so it is a valid BST.


Example 1 — Search, present (Cell A) and absent (Cell B)


Example 2 — Building a tree; duplicate ignored (Cells C, E)


Example 3 — Degenerate insert: the stick (Cell D)

Figure — Binary Search Tree (BST) — BST property, insert, search, delete (3 cases)
  1. → root.
  2. → right child of .
  3. → right; → right child of .
  4. → right; → right; → right child of . Why this happens? Every new key is larger than all existing keys, so it always turns right, forever. There is never a left branch to balance things.

Result: a right-leaning "stick" of height edges.

Verify: Searching for now touches all 4 nodes → 4 comparisons — as slow as scanning a linked list, not . Same set of keys, worst order. This is exactly why AVL Tree and Red-Black Tree self-balance. In-order walk = ✓ (still a valid BST — just a bad-shaped one). See Tree Height vs Depth.


Example 4 — Delete a leaf (Cell F, Case 1)


Example 5 — Delete a one-child node (Cell G, Case 2)


Example 6 — Delete a two-child node, successor is a leaf (Cell H, Case 3)

Figure — Binary Search Tree (BST) — BST property, insert, search, delete (3 cases)
  1. Search finds = root, with two children → Case 3.
  2. Find the in-order successor = find_min(right subtree) = leftmost node of the subtree rooted at . Walk: has left child ; has no left child → successor . Why the successor? We need a replacement that is bigger than everything on the left (so the left subtree stays valid) and smaller than everything else on the right. The smallest key in the right subtree is exactly that borderline value.
  3. Copy 's key into the root: root's key becomes . Why copy, not move pointers? Copying the key is cheaper and keeps all subtree links intact; we only need to erase the duplicate left behind.
  4. Delete from the right subtree. is a leaf → Case 1, just remove it. Why is this always easy? The leftmost node has no left child by definition, so deleting the successor is always Case 1 or 2 — never another Case 3. The recursion cannot loop.

Verify: New in-order walk = . Sorted ✓. Root is now , left subtree () all , right () . Invariant holds. ✓


Example 7 — Case 3 where the successor has a right child (Cell I)


Example 8 — Empty tree and deleting a missing key (Cell J)


Example 9 — The sneaky invalid tree (Cell K, validation)


Example 10 — Word problem (Cell L)


Example 11 — Exam twist: rebuild from traversals (Cell M)


Wrap-up flow

zero

one

two

Delete key found

How many children

Case 1 remove leaf

Case 2 splice child up

Case 3 copy successor then delete it

Successor has no left child so it is Case 1 or 2

Recall Self-test before moving on

Which delete case fires when you remove a node whose left child is None but right child exists? ::: Case 2 (one child) — root.left is None is true, so we return root.right. Why can Case 3's successor-deletion never itself be another Case 3? ::: The successor is the leftmost node of the right subtree, so it has no left child — at most one child, hence Case 1 or 2. In a valid BST, why does knowing the in-order walk give you no shape information? ::: In-order of any BST is always the sorted keys, identical for every shape holding that key set. If a search ends at a node of depth , how many comparisons did it make? ::: — one per node touched along a path of edges. What structure fixes the degenerate stick from Example 3? ::: A self-balancing BST such as AVL Tree or Red-Black Tree.


Back to the parent BST note. Related contrasts: Heap vs BST, Binary Tree — terminology & traversals.