Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

EXPERIMENT-6

DEVELOP AND PRACTICE C++APPLICATION USING TREE


AIM/OBJECTIVE: To write and execute C++ programs to demonstrate tree transversal in
binary. Simulate the use using c++ compiler

SOFTWARE USED: Online C++ Compiler


Pre lab Questions
1. List the applications of binary trees.

2. Trees are data structure

3. What are the advantages of tree data structure?

4. Process of inserting an element in stack is called


1. Write a c++ program to demonstrate tree transversal in binary .

1
/ \
12 9
/ \
5 6
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL; }};
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
cout << node->data << "->";
preorderTraversal(node->left);
preorderTraversal(node->right);}
void postorderTraversal(struct Node* node) {
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";}
void inorderTraversal(struct Node* node) {
if (node == NULL)
return;
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);}
int main() {
struct Node* root = new Node(1);
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6);
cout << "Inorder traversal ";
inorderTraversal(root);
cout << "\nPreorder traversal ";
preorderTraversal(root);
cout << "\nPostorder traversal ";
postorderTraversal(root);}
Post lab questions
1. List four types of trees.

2. For the following tree give the preorder, inorder and postorder tree traversal

RESULT AND CONCLUSION: C++ program on tree transversal in binary was completed
and desired output was obtained

You might also like