In: Computer Science
Write a C program that demonstrate tree traversal.
A. first is to ask the user to enter data
B. ask how many nodes
THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE.
See for the sample output.
Sample Output:
How many node do you have in your tree : 5
Enter root: 20
if you want to add to the left press 0 otherwise press 1
answer: 0
Enter next node: 10
if you want to add to the left press 0 otherwise press 1
answer: 0
Enter next node: 8
if you want to add to the left press 0 otherwise press 1
answer: 0
Enter next node: 15
if you want to add to the left press 0 otherwise press 1
answer: 1
Enter next node: 21
Display tree
20
/ \
10 21
/
8
/
5
Preorder – 20, 10, 8, 5, 21
       Inorder – 5, 8, 10, 20, 21
       Postorder – 5, 8, 10, 21, 20
COMMENT CODE PLS....
SOLUTION :
I have followed the given sample to build my code. If you have any confusion, let me know in the comment box, i will resolve it.
#include<stdio.h>
#include<stdlib.h>
struct node{
        int data;
        struct node *lchild;
        struct node *rchild;
};
struct node *root=NULL; //root node declared globally so that it can be accessed directly by all the functions
void createnode(int data, int choice) //function to create new node with given data and then add it to the left sub-tree or right sub-tree according to the choice by user
        {
        struct node *ptr = (struct node *)malloc(sizeof(struct node)); //dynamically allocating memory
        ptr->data = data;
        ptr->lchild = ptr->rchild = NULL;
        if(root == NULL){
                root=ptr;
                return;
        }
        //adds the node to the left
        else if(choice == 0){
                struct node *s = root;
                while(s->lchild!=NULL)
                        s=s->lchild;
                s->lchild = ptr;
        }
        //adds the node to the right
        else if(choice == 1){
                struct node *s = root;
                while(s->rchild!=NULL)
                        s=s->rchild;
                s->rchild = ptr;
        }
        
}
//recursive function to print the tree in pre-order
void preorder(struct node *root){
        if(root!=NULL){
                printf("%d\t",root->data);
            preorder(root->lchild);
            preorder(root->rchild);
    }
}
//recursive function to print the tree in inorder
void inorder(struct node *root){
        if(root!=NULL){
            inorder(root->lchild);
            printf("%d\t",root->data);
            inorder(root->rchild);
    }
}
//recursive function to print the tree in postorder
void postorder(struct node *root){
        if(root!=NULL){
            postorder(root->lchild);
            postorder(root->rchild);
            printf("%d\t",root->data);
    }
}
//driver function
int main(){
        int n,data,answer;
        printf("Enter the number of nodes you want in your tree :");
        scanf("%d",&n);
        //loop iterates n times which is specified by user
        while(n!=0){
                if(root == NULL){
                        printf("Enter root :");
                        scanf("%d",&data);
                        createnode(data, -1);
                }
                else{
                        printf("If you want to add to the left press 0 otherwise press 1 :");
                        scanf("%d",&answer);
                        printf("Enter new node :");
                        scanf("%d",&data);
                        createnode(data, answer);
                }
                n--;
        }
        printf("Preorder-  ");
        preorder(root);
        printf("\nInorder-   ");
        inorder(root);
        printf("\nPostorder- ");
        postorder(root);
        free(root);
        
  return 0;
}
OUTPUT :
