Binary Tree: Pre-order Traversal
Representation
We represent the node as:
Node =>
Value
Node Left
Node Right
Pre-order Traversal
The pre-order traversal is a kind of depth-first traversal. We perform the following steps:
- Access the node
- Recursively traverse the node’s left subtree in pre-order
- Recursively traverse the node’s right subtree in pre-order
After traversing the left and the right subtrees of the root node, we consider the traversal complete.
Example
We will do the pre-order traversal on the following binary tree:
The nodes in yellow are not yet visited, and the subtrees with dashed edges are also not visited yet. The pre-order traversal visits the nodes A, B, D, G, E, H, C, and F.
Let’s take a look at each visit separately. We start with the root node, i.e…