📄 Paper 1 · 4.3 Algorithms
⭐ Pro
4.3.2 Tree Traversal
AQA 7517 · A-Level Computer Science · ~18 min read

Tree Traversal

Tree traversal algorithms visit every node in a tree exactly once. There are three main traversal orders: pre-order, in-order, and post-order. All three are recursive (or can use a stack).

Pre-Order Traversal (Root → Left → Right)

Visit the root first, then recursively traverse the left subtree, then the right subtree.

PROCEDURE preOrder(node)
  IF node ≠ null THEN
    process(node)        // Visit root first
    preOrder(node.left)  // Then left subtree
    preOrder(node.right) // Then right subtree
  END IF
END PROCEDURE

Use: Copying or serialising a tree structure; prefix notation for expressions.

In-Order Traversal (Left → Root → Right)

Recursively traverse the left subtree, visit the root, then recursively traverse the right subtree.

PROCEDURE inOrder(node)
  IF node ≠ null THEN
    inOrder(node.left)   // Left subtree first
    process(node)        // Visit root in middle
    inOrder(node.right)  // Then right subtree
  END IF
END PROCEDURE

Key property: In-order traversal of a Binary Search Tree (BST) produces nodes in ascending sorted order.

Post-Order Traversal (Left → Right → Root)

Recursively traverse the left subtree, then the right subtree, then visit the root last.

PROCEDURE postOrder(node)
  IF node ≠ null THEN
    postOrder(node.left)  // Left subtree
    postOrder(node.right) // Right subtree
    process(node)         // Visit root last
  END IF
END PROCEDURE

Use: Deleting a tree (children must be deleted before parent); evaluating expression trees.

Worked Example

// Binary tree:
//         5
//        / \
//       3   8
//      / \   \
//     1   4   9

Pre-order:  5, 3, 1, 4, 8, 9   (Root first)
In-order:   1, 3, 4, 5, 8, 9   (Sorted — it's a BST)
Post-order: 1, 4, 3, 9, 8, 5   (Root last)

Expression Trees

An expression tree stores an arithmetic expression. Operators are internal nodes; operands are leaves.

// Expression: (3 + 4) × 2
//        ×
//       / \
//      +   2
//     / \
//    3   4

Pre-order  (prefix):  × + 3 4 2    → Polish notation
In-order   (infix):   3 + 4 × 2    → Infix (normal)
Post-order (postfix): 3 4 + 2 ×    → Reverse Polish Notation (RPN)

Comparison Summary

TraversalOrderKey Application
Pre-orderRoot → Left → RightCopying tree; prefix notation
In-orderLeft → Root → RightSorted output from BST
Post-orderLeft → Right → RootEvaluate/delete tree; RPN
Exam tip: You must be able to trace all three traversals on a given tree diagram. Remember the mnemonic: Pre = root first; In = root in middle; Post = root last. In-order on a BST gives sorted output — a key fact AQA regularly tests. Post-order relates to RPN (covered in 4.3.3).
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.3.2 Tree Traversal

8 questions · instantly marked · AQA 7517 standard

Q1State the order of node visiting for pre-order, in-order, and post-order traversal.[3]
✅ Mark scheme
Mark scheme
Pre-order: Root, Left, Right [1]; In-order: Left, Root, Right [1]; Post-order: Left, Right, Root [1].
Q2What special property does in-order traversal have when applied to a Binary Search Tree?[1]
✅ Mark scheme
Mark scheme
In-order traversal of a BST produces nodes in ascending (sorted) order [1].
Q3Given the tree: root=10, left child=6, right child=14, 6's left=4, 6's right=8. Write the pre-order traversal.[2]
✅ Mark scheme
Mark scheme
Pre-order (Root, Left, Right): 10, 6, 4, 8, 14 [2] (allow 1 mark for correct partial trace showing root visited first).
Q4Write the in-order and post-order traversals for the same tree (root=10, left=6, right=14, 6's left=4, 6's right=8).[4]
✅ Mark scheme
Mark scheme
In-order: 4, 6, 8, 10, 14 [2]; Post-order: 4, 8, 6, 14, 10 [2].
Q5An expression tree has × at the root, + as left child, 2 as right child, and 3 and 4 as children of +. Write the pre-order, in-order, and post-order traversals of this expression tree.[3]
✅ Mark scheme
Mark scheme
Pre-order: × + 3 4 2 [1]; In-order: 3 + 4 × 2 [1]; Post-order: 3 4 + 2 × [1].
Q6Why is post-order traversal appropriate when deleting a tree from memory?[2]
✅ Mark scheme
Mark scheme
Post-order visits (and can delete) children before the parent [1]; if the parent were deleted first, access to children would be lost as pointers would be gone [1].
Q7Write pseudocode for a recursive in-order traversal procedure.[3]
✅ Mark scheme
Mark scheme
PROCEDURE inOrder(node) [1]: IF node ≠ null THEN inOrder(node.left) [1]; process(node); inOrder(node.right) [1]; END IF. Award marks for correct structure: base case (null check), correct order (left, process, right).
Q8A student claims BFS is a type of tree traversal. Is this correct? Explain.[2]
✅ Mark scheme
Mark scheme
Partially correct — BFS can traverse a tree level by level [1]; however pre/in/post-order are specific to trees and use different orderings, whereas BFS is a general graph algorithm also used on trees [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Tree Traversal

10 questions · 10 minutes

← 4.3.1 Graph Traversal
20 of 70 · AQA 7517
4.3.3 Reverse Polish Notation →