What is a tree and how is a binary search tree used?
Understand trees as a connected, undirected graph with no cycles, the terms root, child, parent, leaf and subtree, and the structure and use of a binary search tree.
A focused answer to AQA A-Level Computer Science 4.2.5, covering trees as connected acyclic graphs, the terminology root, child, parent and leaf, binary trees, and the structure and use of a binary search tree.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
What this dot point is asking
AQA wants you to define a tree as a connected, undirected graph with no cycles, use the terminology (root, child, parent, leaf, subtree), and explain the structure and use of a binary search tree.
Trees and terminology
Trees model hierarchical data such as file systems (folders containing folders and files), family trees, organisation charts, the document object model of a web page, and the structure of a parsed expression. Two further terms are useful: the height of a tree is the number of levels from the root to the deepest leaf, and a node's depth is its distance from the root. Height is what determines how long operations on a tree take.
Binary trees
A binary tree is a tree in which each node has at most two children, conventionally called the left child and the right child. Each node typically stores a data value plus pointers (references) to its left and right children, with a null pointer where a child is absent. This pointer-based structure is dynamic: nodes are created and linked at run time, so the tree grows and shrinks as needed, which is one reason trees are studied alongside other dynamic structures.
Binary search trees
The BST therefore combines two useful properties: fast search (like binary search on a sorted array) and easy insertion (no need to shift elements as you would in an array). The price is that the good performance depends on the tree staying reasonably balanced. If items are inserted in already-sorted order the tree becomes unbalanced, effectively a linked list of height , and operations degrade to . Self-balancing variants address this in practice, but the core specification focuses on the ordered structure and its traversal.
It is worth comparing a BST with a hash table, since both store data for fast retrieval. A hash table gives average look-up by key but keeps the data unordered, so it cannot easily produce the items in sorted order or answer range queries (all values between two bounds). A binary search tree gives look-up when balanced, which is slightly slower, but its in-order traversal yields sorted output for free and range queries are straightforward. Choosing between them is a genuine design decision: a hash table when you only ever look up exact keys, a search tree when order matters.
Deletion from a BST is the operation students find hardest because there are three cases: removing a leaf simply detaches it; removing a node with one child links the parent directly to that child; removing a node with two children requires replacing it with its in-order successor (the smallest value in its right subtree) so the ordering rule is preserved. Understanding why the successor is chosen, namely that it is the next value in sorted order and so keeps everything to its left smaller and right larger, shows a real grasp of the structure rather than rote recall.
Exam-style practice questions
Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AQA 20194 marksThe values 8, 3, 10, 1, 6 are inserted, in that order, into an initially empty binary search tree. Describe the resulting tree by stating the root and the left and right child of each node, and explain how the value 6 reaches its position.Show worked answer →
8 becomes the root (first value). 3 is less than 8, so it goes to 8's left. 10 is greater than 8, so it goes to 8's right. 1 is less than 8 (go left to 3) then less than 3, so it goes to 3's left. 6 is less than 8 (go left to 3) then greater than 3, so it goes to 3's right.
Resulting tree: root 8; 8's left child is 3 and right child is 10; 3's left child is 1 and right child is 6.
The value 6 reaches its position by comparison: starting at the root 8, so move to the left child 3; then so move to 3's right, which is empty, so 6 is inserted there.
Markers reward the correct tree structure and the comparison path (8 then left to 3 then right) that places 6.
AQA 20213 marksExplain why a binary search tree can give O(log n) search time, and state the condition under which this performance degrades to O(n).Show worked answer →
In a binary search tree every node's left subtree holds smaller values and its right subtree holds larger values. A search compares the target with the current node and moves to just one subtree, discarding the other, so each comparison roughly halves the remaining nodes. On a balanced tree the height is about , so a search makes about comparisons, giving .
Performance degrades to when the tree is unbalanced, in the extreme when values are inserted in already-sorted order: each new value goes to one side, producing a tree that is effectively a linked list of height , so a search may have to pass through all nodes.
Markers reward the halving argument for and identifying the unbalanced (sorted-insertion) case for .
Related dot points
- Understand graphs, vertices and edges, directed, undirected and weighted graphs, and how a graph is represented as an adjacency matrix or an adjacency list.
A focused answer to AQA A-Level Computer Science 4.2.4, covering graphs, vertices and edges, directed, undirected and weighted graphs, and the adjacency matrix and adjacency list representations with their trade-offs.
- Understand depth-first and breadth-first graph traversal, the data structures they use, and the pre-order, in-order and post-order tree traversal algorithms.
A focused answer to AQA A-Level Computer Science 4.3.1, covering depth-first and breadth-first graph traversal and the data structures they use, plus the pre-order, in-order and post-order tree traversal algorithms.
- Understand a hash table, the role of a hashing algorithm, how a key maps to an index, the meaning of a collision, and collision-resolution methods such as rehashing and chaining.
A focused answer to AQA A-Level Computer Science 4.2.6, covering hash tables, hashing algorithms, mapping a key to an index, collisions, load factor, and collision-resolution methods including rehashing and chaining.
- Understand the linear search and binary search algorithms, how each works, their requirements, and their time complexity.
A focused answer to AQA A-Level Computer Science 4.3.2, covering the linear search and binary search algorithms, how each works, the requirement that binary search needs sorted data, and their time complexity.
- Understand a dictionary as an abstract data type of key-value pairs, its operations, how it is typically implemented using a hash table, and when a dictionary is appropriate.
A focused answer to AQA A-Level Computer Science 4.2.7, covering the dictionary abstract data type of key-value pairs, its operations, its typical implementation using a hash table, and when to use a dictionary.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)