C Program to construct binary tree from inorder and postorder

By | 13.04.2017

Construct tree from inorder and postorder


Write a C Program to construct binary tree from inorder and postorder. Here’s simple Program to construct binary tree from inorder and postorder 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 postorder 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 postorder */

#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;
}*post=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 *postptr, int num);
void inorder(struct treenode *ptr);
void preorder(struct treenode *ptr);
void postorder(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 postorder:\n");
        post = create_list(post,n);

        root = construct(in,post,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;

}/*End of main()*/

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

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

        ptr=postptr;
        for(i=1;i<num;i++)
                ptr = ptr->next;

        /*Now ptr points to last node of postorder which is root*/

        temp=(struct treenode *)malloc(sizeof(struct treenode));
        temp->info=ptr->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!=ptr->info; i++ )
                q = q->next;

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

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

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

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

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( )*/

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()*/

OUTPUT : :


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

Enter the number of nodes  :  7

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 the element to be inserted : 7

Enter postorder:
Enter the element to be inserted : 7
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  7

Postorder : 7  6  5  4  3  2  1

Preorder : 1  2  3  4  5  6  7


Tree is :

                        7
                    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….

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments