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 search 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.
A binary tree is given as follows.
Inorder Traversal is: 1 4 5 6 8
The program to perform in-order recursive traversal is given as follows.
 Live Demo
#include<iostream>
using namespace std;
struct node {
   int data;
   struct node *left;
   struct node *right;
};
struct node *createNode(int val) {
   struct node *temp = (struct node *)malloc(sizeof(struct node));
   temp->data = val;
   temp->left = temp->right = NULL;
   return temp;
}
void inorder(struct node *root) {
   if (root != NULL) {
      inorder(root->left);
      cout<<root->data<<" ";
      inorder(root->right);
   }
}
struct node* insertNode(struct node* node, int val) {
   if (node == NULL) return createNode(val);
   if (val < node->data)
   node->left = insertNode(node->left, val);
   else if (val > node->data)
   node->right = insertNode(node->right, val);
   return node;
}
int main() {
   struct node *root = NULL;
   root = insertNode(root, 4);
   insertNode(root, 5);
   insertNode(root, 2);
   insertNode(root, 9);
   insertNode(root, 1);
   insertNode(root, 3);
   cout<<"In-Order traversal of the Binary Search Tree is: ";
   inorder(root);
   return 0;
}
In-Order traversal of the Binary Search Tree is: 1 2 3 4 5 9
In the above program, the structure node creates the node of a tree. This structure is a self referential structure as it contains pointers of struct node type. This structure is shown as follows.
struct node {
   int data;
   struct node *left;
   struct node *right;
};
The function createNode() creates a node temp and allocates it memory using malloc. The data value val is stored in data of temp. NULL is stored in left and right pointers of temp. This is demonstrated by the following code snippet.
struct node *createNode(int val) {
   struct node *temp = (struct node *)malloc(sizeof(struct node));
   temp->data = val;
   temp->left = temp->right = NULL;
   return temp;
}
The function inorder() takes the root of the binary tree as argument and prints the elements of the tree in inorder. It is a recursive function. It is demonstrated using the following code.
void inorder(struct node *root) {
   if (root != NULL) {
      inorder(root->left);
      cout<<root->data<<" ";
      inorder(root->right);
   }
}
The function insertNode() inserts the required value in the binary tree at its correct position. If the node is NULL, then createNode is called. Otherwise, the correct position for the node is found in the tree. This can be observed in the following code snippet.
struct node* insertNode(struct node* node, int val) {
   if (node == NULL) return createNode(val);
   if (val < node->data)
   node->left = insertNode(node->left, val);
   else if (val > node->data)
   node->right = insertNode(node->right, val);
   return node;
}
In the main() function, the root node is first defined as NULL. Then all the nodes with the required values are inserted into the binary search tree. This is shown below.
struct node *root = NULL;
root = insertNode(root, 4);
insertNode(root, 5);
insertNode(root, 2);
insertNode(root, 9);
insertNode(root, 1);
insertNode(root, 3);
Finally, the function inorder() is called using the root node of the tree and all the tree values are displayed in inorder. This is given below.
cout<<"In-Order traversal of the Binary Search Tree is: ";
inorder(root);