IB Computer Science HL · Year 2 · Lesson 25

Binary search trees: structure and insertion

Sketch, search and insert in a BST while preserving its ordering property.

Paper 250 minutesB4.1.4
Targets

Every comparison chooses one subtree

2 / 10
  • explain how BSTs organize data;
  • sketch a BST from an insertion sequence;
  • trace search and insertion decisions;
  • explain why recursion maps naturally to subtree operations.
B4.1.4
Learn + retrieve

Rapid Recall Deck

3 / 10

Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.

Search

The ordering rule prunes impossible branches

4 / 10
        50
       /  \
     30    70
    / \    / \
   20 40  60 80

Search path

Trace the comparisons made when searching for 60.

Compare 60 with 50: go right. Compare 60 with 70: go left. Compare with 60: found. Path: 50 → 70 → 60.

Missing value

Trace the search for 65 and state when the algorithm knows it is absent.

50 → right to 70 → left to 60 → right. The right child of 60 is empty, so the only valid path for 65 has ended and the value is absent.

B4.1.4 · Sketch

Insertion order determines the shape

5 / 10

Sketch from insertion order

Starting from an empty BST, insert these values in order: 50, 30, 70, 20, 40, 60, 80, 65. Sketch the final tree.

        50
       /  \
     30    70
    / \    / \
   20 40  60 80
           \
            65
Recursion link

A subtree is a smaller instance of the same structure

6 / 10
class TreeNode:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def insert(root, value):
    if root is None:
        return TreeNode(value)

    if value < root.value:
        root.left = insert(root.left, value)
    elif value > root.value:
        root.right = insert(root.right, value)

    return root

Explain why this insertion is recursive

Recursive subproblem — identify which smaller subtree receives the same insertion task.
Base case — identify when the new node is created.
Return path — explain why each call returns its subtree root.

After comparing the new value with a node, insertion becomes the same problem on either the left or right subtree. The base case occurs when that subtree root is None, where the new node is created. Each recursive call returns the updated subtree root so the parent keeps the correct child reference.

Properties

BST ordering works with comparable keys

7 / 10

Balanced shape

Consider this BST: 50
├─ 30
│  ├─ 20
│  └─ 40
└─ 70
   ├─ 60
   └─ 80
. Explain why this reasonably balanced shape can search faster than a chain-shaped BST containing the same seven values.

A reasonably balanced tree reduces the remaining candidate set through branching, so the path height stays much smaller. A chain-shaped BST may require visiting nearly every node, behaving like a linear structure.

Ordering with strings

A BST stores names using normal lexicographic string comparison. Starting with root "Maya", where would "Ari" and "Zoe" be inserted relative to the root?

"Ari" is less than "Maya", so it goes left. "Zoe" is greater, so it goes right.

Exam lens

Practice the exact sketching skill seen in the specimen

8 / 10

Paper 2-style sketch

The current BST is:

        Martin
       /      \
    Jeanne    Pedro
    /   \
  Hwang  Manuel

Using lexicographic comparison, insert Louis and then Indie. Sketch only the final tree.

        Martin
       /      \
    Jeanne    Pedro
    /   \
  Hwang  Manuel
    \    /
   Indie Louis

Louis follows Martin → Jeanne → Manuel → left. Indie follows Martin → Jeanne → Hwang → right.

Exit check

Use the invariant, not guesswork

9 / 10

Exit explanation

Why does a BST search not normally need to inspect both children after comparing the target with a node?

The ordering property tells which side could contain the target. If the target is smaller, every larger/right-side path can be rejected; if larger, the left side can be rejected.

Homework

Finish the learning cycle

10 / 10

IA — main task

Continue Criterion D implementation/testing and document one implementation choice with evidence of why it works.

Paper 2 — short practice

Sketch BSTs for two different insertion orders using the same values, then explain why their shapes — and search performance — can differ.