CS 307 – Final – Spring 2008 1 Points off 1 2 3 4 5 Total off Net Score CS 307 – Final – Spring 2008 Name__________________________________________ UTEID login name _______________________________ Instructions: 1. Please turn off your cell phones. 2. There are 5 questions on this test. 3. You have 3 hours to complete the test. 4. You may not use a calculator on the test. 5. When code is required, write Java code. 6. You may add helper methods if you wish when answering coding questions. 7. When answering coding questions assume the preconditions of the methods are met. 8. There is a quick reference sheet with some of the classes from the Java standard library attached to the test. 1. (1.5 point each, 30 points total) Short answer. Place you answers on the attached answer sheet. For questions that ask what is the output: • If the code contains a syntax error or other compile error, answer “Compiler error”. • If the code would result in a runtime error or exception answer “Runtime error”. • If the code results in an infinite loop answer “Infinite loop”. On questions that ask for the Big O of a method or algorithm, recall that when asked for Big O your answer should be the most restrictive Big O function. For example Selection Sort has an expected case Big O of O(N^2), but per the formal definition of Big O it is correct to say Selection Sort also has a Big O of O(N^3). Give the most restrictive, correct Big O function. (Closest without going under.) A. The following values are inserted one at a time in the order shown into an initially empty binary search tree using the traditional insertion algorithm. Draw the resulting tree. 7 0 -5 5 3CS 307 – Final – Spring 2008 2 Consider the following binary tree: C / \ A E / / K M \ A / \ J S B. What is the result of a pre-order traversal of the binary tree? C. What is the result of a in-order traversal of the binary tree? D. What is the result of a post-order traversal of the binary tree? E. Is the tree shown above a binary search tree? F. What is the height of the tree shown above? G. What is the average case Big O for adding an element to a Red Black Tree that already contains N elements? H. The following Java code will result in a syntax error. Explain the cause of the syntax error ArrayList list = new ArrayList(); list.add("Texas"); list.add("Nevada"); System.out.println( list.get(1).charAt( 2 ) ); I. The following code attempts to determine if an element is present in a list but has a logic error. Explain what the logic error is. public boolean isPresent(ArrayList<String> list, String tgt){ boolean found = false; Iterator<String> it = list.iterator(); while( it.hasNext() ) found = found || it.next().equals( tgt ); return found; }CS 307 – Final – Spring 2008 3 J. What is the output of the following code? String data = "HAT"; Queue<Character> q = new Queue<Character>(); Stack<Character> st = new Stack<Character>(); for(int i = 0; i < data.length; i++){ q.enqueue( data.charAt(i) ); st.push( data.charAt(i) ); } while( !q.isEmpty() ){ System.out.print( st.pop() ); System.out.print( q.dequeue() ); } K. An UnsortedSet uses an ArrayList as its internal storage container. The UnsortedSet contains N Strings. The UnsortedSet includes a method to find the minimum value in the UnsortedSet. What is the expected Big O of the method? L. An SortedSet uses a Java TreeSet as its internal storage container. The SortedSet contains N Strings. The SortedSet includes a method to find the maximum value in the SortedSet. What is the expected Big O of the method? M. What is the Big O of the following method? The LinkedList initially contains N items. public void addSome(LinkedList<Integer> list){ for(int i = 0; i < 5; i++) list.add( list.size() / 2, i ); } N. What is the Big O of the following method? The ArrayList initially contains N items. public void removeAllVersion1(ArrayList<String> list){ int limit = list.size(); for(int i = 0; i < limit; i++) list.remove( list.size() - 1 ); } O. What is the Big O of the following method? The ArrayList initially contains N items. public void removeAllVersion2(ArrayList<String> list){ while( !list.isEmpty() ) list.remove( 0 ); }CS 307 – Final – Spring 2008 4 P. What is the Big O of the following method? The BST class is a binary search tree that uses the traditional insertion algorithm. public BST<Integer> createTree(int n){ BST<Integer> result = new BST<Integer>(); for(int i = 0; i < n * 3; i++) result.add( i ); return result; } Q. You are using a sorting method that uses the insertion sort. It takes the method 1 second to sort 1000 items that are initially in random order. How long do you expect the method to take to sort 4000 items that are initially in random order? R. You have 1,000,000 integers in an array that you need to search. The integers are currently unsorted. You expect to have to perform 10,000 searches before the data is changed. Should you sort the data or not before doing the searches? Justify your answer with calculations. S. You need to encode the integers between 10,000 and 12,000. What is the minimum number of bits needed per integer to do this? T. What does the following postfix expression evaluate to? 5 10 + 8 2 + *CS 307 – Final – Spring 2008 5 2. (Binary Trees, 20 points) This question has two parts. Part A. (6 points) Complete a constructor for a HuffmanTree class. The HuffmanTree class contains a Huffman code tree. It has a single instance variable, a HuffNode object, which is the root of the Huffman code tree. The constructor has one parameter a Map. A Map is a data structure that stores key-value pairs. Each key is associated with a value. The methods for the Map class are listed on the quick reference sheet included with the exam. The Map sent to the HuffmanTree constructor has keys of type Character, the wrapper class for primitive chars. The char value of a Character object may be accessed by the method charValue or
View Full Document