0% found this document useful (0 votes)
3 views

Expt7- DSA d024 (Binary Tree Traversal)

The document outlines a project on Binary Tree traversal algorithms, detailing the structure of binary trees, their traversal methods (preorder, inorder, postorder), and the implementation in C++. It includes code snippets for inserting nodes and performing the traversals, along with observations on their applications in real-world scenarios. The document concludes with answers to questions regarding the utility of different traversal methods and their implications in data structures.

Uploaded by

kavyaraval80
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Expt7- DSA d024 (Binary Tree Traversal)

The document outlines a project on Binary Tree traversal algorithms, detailing the structure of binary trees, their traversal methods (preorder, inorder, postorder), and the implementation in C++. It includes code snippets for inserting nodes and performing the traversals, along with observations on their applications in real-world scenarios. The document concludes with answers to questions regarding the utility of different traversal methods and their implications in data structures.

Uploaded by

kavyaraval80
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Part A

Name:- Kavya Raval


Roll No:- D024
Subject:- Data Structures and Algorithms
Program: B Tech (EXTC)2nd Year, Sem III
Aim:
To study and implement Binary Tree traversal algorithms.
Prerequisite: C++ Programming
Outcome: Representation of Binary trees in memory and implementation of
Binary Tree traversal algorithms
Theory:
Binary tree
Tree with 0, 1 or atmost 2 children per node.
A node that has zero children is called a leaf node or a terminal node.
Every node contains a data element, a left pointer which points to the left child, and a right
pointer which points to the right child.
The root element is pointed by a 'root' pointer. If root = NULL, then it means the tree is empty

Binary tree- Node representation


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

TRAVERSING A BINARY TREE


Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
Unlike linear data structures in which the elements are traversed
sequentially, tree is a nonlinear data structure in which the elements can be
traversed in many different ways.
There are different algorithms for tree traversals:
Inorder traversal
Postorder traversal
Preorder 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;

struct node *getNewNode(int val)


{
struct node *newNode = new node;
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

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:

Observation & Learning:


Write your Observations & Learning after performing task

Answer the Questions:


Q1 What are some real-world applications of Preorder traversal?
Ans The real world applications of Preorder Traversal are as follows:
 Expression Tree Evaluation: Used to generate prefix notation for expressions,
which is helpful in compilers and interpreters.
 File System Navigation: Employed in directory traversal to list files and folders,
preserving their hierarchical structure.
 Serialization of Data Structures: Useful for converting tree structures into a
linear format for storage or transmission, allowing for easy reconstruction.
 Hierarchy Representation: Employed in applications like organizational charts
and decision trees to capture the parent-child relationships clearly.
 Graphics Rendering: Used in rendering scenes in computer graphics, where
objects are drawn based on their hierarchical relationship.
 Game Development: Helps in artificial intelligence to traverse decision trees or
behavior trees for NPC actions.

Q2 In which scenarios would Postorder traversal be useful?


Ans Post Order tRaversal can be utilized in the following:
 Expression Tree Evaluation: It allows evaluation of expressions in postfix
notation, facilitating calculation order in compilers.
 Memory Management: Used in garbage collection to safely delete nodes,
ensuring child nodes are processed before their parents.
 Tree Deletion: Essential for deleting trees, ensuring all child nodes are freed
before the parent node.
 File System Backups: Useful for backing up data by processing children before
their parent directories, maintaining integrity.
 Dynamic Programming on Trees: Helps in algorithms that require processing
child nodes to compute values for parent nodes.

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.

 Searching: Inorder traversal can be used to efficiently retrieve elements in sorted


order, making it ideal for tasks that require sorted data.
 Range Queries: It allows for efficient range queries, where you can retrieve all
values within a specified range by simply traversing the tree in order.
 Data Structure Maintenance: It aids in maintaining the properties of the BST
when adding or removing nodes, ensuring that the overall structure remains
balanced.

Q4 Can it is possible to determine a tree uniquely using only Preorder or Postorder


traversals? Why or why not?
Ans  It is not possible to uniquely determine a tree using only preorder or postorder
traversals because these traversals do not provide enough information about the
structure of the tree. Both traversals can yield multiple trees that have the same
sequence of nodes.
 For instance, with just the preorder traversal, different trees can yield the same
sequence since it only indicates the root and the order of children without
specifying the exact structure.
 Similarly, postorder traversal also lacks sufficient information to ascertain
unique parent-child relationships. To uniquely reconstruct a binary tree, both
preorder or postorder traversal must be combined with either inorder traversal or
additional information about node relationships.

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.

You might also like