Question

In: Computer Science

Write a C program that demonstrate tree traversal. A. first is to ask the user to...

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

Solutions

Expert Solution

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 :


Related Solutions

Traversal tree program in c/c++
Traversal tree program in c/c++
Traversal tree program
Traversal tree program
C program that demonstrate tree traversal. see for the sample output. Sample Output: How many node...
C program that demonstrate tree traversal. 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...
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below....
Modifying the tree traversal C program shown below to do reversed tree traversal as defined below. Reversed PreOrder: Root, Right, Left. Reversed InOrder:     Right, Root, Left. Reversed PostOrder: Right, Left, Root. The tree is: Root = 1, Left(1) = -2, Right(1) = -3; Left(-2) = 4, Right(-2) = 5; Left(-3) = 6, Right(-3)= 7; Left(5) = -8, Right(5)= -9; Left(7) = 10, Right(7) = 11; Left(11) = -12, Right(11) = -13; Left(-13) = 14. Your program should output the printed...
Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree....
Use C programming to Implement the breadth-first tree traversal. The tasks are: a) Build the tree. b) Perform breadth first traversal on the tree that you have built at step a). Note: "Please comment the program"
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT