0% found this document useful (0 votes)
25 views22 pages

DS Question Bank - With Answers Unit-5

This document is a question bank for the Data Structures course at St. Peter’s Engineering College for the academic year 2023-24. It includes various questions related to binary trees, AVL trees, graph traversal methods, and operations on data structures, categorized by Bloom's taxonomy levels. The questions cover definitions, comparisons, and practical applications of data structures, along with diagrams and examples.

Uploaded by

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

DS Question Bank - With Answers Unit-5

This document is a question bank for the Data Structures course at St. Peter’s Engineering College for the academic year 2023-24. It includes various questions related to binary trees, AVL trees, graph traversal methods, and operations on data structures, categorized by Bloom's taxonomy levels. The questions cover definitions, comparisons, and practical applications of data structures, along with diagrams and examples.

Uploaded by

meetsanthosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

SR 22

CSE, CSM,
St. Peter’s Engineering College (Autonomous) Dept. :
CSD, CSC
Dullapally (P), Medchal, Hyderabad – 500100. Academic Year
QUESTION BANK 2023-24
Subject Code : AS22-05ES07 Subject : Data Structures
Class/Section : B.Tech. Year : I Semester : II

BLOOMS LEVEL
Remember L1 Understand L2 Apply L3
Analyze L4 Evaluate L5 Create L6

*****

Q. No Question (s) Marks BL CO


UNIT - I
1 a) What is Binary Tree? Draw the diagram.? 1M L1 C123.5
In a normal tree, every node can have any number of children. A binary tree is a special type of tree
data structure in which every node can have a maximum of 2 children. One is known as a left child
and the other is known as right child. In a binary tree, every node can have either 0 children or 1 child
or 2 children but not more than 2 children.

b) List the various operations in binary search tree. 1M L2 C123.5

Binary search tree is a binary tree in which all the nodes in the left subtree of any node contains smaller

values and all the nodes in the right subtree of any node contains larger values. Every binary search

tree is a binary tree but every binary tree need not to be binary search tree.
1.Search
2.Insertion
3.Deletion
c) What are the tree traversal methods. 1M L1 C123.5
SR 22

1. In - Order Traversal

2. Pre - Order Traversal

3. Post - Order Traversal

d) Write the balance factor formula for AVL tree 1M L1 C123.5


Balance factor = height of Left Subtree – height of Right Subtree
OR
Balance factor = height of Right Subtree – height of Right Subtree

e) Define Graph traversal. 1M L1 C123.5


Graph traversal is a technique used for a searching vertex in a graph. There are two graph traversal
techniques and they are as follows:
1.BFS stands for Breadth First Search
2.DFS stands for Depth First Search.

2 a) Differentiate between BFS and DFS 3M L2 C123.5

Parameters BFS DFS

Stands for BFS stands for Breadth First Search. DFS stands for Depth First Search.

BFS (Breadth First Search) uses


DFS (Depth First Search) uses Stack data
Queue data structure for finding the
structure.
Data Structure shortest path.

DFS is also a traversal approach in which


BFS is a traversal approach in which
the traverse begins at the root node and
we first walk through all nodes on the
proceeds through the nodes as far as
same level before moving on to the
possible until we reach the node with no
next level.
Definition unvisited nearby nodes.

Conceptual
BFS builds the tree level by level. DFS builds the tree sub-tree by sub-tree.
Difference

It works on the concept of FIFO It works on the concept of LIFO (Last In


Approach used (First In First Out). First Out).

BFS is more suitable for searching DFS is more suitable when there are
Suitable for vertices closer to the given source. solutions away from source.
SR 22

Applications bipartite graphs, shortest paths, etc. acyclic graphs

3M L2 C123.5
b) Show the Left Child - Right Sibling Representation of a given Tree.

c) List the varoius types of a tree. 3M L1 C123.5

1. Strictly Binary Tree: A binary tree in which every node has either two or zero number of children is
called Strictly Binary Tree.

2. Complete Binary Tree: A binary tree in which every internal node has exactly two children and all leaf
nodes are at same level is called Complete Binary Tree.

3. Extended Binary Tree: The full binary tree obtained by adding dummy nodes to a binary tree is called
as Extended Binary Tree.
SR 22

d) Write a short note on AVL tree. 3M L1 C123.5


AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary search tree but
it is a balanced tree. A binary tree is said to be balanced if, the difference between the heights of left and
right subtrees of every node in the tree is either -1, 0 or +1. In other words, a binary tree is said to be
balanced if the height of left and right children of every node differ by either -1, 0 or +1.

Balance factor = height of Left Subtree – height of Right Subtree

e) Enumerate the adjacent matrix representation of graph data 3M L3 C123.5


