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
- π― What is Tree Traversal?
- π PreOrder Traversal (Root β Left β Right)
- π InOrder Traversal (Left β Root β Right)
- π PostOrder Traversal (Left β Right β Root)
- π¨ Visualizing Traversal Orders (Mermaid Graphs)
- π» Code Examples
- π 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:
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:
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:
The PostOrder traversal will print: 4 5 2 3 1
π¨ 5. Visualizing Traversal Orders (Mermaid Graphs)
π PreOrder Traversal:
Traversal Order: 1 β 2 β 4 β 5 β 3
πΏ InOrder Traversal:
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! ππ³