0% found this document useful (0 votes)
55 views6 pages

Binary Tree Implementation Lab Manual

This lab manual for the B. Tech program at SVKM’s NMIMS focuses on the implementation of Binary Tree structures and traversal operations using C++. It outlines prerequisites, learning outcomes, and detailed algorithms for creating and traversing binary trees, as well as practical applications of binary trees in computer science. Students are required to implement a menu-driven program for binary tree creation and traversal, and complete various tasks related to binary trees.

Uploaded by

sachinchavan68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views6 pages

Binary Tree Implementation Lab Manual

This lab manual for the B. Tech program at SVKM’s NMIMS focuses on the implementation of Binary Tree structures and traversal operations using C++. It outlines prerequisites, learning outcomes, and detailed algorithms for creating and traversing binary trees, as well as practical applications of binary trees in computer science. Students are required to implement a menu-driven program for binary tree creation and traversal, and complete various tasks related to binary trees.

Uploaded by

sachinchavan68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering /


School of Technology Management & Engineering

B. Tech Lab Manual Academic Year- 2025-26

Year:-Second Subject:- Data Structures and Algorithms Semester: - Third

Experiment: 6

PART A

(PART A: TO BE REFERRED BY STUDENTS)

Aim. Implementation of Binary Tree Structure in memory and traversal operations on Binary
Tree using CPP

Pre-reading: Concept and operations of Binary Tree and tree traversal algorithms from
class/study material.

Pre-requisite :

1. Basic knowledge of Programming with classes and objects in C/CPP.


2. Knowledge of pointers and dynamic memory allocation.
3. Familiarity with data structures and algorithms.

Learning Outcomes: The learner would be able to

1. Ability to implement a Binary Tree in C++.


2. Proficiency in applying traversal operation such as Inorder, Preorder and Post Order
Traversal on Binary Tree.
3. Understanding of the practical applications of Binary Tree in computer science.

Theory:

1. Node Structure

A node is the fundamental building block of a linked list. In C++, it can be defined
using a struct or a class. Each node contains:

 Data: The value stored in the node.


 Left Pointer: A pointer to the Left child in the binary tree.
 Right Pointer: A pointer to the Right child in the binary tree.

2. Dynamic Memory Allocation


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech Lab Manual Academic Year- 2025-26

Year:-Second Subject:- Data Structures and Algorithms Semester: - Third

Binary Tree use dynamic memory allocation to create nodes at runtime. This allows
the tree to grow and shrink as needed. In C++, the new keyword is used to allocate
memory for a new node.

3. Creating Binary tree

Creating a binary tree from given data using a queue involves a systematic approach.
The steps for the same are given below:

1. Initialize the Root: Start by creating the root node of the binary tree with the first
element of the data.
2. Initialize the Queue: Create a queue and enqueue the root node. This queue will
help in level-order insertion.
3. Iterate Through Data:

For each subsequent element in the data, follow these steps:

 Dequeue the front node from the queue. This node is the current parent
node.
 Create a new node with the current data element.
 If the current parent node does not have a left child, set this new node as
the left child.
 If the current parent node already has a left child but no right child, set this
new node as the right child.
 Enqueue the new node to the queue.

4. Repeat Until Data is Exhausted: Continue the process until all elements in the
data have been inserted into the binary tree.

4. Binary Tree Traversal

Traversal involves visiting each node in the binary tree to perform operations such as
displaying the data.

Algorithms:

1. Algorithm for Creating Binary Tree from the given data

Step 1 : INITIALIZE:

o CREATE ROOT NODE WITH DATA[0].


o INITIALIZE QUEUE AND ENQUEUE ROOT.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech Lab Manual Academic Year- 2025-26

Year:-Second Subject:- Data Structures and Algorithms Semester: - Third

Step 2 : FOR EACH ELEMENT DATA[I] FROM DATA[1] TO DATA[N-1]:

o DEQUEUE CURRENT NODE FROM QUEUE.


o CREATE NEW_NODE WITH DATA[I].
o IF [Link] IS NONE:
 SET [Link] TO NEW_NODE.
o ELSE IF [Link] IS NONE:
 SET [Link] TO NEW_NODE.
o ENQUEUE NEW_NODE TO QUEUE.

Step 3 : END.

2. Algorithms to Perform in-order traversal for a Binary tree


Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: INORDER(TREE-> LEFT)
Step 3: Write TREE DATA
Step 4: INORDER(TREE-> RIGHT) [END OF LOOP]
Step 5: END

