CS134S05FinalExam
CS134S05FinalExam
Final Exam
Mon. June. 20, 2005
Please check your tutorial (TUT)
section from the list below:
TUT 101: F 11:30, MC 4042
TUT 102: M 10:30, MC 4042
TUT 103: M 11:30, MC 4058
TUT 104: F 10:30, MC 4042
TUT 105: M 12:30, STJ 2009
TUT 106: Th 4:30, MC 4042
TUT 107: M 8:30, MC 4042
Uncertain
Cheating is an academic offence. Your signature on this exam indicates that you
understand and agree to the University’s policies regarding cheating on exams.
Signature:
1. The examination starts at 7:00 pm and ends at 9:00 pm. You may not leave for the first
half hour or in the last ten minutes.
2. You should have 11 pages in this exam (including the cover page), plus a separate 2-
sided reference sheet.
3. No additional materials are permitted.
4. Complete all answers in the spaces provided.
5. Write neatly so that you do not lose marks unnecessarily.
6. Proctors will only confirm or deny the existence of errors on the exam. In the case of
perceived ambiguity, state a clear assumption and proceed to answer the question. Your
assumption must not trivialize the question.
Question # Out Of Marks Marker
1 10
2 10
3 12
4 12
5 5
6 5
7 6
Total 60
Page 2 of 15 Student id: _____________________
Question I Multiple Choice, True/False, Short Answer
[21 Marks - 1 mark per question]
There were 21 Multiple Choice/True-False/Short Answer questions on this exam worth a total of
21 marks.
inorder: A, W, L, E, T, R level-order: W, A, T, E, R, L
Construct all trees that are consistent with both traversals or show no such tree exists.
b) [3 marks] Suppose we have the following results from performing a preorder traversal on a
binary search tree containing numeric keys:
Construct all trees that are consistent with the above traversal or show no such tree exists.
a) [2 marks] Beginning with an empty binary search tree, what binary search tree is formed
when you insert the following values in the order given: 1, 4, 3, 7, 5, 2, 6. You are only required
to draw the final tree for all or nothing credit. If you draw the intermediate trees, you may
receive partial credit.
b) [2 marks] Show the two possible trees that result after the string kangaroo is removed from
the binary search tree below using the binary search tree deletion algorithm.
c) [2 marks] Give a perfect binary search tree with nodes labeled 1,2,3,4,5,6,7.
d) [2 marks] You are given a binary search tree T where all nodes are uniquely labeled with
integers between 1 and 99 inclusive (not all integers in that range are necessarily used). The root
node is “4” and its left and right subtrees have the same height. Draw the tree that has the
maximum possible sum over all the nodes?
e) [2 marks] What is the worst case performance of searching for a key in a perfect binary
search tree containing n keys. Give your answer in order notation in terms of n. Compare this
to searching for a key in a possibly non-perfect binary search tree.
Perfect:
Non-Perfect:
a)
int x = n;
System.out.println( x );
for (int i = 1; i <= 10000; i++) {
bigarray1[i] = bigarray2[i] + x;
}
b)
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
System.out.println(“We want tight bounds!”);
}
}
c)
for( int i = 1; i <= n; i++ ) {
x++;
}
for(int i = 1; i <= n*n*n; i = i + n) {
System.out.println(“I love CS”);
}
d)
int i = 1;
while (i < n) {
i = i * 2;
System.out.println(“Enjoy CS 241 or CS 230!”);
}
e)
for( int i = 1; i <= n; i++ ) {
if ( i == (n / 2) ) {
for( int j = 0; j <= n* n ) {
System.out.println( “Halfway there!” );
}
} else {
System.out.println( “Lots to do, yet” );
}
}
( )
g) [2 marks] Give two reasons that we might prefer an algorithm with an O n 2 running time
over an algorithm with an O (n ) running time.
Write the following two methods which could be added to any implementation of the ADT
BinaryTreeInterface. You must use recursion whenever you need to traverse the tree.
Specifically, you are not allowed to use for, while or do loops in any methods:
}
CS 134 Final Spring 2005
Page 9 of 15 Student id: _____________________
Question VI Inductive Proof [6 Marks]
Prove by induction on h that the number of leaves, L, in a tree of height h satisfy:
L ≤ 2 h −1
For example, the following tree has two leaves and height 3 which satisfies the above relation:
Use the space on the next page if you run out of room.
b) [1 mark] Quicksort
e) [1 mark] Assume the array 20, 10, 80, 40, 15, 75 is sent as input to Selection Sort. Show the
contents of the array immediately after the second swap
f) [1 mark] Give an example of the best case input of size 6 for Insertion Sort.
g) [1 mark] Assume the array 20, 10, 80, 40, 15, 5 is sent as input to Merge Sort. Show the
contents of the array immediately before the final merge takes place.
h) [1 mark] Assume the array 20, 10, 80, 40, 15, 75 is sent as input to Quick Sort. Show the
contents of the array immediately after the first partition operation takes place. Use the first
element as the pivot.
Complete the body of the HogwartsSort method as described above. Assume the existence of the
merge and selectionSort methods described in the Sorting class of the Reference booklet.
i) [2 bonus marks] Analyze the running time of HogwartsSort. Do not attempt this until
you have finished the rest of the exam. Show your work on the back of the previous page, but
put your final answer here.
}
CS 134 Final Spring 2005
Page 14 of 15 Student id: _____________________
[2 marks] Suppose the initial number of items is N, where N is a power of 3. How many times
is the triSearch method called in the worst case (including the original call)? Give the exact
answer, and justify your answer.
[1 mark] Is this version of searching asymptotically more efficient than binary search? Justify
in one sentence.