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

MTS3023: Data Structures 2

SECTION A: Multiple Choice


Instruction : Answer all questions in the OMR form provided.

1. Consider directed graph in Figure 1. Which of the following is a valid order of node
visitation during depth first search (DFS) traversal?

D F

A B

E C
Figure 1
A. ADBCFE
B. BFEADC
C. CFDAEB
D. DBFCEA

2. Consider undirected graph in Figure 2. Which of the following is not a valid order of node
visitation during breadth first search (BFS) traversal?

D F

A B

E C

Figure 2

A. ADECBF
B. BDFCEA
C. CEBFAD
D. DABFEC
MTS3023: Data Structures 3

3. Consider the algorithm Multiply(A,n) below. Parameter A is an array of size n storing


integer values. What is the best characterization of the best and worst case asymptotic time
complexity of the following algorithm?

Algorithm Multiply (A, n)


result = 0
for i = 0 to n/2 do
j = 0
while A[j] < 0 and j < n/2 do
result = result + A[i] * A[j]
j = j+1
return result

A. Best case O(1), worst case O(n)


B. Best case O(n), worst case O(n)
C. Best case O(1), worst case O(n2)
D. Best case O(n), worst case O(n2)

4. Consider a linked list of n elements. What is the time taken to insert an element after the
element pointed by same pointer?

A. O(n)
B. O(log n)
C. O(n-1)
D. O(1)

5. Given the declaration for class Stack in Program 1:

struct Snode {
Snode*next;
char item;
};
class stack{
private: Snode *top;
void push(int);
void pop(int);
void stack(){ top = NULL; }
};
void stack::push(int item){
Snode*temp=new Snode;
temp->item=item;
temp->next=top;
top= temp;
MTS3023: Data Structures 4

}
void stack::pop(int i){
Snode*temp=top;
top=top->next;
temp->next=NULL;
delete temp;
}

Program 1

What will happen if the following code fragment to is execute is to be executed?

void main()
char x1, x2, x3;
s.push(‘A’);
s.push(‘B’);
s.push(‘C’);
x1 =s.pop();
x2 = s.top();
push(x2);
x3 =s.pop();
x1 = s.top();

A. Stack contains B (top), A (bottom); x1 = B, x2 = B, x3 = B


B. Stack contains C (top), A (bottom x1 = B, x2 = B, x3 = B
C. Stack contains B (top), A (bottom); x1 = C, x2 = B, x3 = B
D. Stack contains C (top), A (bottom); x1 = C, x2 = B, x3 = B

6. Consider the operations on a Queue data structure that stores int values below.

QUEUE q = new QUEUE();


q.enqueue(3);
q.enqueue(5);
q.enqueue(9);
cout<<q.dequeue( )<<endln;
q.enqueue(2);
q.enqueue(4);
cout<<q.dequeue( )<<endln;
cout<<q.dequeue( )<<endln;
q.enqueue(1);
q.enqueue(8);

After the above operations has been executed, how many elements remain in q?

A. 4
B. 5
MTS3023: Data Structures 5

C. 6
D. 7

7. Bubble sort is used to sort the following array (where A[0] shown as the left-most value) in
an ascending order:

10, 5, -3, 12, 2, 1, -9

After step 3, what is the position value in the array?

A. 1, 2, -3, -9, 5, 10, 12


B. -3, 5, 10, 12, 2, 1, -9
C. -3, 2, 1, -9, 5, 10, 12
D. -3, 1, 2, -9, 5, 10, 12

8. What is the best type of data structure if the a developer needs to develop recursion function?

A. Stack
B. Queue
C. Linked list
D. Tree

9. What is the best type of data structure if a developer needs to access the elements using the
index?

A. Stack
B. Graph
C. Tree
D. Array

Section A (30 Marks)


Instruction: Answer all questions in the answer booklet provided.

1. Based on what you have learned on recursion concept, what is the result of x=7, and x=4?

#include<iostream>
using namespace std;
int factorial(int a){
MTS3023: Data Structures 6

if(a<=1)
return 1;
else
return (a*factorial(a-1));
}
void main(){
int x;
cin>> x;
cout<< factorial(x);
}
[ 3 marks ]

2. To insert an item into a doubly linked list at position n, there are some changes need to be
done.

void list::insert(int index,int value){

dnode *cur = find (index+1);


temp-> value = value;
temp-> after = cur-> after;
temp-> before = cur;
if (cur-> after == NULL)
back = temp;
else {
cur-> after = temp;
(cur-> after) -> before = temp;
}

}
hint, *cur=find (index+1) .
[ 3 marks ]

3. From your understanding to of the concepts of queue and stack,

a) What are the main differences between queue and stack?


[ 2 marks ]
b) Give one example of a real life application for each queue and stack.
[ 2 marks ]
c) Explain graphically, how the stack and queue are working?
[ 4 marks ]
MTS3023: Data Structures 7

4. Assume that you have the linked list implemented using an array below:
1 → 20→ 37→ 44→ 45→ 60,

a) How to delete the head and index 3? Explain in graphs using an appropriate diagram.
[ 2 marks ]
b) Based on the original linked list, how to insert an item in the head and index 3? Explain in
graphs using an appropriate diagram.
[ 2 marks ]

5. Assume a sort function is a function to reorder the items in a doubly linked list. Based on the
given code below, according to the below code, explain graphically the result of applying this
function on a doubly linked list.
Head =200 100 300 150

4 100 16 300 7 150 3 Null

void list::sortn(){
node*before;
node*second;
node*first;
for(int d=1;d<=size;d++){
for(int j=1;j<=size-d;j++){
first=finde(j);
before=finde(j-1);
second=finde(j+1);
What is this finde() function for? we do not know the
initial value of first, second and before.Do student understand this
concept?

if(first->item>second->item){
first->next=second->next;
if(before==NULL)
head=second;
else{
before->next=second;
second->next=first;
}
}
}
}
} [ 3 marks ]

6. From your understanding to the concept of binary tree, please answer the below question.
a) Consider the binary tree:
MTS3023: Data Structures 8

c p

r d a

List the letters of the nodes of the tree in: (i) preorder, (ii) inorder, and (iii) postorder.
[ 3 marks ]

b) Explain the three cases of the delete function for the below tree:

1
2

5 1
5

3 1 1
7
3 7

1 1 2
9
4 0

1
8 1 8
1

Please name the nodes for each case in your explanation.

[ 6 marks ]

You might also like