How are graphs and trees systematically traversed?
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.
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
AQA wants you to describe depth-first and breadth-first traversal of a graph, name the data structure each uses, and apply the pre-order, in-order and post-order traversals of a binary tree.
Graph traversal
A visited list (or marking) is kept in both so that vertices are not processed twice, which matters in graphs with cycles: without it, a cyclic graph would cause an infinite loop. The choice of stack versus queue is exactly what gives each its character: a stack returns the most recently added vertex (so the search dives deep), while a queue returns the oldest (so the search spreads wide before going deeper).
Tree traversal
preOrder(node): visit(node); preOrder(left); preOrder(right)
inOrder(node): inOrder(left); visit(node); inOrder(right)
postOrder(node): postOrder(left); postOrder(right); visit(node)
In-order traversal of a binary search tree outputs the values in ascending order, which is a handy way to test whether a tree is a valid BST. Pre-order can copy or serialise a tree because it records the root first, letting the same structure be rebuilt; post-order is used to evaluate an expression tree and to delete a tree safely, because children are processed before the parent.
These three orderings are all forms of depth-first traversal applied to a tree, so they too are naturally recursive: each definition processes a node and then calls itself on the left and right subtrees in a particular order. The recursion uses the call stack, which is the tree analogue of the explicit stack used for depth-first traversal of a general graph. Breadth-first traversal of a tree, by contrast, visits the tree level by level and uses a queue, just as it does on a graph; this level-order traversal is useful when you want the nodes nearest the root first.
The choice of traversal is driven by what you need to do with the nodes. If you must process a parent before its children (for example printing a directory before its contents, or copying a structure top down) you use pre-order. If you need the values in sorted order from a search tree you use in-order. If you must process all children before their parent (for example freeing memory, or evaluating an arithmetic expression where operands must be known before the operator is applied) you use post-order. Being able to justify the choice, not just produce the sequence, is what higher-mark questions reward.
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 20184 marksA binary tree has root 8, with left child 3 (whose children are 1 and 6) and right child 10 (whose right child is 14). State the order in which the nodes are visited by (a) an in-order traversal and (b) a pre-order traversal.Show worked answer →
In-order visits left subtree, node, right subtree. Starting at 8: traverse the left subtree rooted at 3 (its left 1, then 3, then its right 6), then 8, then the right subtree rooted at 10 (10 has no left child, then 10, then 14). Order: 1, 3, 6, 8, 10, 14. This is ascending order, confirming the tree is a binary search tree.
Pre-order visits node, left subtree, right subtree. Starting at 8: visit 8, then the left subtree (3, then 1, then 6), then the right subtree (10, then 14). Order: 8, 3, 1, 6, 10, 14.
Markers reward applying the correct visit order recursively and producing both sequences; the in-order sequence being sorted is a useful check.
AQA 20213 marksCompare depth-first and breadth-first graph traversal, stating the data structure each uses and one situation in which each is the better choice.Show worked answer →
Depth-first traversal explores as far along one branch as possible before backtracking and uses a stack (often through recursion). Breadth-first traversal explores all neighbours of a vertex before moving outward level by level and uses a queue.
Depth-first is the better choice when you need to explore all the way down paths, for example solving a maze or detecting a cycle. Breadth-first is better when you need the path with the fewest edges, for example finding the shortest route in an unweighted graph or the nearest match.
Markers reward the stack versus queue distinction and a valid scenario for each (maze or cycle detection for DFS; fewest-edges shortest path for BFS).
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 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.
- Understand Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex, the role of a priority queue, and its applications.
A focused answer to AQA A-Level Computer Science 4.3.4, covering Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex in a weighted graph, the role of a priority queue, and its applications.
- Understand the stack abstract data type, LIFO behaviour, the push, pop and peek operations using a stack pointer, and the use of stacks for subroutine calls and recursion.
A focused answer to AQA A-Level Computer Science 4.2.3, covering the stack abstract data type, LIFO behaviour, the push, pop and peek operations with a stack pointer, overflow and underflow, and the use of the call stack.
- Understand the queue abstract data type, FIFO behaviour, linear, circular and priority queues, and the enqueue and dequeue operations using front and rear pointers.
A focused answer to AQA A-Level Computer Science 4.2.2, covering the queue abstract data type, FIFO behaviour, linear, circular and priority queues, and the enqueue and dequeue operations with front and rear pointers.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)