
- 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 to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
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 Declare a structure n. Declare d of the integer datatype. Declare a pointer l against structure n. Declare a pointer r against structure n. Declare a constructor of structure n. Pass an integer variable d to parameter. this->d = d l = r = NULL Declare inOrder(struct n *root) function. Declare a stack s. Declare a pointer current against structure n. Initialize n *current = root. while (current != NULL || s.empty() == false) while (current != NULL) s.push(current) current = current->l current = s.top() s.pop() print current->d. current = current->r. insert values in nodes of tree. Call inOrder(root) function to travern the tree. End.
Example
#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(6); root->l = new n(4); root->r= new n(7); root->l->l = new n(8); root->l->r= new n(5); root->r->l = new n(9); root->r->r = new n(10); inOrder(root); return 0; }
Output
8 4 5 6 9 7 10
- Related Articles
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree
- Program to perform an Inorder Traversal of a binary tree in Python
- Golang program to traverse a given tree in Inorder Traversal (Recursive).
- Golang program to perform inorder tree traversal
- Java Program to Perform the inorder tree traversal
- Construct Special Binary Tree from given Inorder traversal in C++
- Inorder Traversal of a Threaded Binary Tree in C++
- Golang Program to traverse a given binary tree in Preorder Traversal (Recursive)
- Binary Tree Inorder Traversal in Python
- Program to perform level order traversal of binary tree in C++
- C++ Program for Inorder Tree Traversal without Recursion

Advertisements