0% found this document useful (0 votes)
52 views

CS 202 Data Structures Algorithms-1 (1)

The document outlines the examination details for the course 'Data Structures and Algorithms Analysis' for the January-April 2023 semester, including the exam date, duration, and structure. It specifies that students must answer five out of ten questions, each worth 20 marks, covering various topics related to data structures and algorithms. Additionally, it includes sample questions that test students' understanding of queues, binary search trees, heaps, and sorting algorithms.

Uploaded by

otim.micheal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

CS 202 Data Structures Algorithms-1 (1)

The document outlines the examination details for the course 'Data Structures and Algorithms Analysis' for the January-April 2023 semester, including the exam date, duration, and structure. It specifies that students must answer five out of ten questions, each worth 20 marks, covering various topics related to data structures and algorithms. Additionally, it includes sample questions that test students' understanding of queues, binary search trees, heaps, and sorting algorithms.

Uploaded by

otim.micheal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Examinations for January – April 2023 Semester

Course Code J23. CS 202

Course Name Data Structures and Algorithms Analysis

Programme(s) CS, IT, IS

School(s) College of Engineering and Computing

Year of Study 1 Semester 1

Examination Date 26 April 2023

Starting Time Ending Time

Instructions:

1. The duration of the examination is THREE hours

2. The exam has 10 Questions

3. Each questions has 20 marks

4. Attempt Only Five (5) Questions

UTAMU
Tel www.utamu.ac.ug
+256702 646 093 +256750 599 736
[email protected]
Question 1 (20 marks)

(i) Insert the numbers 2, 4, 3, 7, one at a time in that order into to an initially empty queue
(Marks 3).

(ii) Represent that process above using the standard constructors push and EmptyQueue.
(Marks 3).

(iii) Show, in the standard two-cell notation, the resulting queue.


(Marks 3).

(iv) What is the result of the operation top on that queue?


(Marks 3).

(v) What is the result of the operation pop on the original queue you created?
(Marks 3).

(vi) What is the result of the operation pop followed by pop followed by top on the original queue
you created? (Marks 5).

Question 2 (20 marks)

(i) How many different orderings of the four numbers {1, 2, 3, 4} are there? (Marks 5).

(ii) By considering all those possible orderings, draw all possible binary search trees of size four
with nodes labelled by the four numbers {1, 2, 3, 4}. After discarding any duplicate trees, how many
different binary search trees of size four are there? (Marks 10).

(iii) For each different tree, state its height, how many leaf nodes it has, and whether it is perfectly
balanced. (Marks 5).

Question 3 (20 marks)


It is sometimes useful to treat collections of items as sets upon which standard set theory operations
(like membership, subset, intersection, and union) can be applied. A convenient representation of a
set is as a list in which repeated items are not allowed and the order of the items does not matter.

Suppose you have sets S1 and S2 represented as linked-lists, and access to the standard
primitive list operators first , rest and isEmpty.

(i) Write a recursive procedure member(x,S1) that returns true if item x is in set S1, and
false if it is not (5 Marks).

(ii) Provide an argument that leads to the average-case and worst-case time complexities of your
member(x,S1) algorithm in Big O notation (5 Marks).

(iii) Now write a recursive procedure subset(S1,S2) that returns true if set S1 is a subset of set
S2, and false if it is not. It is only allowed to call the standard primitive list operators first,
rest and isEmpty and your member procedure (5 Marks).

Page 1 of 6
(iv) Finally, write a recursive procedure union(S1,S2) that returns the union of sets S1 and S2
represented as linked-lists. It is only allowed to call the standard primitive list operators
makelist, first, rest and isEmpty and your member procedure (5 Marks).

Question 4 (20 Marks)


(i) Draw the binary search tree that results from inserting the items [19, 30, 36, 10, 40, 25, 33] in
that order into an initially empty tree (2 Marks).
(ii) Show how the tree rotation approach that can be used to balance that tree (3 Marks).
(iii) Draw tree that results from deleting the item 30 from your balanced tree using the delete
algorithm (3 Marks).
(iv) In the class Lectures a simple procedure isbst(t) was defined that returns true if t is a
binary search tree and false if it is not. Explain why that algorithm is not efficient, and
illustrate your explanation with a simple example (5 Marks).

Question 5 (20 marks)


( i) Derive expressions for C(h) and A(h) which generalize the total and average number of
comparisons computed in Question 5 to the case of a full binary search tree of any height h (15
Marks). [Hint: This question is quite challenging! If your mathematics is strong, it is possible to
write C(h) as the sum of the number of comparisons at each level, and then simplify that using the
usual algebraic tricks for summing series.

( ii) What can you deduce about the average number of comparisons required, and hence the
average time complexity of the search, for large trees? (5 Marks).

Question 6 (20 marks)

Numbers [25, 12, 6, 17, 29, 18] need to be inserted one at a time, in that order, into an
initially empty Binary Heap Tree.

(i) Draw the state of the Heap Tree after each number has been inserted (5 Marks).
(ii) Now draw the Heap Tree after each of the first two highest priority items have
been removed from the resulting Heap Tree. (5 Marks).
(iii) Finally, draw the Heap Tree after each of the two removed items have been added
back to the Heap Tree in the order they were removed (5 Marks).
(iv) To what extent does the order of the initial list of items affect the heap tree that is
produced and the order in which the highest priority items are removed?(Marks
5)

Question 7 (20 marks)

(i) Write an efficient recursion-based procedure isHeap(t) that returns true if the binary
tree t is a heap tree, and false if it is not. You can call any of the standard primitive
binary tree procedures isEmpty(t), root(t) , left(t) and right(t), and also a procedure
complete(t) that returns true if t is complete, and false if it is not. (Marks 10)

(ii) What can be said about the overall complexity of your algorithm? (Marks 10)

Page 2 of 6
Question 8 (20 marks)

(i) Insert the integers [5, 4, 9, 1, 3, 8] in that order into an initially empty binomial
heap. Show the heap after each item is inserted. (Marks 10)

(ii) Now delete, one at a time, the two highest priority items. Show the binomial heap that
is left after each item has been deleted. (Marks10)

Question 9 (20 marks)

(i) The following segment of C/Java code will sort an array a of finite size n:
for( i = 1 ; i != n ; i++ ) {

j = i;
t = a[j];
while( j > 0 && t < a[j-1] ) a[j] = a[--j];
a[j] = t;
}
Which sorting algorithm discussed in the lectures is this an implementation of? (Marks 5)
(ii) Work through the sorting of the array [6, 4, 8, 5, 2, 7] using this algorithm, writing
down the values of i, j, t and the array a at the end of each iteration of the
for loop. (Marks 10)

(iii) State an appropriate loop invariant for this algorithm, and use that to argue why the
algorithm is guaranteed to terminate with the array a sorted. C

Question 10 (20 marks)

(i) To get the individual terms in the sum, think how many nodes there are at each level i in the
tree, and how many nodes further down the tree each of them is compared to. (Marks 10)
(ii) Deduce the time complexity of this algorithm in terms of the size n of the tree. [Hint: Derive
approximate expressions for the size n and complexity C as functions of large h, and hence
obtain an approximate expression for the complexity C as functions of large n. (Marks 10).

End

Page 3 of 6
Page 4 of 6
Page 1 of 6

You might also like