Data Structures Mid Term V2
Data Structures Mid Term V2
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
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)
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
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();
6. Consider the operations on a Queue data structure that stores int values below.
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:
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
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.
}
hint, *cur=find (index+1) .
[ 3 marks ]
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
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
[ 6 marks ]