
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inorder Non-Recursive Traversal of a Given Binary Tree in C++
Binary Tree traversal is a process of visiting all the nodes in a certain order. In this article, we will learn how to perform inorder non-recursive traversal of a binary tree using a stack in C++.
What is Inorder Non-Recursive Traversal?
Inorder traversal is a type of tree traversal, where we first visit the left subtree, then the root node, and then the right subtree. In a non-recursive approach, we are not allowed to use recursive functions to track nodes for traversing the tree. Instead, we can use an explicit stack data structure to track nodes and traverse through the tree. This approach is useful in cases where recursion is not allowed or where stack overflow error needs to be avoided.
For example, for a binary tree like below:
1 / \ 2 3 / \ 4 5
The inorder traversal will be: 4 2 5 1 3
Implement Inorder Non-Recursive Traversal in C++
To implement inorder traversal without recursion, we use a stack data structure to mimick the system's recursion call stack. Below are some key points that will make you understand the method:
- Structure: Each node contains a data value and two pointers to the left and right child.
- Logic: Use a loop to move to the leftmost node. While moving, push all nodes into a stack, then process nodes and move to the right subtree.
Steps to Perform Inorder Non-Recursive Traversal
Following are steps/algorithm to perform inorder traversal without recursion:
- Create a binary tree node structure with data, left, and right pointers.
- Initialize an empty stack and set the current node to root.
- Loop while current is not NULL or stack is not empty.
- Push current node to stack and move to its left child until NULL.
- Pop node from stack, process it (print value), then move to its right child.
C++ Program to Perform Inorder Non-Recursive Traversal
In the below code, we have implemented a binary tree and used a stack to perform inorder traversal without recursion.
#include <iostream> #include <stack> using namespace std; // Define a node of the binary tree struct Node { int data; Node* left; Node* right; Node(int val) { data = val; left = right = nullptr; } }; // Function to perform inorder non-recursive traversal void inorderTraversal(Node* root) { stack<Node*> st; Node* current = root; while (current != nullptr || !st.empty()) { while (current != nullptr) { st.push(current); current = current->left; } current = st.top(); st.pop(); cout << current->data << " "; current = current->right; } } int main() { // Creating tree nodes Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); cout << "Inorder Traversal (Non-Recursive): "; inorderTraversal(root); return 0; }
The output of above code will be:
Inorder Traversal (Non-Recursive): 4 2 5 1 3
Time and Space Complexity
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Space Complexity: O(h), where h is the height of the tree due to the stack usage.