
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program for Inorder Tree Traversal without Recursion
If a binary tree is traversed in-order, the left subtree is visited first, then the root and later the right sub-tree. The output the key in ascending order in in_order traversal. This is a C++ Program for Inorder Tree Traversal without Recursion.
Algorithm
Begin Function inOrder(): Declare a stack s. Declare the current node as root. While current node is not null and stack is not empty do While current node is not null do Push the current node on the top of the stack Make the left child node as current node Point the current node at the top of the stack. Pop the top most node from the stack Print the current node. Make the right node as current node. Insert some elements at root, left node and right node in stack. Call the inOrder() function to traverse the tree. End.
Example Code
#include<bits/stdc++.h> using namespace std; struct n { int d; struct n* l; struct n* r; n (int d) { this->d = d; l = r = NULL; } }; void inOrder(struct n *root) { stack<n *> s; n *current = root; while (current != NULL || s.empty() == false) { while (current != NULL) { s.push(current); current = current->l; } current = s.top(); s.pop(); cout << current->d << " "; current = current->r; } } int main() { struct n *root = new n(7); root->l = new n(6); root->r = new n(2); root->l->l = new n(1); root->l->r = new n(9); inOrder(root); return 0; }
Output
1 6 9 7 2
- Related Articles
- Golang program to perform inorder tree traversal
- Java Program to Perform the inorder tree traversal
- Postorder traversal of Binary Tree without recursion and without stack in C++
- Preorder Traversal of N-ary Tree Without Recursion in C++
- Binary Tree Inorder Traversal in Python
- Program to generate tree using preorder and inorder traversal in python
- Golang program to traverse a given tree in Inorder Traversal (Recursive).
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- Program to perform an Inorder Traversal of a binary tree in Python
- Inorder Traversal of a Threaded Binary Tree in C++
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input
- Python Program to Find the Largest value in a Tree using Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal in Python

Advertisements