Revision Notes
OxfordAQA A-Level Computer Science (9645): Tree algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.11.2 Tree algorithms: the three traversals, code, array trees, uses, common errors and a self-test.
- Subject
- Computer Science
- Level
- A LEVEL
- Topic
- Topic 11 – Advanced algorithms
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA A Level Computer Science (9645), 2024-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA A Level Computer Science.
Syllabus points this page covers
9645 (A Level)
- 3.11.2 Tree algorithms
This page covers 1 of the 2 subtopics in topic 11, Advanced algorithms.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9645).
These notes condense sub-topic 3.11.2 Tree algorithms of OxfordAQA International AS and A-level Computer Science (9645), Version 1.1 (International A-level content in topic 3.11 Advanced algorithms). For worked traces and the call-by-call recursion, see the tree algorithms study guide, then practise with the tree algorithms practice questions.
What the specification requires
- Trace pre-order, post-order and in-order tree traversal.
- Understand how to implement each traversal.
- Describe uses of the traversals.
Code in the Unit 3 on-screen exam is C#, Python or VB.Net. The specification sets no pseudo-code standard, so these notes use Python.
Key terms
| Term | Meaning |
|---|---|
| Binary tree | Every node has at most two children: left and right |
| Root | The top node, with no parent |
| Leaf | A node with no children |
| Subtree | A node and everything below it |
| Traversal | Visiting every node exactly once in a defined order |
| Binary search tree | Left subtree values are smaller than the node; right subtree values are larger |
| Expression tree | Operators at internal nodes, operands at leaves |
| Prefix, infix, postfix | Operator before, between or after its operands |
The three orders
| Traversal | Order at each node | First item output | Last item output |
|---|---|---|---|
| Pre-order | node, left, right | the root | the last leaf on the right |
| In-order | left, node, right | the leftmost node | the rightmost node |
| Post-order | left, right, node | the leftmost leaf | the root |
The left subtree is always finished before the right subtree.
Worked reminder: one tree, three outputs
Insert 55, 30, 80, 20, 45, 70, 95 and 40 into an empty binary search tree.
55
/ \
30 80
/ \ / \
20 45 70 95
/
40
| Traversal | Output |
|---|---|
| Pre-order | 55, 30, 20, 45, 40, 80, 70, 95 |
| In-order | 20, 30, 40, 45, 55, 70, 80, 95 |
| Post-order | 20, 40, 45, 30, 70, 95, 80, 55 |
The tree has 8 nodes and each output has 8 items. The in-order list is ascending because this is a binary search tree.
Implementation pattern
def in_order(node):
if node is not None: # base case: an empty subtree does nothing
in_order(node.left)
print(node.data) # this line moves for the other orders
in_order(node.right)
- Pre-order: move the
printline above both calls. - Post-order: move the
printline below both calls. - Each node causes one call and each empty child causes one more, so for n nodes there are 2n + 1 calls and the time complexity is O(n).
- The depth of the recursion equals the height of the tree.
Tree held in arrays
| Index | Data | Left | Right |
|---|---|---|---|
| 0 | 55 | 1 | 2 |
| 1 | 30 | 3 | 4 |
| 2 | 80 | 5 | 6 |
| 3 | 20 | −1 | −1 |
| 4 | 45 | 7 | −1 |
| 5 | 70 | −1 | −1 |
| 6 | 95 | −1 | −1 |
| 7 | 40 | −1 | −1 |
Start at index 0. Replace node is not None with i != -1, node.left with left[i], and node.data with data[i]. A rogue value of −1 means “no child”.
Uses
| Traversal | Use | Reason |
|---|---|---|
| Pre-order | Copying a tree | Parents are created before their children |
| Pre-order | Prefix expression from an expression tree | Operator, then operands |
| In-order | Ascending output of a binary search tree | Smaller values, node, larger values |
| Post-order | Postfix expression from an expression tree | Operands, then operator |
| Post-order | Emptying a tree | Delete children before the parent |
Expression tree reminder
For (a + b) × c − d ÷ e the three outputs are:
| Traversal | Output |
|---|---|
| Pre-order (prefix) | − × + a b c ÷ d e |
| In-order | a + b × c − d ÷ e (brackets lost) |
| Post-order (postfix) | a b + c × d e ÷ − |
A postfix expression is evaluated with a stack: push operands, and for each operator pop two values, apply it and push the result. The first value popped is the right-hand operand.
Exam traps
- Skipping levels. Finish the whole left subtree before touching the right subtree.
- In-order on a non-search tree. It is not ascending unless the tree is a binary search tree.
- Wrong output for the use. Prefix needs pre-order, postfix needs post-order. In-order loses the brackets.
- No base case. Without the
is not Nonetest the code fails on an empty child. - Counting. Always check that the number of outputs equals the number of nodes.
- Search cost. Searching a binary search tree is O(log n) when balanced but O(n) when the tree has degenerated into a chain.
Quick self-test
- State the visiting order for pre-order traversal.
- Which traversal outputs a binary search tree in ascending order?
- A tree has 12 nodes. How many items does a post-order traversal output?
- Which item is always output last in post-order?
- Which traversal produces a postfix expression?
- Give one reason pre-order is used to copy a tree.
- Why is post-order used to empty a tree?
- In an array tree, what does −1 in the Left column mean?
- Give the post-order output for a root 6 with left child 2 and right child 9.
- State the time complexity of a traversal of n nodes.
- A binary search tree is built from 5, 10, 15, 20. What is the worst-case search cost, in comparisons, for 20?
- Name the traversal that outputs the root first.
Answers
- Node, then left subtree, then right subtree.
- In-order.
-
- The root.
- Post-order.
- A parent is created before its children, so each child can be attached to an existing node.
- Both subtrees are deleted before the node, so no node is removed while it is still the only route to others.
- The node has no left child.
- 2, 9, 6.
- O(n).
- 4 comparisons, because the tree is a chain of four nodes.
- Pre-order.
Official syllabus
OxfordAQA International AS and A-level Computer Science (9645) specification, Version 1.1, for International AS exams May/June 2025 onwards and International A-level exams May/June 2026 onwards: sub-topic 3.11.2 Tree algorithms.
Related resources
-
Study guides
OxfordAQA A-Level Computer Science (9645): Graph algorithms
OxfordAQA 9645 sub-topic 3.11.1 in depth: tracing and coding breadth-first and depth-first search, applications, and Dijkstra's shortest path algorithm with traces.
Computer Science · OxfordAQA · A Level
Read guide -
Practice questions
OxfordAQA A-Level Computer Science (9645): Graph algorithms – Practice Questions
Ten original OxfordAQA 9645 questions on sub-topic 3.11.1: tracing breadth-first and depth-first search, coding them, and tracing Dijkstra's algorithm, with answers.
Computer Science · OxfordAQA · A Level
Try questions -
Revision notes
OxfordAQA A-Level Computer Science (9645): Graph algorithms – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.11.1: breadth-first and depth-first search, their uses, Dijkstra's algorithm, trace methods and a self-test.
Computer Science · OxfordAQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.