Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Course Code: BTAIC304

JAGRAN LAKECITY UNIVERSITY, BHOPAL


B.TECH THIRD SEMESTER 2020-21
END Semester Examination
Course Code: BTAIC304, Course Name: DATA STRUCTURES
Maximum Marks: 100
Answer Sheet
******TO BE FILLED BY STUDENT******
JLU ID JLU05054
Roll Number 2019BTCSAI010
Name of Student Priyanshu Sharma
Mode of Exam Assessment through assignment
Regular/ Backlog REGULAR
Date of Examination (Issue Date) 11-12-2020
Submission Date & Time 11-12-2020( till 23:59 pm)
Length (word count) o Part A 960
[For Word File/ PDF File based o Part B 847
Submission of Answer sheet] Total word count = 1807
Length (No. of Pages) o Part A
[For Handwritten & Scanned PDF o Part B
File / Image File based Submission of
Answer sheet] Total Number of Pages =
******TO BE FILLED BY EXAMINER******
Marks distribution for every section as per mode of examination
Part A:
Marks distribution for every section Question No. Marks
of the assignment Q. 1
Q. 2
Q. 3
Q.4
Q.5
Part B:
Question No. Marks
Q. 1
Q. 2
Q. 3
Q.4
Q.5
Q.6
Total marks (A+B)
Marks distribution for every section Part A: Project work =
of the Project/case studies Part B: Viva Voice =
Total marks (A+B)
**If any other Please specify here:
Name of Examiner: Mr. Rajit Nair Date:
Designation: Assistant Professor

Important Instructions
1. All questions are compulsory.
2. Answer all the questions in the same sequence of Question Paper.
3. Assignment’s content should be free of plagiarism and it should be submitted within given deadline.
4. The fonts for writing your answers should be either Calibri (12 pts) or Times New Roman (12 pts).

SECTION – A
Short answer type questions (5 x 8 = 40 marks)
1. List out the areas in which data structures are applied extensively?
Ans-

Data structures refers to the way data is organized and manipulated. It helps to find ways to
make data access more efficient. When dealing with data structure, we not only focus on one
piece of data, but rather different set of data and how they can relate to one another in an
organized manner.

Areas where data structures are applied

 Compiler Design,
 Operating System,
 Database Management System,
 Statistical analysis package,
 Numerical Analysis,
 Graphics,
 Artificial Intelligence,
 And Simulation.

2. What is linked list? Write an algorithm to traverse a linked list.


Ans-
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers. In simple words, a linked list
consists of nodes where each node contains a data field and a reference(link) to the next node in
the list.

Following are the various types of linked list.


 Simple Linked List − Item navigation is forward only.
 Doubly Linked List − Items can be navigated forward and backward.
 Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.
Traversal algorithm
Beginning from the head,

1. check, if the end of a list hasn't been reached yet;


2. do some actions with the current node, which is specific for particular algorithm;
3. current node becomes previous and next node becomes current. Go to the step 1.

3. Construct a tree for the given inorder and postorder traversals.

Inorder: DGBAHEICF

Postorder: GDBHIEFCA

Ans-
4. Write an algorithm to perform insertion and selection sort.
Ans-

Insertion Sort
Insertion Sort is a simple comparison based sorting algorithm. It inserts every array element into
its proper position. 
Algorithm: 
InsertionSort (Arr, N) // Arr is an array of size N.
{
For ( I:= 2 to N ) // N elements => (N-1) pass
{
// Pass 1 is trivially sorted, hence not considered
// Subarray { Arr[1], Arr[2], ..., Arr[I-I] } is already sorted

insert_at = I; // Find suitable position insert_at, for Arr[I]


// Move subarray Arr [ insert_at: I-1 ] to one position right
item = Arr[I]; J=I-1;
While ( J ? 1 && item < Arr[J] )
{ Arr[J+1] = Arr[J]; // Move to right
// insert_at = J;
J--; }
insert_at = J+1; // Insert at proper position
Arr[insert_at] = item; // Arr[J+1] = item;
} }}
Selection sort
This algorithm divides the array into two parts: sorted (left) and unsorted (right) subarray. It
selects the smallest element from unsorted subarray and places in the first position of that
subarray (ascending order). It repeatedly selects the next smallest element. 

