Uploaded by alianwar0305

Data Structures & Algorithms Exam - Spring 2021

advertisement
COMSATS University Islamabad, Lahore Campus
□
Sessional-1
Course Title:
Course
Instructor/s:
Semester:
Time Allowed:
□Sessional-II √Terminal Examination – Spring 2021
Data Structure and Algorithms
Course Code:
CSC211 Credit Hours:
Program Name:
Batch:
Section:
180 min
Student’s Name: Muhammad Ali
Date:
Maximum Marks:
Reg. No.
5-7-2021
100
FA19-BSE-012
Important Instructions / Guidelines:
 Attempt all questions.
Total
1
2
3
4
5
10
20
20
5
20
6
25
100
Obtained
Question 1.
[Marks: 1+2+2+5=10]
a) Consider the following linked list while answering the questions given below.
[1+2+2= 5]
head
Suppose a node contains two parts that is data and next. Write statements to do the following,
i.
Update B so it will refer to the last node in the list.
Node* temp = head;
While(temp -> nest != NULL) {
Temp = temp2->next;
}
ii.
Set the value of the node containing 16 to 71.
Node* temp = head;
While(temp -> data!= 16){
Temp = temp->next;
}
Temp->data = 71;
1(0,1)
iii.
Create and insert the node with info 10 after the node pointed to by A.
Struct node{
Int data;
Node* node;
};
Node* newnode(int data){
This.data = data;
Node -> NULL;
}
Node* temp =head;
Node* x;
While(temp -> data != head) {
Temp = temp->next;
}
X = temp-> next;
Temp->next = newnode->data;
Newnode->next = x;
b) Describe the output and draw the stack for each of the following stack operation:
1. push(5),
5
2. push(3),
3
5
3. pop(),
5
4. push(7),
7
[5]
5
5. peek(),
7
6. push(6),
6
7
5
7. pop(),
7
5
8. pop(),
5
9. peek(),
5
10. push(4),
4
5
11. pop(),
5
12. pop().
Question 2.
a)
[Marks:10 + 10 = 20]
Convert the following infix expressions to postfix.
i.
[10]
(3+1) * ((1+2) * (5+4))
Answer: (31+) * ((12+) * (54+))
(31+) * (12+54+*)
31+12+54+**
b)
What is the value of following postfix expressions? Show the stack contents as each operand and
operator is processed. Make it clear where the top of the stack is at each step?
[10]
i.
Answer:
5
51/3*31–*
1
5
5
3
5
15
1
3
15
2
15
30
Question 3.
a)
[Marks:10+5+5= 20]
Draw a binary search tree step by step by inserting the following numbers
11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
[10]
b)
Consider the following BST.
Insert the following items into this BST and then show a post-order traversal of the tree.
i.
23 55 40
[5]
Then, insert the following elements into the resultant tree and show the pre-order traversal of
the tree.
ii.
90 0 20
[5]
Question 4.
[Marks:5]
a) Show step by step procedure for sorting following data items stored in an array using insertion sort
technique.
29 10 14 37 13
1) Starting Insertion Sort.
29
10
14
37
13
2) Records to the left are always sorted. We begin with the record in position 0 in the sorted portion, and
we will be moving the record in position 1. Swap
10
29
14
37
13
3) Processing record in position 2. Swap
10
14
29
37
13
37
13
4) Processing record in position 3.
10
14
29
5) Processing record in position 4. Swap
10
14
29
13
37
14
13
29
37
13
14
29
37
6) Swap
10
7) Swap
10
Question 5.
[Marks:4+8+8 = 20]
a) What will be the adjacency matrix representation of the following graph?
[4]
S
A
B
C

S
A
B
C
D
T
D
T







b) Consider the following graph:
Start with node s and apply the Dijkstra’s algorithm step by step.
i.
ii.
Show the priority queue at each step of traversing a node.
[8]
Also, show final paths (along with the path length) from node ‘s’ to every other reachable node.
[8]
Dijkstra’s Shortest Paths:
(a)
(b)
(c)
(d)
(e)
s ->
s ->
s ->
s ->
s ->
A
B
C
D
t
(Path length 1/cost: 4)
(Path length 2/cost: 5)
(Path length 1/cost: 1)
(Path length 2/cost: 6)
(Path length 3/cost: 8)
Priority Queue (Step-by-step):
Step
0
1
2
Dequeued node
Priority queue
[s 0]
3
4
5
6
Question-6:
[10 +10 + 5 = 25]
Write definition of the following method in max nLog(n) time; write the name of both implemented
algorithms.
bool Search (int ary[], int counter, int num)
implemented algorithms: Balanced Binary Tree
Hint: first of all sort the array and then search a number.
Download