3. Algorithms to Perform Pre-order traversal for a Binary tree


Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE DATA
Step 3: PREORDER(TREE-> LEFT)
Step 4: PREORDER(TREE-> RIGHT) [END OF LOOP]
Step 5: END

4. Algorithms to Perform Post-order traversal for a Binary tree

Step 1: Repeat Steps 2 to 4 while TREE != NULL


Step 2: POSTORDER(TREE-> LEFT)
Step 3: POSTORDER(TREE -> RIGHT)
Step 4: Write TREE DATA [END OF LOOP]
Step 5: END

Applications of Binary Trees :

The application of binary trees are given below :

1. Binary Search Trees (BSTs): Used for efficient searching, insertion, and deletion
operations. They maintain a sorted order, which allows for quick lookup of elements.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech Lab Manual Academic Year- 2025-26

Year:-Second Subject:- Data Structures and Algorithms Semester: - Third

2. Expression Trees: Used to represent arithmetic expressions. In these trees, internal


nodes represent operators, and leaf nodes represent operands. They are useful for
evaluating expressions and performing operations in compilers.
3. Huffman Coding Trees: Used in data compression algorithms. Huffman trees assign
variable-length codes to input characters, with shorter codes assigned to more
frequent characters, optimizing the overall data size.
4. Binary Heaps: A type of binary tree used to implement priority queues. They are
useful in algorithms like Dijkstra’s shortest path and heap sort.
5. Syntax Trees: Used in compilers to represent the structure of source code. They help
in parsing and analyzing the syntax of programming languages.
6. AVL Trees and Red-Black Trees: These are self-balancing binary search trees. They
ensure that the tree remains balanced, providing efficient operations even in the
worst-case scenarios.
7. Morse Code Trees: Used to represent Morse code, where each node represents a dot
or dash, and the leaves represent characters.
8. Decision Trees: In simpler forms, binary trees are used in decision-making processes,
where each node represents a decision based on a condition, and the leaves represent
outcomes.

Tasks: Implement a menu driven program to various algorithms, given above, for
Binary Tree Creation and Traversing the binary tree.

Consider the given data items for the creating and traversing the binary tree : 12, 43,
23, 62, 44, 56, 87, 18, 20, 21.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech Lab Manual Academic Year- 2025-26

Year:-Second Subject:- Data Structures and Algorithms Semester: - Third

PART B

(PART B: TO BE COMPLETED BY STUDENTS)

Students must implement the code in Laboratory. The soft copy must be uploaded on the
portal. The filename should be DSA_batch_rollno_experimentno Example:
DSA_A1_A001_P1

Roll No.: Name:

Prog/Yr/Sem: Batch:

Date of Experiment: Date of Submission:

For each task to be performed, write

 Task to be performed :
 Insert the Sample Input/Output here:
 Insert screen shot of the C/CPP Code here. It should have your name/roll
number mentioned in screen shot.
 Explanation/justification, if any.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech Lab Manual Academic Year- 2025-26

Year:-Second Subject:- Data Structures and Algorithms Semester: - Third

Questions of Curiosity:
1. Given a binary tree, write a function to find the maximum depth of the binary tree.

2. Give a binary tree and find in-order, preorder, and post-order traversal for the same.
1. Set 1: [40, 27, 39, 86, 41, 83, 76, 85]
2. Set 2: [4, 40, 72, 63, 61, 3, 67, 79]
3. Set 3: [3, 89, 5, 22, 68, 48, 23, 90, 85]
4. Set 4: [14, 2, 69, 28, 58, 49, 36, 72, 73, 24]
5. Set 5: [11, 74, 63, 50, 91, 35, 30, 55]
6. Set 6: [51, 27, 97, 68, 41, 10, 25, 95, 75, 5]

3. Construct the binary tree from the given inorder and preorder traversals.

 Inorder Traversal: [4, 2, 5, 1, 6, 3, 7]


 Preorder Traversal: [1, 2, 4, 5, 3, 6, 7]

4. Construct the binary tree from the given inorder and postorder traversals.

 Inorder Traversal: [9, 3, 15, 20, 7]


 Postorder Traversal: [9, 15, 7, 20, 3]

5. Construct the binary tree from the given preorder and postorder traversals

 Preorder Traversal: [1, 2, 4, 5, 3, 6]


 Postorder Traversal: [4, 5, 2, 6, 3, 1]

You might also like