Tree Traversals: Inorder Postorder Preorder In C++

Tree Traversals: Inorder Postorder Preorder in C++

Tree traversal is a fundamental concept in data structures, allowing us to visit every node of a tree in a specific sequence. A tree can be navigated using two major approaches Level Order Traversal and Depth First Traversal. Under depth first traversal, we primarily use three methods: Inorder, Preorder, and Postorder.

Each of these techniques follows a unique visiting pattern and is widely used in various C++ applications such as expression tree evaluation, binary search tree operations, and hierarchical data processing.

In this article, we will explore all three depth first traversals with explanations and examples to help you understand how they work in C++.

Tree Traversals - Inorder,Postorder and preorder

More About Preorder Traversal

  1. Preorder traversal is a depth first algorithm.
  2. We first print the value of the node,them move to the left subtree and finally to the right subtree.
  3. If we need to explore more about the node itself before inspecting its leaves, we use preorder traversal.
  4. Preorder traversal is used to find the prefix expression of an expression tree.

More About Inorder Traversal:

  1. Inorder Traversal is a depth first algorithm.
  2. In Inorder Traversal, we first move to the left subtree, then print the node and finally move to the right subtree.
  3. If you want the orginal sequence or how the tree was made, we use the inorder sequence.
  4.  Inorder Traversal of a binary search tree gives the sequence in non decreasing order.

More About Postorder Traversal:

  1. Postorder traversal is a depth first algorithm.
  2. In postorder traversal, we first move to the left subtree then to the right subtree and finally print the node.
  3. Post order traversal is used when we want to free the nodes of the tree.
  4. It is also used to find the postfix expression.
Tree TraversalsInorder,Preorder and Postorder in C-1

Algorithm To Find Preorder Traversal:

  1. If root is NULL, return NULL.
  2. Print the node.
  3. Visit left subtree.
  4. Visit right subtree.

Algorithm To Find Inorder Traversal:

  1. If root is NULL, return NULL.
  2. Visit the left subtree.
  3. Print the node.
  4. Visit right subtree.

Algorithm To Find Postorder Traversal:

  1. If root is NULL, return NULL.
  2. Visit the left subtree.
  3. Visit the right subtree
  4. Print the data.

Code Implementation for various Tree traversals

Code implementation for various tree traversals Inorder, Preorder, and Postorder helps you understand how data is accessed in different depth first patterns. These traversal methods play a crucial role in solving problems related to binary trees, searching, and expression evaluation. In C++, implementing each traversal provides a clear view of how nodes are visited and processed systematically.

Run

#include<bits/stdc++.h>
using namespace std;
class Tree
{
public:
  int data;
  Tree *left = NULL, *right = NULL;
  // Constructor initialised
    Tree (int x)
  {
    data = x;
    left = NULL;
    right = NULL;
  }
};
void preorder_traversal (Tree * root)
{
  if (root == NULL)
    return;
  // Print the data
  cout << root->data << " "; // Visit Left subtree preorder_traversal (root->left);
  // Visit right subtree
  preorder_traversal (root->right);
}

void inorder_traversal (Tree * root)
{
  if (root == NULL)
    return;
  // Visit Left subtree
  inorder_traversal (root->left);
  // Print the data
  cout << root->data << " "; // Visit right subtree inorder_traversal (root->right);
}

void postorder_traversal (Tree * root)
{
  if (root == NULL)
    return;
  // Visit Left subtree
  postorder_traversal (root->left);
  // Visit right subtree
  postorder_traversal (root->right);
  // Print the data
  cout << root->data << " "; } int main () { Tree *root = new Tree (17); root->left = new Tree (10);
  root->right = new Tree (11);
  root->left->left = new Tree (7);
  root->right->left = new Tree (27);
  root->right->right = new Tree (9);
  cout << "Preorder => ";
  preorder_traversal (root);
  cout << endl;
  cout << "Inorder => ";
  inorder_traversal (root);
  cout << endl;
  cout << "Postorder => ";
  postorder_traversal (root);
  cout << endl;
  return 0;
}

Output:

Preorder => 17 10 7 11 27 9 
Inorder => 7 10 17 27 11 9 
Postorder => 7 10 27 9 11 17 

Explanation:

  • The code creates a Tree class where each node stores data and two child pointers initialized to NULL.
  • Traversal functions use recursion, each checking if the current node is NULL before continuing.
  • Preorder prints the node first, inorder prints after visiting the left subtree, and postorder prints after both subtrees.
  • The main function manually builds a binary tree by linking nodes together as left and right children.
  • Each traversal function is called in main, and the visited nodes are printed according to their respective order.

Time and Space Complexity:

Operation / TraversalTime ComplexitySpace Complexity
Preorder TraversalO(n)O(h)
Inorder TraversalO(n)O(h)
Postorder TraversalO(n)O(h)
Tree ConstructionO(n)O(1)

To Wrap It Up:

Tree traversals like Inorder, Preorder, and Postorder play a crucial role in understanding how data is accessed and processed in binary trees. These traversal methods help programmers navigate a tree efficiently while preserving different logical sequences based on specific requirements.

By mastering these three depth-first techniques, you can easily handle tasks such as expression evaluation, tree construction, and sorted data extraction. With clear logic and simple implementation in C++, these traversals form a strong foundation for more advanced tree-based algorithms.

FAQs

Choose the traversal based on your requirement  inorder for sorted output, preorder for tree construction, and postorder for operations that require child processing first.

Yes, all three traversals can be implemented using stacks, allowing them to work iteratively without relying on recursive calls.

Traversal order determines how data is visited, displayed, or processed, which directly impacts tasks like searching, evaluating expressions, and serialization.

While these specific orders are mainly used for binary trees, similar traversal concepts can be adapted for multi way trees with slight modifications.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Stacks

  • Introduction to Stack in Data Structure
    Click Here
  • Operations on a Stack
    Click Here
  • Stack: Infix, Prefix and Postfix conversions
    Click Here
  • Stack Representation in –
    C | C++ | Java
  • Representation of a Stack as an Array. –
    C | C++ | Java
  • Representation of a Stack as a Linked List. –
    C | C++ | Java
  • Infix to Postfix Conversion –
    C | C++ | Java
  • Infix to prefix conversion in –
    C | C++ | Java
  • Postfix to Prefix Conversion in –
    C | C++ | Java

Queues

  • Queues in Data Structures (Introduction)
    Click Here
  • Queues Program in C and implementation
    Click Here
  • Implementation of Queues using Arrays | C Program
    Click Here
  • Types of Queues in Data Structure
    Click Here
  • Application of Queue Data Structure
    Click Here
  • Insertion in Queues Program (Enqueuing) –
    C | C++ | Java
  • Deletion (Removal) in Queues Program(Dequeuing) –
    C | C++ | Java
  • Reverse a Queue –
    C | C++ | Java
  • Queues using Linked Lists –
    C | C++ | Java
  • Implement Queue using Stack –
    C | C++ | Java
  • Implement Queue using two Stacks –
    C | C++ | Java

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java