Question

In: Computer Science

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
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 PLEASE**

Solutions

Expert Solution

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

struct node {
int data;
struct node *left;
struct node *right;
};

typedef struct node tree;

tree *root = NULL;

tree* newnode(int data) {

tree *new = NULL;
new = (tree *)malloc(sizeof(tree));

new->data = data;
new->left = NULL;
new->right = NULL;

return(new);
}

void preorder(tree *root) {

if(root == NULL) {return;}

printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}

void inorder(tree *root) {

if(root == NULL) {return;}

inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}

void postorder(tree *root) {

if(root == NULL) {return;}

postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}

int main() {
root = newnode(20);
root->left = newnode(10);
root->right = newnode(21);
root->left->left = newnode(8);
root->left->left->left = newnode(15);
printf("Preorder traversal of binary tree is : ");
preorder(root);
printf("\n");
printf("Inorder traversal of binary tree is : ");
inorder(root);
printf("\n");
printf("Postorder traversal of binary tree is : ");
postorder(root);

getchar();
return 0;
}


Related Solutions

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...
Traversal tree program in c/c++
Traversal tree program in c/c++
Traversal tree program
Traversal tree program
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...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output post-order traversal. Input Two lines. Pre-order traversal and in-order traversal. Output Post-order traversal. Sample Input GDAFEMHZ ADEFGHMZ Sample Output AEFDHZMG
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in...
Please ready a program should output Inorder Tree Traversal without recursion and without stack!, output in ALGOL W programming only. Thumbs down for wrong answers Make a program to perform Heap Sort, must run in Alice programming only. Only correct answers needed should be in given language
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
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"
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT