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

Traversal tree program in c/c++
Traversal tree program in c/c++
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.
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
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Write a C++ or Java program name that conducts the BFS traversal of a graph and...
Write a C++ or Java program name that conducts the BFS traversal of a graph and displays city names in the range of hop(s) from a starting city Input format: This is a sample input from a user. 4 Monterey LA SF SD 6 Monterey LA LA SD SD Monterey SF Monterey SF LA SF SD Monterey 2 The first line (= 4 in the example) indicates that there are four vertices in the graph. The following four lines describe...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
P1. Construct the tree with post-order traversal string = i b d a h g c...
P1. Construct the tree with post-order traversal string = i b d a h g c f, and in-order traversal string = b i a d f g h c. P2. Construct the tree with pre-order traversal string = 10, 20, 40, 50, 80, 60, 70, 90, and in-order traversal string = 40, 50, 20, 10, 80, 70, 90, 60.
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT