CS134 Spring 2005 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. Question II Traversals [6 marks] a) [3 marks] Suppose we have the following results from performing an inorder and levelorder traversal on a binary tree: 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: preorder: 45, 32, 11, 29, 64, 50 Construct all trees that are consistent with the above traversal or show no such tree exists.
Question III Binary Search Trees [10 Marks] Page 3 of 15 Student id: 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.
Page 4 of 15 Student id: A perfect binary tree is tree where each node is either a leaf or has two children, and all leaves have the same depth. 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:
Question IV Order Notation [10 Marks] Page 5 of 15 Student id: Use order notation to express the worst-case running time of each of the following code fragments in terms of n. Give as tight an asymptotic upper bound as possible. A println statement is considered to be an O(1) operation. a) b) c) d) e) int x = n; System.out.println( x ); for (int i = 1; i <= 10000; i++) { bigarray1[i] = bigarray2[i] + x; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { System.out.println( We want tight bounds! ); 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 ); int i = 1; while (i < n) { i = i * 2; System.out.println( Enjoy CS 241 or CS 230! ); 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 );
Page 6 of 15 Student id: 2 f) [3 marks] Prove by applying the definition of big-oh that f n) 5n 4n O n ( = is ( ) 2 g) [2 marks] Give two reasons that we might prefer an algorithm with an ( n ) over an algorithm with an O( n) running time. O running time
Question V Recursive Program [7 Marks] Page 7 of 15 Student id: A heap is a special binary tree which is either empty, or one where the value stored in all parent nodes in the tree is greater than all its children (if they exist). The following tree is a heap: 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: public int maxvalue() { // pre: this is a heap of positive Integer objects // post: returns the maximum number contained in the heap, // or returns -1 if the tree is empty
Page 8 of 15 Student id: public boolean isheap() { // pre: all items in the tree are positive Integer objects // post: returns true if this is a heap
Question VI Inductive Proof [6 Marks] Page 9 of 15 Student id: 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.
Question VI Inductive Proof Continued Page 10 of 15 Student id:
Question VII Sorting [12 Marks] Page 11 of 15 Student id: Carefully explain under what circumstances each of the following sorting algorithms would be the best sorting algorithm to use in an application. a) [1 mark] Mergesort b) [1 mark] Quicksort c) [1 mark] Selection sort d) [1 mark] Insertion sort 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.
Page 12 of 15 Student id: i) [4 marks] Professor Dumbledore is developing a course on the Magic of Sorting. He proposes a new sorting algorithm called HogwartsSort based on the ideas of merge and selection sort. Using divide and conquer, we divide a full array (i.e. it is not partially filled) into two equal sized regions as we would for merge sort, until a region has less than n items. At that point each region is sorted using selection sort. Then the regions are merged as normal for merge sort. 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. public static void HogwartsSort(Comparable[] ta, int first, int last){ // pre: 0 <= first <= last < ta.length and // ta and ta[first..last] are non-null // post: ta[first..last] is in ascending order 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.
Page 13 of 15 Student id: Question VIII Searching Ordered Arrays [9 Marks] [6 marks] Recall that binary search splits the array in half. Suppose we wrote a variation of binary search which narrowed the search range to either the first, middle or last third of the array. Write the recursive body of this trisearch method assuming that it would appear inside of an ordered array implementation of the table ADT. Note the return type of the method. public class OrderedArray implements TableInterface { private KeyedItem[] items; // instance variable holding the sorted array private int size; // number of items in the table private KeyedItem trisearch( int lo, int hi, Comparable key ) { // pre: 0 <= lo < size; 0 <= hi < size; key!= null // post: if key is in items[lo..hi], return the corresponding KeyedItem // otherwise return null
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.
Question IX Iterators [4 Marks] Page 15 of 15 Student id: Write a method which creates a duplicate copy of an existing TableInterface, T. This copy will contain exactly the same set of KeyedItems as the original. You should not alter the contents of T You can assume that the TableInterface defines a method tabletraverse which returns an Iterator for the given table (see the reference sheet) public static TableInterface duplicate ( TableInterface T ) { // pre: T is non-null // post: returns a new TableInterface containing the same set of // KeyedItems as T