Tree traversal program in c is methods to visit or traverse the tree.

What is tree traversal?
The traversing tree means visiting all its nodes to get the complete tree information.

Tree traversal methods
There are three methods of tree traversal

  1. Preorder
  2. Inorder
  3. Postorder

1. Preorder traversal
This is the first method of tree traversal in the data structure.
In this method we first visit the root node, next we visit all left subtree and at last, we visit the right subtree.
Preorder traversal algorithm
Visit root
Traverse the left subtree in Preorder
Traverse right subtree in Preorder

2. Inorder traversal
This is the second method of tree traversal in data structure.
In this method we first visit all left subtree, next we visit the root node, and at last, we visit right subtree.
Inorder traversal algorithm
Traverse the left subtree in inorder
Visit root
Traverse the right subtree in inorder

3. Postorder traversal
This is the third method of tree traversal in the data structure.
In this method we first visit all left subtree, next we visit the right subtree, and at last visit root node.
Preorder traversal algorithm
Traverse left subtree in post order
Traverse right subtree in post order
Visit root
Tree traversal example

Tree traversal example

Consider above tree with A,B,D,E,F,G nodes of tree.
Lets traverse the tree using tree traversal methods in data structure.
Preorder traversal = A B E G D F
Inorder traversal = E B G A F D
Postorder traversal = E G B F D A

Tree traversal algorithm

  1. First we create a structure to store tree value and left and right pointer
  2. Write Function to inorder traversal
  3. Next write function for preorder traversal
  4. Similarly write function for postorder traversal
  5. Write main function
  6. We create a node of tree dynamically and arrange in tree
  7. Traverse tree using tree traversal method

Tree traversal program in c

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* left;
struct node* right;
};

void inorder(struct node* root){
if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}

void preorder(struct node* root){
if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}

void postorder(struct node* root) {
if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}


struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

struct node* insertLeft(struct node *root, int value) {
root->left = createNode(value);
return root->left;
}


struct node* insertRight(struct node *root, int value){
root->right = createNode(value);
return root->right;
}


int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");
inorder(root);

printf("\nPreorder traversal \n");
preorder(root);

printf("\nPostorder traversal \n");
postorder(root);
}

Output :

Tree traversal  program in cTree traversal program in cTree traversal program in c

Write A Comment