C Program to construct binary tree from inorder and preorder

By | 13.04.2017

Construct binary tree from inorder and preorder


Write a C Program to construct binary tree from inorder and preorder. Here’s simple Program to construct binary tree from inorder and preorder in C Programming Language.


What is Tree ?


In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.

A tree data structure can be defined as follows…

  • Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.

A tree data structure can also be defined as follows…

  • Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.

Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.


Below is the source code for C Program to construct binary tree from inorder and preorder which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to construct binary tree from inorder and preorder*/


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

struct treenode
{
        int info;
        struct treenode *lchild;
        struct treenode *rchild;
}*root=NULL;

struct listnode
{
        struct listnode *prev;
        int info;
        struct listnode *next;
}*pre=NULL, *in=NULL;

void display(struct treenode *ptr,int level);
struct listnode *addtoempty(struct listnode *start,int data);
struct listnode *addatend(struct listnode *start,int data);
struct listnode *create_list(struct listnode *start, int n);
struct treenode *construct(struct listnode *inptr,struct listnode *preptr, int num);
void inorder(struct treenode *ptr);
void postorder(struct treenode *ptr);
void preorder(struct treenode *ptr);

int main( )
{
        int n;

        printf("Enter the number of nodes  :  ");
        scanf("%d",&n);

        printf("\nEnter inorder\n");
        in = create_list(in, n);

        printf("\nEnter preorder\n");
        pre=create_list(pre, n);

        root = construct(in,pre,n);

        printf("\nInorder : ");  inorder(root);
        printf("\n\nPostorder : ");  postorder(root);
        printf("\n\nPreorder : "); preorder(root);
        printf("\n\n\nTree is : \n");
        display(root,0);
        printf("\n");

        return 0;
}

struct treenode *construct(struct listnode *inptr,struct listnode *preptr, int num )
{
        struct treenode *temp;
        struct listnode *q;

        int i,j;
        if(num==0)
                return NULL;

        temp=(struct treenode *)malloc(sizeof(struct treenode));
        temp->info=preptr->info;
        temp->lchild = NULL;
        temp->rchild = NULL;

        if(num==1)/*if only one node in tree */
                return temp;

        q = inptr;
        for(i=0; q->info != preptr->info; i++)
                q = q->next;

        /*Now q points to root node in inorder list and
         and number of nodes in its left subtree is i*/

        /*For left subtree*/
        temp->lchild = construct(inptr, preptr->next, i);

        /*For right subtree*/
        for(j=1;j<=i+1;j++)
                preptr=preptr->next;
        temp->rchild = construct(q->next, preptr, num-i-1);

        return temp;
}/*End of construct()*/

void display(struct treenode *ptr,int level)
{
        int i;
        if ( ptr!=NULL )
        {
                display(ptr->rchild, level+1);
                printf("\n");
                for (i = 0; i < level; i++)
                        printf("    ");
                printf("%d", ptr->info);
                display(ptr->lchild, level+1);
        }
}/*End of display()*/

struct listnode *create_list(struct listnode *start, int n)
{
        int i, data;
        start=NULL;
        for(i=1;i<=n;i++)
        {
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                if(start==NULL)
                        start=addtoempty(start,data);
                else
                        start=addatend(start,data);
        }
        return start;
}/*End of create_list()*/

struct listnode *addtoempty(struct listnode *start,int data)
{
        struct listnode *tmp;
        tmp=(struct listnode*)malloc(sizeof(struct listnode));
        tmp->info=data;
        tmp->prev=NULL;
        tmp->next=NULL;
        start=tmp;
        return start;
}/*End of addtoempty( )*/
struct listnode *addatend(struct listnode *start,int data)
{
        struct listnode *tmp,*p;
        tmp=(struct listnode*)malloc(sizeof(struct listnode));
        tmp->info=data;
        p=start;
        while(p->next!=NULL)
                p=p->next;
        p->next=tmp;
        tmp->next=NULL;
        tmp->prev=p;
        return start;
}/*End of addatend( )*/

void inorder(struct treenode *ptr)
{
        if(root==NULL)
        {
                printf("Tree is empty");
                return;
        }
        if(ptr!=NULL)
        {
                inorder(ptr->lchild);
                printf("%d  ",ptr->info);
                inorder(ptr->rchild);
        }
}/*End of inorder( )*/
void postorder(struct treenode *ptr)
{
        if(root==NULL)
        {
                printf("Tree is empty");
                return;
        }
        if(ptr!=NULL)
        {
                postorder(ptr->lchild);
                postorder(ptr->rchild);
                printf("%d  ",ptr->info);
        }
}/*End of postorder( )*/

void preorder(struct treenode *ptr)
{
        if(root==NULL)
        {
                printf("Tree is empty");
                return;
        }
        if(ptr!=NULL)
        {
                printf("%d  ",ptr->info);
                preorder(ptr->lchild);
                preorder(ptr->rchild);
        }
}/*End of preorder( )*/

OUTPUT : :


/* C Program to construct binary tree from inorder and preorder*/

Enter inorder :
Enter the element to be inserted : 1
Enter the element to be inserted : 2
Enter the element to be inserted : 3
Enter the element to be inserted : 4
Enter the element to be inserted : 5
Enter the element to be inserted : 6

Enter preorder :
Enter the element to be inserted : 6
Enter the element to be inserted : 5
Enter the element to be inserted : 4
Enter the element to be inserted : 3
Enter the element to be inserted : 2
Enter the element to be inserted : 1

Inorder : 1  2  3  4  5  6

Postorder : 1  2  3  4  5  6

Preorder : 6  5  4  3  2  1

Tree is :

6
    5
        4
            3
                2
                    1


Process returned 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.3 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments