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).
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 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 :
- A balanced tree has height about , so search, insert and delete are .
- 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 , making operations , 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 , 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 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 , keeping search, insert and delete at rather than degrading to .
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 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 ; worst case .
(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 - a "degenerate" or "stick" tree.
(c) A balanced tree keeps its height near , so search, insertion and deletion stay . An unbalanced tree can have height up to , degrading those operations to - 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 , and balance keeping height logarithmic for operations.
Related dot points
- Describe a linked list as nodes joined by pointers, implement insertion and deletion, and contrast it with an array
A focused answer to the H2 Computing outcome on linked lists. Nodes and pointers, traversal, inserting and deleting nodes by relinking, and the trade-offs against arrays for access and modification.
- Describe a hash table and hash function, explain collision resolution by chaining and open addressing, and analyse the effect of load factor
A focused answer to the H2 Computing outcome on hash tables. Hash functions mapping keys to buckets, average O(1) lookup, collisions resolved by chaining or open addressing, and how the load factor affects performance.
- Define recursion in terms of base and recursive cases, trace recursive calls, and compare recursion with iteration
A focused answer to the H2 Computing outcome on recursion. Base and recursive cases, tracing the call stack, the divide-and-conquer pattern, and the trade-offs between recursion and iteration.
- Describe graphs and their terminology, represent them as adjacency matrices and adjacency lists, and compare the two representations
A focused answer to the H2 Computing outcome on representing graphs. Vertices and edges, directed and weighted graphs, adjacency matrix versus adjacency list, and the space and lookup trade-offs between them.
- Implement and trace linear search and binary search, state their complexities, and justify when each applies
A focused answer to the H2 Computing outcome on searching. Linear search and binary search, tracing each on an example, their O(n) and O(log n) complexities, and the precondition that binary search needs sorted data.