TutorChase logo
Login
AQA A-Level Computer Science

12.2.2 Pre-order traversal

Pre-order traversal is a systematic way of visiting all the nodes in a binary tree by visiting the root first, then the left subtree, and finally the right subtree.

What is pre-order traversal?

Pre-order traversal is one of the three main depth-first traversal strategies used to explore or process nodes in a binary tree. The key feature of pre-order traversal is that it visits the root node before its subtrees, which distinguishes it from other traversal methods like in-order and post-order.

The specific order followed in pre-order traversal is:

  • Visit the root node first

  • Recursively traverse the left subtree

  • Recursively traverse the right subtree

This traversal pattern ensures that each node is processed before its descendants. This top-down approach makes pre-order traversal especially useful in scenarios where the root node needs to be handled before the rest of the tree, such as copying a tree or generating prefix expressions from expression trees.

It is important to understand that pre-order traversal is inherently recursive in structure, reflecting the natural hierarchy of trees.

Why use pre-order traversal?

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

If a node has only one child during pre-order traversal—whether it’s a left or right child—the traversal proceeds by visiting the node first, as always, and then continues into the single available child. The absence of one child does not affect the process but simply shortens the path taken. For example, if a node has only a left child, the algorithm visits the root, moves to the left subtree and continues recursively, skipping the right as it’s null. If it has only a right child, the left subtree is skipped instead. The structure of the traversal remains consistent: visit root, then left, then right—except the traversal only continues into whichever subtree is present. This ensures that all non-null nodes are visited in the correct pre-order sequence, and null branches are bypassed naturally. This behaviour is particularly important when traversing skewed trees or incomplete trees, which frequently arise in practical applications like data compression trees or partially constructed binary heaps.

Pre-order traversal does not make any assumptions about the uniqueness of node values—it simply processes nodes in the specified order regardless of their data. When duplicate values are present, the traversal treats them just like any other node, visiting the root first, then recursively the left and right subtrees. The algorithm does not compare or attempt to resolve duplicates—it merely follows the structure of the tree. As a result, if multiple nodes contain the same value, the traversal will include each instance based on its position in the tree. The appearance of the duplicate values in the output will reflect the order in which those nodes are encountered during traversal. This is important when serialising a tree or evaluating an expression tree where duplicate values can occur naturally, such as repeated operators or operands. It is the structure—not the uniqueness of values—that defines the path and outcome of pre-order traversal.

Yes, pre-order traversal can be extended to work on general trees—also called n-ary trees—where each node can have more than two children. The concept remains the same: visit the node itself first, then recursively traverse each of its children in the order they appear. The only difference is that instead of two fixed calls (left and right), the algorithm must iterate through a list or collection of children and apply the same traversal logic to each. This approach is commonly used in real-world data structures like file system trees, syntax trees in compilers, and scene graphs in game engines. The key requirement is that the tree's structure allows for ordered access to children. The traversal order becomes: root → child1 → child2 → ... → childN. As long as the tree maintains hierarchical relationships, pre-order traversal will work effectively and maintain the root-first property, which is often critical for preserving structure in serialisation and hierarchy-aware processing.

Yes, the structure of the tree—whether balanced or unbalanced—affects the order of traversal, though not the method itself. Pre-order traversal always follows the same rule: root → left → right. In a balanced tree, where nodes are evenly distributed across levels, the output appears more uniform and predictable. The left and right subtrees of each node are visited in a relatively symmetrical pattern. In contrast, an unbalanced tree—where nodes predominantly exist on one side (e.g. all left children)—results in a traversal path that is more linear, almost resembling a linked list. In such cases, the output may heavily favour one side, producing a sequence that reflects the skewed structure. This affects not just the readability of the traversal but also its memory usage and efficiency. While the algorithm doesn't change, the actual sequence and recursion depth will vary based on balance, which can have performance implications in large or deep trees.

Pre-order traversal is not efficient for searching in arbitrary binary trees, especially when compared to more specialised traversal methods like in-order traversal in binary search trees (BSTs). Since pre-order does not take advantage of any value ordering, it may end up exploring large parts of the tree unnecessarily before locating the target value. For example, if the value being searched for is deep in the rightmost branch, pre-order will still process the entire left subtree first. In the worst case, this leads to O(n) time complexity, where n is the total number of nodes. Furthermore, pre-order traversal does not guarantee that the smallest or largest values are encountered early, making it unsuitable for operations that rely on ordering. It also does not terminate early once a match is found unless additional logic is implemented to stop recursion, which adds complexity. Therefore, while useful for structure-related tasks, pre-order is not ideal for efficient searching.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email