Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Binary Search Trees

4.2.5.1 Trees (including binary trees)·4.3.4.3 Binary tree search·4.3.2.1 Simple tree-traversal algorithms

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
8 min
Published
6 June 2026
Updated
1 July 2026
On this page
  1. 1.Trees and Binary Trees
  2. 2.Building a BST: Insertion
  3. 3.Searching a BST
  4. 4.In-Order Traversal
  5. 5.Pre-Order and Post-Order Traversal
  6. 6.Time Complexity of BST Search
  7. 7.Common Exam Mistakes

Key takeaways

  • A binary tree is a rooted tree in which every node has at most two children, the left child and the right child.
  • In a binary search tree, for every node all values in its left subtree are less than the node's value and all values in its right subtree are greater, and this holds at every node.
  • In-order traversal (left subtree, root, right subtree) outputs the values of a BST in ascending sorted order, but only for a tree that satisfies the ordering property.
  • The three traversals differ only in when the root is processed: in-order is in the middle, pre-order is before the children, and post-order is after the children.
  • BST search is O(log n) only for a balanced tree; a degenerate tree built from a sorted insertion sequence behaves like a linked list and degrades to O(n).

Trees and Binary Trees

A tree is a connected, undirected graph with no cycles. Unlike a general graph, a tree has no loops — there is exactly one path between any two vertices.

A rooted tree is a tree in which one vertex is designated the root. This creates a hierarchy:

  • The root has no parent
  • Every other node has exactly one parent
  • Nodes with no children are called leaves

A binary tree is a rooted tree in which every node has at most two children, referred to as the left child and right child.

A tree does not have to have a root — but a rooted tree does. A binary tree is always rooted.

The most practically important application of a binary tree is the binary search tree (BST). A BST is a binary tree with an ordering property that makes efficient searching possible:

BST ordering property: for every node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater than the node's value.

This property holds at every node in the tree, not just at the root.

Building a BST: Insertion

Inserting a sequence of values into a BST produces a tree whose structure depends on the order of insertion. Each new value is compared against existing nodes starting at the root and routed left or right until an empty position is found.

Worked example — insert in order: 5, 3, 7, 1, 4, 6, 8

InsertAction
5First value — becomes the root
33 < 5 → left child of 5
77 > 5 → right child of 5
11 < 5 → go left → 1 < 3 → left child of 3
44 < 5 → go left → 4 > 3 → right child of 3
66 > 5 → go right → 6 < 7 → left child of 7
88 > 5 → go right → 8 > 7 → right child of 7

Resulting BST:

The ordering property holds at every node: all values in the left subtree of 5 (1, 3, 4) are less than 5; all values in the right subtree (6, 7, 8) are greater. The same applies at each internal node.

Searching a BST

Searching a BST uses the ordering property to eliminate half the remaining tree at each comparison — identical in logic to binary search on a sorted array.

Algorithm:

  1. Start at the root
  2. If the target equals the current node → found
  3. If the target is less than the current node → move to left child
  4. If the target is greater than the current node → move to right child
  5. If the current node is null → target not in tree

Worked trace — search for 4 in the tree above:

StepCurrent nodeCompare with 4Decision
154 < 5Go left
234 > 3Go right
344 = 4Found

3 comparisons for a 7-node tree. For a balanced BST with nodes, the height is approximately , giving a time complexity of .

Trace — search for 2 (not in tree):

StepCurrent nodeCompare with 2Decision
152 < 5Go left
232 < 3Go left
312 > 1Go right
4nullNot found

In-Order Traversal

In-order traversal visits nodes in the order: left subtree → root → right subtree, applied recursively at every node.

InOrder(node):
    IF node IS NOT null:
        InOrder(node.left)
        OUTPUT node.value
        InOrder(node.right)

Trace on the example BST:

In-order visits: 1, 3, 4, 5, 6, 7, 8

Key property of in-order traversal on a BST: it outputs all values in ascending sorted order. This makes in-order traversal the standard algorithm for printing or processing the contents of a BST in sequence.

The reason: the BST ordering property ensures that everything in the left subtree is smaller (visited first), the root is next, and everything in the right subtree is larger (visited last). Applied recursively, this produces a globally sorted sequence.

Use: outputting the contents of a binary search tree in ascending order.

Worth saving these ideas?

Turn what you've read into instant revision cards. Free to get started.

Make flashcards

Pre-Order and Post-Order Traversal

Two other traversal orders are required by the spec. Both use the same recursive approach with the processing step in a different position.

Pre-order: root → left → right

PreOrder(node):
    IF node IS NOT null:
        OUTPUT node.value
        PreOrder(node.left)
        PreOrder(node.right)