structure.
In this representation, the graph is represented using a matrix of size total number of vertices by a total
number of vertices. That means a graph with 4 vertices is represented using a matrix of size 4X4. In this
matrix, both rows and columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents
that there is an edge from row vertex to column vertex and 0 represents that there is no edge.
For example, consider the following undirected graph representation
SR 22

3 a) Explain AVL tree LL & RR rotations 5M L2 C123.5


In AVL tree, after performing operations like insertion and deletion we need to check the balance
factor of every node in the tree. Whenever the tree becomes imbalanced due to any operation we
use rotation operations to make the tree balanced.
In LL Rotation, every node moves one position to left from the current position. To understand LL
Rotation, let us consider the following insertion operation in AVL Tree.

In RR Rotation, every node moves one position to right from the current position. To understand RR
Rotation, let us consider the following insertion operation in AVL Tree.

b) Construct the Binary Search Tree by inserting the following 5M


L5 C123.5
sequence of numbers 10,12,5,4,20,8,7,15 and 13
SR 22

c)Describe the terms used in tree data structure. 5M


L1 C123.5
SR 22

Terminology Description Diagram


Root node is a node from which the
Root entire tree originates. It does not have a Node A
parent
An immediate predecessor of any node
Parent Node B is parent of E & F
is its parent node.
All immediate successors of a node are
its children. The relationship between
Child Node F & E are children of B
the parent and child is considered as the
parent-child relationship.

Node which does not have any child is a


leaf. Usually the boundary nodes of a
Leaf E, F, J, K, H, I are the leaf nodes.
tree or last nodes of the tree are the leaf
or collectively called leaves of the tree.

Siblings in real life means people with


the same parents, similarly in the case of
Siblings H&I are siblings
trees, nodes with common parents are
considered to be siblings.
A, C, G, K form a height. Height
of A is no. of edges
Height of a node represents the number
Height of between A and K,, which is 3.
of edges on the longest path between
Node Similarly the height of G is 1 as it
that node and a leaf.
has just one edge until the next
leaf node.
Level of a node represents the
generation of a node. If the root node is
Levels of Level of H, I & J is 3. Level of D, E,
at level 0, then its next child node is at
node F & G is 2
level 1, its grandchild is at level 2, and so
on
Degree of Degree of a node implies the number of
Degree of D is 2 and of C is 3
Node child nodes a node has.
A node that has at least one child is All the nodes except E, F, J, K, H,
Internal Node
known as an internal node. I are internal.
An ancestor or ancestors to a node are
all the predecessor nodes from root until
Ancestor A, C & G are ancestor
that node. I.e. any parent or grandparent
node to K and J nodes
and so on of a specific node are its
ancestors.
Immediate successor of a node is its
Descendant K is descendent of G
descendent.
Descendants of a node represent
subtree. Tree being a recursive data Nodes B, E, F represent one
Sub tree
structure can contain many subtrees subtree.
inside of it.
SR 22

d) Analyze the steps invloved in the insertion operation of 5M


L4 C123.5
Binary Search Tree.

In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary

search tree, new node is always inserted as a leaf node. The insertion operation is performed as

follows...

 Step 1 - Create a newNode with given value and set its left and right to NULL.

 Step 2 - Check whether tree is Empty.

 Step 3 - If the tree is Empty, then set root to newNode.

 Step 4 - If the tree is Not Empty, then check whether the value of newNode

is smaller or larger than the node (here it is root node).

 Step 5 - If newNode is smaller than or equal to the node then move to its left child. If

newNode is larger than the node then move to its right child.

 Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL).
 Step 7 - After reaching the leaf node, insert the newNode as left child if the newNode
is smaller or equal to that leaf node or else insert it as right child.
SR 22
SR 22

e) Explain the concept of incidence matrix and adjacency 5M


L2 C123.5
list of graph representations data structure.
Incident Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a
total number of edges. That means graph with 4 vertices and 6 edges is represented using a matrix
of size 4X6. In this matrix, rows represent vertices and columns represents edges. This matrix is
filled with 0 or 1 or -1. Here, 0 represents that the row edge is not connected to column vertex, 1
represents that the row edge is connected as the outgoing edge to column vertex and -1 represents
that the row edge is connected as the incoming edge to column vertex.
For example, consider the following directed graph representation

Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices. For example,
consider the following directed graph representation implemented using linked list
SR 22

a) Explain the operations on AVL Tree.


4 10M L2 C123.1
The following operations are performed on AVL tree...

1. Search
2. Insertion
3. Deletion

1. Search Operation in AVL Tree

In an AVL tree, the search operation is performed with O(log n) time complexity. The search operation
in the AVL tree is similar to the search operation in a Binary search tree. We use the following steps
to search an element in AVL tree...

 Step 1 - Read the search element from the user.


 Step 2 - Compare the search element with the value of root node in the tree.
 Step 3 - If both are matched, then display "Given node is found!!!" and terminate the function
 Step 4 - If both are not matched, then check whether search element is smaller or larger than
