No history yet

Tree Traversals

Visiting Every Node

A tree is a powerful way to organize data hierarchically, but how do you systematically access every single item within it? This process is called traversal. It's simply a method for visiting each node in a tree exactly once in a predictable order. Unlike linear data structures like arrays, which have a clear beginning and end, trees can be explored in multiple, meaningful ways.

Imagine you have a simple family tree. You could list everyone by generation (grandparents, then parents, then children), or you could follow one family line all the way down before moving to the next. Tree traversals provide this kind of structured exploration for any tree data structure. We'll use this sample binary tree to explore the different traversal methods:

Depth-First Traversals

The first group of traversals we'll look at are 'depth-first'. This means we go as deep as possible down one path before we backtrack and explore another. The three main depth-first methods are Pre-order, In-order, and Post-order. Their names refer to the position of the root node in the traversal pattern relative to its left and right subtrees.

For these examples, "visiting" a node means processing its data. In our case, we'll just be printing its letter.

Pre-order Traversal

In a pre-order traversal, we visit the current node first, then recursively travel down its left subtree, and finally its right subtree. The pattern is always Root, then Left, then Right.

Pre-order Pattern: Root → Left → Right

Let's trace this on our example tree:

  1. Start at the root, F. Visit F.
  2. Go left to B. Visit B.
  3. Go left to A. Visit A. A has no children, so backtrack to B.
  4. From B, go right to D. Visit D.
  5. Go left to C. Visit C. Backtrack to D.
  6. From D, go right to E. Visit E. Backtrack to D, then B, then F.
  7. From F, go right to G. Visit G.
  8. G has no left child. Go right to I. Visit I.
  9. Go left to H. Visit H. Backtrack up to the root.

The final pre-order traversal is: F, B, A, D, C, E, G, I, H.

In-order Traversal

For an in-order traversal, we recursively travel the left subtree first, then visit the current node, and finally travel the right subtree. The pattern is Left, Root, Right.

In-order Pattern: Left → Root → Right

Using our tree again:

  1. Start at F. Go left to B. Go left to A.
  2. A has no left child. Visit A. Backtrack to B.
  3. Now visit the root of this subtree, B.
  4. Go right to D. Go left to C.
  5. C has no left child. Visit C. Backtrack to D.
  6. Visit the root of this subtree, D.
  7. Go right to E. E has no left child. Visit E. Backtrack up to F.
  8. Now that the entire left subtree of F is done, visit F.
  9. Go right to G. G has no left child. Visit G.
  10. Go right to I. Go left to H.
  11. H has no left child. Visit H. Backtrack to I.
  12. Visit I. The traversal is complete.

The final in-order traversal is: A, B, C, D, E, F, G, H, I.

Notice something interesting? If the tree were a Binary Search Tree (where left children are smaller and right children are larger), an in-order traversal would visit the nodes in sorted ascending order.

Post-order Traversal

Post-order traversal puts the root last. We recursively traverse the left subtree, then the right subtree, and finally visit the current node.

Post-order Pattern: Left → Right → Root

One last time with our example:

  1. Start at F. Go fully down the left subtree to B, then A. A has no children, so visit A.
  2. Backtrack to B. Go down its right subtree to D. Go left to C. Visit C.
  3. Backtrack to D. Go right to E. Visit E.
  4. Backtrack to D. Now that both children are visited, visit D.
  5. Backtrack to B. Now that both its children's subtrees are visited, visit B.
  6. Backtrack to F. Now go fully down the right subtree to G, then I, then H. Visit H.
  7. Backtrack to I. It has no right child. Visit I.
  8. Backtrack to G. It has no left child. Visit G.
  9. Backtrack to F. Both subtrees are done. Visit F.

The final post-order traversal is: A, C, E, D, B, H, I, G, F.

Breadth-First Traversal

There's another major way to traverse a tree that isn't depth-first. Level-order traversal, also known as breadth-first traversal, explores the tree layer by layer. It visits all the nodes at depth 0 (the root), then all nodes at depth 1, then depth 2, and so on.

To keep track of the nodes to visit, this algorithm uses a queue. You start by adding the root to the queue. Then, you enter a loop that continues as long as the queue isn't empty. In each step, you remove a node from the front of the queue, visit it, and then add its children (if any) to the back of the queue.

Lesson image

Let's trace a level-order traversal on our example tree (F, B, G, A, D, I, C, E, H):

  1. Queue: [F]. Dequeue and visit F. Enqueue B and G. Queue: [B, G]
  2. Dequeue and visit B. Enqueue A and D. Queue: [G, A, D]
  3. Dequeue and visit G. Enqueue I. Queue: [A, D, I]
  4. Dequeue and visit A. Enqueue nothing. Queue: [D, I]
  5. Dequeue and visit D. Enqueue C and E. Queue: [I, C, E]
  6. Dequeue and visit I. Enqueue H. Queue: [C, E, H]
  7. Dequeue and visit C. Queue: [E, H]
  8. Dequeue and visit E. Queue: [H]
  9. Dequeue and visit H. Queue is now empty, so we're done.

The final level-order traversal is: F, B, G, A, D, I, C, E, H.

Applications

Different traversals are useful for different tasks. It's not just about listing nodes; the order itself has meaning.

TraversalCommon Use Cases
In-orderRetrieving data in sorted order from a Binary Search Tree.
Pre-orderCreating a copy of a tree. Getting prefix notation (e.g., + a b) from an expression tree.
Post-orderDeleting a tree, as it deletes children before the parent. Getting postfix notation (e.g., a b +).
Level-orderFinding the shortest path between two nodes. Can be used in algorithms like finding the lowest common ancestor.

Understanding these four traversals is fundamental. They are the building blocks for many more complex tree algorithms and operations.

Quiz Questions 1/6

Given the sample tree from the lesson, what is the correct pre-order traversal sequence?

Quiz Questions 2/6

Which traversal method explores the tree layer by layer, visiting all nodes at a given depth before moving to the next level?