CS 202 Data Structures Algorithms-1 (1)
CS 202 Data Structures Algorithms-1 (1)
Instructions:
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).
(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).
(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).
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).
( 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).
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)
(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)
(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
(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