IT2070 - Data Structures and Algorithms
IT2070 - Data Structures and Algorithms
Final Examination
Year 2, Semester 2 (2022)
November 2022
Instructions to Candidates:
• This paper has 4 questions.
• Answer all questions in the booklet given.
• The total marks for the paper is 100.
• This paper contains 8 pages, including the cover page.
• Electronic devices capable of storing and retrieving text, including
calculators and mobile phones are not allowed.
• 10 minutes reading time is allowed
oo71\
uestion 1 25 marks
int[] stackArray
int maxsize
int top
void push(double j)
double pop( )
double peek( )
boolean isEmpty( )
boolean isFull( )
a) An object of Stackx class is created as mystack with size 5. Start from the stack frame given
in Figure 1 and draw the stack frame after each statement given below. (5 marks)
<top
Figure I
i) mysatck.push(6);
ii) mystack.peek();
iii) mystack.push(2);
iv) mystack.pop( );
v) mystack.pop( );
b) Assuring that the Stackx class has been implemented usingjava, complete the followingjava
program StackApp to do the following.
class StackApp {
public static void main(String[] args) {
00716
class Queuex {
private int maxsize;
private int [] queArray;
private int front;
private int rear;
private int nltems;
Complete the following table with correct boolean expression used in the given method.
(6 marks)
boolean isEmply( )
boolean isFull( )
3
oo,1\
uestion 2 25 marks
a) Draw the link list created after executing the below code segment. insertFirst() method will
insert the link as the first link and insertLast() method inserts the link as the last link.
deleteFirst() method removes the first link from the link list. (3 marks)
b) In a hospital management system, patient beds are stored in a link list. patientBed class and
linkList classes are given below.
`Vacant' attribute of patientBed class will be 0 when the patient is admitted to the hospital and
assigned the bed. It will be 1 when the patient is discharged.
patientBed lillkList
i) You need to modify the linkList class by adding another method called countvacantBeds()
in the linkList class. This method returns the number of vacant patient beds. Implement
the method. (5 marks)
ii) findvacant() method in the linkList class returns the first vacant patientBed. If there are
no vacant beds, it will retuni NULL. Write code segment in your main application to flnd
a vacant bed and assign the bed. Display the assigned bedNo. If there are no vacant beds
in the hospital display the message "Patient beds are not available". (5 marks)
/I, 00716
Implement a method of the tree class called displayGreaterThanRoot() to display all the nodes
greater than the root in ascending order. (5 marks)
Ouestion 3 25 marks)
a) Find the step count of the following pseudocode using RAM Model. (7 marks)
ii)
int j - 0 int i - 1
for (i = 1 to 5) while (i <-
print i i-i
while (j < 5) print
j-j+1
print j
b) State the complexity using Big(O) notations of the following running time equations. (6 marks)
i) T(n)=n+nlogn+n2+5
ii) T(n) -2n + 10
iii) T(n) = 3n + n! + 4n2 + nn _ 8
i) What is the recurrence equation for the Merge Sort Algorithm? (2 marks)
ii) State the reason of having only the best case in the Merge sort Algorithm. (2 marks)
5
Pseudo code of the MERGESORT algorithm is given below
MERGESORT (A, p, r)
1. if p < r
q -L(p+r )/2J
MERGESORT (A, p, q)
MERGESORT (A, q+1, r)
MERGE (A, p, q, r)
PARTITION(A, p, r)
x -A[r]
- p to r - 1
A[j] < x
i-i+1
exchange A[i] with A[j]
exchange A[i + 1] with A[r]
8 return i + 1
Ouestion 4 25 marks
a)
i) Pseudo code of the BUILD_MAX_HEAP algorithm is given below. Complete line number
2 of the algorithln and explain the purpose of that line. (4 marks)
6
/,,-- 00716
ii) Identify the violating node of the below max heap and illustrate the operations of the
MAX_HEAPIFY (A,i) for the array A given below. (Use diagralnmatic way to reach the
answer) (6 marks)
Array A
c) Draw the state transition diagram for a string-matching automation for the pattern P = abb and
take the input alphabet as {a,b} (6 marks)
Naive-String-Matcher (T, P)
1. n = length[T]
2. in - |ength[p]
3. for s = 0 to n-in
4. if P[1..m] = T[s+1..s+m]
5. print "Pattern occurs with shift" s
7
ci,0716`
a A b b b a a a b a
How many comparisons would occur in this algorithm for the pattern "abb"? (4 marks)