Expt7- DSA d024 (Binary Tree Traversal)
Expt7- DSA d024 (Binary Tree Traversal)
Preorder traversal:
struct node
{ int data;
struct node *left;
struct node *right;
};
struct Node *root = NULL;
void preorder(struct node *root)
{ if (root != NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
// Inorder traversal
void inorder(struct node *root)
{ if (root != NULL)
{
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
// Postorder traversal
void postorder(struct node *root)
{ if (root != NULL)
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
//Insert
struct node *insert(struct node *root, int val)
{
if(root == NULL)
return getNewNode(val);
if(root->key < val)
root->right = insert(root->right,val);
else if(root->key > val)
root->left = insert(root->left,val);
return root;
TASK 1:
Write a C/C++ program to implement binary tree and perfrom the following
operations:
i. Insert
ii. Inorder traversal
iii. Preorder traversal
iv. Postorder traversal
Procedure:
1. Open CodeBlock editor or visual studio editor and write the code in C++.
2. Complile and run the code
Instructions:
1. Copy code & paste in code section and output of Part B.
Part B
Code:
#include <iostream>
using namespace std;
struct Node{
int data;
Node *left;
Node *right;
};
Node* createNode(int value){
Node* newNode=new Node();
newNode->data=value;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}
Node* root=NULL;
void preorder(Node *root){
if(root!=NULL){
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
void inorder(Node *root){
if(root != NULL){
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
void postorder(Node *root){
if(root!=NULL){
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
Node* insert(Node *root,int value){
if(root==NULL){
return createNode(value);
}
if(value<root->data){
root->left=insert(root->left,value);
}else if(value>root->data){
root->right=insert(root->right,value);
}
return root;
}
int main(){
root=createNode(1);
root->left=createNode(2);
root->right=createNode(3);
root->left->left=createNode(4);
root->left->right=createNode(5);
root->right->left=createNode(6);
root->right->right=createNode(7);
cout<<"Preorder Traversal:"<<endl;
preorder(root);
cout<<endl;
cout<<"Inorder Traversal:"<<endl;
inorder(root);
cout<<endl;
cout<<"Post Order Traversal:"<<endl;
postorder(root);
cout<<endl;
return 0;
}
Output:
Q3 Why is Inorder traversal particularly useful for Binary Search Trees (BSTs)?
Ans In Order Traversal is useful for BSTs in the following manner:
Inorder traversal is particularly useful for Binary Search Trees (BSTs) because it
produces a sorted sequence of the elements stored in the tree. Here's why this is
significant:
Sorted Output: In a BST, the left subtree contains values less than the root, and
the right subtree contains values greater than the root. Inorder traversal visits the
left subtree, the root, and then the right subtree, ensuring that elements are
accessed in ascending order.
Q5 Which tree traversal results in a sorted order of elements for a Binary Search Tree
(BST)?
Ans Inorder traversal results in a sorted order of elements for a Binary Search Tree
(BST). When performing an inorder traversal, the left subtree is visited first,
followed by the root node, and then the right subtree.
This order reflects the properties of a BST, where all nodes in the left subtree are
less than the root, and all nodes in the right subtree are greater, thus producing a
sorted sequence of the tree's elements.
Conclusion:
We implemented stack its operations usng linked list and using C++ program.