Tree Traversal Algorithms: Inorder, Preorder and Postorder
Tree traversal is the process of visiting exactly once every node in a tree data structure. It defines the specific order in which nodes are accessed, typically to update, delete, or display the data they contain.
Theory & Explanation
Tree Traversal Algorithms: Inorder, Preorder, and Postorder
1. Definition and Overview
Tree Traversal is the process of visiting (checking and/or updating) each node in a tree data structure, exactly once. Unlike linear data structures (arrays, linked lists) which have a canonical linear ordering, trees can be traversed in multiple ways.
The three most common Depth-First Search (DFS) traversal algorithms for binary trees are defined by the position of the parent node (Root) relative to visiting its Left and Right subtrees:
- Preorder: Root → Left → Right
- Inorder: Left → Root → Right
- Postorder: Left → Right → Root
2. Graphical Representation of the Sample Tree
We will use the following binary tree for our examples and traversal traces.
Sample Binary Tree Structure
3. Traversal Algorithms: Step-by-Step Trace and Example
Using the tree above, we recursively trace each algorithm.
A. Preorder Traversal (Root → Left → Right)
The root is visited first, followed by the recursive preorder traversal of the left subtree, then the recursive preorder traversal of the right subtree.
- 1. Visit Root: 1
- 2. Traverse Left Subtree of 1:
- 2a. Visit Root: 2
- 2b. Traverse Left Subtree of 2: Visit 4
- 2c. Traverse Right Subtree of 2: Visit 5
- 3. Traverse Right Subtree of 1: Visit 3
Preorder Output: [1, 2, 4, 5, 3]
B. Inorder Traversal (Left → Root → Right)
The left subtree is visited first (recursively), then the root node, and finally the right subtree (recursively). For a Binary Search Tree (BST), this visits nodes in non-decreasing order.
- 1. Traverse Left Subtree of 1:
- 1a. Traverse Left Subtree of 2: Visit 4
- 1b. Visit Root: 2
- 1c. Traverse Right Subtree of 2: Visit 5
- 2. Visit Root: 1
- 3. Traverse Right Subtree of 1: Visit 3
Inorder Output: [4, 2, 5, 1, 3]
C. Postorder Traversal (Left → Right → Root)
Both the left and right subtrees are visited (recursively) before the root node. This is commonly used to delete a tree or evaluate postfix expressions.
- 1. Traverse Left Subtree of 1:
- 1a. Traverse Left Subtree of 2: Visit 4
- 1b. Traverse Right Subtree of 2: Visit 5
- 1c. Visit Root: 2
- 2. Traverse Right Subtree of 1: Visit 3
- 3. Visit Root: 1
Postorder Output: [4, 5, 2, 3, 1]
4. C Implementation of Tree Traversals
#include <stdio.h>
#include <stdlib.h>
// Definition of a Tree Node
struct Node {
int data;
struct Node left;
struct Node right;
};
// Helper function to create a new node
struct Node createNode(int data) {
struct Node newNode = (struct Node)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 1. Preorder Traversal (Root, Left, Right)
void preorderTraversal(struct Node root) {
if (root == NULL) return;
printf("%d ", root->data); // Visit Root
preorderTraversal(root->left); // Traverse Left
preorderTraversal(root->right); // Traverse Right
}
// 2. Inorder Traversal (Left, Root, Right)
void inorderTraversal(struct Node root) {
if (root == NULL) return;
inorderTraversal(root->left); // Traverse Left
printf("%d ", root->data); // Visit Root
inorderTraversal(root->right); // Traverse Right
}
// 3. Postorder Traversal (Left, Right, Root)
void postorderTraversal(struct Node root) {
if (root == NULL) return;
postorderTraversal(root->left); // Traverse Left
postorderTraversal(root->right); // Traverse Right
printf("%d ", root->data); // Visit Root
}
int main() {
// Constructing the sample tree
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
return 0;
}
Expected Output (C):
Preorder Traversal: 1 2 4 5 3
Inorder Traversal: 4 2 5 1 3
Postorder Traversal: 4 5 2 3 1
5. Python Implementation of Tree Traversals
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# 1. Preorder Traversal (Root, Left, Right)
def preorder_traversal(root, result=[]):
if not root:
return result
result.append(root.data) # Visit Root
preorder_traversal(root.left, result) # Traverse Left
preorder_traversal(root.right, result) # Traverse Right
return result
# 2. Inorder Traversal (Left, Root, Right)
def inorder_traversal(root, result=[]):
if not root:
return result
inorder_traversal(root.left, result) # Traverse Left
result.append(root.data) # Visit Root
inorder_traversal(root.right, result) # Traverse Right
return result
# 3. Postorder Traversal (Left, Right, Root)
def postorder_traversal(root, result=[]):
if not root:
return result
postorder_traversal(root.left, result) # Traverse Left
postorder_traversal(root.right, result) # Traverse Right
result.append(root.data) # Visit Root
return result
if __name__ == "__main__":
# Constructing the sample tree
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
# Note: We pass a fresh list for each call because Python
# default arguments persist between calls.
print("Preorder Traversal: ", preorder_traversal(root, []))
print("Inorder Traversal: ", inorder_traversal(root, []))
print("Postorder Traversal:", postorder_traversal(root, []))
Expected Output (Python):
Preorder Traversal: [1, 2, 4, 5, 3]
Inorder Traversal: [4, 2, 5, 1, 3]
Postorder Traversal: [4, 5, 2, 3, 1]
