Skip to content

Depth frist search

🌳 Tree Traversal Tutorial: PreOrder, InOrder, and PostOrder Traversals

In this tutorial, we'll dive into three fundamental ways to traverse a tree data structure: PreOrder, InOrder, and PostOrder. We'll use easy-to-understand examples, code snippets, and illustrations using Mermaid graphs. Let's explore the differences between these traversal methods, with a breakdown of how they work and when to use them.


πŸ“‘ Table of Contents

  1. 🎯 What is Tree Traversal?
  2. πŸ” PreOrder Traversal (Root β†’ Left β†’ Right)
  3. πŸ” InOrder Traversal (Left β†’ Root β†’ Right)
  4. πŸ” PostOrder Traversal (Left β†’ Right β†’ Root)
  5. 🎨 Visualizing Traversal Orders (Mermaid Graphs)
  6. πŸ’» Code Examples
  7. πŸ”š Conclusion

🎯 1. What is Tree Traversal?

Tree traversal refers to the process of visiting each node in a tree in a specific order. Depending on how we traverse the tree (which node we visit first and the order we visit its children), we can classify tree traversal into different methods:

  • PreOrder
  • InOrder
  • PostOrder

These three are Depth-First Search (DFS) strategies, as they explore as far down a tree branch as possible before moving to another branch.


πŸ” 2. PreOrder Traversal (Root β†’ Left β†’ Right) 🌟

In PreOrder traversal, the root node is visited first, then the left subtree, and finally the right subtree.

  • Steps:
  • Visit the root.
  • Recursively traverse the left subtree.
  • Recursively traverse the right subtree.

Example:

def pre_order(root):
    if root:
        print(root.data, end=" ")  # Visit root
        pre_order(root.left)       # Traverse left subtree
        pre_order(root.right)      # Traverse right subtree

For the tree:

      1
     / \
    2   3
   / \
  4   5

The PreOrder traversal will print: 1 2 4 5 3


πŸ” 3. InOrder Traversal (Left β†’ Root β†’ Right) 🌿

In InOrder traversal, the left subtree is visited first, then the root, and finally the right subtree.

  • Steps:
  • Recursively traverse the left subtree.
  • Visit the root.
  • Recursively traverse the right subtree.

Example:

def in_order(root):
    if root:
        in_order(root.left)        # Traverse left subtree
        print(root.data, end=" ")  # Visit root
        in_order(root.right)       # Traverse right subtree

For the same tree:

      1
     / \
    2   3
   / \
  4   5

The InOrder traversal will print: 4 2 5 1 3


πŸ” 4. PostOrder Traversal (Left β†’ Right β†’ Root) πŸ‚

In PostOrder traversal, both subtrees are visited before the root.

  • Steps:
  • Recursively traverse the left subtree.
  • Recursively traverse the right subtree.
  • Visit the root.

Example:

def post_order(root):
    if root:
        post_order(root.left)      # Traverse left subtree
        post_order(root.right)     # Traverse right subtree
        print(root.data, end=" ")  # Visit root

For the tree:

      1
     / \
    2   3
   / \
  4   5

The PostOrder traversal will print: 4 5 2 3 1


🎨 5. Visualizing Traversal Orders (Mermaid Graphs)

🌟 PreOrder Traversal:

graph TD
    A[1 (Root)] --> B[2 (Left)]
    A --> C[3 (Right)]
    B --> D[4 (Left)]
    B --> E[5 (Right)]

Traversal Order: 1 β†’ 2 β†’ 4 β†’ 5 β†’ 3

🌿 InOrder Traversal:

graph TD
    D[4 (Left)] --> B[2 (Root)]
    E[5 (Right)] --> B
    B --> A[1 (Root)]
    A --> C[3 (Right)]

Traversal Order: 4 β†’ 2 β†’ 5 β†’ 1 β†’ 3

πŸ‚ PostOrder Traversal:

graph TD
    D[4 (Left)] --> B[2 (Left Subtree)]
    E[5 (Right)] --> B
    B --> A[1 (Root)]
    C[3 (Right Subtree)] --> A

Traversal Order: 4 β†’ 5 β†’ 2 β†’ 3 β†’ 1


πŸ’» 6. Code Examples

Here’s a sample implementation for all three tree traversals:

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

def pre_order(root):
    if root:
        print(root.data, end=" ")
        pre_order(root.left)
        pre_order(root.right)

def in_order(root):
    if root:
        in_order(root.left)
        print(root.data, end=" ")
        in_order(root.right)

def post_order(root):
    if root:
        post_order(root.left)
        post_order(root.right)
        print(root.data, end=" ")

# Example usage:
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)

print("PreOrder: ")
pre_order(root)
print("\nInOrder: ")
in_order(root)
print("\nPostOrder: ")
post_order(root)

πŸ”š 7. Conclusion

In this tutorial, we explored the three main tree traversal methods: PreOrder, InOrder, and PostOrder. Each method serves a unique purpose, depending on when you need to visit the root node in relation to its subtrees. Using the visual graphs and examples, you should now have a solid understanding of how each traversal works! 🌟🌳