Algorithm: 
SelectionSort (Arr, N) // Arr is an array of size N.
{
For ( I:= 1 to (N-1) ) // N elements => (N-1) pass
{ // I=N is ignored, Arr[N] is already at proper place.
// Arr[1:(I-1)] is sorted subarray, Arr[I:N] is undorted subarray
// smallest among { Arr[I], Arr[I+1], Arr[I+2], ..., Arr[N] } is at place min_index
min_index = I;
For ( J:= I+1 to N ) // Search Unsorted Subarray (Right lalf)
{ If ( Arr [J] < Arr[min_index] )
min_index = J; // Current minimum }
Swap ( Arr[I], Arr[min_index] );
// Swap I-th smallest element with current I-th place element
}
}

5. Explain graph traversal techniques using an example.


Ans-
The graph is one non-linear data structure. That is consists of some nodes and their connected
edges. The edges may be director or undirected. This graph can be represented as G(V, E).
The following graph can be represented as G({A, B, C, D, E}, {(A, B), (B, D), (D, E), (B, C), (C,
A)})
The graph has two types of traversal algorithms. These are called the Breadth First Search and
Depth First Search.

Breadth First Search (BFS)


The Breadth First Search (BFS) traversal is an algorithm, which is used to visit all of the nodes of
a given graph. In this traversal algorithm one node is selected and then all of the adjacent
nodes are visited one by one. After completing all of the adjacent vertices, it moves further to
check another vertices and checks its adjacent vertices again.

Algorithm
bfs(vertices, start)
Input: The list of vertices, and the start vertex.
Output: Traverse all of the nodes, if the graph is connected.
Begin
   define an empty queue que
   at first mark all nodes status as unvisited
   add the start vertex into the que
   while que is not empty, do
      delete item from que and set to u
      display the vertex u
      for all vertices 1 adjacent with u, do
         if vertices[i] is unvisited, then
            mark vertices[i] as temporarily visited
            add v into the queue
         mark
      done
      mark u as completely visited
   done
End
Depth First Search (DFS)
The Depth First Search (DFS) is a graph traversal algorithm. In this algorithm one starting vertex
is given, and when an adjacent vertex is found, it moves to that adjacent vertex first and try to
traverse in the same manner.
Algorithm
dfs(vertices, start)
Input: The list of all vertices, and the start node.
Output: Traverse all nodes in the graph.
Begin
   initially make the state to unvisited for all nodes
   push start into the stack
   while stack is not empty, do
      pop element from stack and set to u
      display the node u
      if u is not visited, then
         mark u as visited
         for all nodes i connected to u, do
            if ith vertex is unvisited, then
               push ith vertex into the stack
               mark ith vertex as visited
         done
   done
End

SECTION – B
Long answer type questions (Word Limit 400 to 500) (5 x 12 = 60 marks)
1. Define Stack and Queue. Write an algorithm to perform insertion and
deletion in a Queue.
Ans-

Stack
Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Queue

Queue is ordered collection of homogeneous data elements in which insertion and deletion
operation take place at two end . insertion allowed from starting of queue called FRONT point
and deletion allowed from REAR end only

 insertion operation is called ENQUEUE


 deletion operation is called DEQUEUE 

Algorithm for ENQUEUE (insert element in Queue)

Input : An element say ITEM that has to be inserted.


Output : ITEM is at the REAR of the Queue.
Data structure : Que is an array representation of queue structure with two pointer FRONT and REAR.

Steps:

1.  If ( REAR = size ) then //Queue is full


2.        print "Queue is full"
3.        Exit
4.  Else
5.        If ( FRONT = 0 ) and ( REAR = 0 ) then //Queue is empty
6.               FRONT = 1
7.        End if
8.        REAR = REAR + 1 // increment REAR
9.        Que[ REAR ] = ITEM
10. End if
11. Stop

Algorithm for DEQUEUE (delete element from Queue)

Input : A que with elements. FRONT and REAR are two pointer of queue .
Output : The deleted element is stored in ITEM.
Data structure : Que is an array representation of queue structure..

Steps:

1.  If ( FRONT = 0 ) then


2.        print "Queue is empty"
3.        Exit
4.  Else
5.        ITEM = Que [ FRONT ]
6.        If  ( FRONT  =  REAR )
7.               REAR = 0
8.               FRONT = 0
9.        Else
10.             FRONT  =  FRONT + 1
11.      End if
12. End if 
13. Stop 

2. Write an algorithm to perform insertion of the new node at the


beginning, at the middle and at the end of the linked list.
Ans-

// algorithm to insert new node at the beginning


Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL NEXT
Step 4: SET DATA = VAL
Step 5: SET NEW_NODE NEXT = START
Step 6: SET START = NEW_NODE
Step 7: EXIT

Algorithm to insert node at the middle of Singly Linked List

%% Input : n position to insert data in the list

Begin:

createSinglyLinkedList (head)

alloc (newNode)

If (newNode == NULL) then

write ('Unable to allocate memory.')

End if

Else then

read (data)

newNode.data ← data

temp ← head

For i ← 2 to n-1

temp ← temp.next

If (temp == NULL) then

break End if

End for
If (temp != NULL) then

newNode.next ← temp.next

temp.next ← newNode

End if

End else

End

// algorithm to insert new node at the end

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT

3. Define an AVL tree. Obtain a binary search tree and AVL tree by
inserting one integer at a time in the following sequence.

150, 155, 160, 115, 110, 140, 120, 145, 130, 147, 170, 180.
Ans-
AVL tree is a binary search tree in which the difference of heights of left and right subtrees of any
node is less than or equal to one. The technique of balancing the height of binary trees was
developed by Adelson, Velskii, and Landi and hence given the short form as AVL tree or Balanced
Binary Tree.

4. Write an algorithm to perform quick sort? Sort the following elements


using quick sort:

65 70 75 80 85 60 55 50 45
Ans-

Quick Sort Pivot Algorithm


Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list
excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and
right
Step 8 − if left ≥ right, the point where
Quick Sort Algorithm
Step 1 − Make the right-most index value pivot
Step 2 − partition the array using pivot value
Step 3 − quicksort left partition recursively
Step 4 − quicksort right partition

Sorted array

45 50 55 60 65 70 75 80 85

5. Construct a minimum spanning tree according to Kruskal’s and Prim’s


algorithm for the graph

given below:

Ans-

You might also like