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
C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The inorder traversal of a binary tree involves visiting each of the nodes in the tree in the order (Left, Root, Right).
An example of Inorder traversal of a binary tree is as follows.
Here, we start at the leftmost node(5), move up to 6, then to 2, then to root 3, and continue to the right side with 9 then 4 and 8.
Using Recursion for Inorder Traversal
Recursion is a technique where a function calls itself to break down the problem into smaller parts until it reaches a base case. To perform inorder traversal using recursion, we first call the function on the left child. After visiting the left child, we visit the root node, and then recursively move to the right child.
Here's how we do it:
- We create a function called InOrder() that takes the root node as input.
- If the node is NULL, we just return. This is our base case.
- If the node is not NULL, we first call InOrder() on the left child.
- Then, we print the value of the current node.
- Finally, we call InOrder() on the right child.
This way, we visit the nodes in the proper order: left subtree -> root -> right subtree.
C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Here's a complete C++ program to perform inorder traversal using recursion.
#include <iostream>
using namespace std;
// Structure for binary tree node
struct BinaryTreeNode {
int data; // Store node value
BinaryTreeNode* left; // Pointer to left child
BinaryTreeNode* right; // Pointer to right child
// Constructor to initialize the node with a value
BinaryTreeNode(int value) {
data = value; // Set node's data
left = right = nullptr; // Initialize left and right children to nullptr
}
};
// Recursive function for inorder traversal
void InOrder(BinaryTreeNode* root) {
if (root != nullptr) { // If the current node is not null
InOrder(root->left); // Recursively visit the left subtree
cout << root->data << " "; // Visit the current node (print its value)
InOrder(root->right); // Recursively visit the right subtree
}
}
int main() {
// Constructing the binary tree
BinaryTreeNode* root = new BinaryTreeNode(3); // Create root node with value 3
root->left = new BinaryTreeNode(6); // Create left child of root with value 6
root->right = new BinaryTreeNode(5); // Create right child of root with value 5
root->left->left = new BinaryTreeNode(2); //and so on
root->left->right = new BinaryTreeNode(4);
root->right->right = new BinaryTreeNode(9);
root->right->right->left = new BinaryTreeNode(8);
// Perform inorder traversal
cout << "Inorder Traversal: "; // Output the result label
InOrder(root); // Call inorder traversal function
return 0;
}
After running the program, the output you will see shows the nodes visited in inorder traversal:
Inorder Traversal: 2 6 4 3 5 8 9
Time Complexity: O(n) where n is the number of nodes in the tree. We visit each node once.
Space Complexity: O(h) where h is the heigh of the tree because of the recursive call stack. In the worst case this can be O(n).