Skip to main content
SingaporeComputer ScienceSyllabus dot point

How does a binary search tree keep data ordered for fast search, and why does balance matter?

Describe a binary search tree, perform insertion, search and in-order traversal, and explain how balance affects complexity

A focused answer to the H2 Computing outcome on binary search trees. The ordering property, inserting and searching nodes, in-order traversal giving sorted output, and why an unbalanced tree degrades to O(n).

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to describe a binary search tree (BST), perform insertion, search and in-order traversal, and explain how the tree's balance affects its complexity. The central idea is that the BST ordering property lets you discard half the remaining nodes at each step - giving O(logn)O(\log n) operations when the tree is balanced, but only then.

The answer

The ordering property

A binary search tree is a binary tree where each node has up to two children and obeys the rule:

  • every key in a node's left subtree is smaller than the node's key, and
  • every key in its right subtree is larger.

This holds at every node, so the tree encodes a sorted ordering implicitly.

Searching

Searching mirrors binary search. Start at the root; if the target equals the node, you are done; if smaller, go left; if larger, go right; if you reach null, it is absent:

def search(node, target):
    if node is None or node.key == target:
        return node
    if target < node.key:
        return search(node.left, target)
    return search(node.right, target)

Each comparison discards one subtree, so search takes time proportional to the tree's height.

Insertion

Insertion follows the same path until it finds an empty (null) spot, then attaches the new node there:

def insert(node, key):
    if node is None:
        return Node(key)
    if key < node.key:
        node.left = insert(node.left, key)
    else:
        node.right = insert(node.right, key)
    return node

In-order traversal gives sorted output

An in-order traversal visits the left subtree, then the node, then the right subtree. Because left holds smaller keys and right holds larger, this always emits the keys in ascending order:

def in_order(node):
    if node is not None:
        in_order(node.left)
        print(node.key)
        in_order(node.right)

Balance and complexity

The cost of every operation is the tree's height hh:

  • A balanced tree has height about log2n\log_2 n, so search, insert and delete are O(logn)O(\log n).
  • An unbalanced tree - for example one built by inserting already-sorted keys, so every node goes the same way - degenerates into a "stick" of height nn, making operations O(n)O(n), no better than a list.

Self-balancing trees keep the height logarithmic automatically; without balancing, insertion order can ruin performance.

Examples in context

Example 1. An ordered symbol table. A compiler storing variable names so it can both look them up quickly and list them alphabetically uses a balanced BST: search is O(logn)O(\log n), and a single in-order traversal prints every name in sorted order for a symbol dump - two needs met by one structure.

Example 2. Range queries. A BST of timestamps lets a log viewer find all events between two times efficiently: navigate to the lower bound, then traverse in order until the upper bound. The sorted structure makes range scans natural in a way a hash table cannot, since hashing destroys order.

Try this

Q1. What does an in-order traversal of a binary search tree produce? [1 mark]

  • Cue. The keys in ascending sorted order, because left subtrees hold smaller keys and right subtrees larger.

Q2. Inserting the keys 1, 2, 3, 4, 5 in that order into an empty BST gives what shape, and what search complexity? [2 marks]

  • Cue. A right-leaning stick of height 5 (each key larger than the last), giving O(n)O(n) search - the degenerate worst case.

Q3. Why is a balanced binary search tree preferred over an unbalanced one? [2 marks]

  • Cue. Its height stays near log2n\log_2 n, keeping search, insert and delete at O(logn)O(\log n) rather than degrading to O(n)O(n).

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original6 marksStarting from an empty binary search tree, insert the keys in this order: 50, 30, 70, 20, 40, 60. (a) Draw or describe the resulting tree. (b) Give the order keys are visited by an in-order traversal. (c) State what an in-order traversal of a binary search tree always produces.
Show worked answer →

(a) Each key goes left if smaller than the current node, right if larger, starting at the root.

        50
       /  \
     30    70
    /  \   /
  20   40 60

50 is the root; 30 < 50 goes left; 70 > 50 goes right; 20 < 50 < 30 goes left of 30; 40 is between 30 and 50 so right of 30; 60 < 70 goes left of 70.

(b) In-order traversal (left subtree, node, right subtree) visits: 20, 30, 40, 50, 60, 70.

(c) An in-order traversal of a binary search tree always produces the keys in ascending sorted order, because every left subtree holds smaller keys and every right subtree holds larger keys.

Markers reward the correct tree shape from the insertion rule, the in-order sequence, and the fact that in-order yields sorted output.

Original5 marks(a) State the average and worst-case time complexity of searching a binary search tree of nn nodes. (b) Describe the input order that produces the worst case and what the tree looks like. (c) Why is a balanced tree preferred?
Show worked answer →

(a) Average case O(logn)O(\\log n); worst case O(n)O(n).

(b) The worst case occurs when keys are inserted in sorted (or reverse-sorted) order, for example 10, 20, 30, 40. Each new key is larger than all before it, so it always goes right, producing a tree that is effectively a linked list of height nn - a "degenerate" or "stick" tree.

(c) A balanced tree keeps its height near log2n\\log_2 n, so search, insertion and deletion stay O(logn)O(\\log n). An unbalanced tree can have height up to nn, degrading those operations to O(n)O(n) - no better than a linear search - so balance preserves the tree's efficiency advantage.

Markers reward the two complexities, sorted-order insertion giving a stick-shaped tree of height nn, and balance keeping height logarithmic for O(logn)O(\\log n) operations.

Related dot points