that node value.
 Step 5 - If search element is smaller, then continue the search process in left subtree.
 Step 6 - If search element is larger, then continue the search process in right subtree.
 Step 7 - Repeat the same until we find the exact element or until the search element is compared
with the leaf node.
 Step 8 - If we reach to the node having the value equal to the search value, then display
"Element is found" and terminate the function.
 Step 9 - If we reach to the leaf node and if it is also not matched with the search element, then
display "Element is not found" and terminate the function.

2. Insertion Operation in AVL Tree

In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a
new node is always inserted as a leaf node. The insertion operation is performed as follows...

 Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
 Step 2 - After insertion, check the Balance Factor of every node.
 Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
SR 22
 Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to be
imbalanced. In this case, perform suitable Rotation to make it balanced and go for next
operation.

Example: Construct an AVL Tree by inserting numbers from 1 to 8.


SR 22
SR 22

3. Deletion Operation in AVL Tree


The deletion operation in AVL Tree is similar to deletion operation in BST. But after every deletion
operation, we need to check with the Balance Factor condition. If the tree is balanced after deletion go
for next operation otherwise perform suitable rotation to make the tree Balanced.
SR 22

b) Demonstrate the DFS graph traversal algorthim.


10M L3 C123.1

DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without
loops. We use Stack data structure with maximum size of total number of vertices in the graph to
implement DFS traversal.

We use the following steps to implement DFS traversal...

 Step 1 - Define a Stack of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the
Stack.
 Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack
and push it on to the stack.
 Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the
top of the stack.
 Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from
the stack.
 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges
from the graph

Back tracking is coming back to the vertex from which we reached the current vertex.
SR 22
SR 22
SR 22
SR 22
SR 22

c) Discuss traversal methods in a Binary Tree.


10M L2 C123.1

Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree Traversal.
There are three types of binary tree traversals.
4. In - Order Traversal
5. Pre - Order Traversal
6. Post - Order Traversal

Consider the following binary tree...

1. In - Order Traversal ( leftChild - root - rightChild )

In In-Order traversal, the root node is visited between the left child and right child. In this traversal,
the left child node is visited first, then the root node is visited and later we go for visiting the right
child node. This in-order traversal is applicable for every root node of all subtrees in the tree. This is
performed recursively for all nodes in the tree.

In the above example of a binary tree, first we try to visit left child of root node 'A', but A's left child
'B' is a root node for left subtree. so we try to visit its (B's) left child 'D' and again D is a root for subtree
with nodes I and J. So we try to visit its left child 'I' and it is the leftmost child. So first we visit 'I' then
go for its root node 'D' and later we visit D's right child 'J'. With this we have completed the left part
of node B. Then visit 'B' and next B's right child 'F' is visited. With this we have completed left part
of node A. Then visit root node 'A'. With this we have completed left and root parts of node A. Then
we go for the right part of the node A. In right of A again there is a subtree with root C. So go for left
child of C and again it is a subtree with root G. But G does not have left part so we visit 'G' and then
visit G's right child K. With this we have completed the left part of node C. Then visit root node 'C' and
SR 22
next visit C's right child 'H' which is the rightmost child in the tree. So we stop the process.

That means here we have visited in the order of I - D - J - B - F - A - G - K - C - H using In-Order


Traversal.

In-Order Traversal for above example of binary tree is


I-D-J-B-F-A-G-K-C-H

2. Pre - Order Traversal ( root - leftChild - rightChild )

In Pre-Order traversal, the root node is visited before the left child and right child nodes. In this
traversal, the root node is visited first, then its left child and later its right child. This pre-order traversal
is applicable for every root node of all subtrees in the tree.
In the above example of binary tree, first we visit root node 'A' then visit its left child 'B' which is a
root for D and F. So we visit B's left child 'D' and again D is a root for I and J. So we visit D's left
child 'I' which is the leftmost child. So next we go for visiting D's right child 'J'. With this we have
completed root, left and right parts of node D and root, left parts of node B. Next visit B's right child 'F'.
With this we have completed root and left parts of node A. So we go for A's right child 'C' which is a
root node for G and H. After visiting C, we go for its left child 'G' which is a root for node K. So next
we visit left of G, but it does not have left child so we go for G's right child 'K'. With this, we have
completed node C's root and left parts. Next visit C's right child 'H' which is the rightmost child in the
tree. So we stop the process.

That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal.

Pre-Order Traversal for above example binary tree is


A-B-D-I-J-F-C-G-K-H

3. Post - Order Traversal ( leftChild - rightChild - root )

In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left
child node is visited first, then its right child and then its root node. This is recursively performed until
the right most node is visited.

Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-Order Traversal.

Post-Order Traversal for above example binary tree is


I-J-D-F-B-K-G-H-C-A
SR 22

You might also like