Pre-order on the example BST: 5, 3, 1, 4, 7, 6, 8

Pre-order visits the root before its children. This preserves the shape of the tree — a sequence of pre-order values can reconstruct the original BST by inserting them in the same order. Use: copying a tree.

Post-order: left → right → root

PostOrder(node):
    IF node IS NOT null:
        PostOrder(node.left)
        PostOrder(node.right)
        OUTPUT node.value

Post-order on the example BST: 1, 4, 3, 6, 8, 7, 5

Post-order visits a node only after both its children have been processed. Uses: emptying a tree (delete children before parent); evaluating expression trees; converting infix to Reverse Polish Notation (RPN).

TraversalOrderPrimary use
In-orderLeft → Root → RightOutput BST contents in ascending order
Pre-orderRoot → Left → RightCopying a tree
Post-orderLeft → Right → RootEmptying a tree; infix to RPN conversion

Time Complexity of BST Search

The time complexity of searching a BST is — but this assumes the tree is reasonably balanced.

A balanced BST with nodes has height . Each comparison eliminates approximately half the remaining nodes, so the number of steps grows logarithmically with the tree size.

Tree size ()Height (balanced)Maximum comparisons
733
1544
3155
10231010

What happens with an unbalanced tree?

If values are inserted in sorted order — e.g., 1, 2, 3, 4, 5 — each new value is always a right child of the previous, producing a tree that is effectively a linked list:

Searching this tree for 5 requires visiting every node. In the worst case (degenerate BST), search time degrades to .

The AQA spec states the time complexity of binary tree search as , which holds for a balanced or near-balanced tree. In exam questions, the tree given will typically be balanced unless the question explicitly explores the worst case.

Common Exam Mistakes

1. Inserting values in the wrong position

When inserting, compare the new value against every node on the path from root to the insertion point, not just the root. Moving left or right at each node is a separate comparison — show all steps in a trace.

2. Confusing the three traversal orders

The orders differ only in when the root is processed relative to its children: in-order (root middle), pre-order (root first), post-order (root last). Learn the mnemonic: in = in the middle, pre = before children, post = after children.

3. Claiming BST search is always

is the time complexity for a balanced BST. A degenerate tree (all nodes on one side) degrades to . Exam questions that give a sorted insertion sequence or an obviously unbalanced tree are testing awareness of this.

4. Stating that in-order traversal works for any binary tree

In-order traversal outputs values in ascending order only for a binary search tree — one that satisfies the ordering property. For a general binary tree with no ordering, in-order produces an output, but it will not be sorted.

5. Confusing BST search with binary search

Binary tree search (on a BST) and binary search (on a sorted array) both achieve by repeatedly halving the search space. They are related in concept but different in implementation: one follows pointers in a tree structure; the other uses index arithmetic on an array.

Key terms

Tree
A connected, undirected graph with no cycles, so there is exactly one path between any two vertices.
Rooted tree
A tree in which one vertex is designated the root, creating a hierarchy where every other node has exactly one parent.
Leaf
A node with no children.
Binary tree
A rooted tree in which every node has at most two children, referred to as the left child and the right child.
Binary search tree (BST)
A binary tree with an ordering property where each node's left subtree holds smaller values and its right subtree holds larger values.
In-order traversal
A traversal that visits the left subtree, then the root, then the right subtree, outputting a BST's values in ascending order.
Pre-order traversal
A traversal that visits the root, then the left subtree, then the right subtree; used for copying a tree.
Post-order traversal
A traversal that visits the left subtree, then the right subtree, then the root; used for emptying a tree and converting infix to RPN.
Degenerate BST
An unbalanced BST, typically produced by inserting values in sorted order, that is effectively a linked list and degrades search to O(n).

Frequently asked questions

For every node in a BST, all values in its left subtree are less than the node's value, and all values in its right subtree are greater. This property holds at every node in the tree, not just at the root, and it allows searching to eliminate half the remaining tree at each comparison.

In-order traversal, which visits the left subtree, then the root, then the right subtree, outputs the values of a BST in ascending sorted order. This works only for a binary search tree that satisfies the ordering property; for a general binary tree the output will not be sorted.

No. BST search is O(log n) only for a balanced or near-balanced tree, where the height is approximately log₂ n. If values are inserted in sorted order the tree becomes a degenerate chain like a linked list, and in the worst case search degrades to O(n).

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Next

Hash Tables and Dictionaries

Related lessons

5 min

9 min

Lesson

Stacks and Queues

A-Level Computer Science · AQA 7517

2 months ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.