C++ Program to Perform Preorder 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 preorder traversal of a binary search tree involves visiting each of the nodes in the tree in the order (Root, Left, Right).
An example of Preorder traversal of a binary tree is as follows.
A binary tree is given as follows.
Preorder Traversal is: 6 4 1 5 8
The program to perform pre-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 preorder(struct node *root) {
   if (root != NULL) {
      cout<<root->data<<" ";
      preorder(root->left);
      preorder(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<<"Pre-Order traversal of the Binary Search Tree is: ";
   preorder(root);
   return 0;
}
Pre-Order traversal of the Binary Search Tree is: 4 2 1 3 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 preorder() takes the root of the binary tree as argument and prints the elements of the tree in preorder. It is a recursive function. It is demonstrated using the following code.
void preorder(struct node *root) {
   if (root != NULL) {
      cout<<root-->data<<" ";
      preorder(root-->left);
      preorder(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 preorder() is called using the root node of the tree and all the tree values are displayed in preorder. This is given below.
cout<<"Pre-Order traversal of the Binary Search Tree is: ";
preorder(root);