C program for inorder traversal in binary tree.

Posted by Mangesh on August 11, 2018

Program in c for inorder traversals of binary tree.

Description:

In inorder traversal a binary trees, we traversal root node in between of the left side and right side children. The sequence in which we traversal for inorder is the left child then the root(perent node) and at last the right child.

Program :

#include<stdio.h>
#include<conio.h>
typedef struct node
{
  int data;
  struct node *left;
  struct node *right;
} node;

node *create()
{
    node *p;
    int x;
    printf("Enter data(-1 for no node):");
    scanf("%d",&x);

    if(x==-1)
	return NULL;

    p=(node*)malloc(sizeof(node));
    p->data=x;
    printf("Enter left child of %d:\n",x);
    p->left=create();
    printf("Enter right child of %d:\n",x);
    p->right=create();
    return p;
}

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


void main()
{
  node *root;
  clrscr();
  root=create();

  printf("\nThe inorder traversal of tree is: ");
  inorder(root);

  getch();
}


Output:

inorder traversal on binary tree executed and run in turbo c++ 3.2

Written with from Mangesh.

Related Post
1 C program to create a binary tree.
2 C program for inorder traversal in binary tree.
3 C program for preorder traversal in binary tree.
4 C program for postorder traversal in binary tree.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.