CS307 Fall 2005 final exam suggested solutions and grading key 1. answer as written or -2. differences in capitalization and spacing okay. on big o missing O( ) okay, but no extra terms A. 32 / \ 5 74 / \ 4 31 B. 52 25 9 5 30 72 70 75 C. 5 9 30 25 52 70 72 75 D. 5 30 9 25 70 75 72 52 E. 52 25 72 9 70 75 5 30 F. No G. front back 3 4 5 3 H. front back 2 1 5 I. 999 (1000 okay) J. any answers 15 - 35 K. O(N) L. interfaces allow for a limited form of multiple inheritance. They also allow design without any implementation details. This allows many implementations. (or words to that effect) -1 at discretion of grader M. 4N + 7 (+/- 2) N. O(N^2 log N) O. O(N^3) 2. public boolean isPresent(Comparable data) { return helper( data, myRoot ); } private boolean helper( Comparable data, RBTreeNode n) { if( n == null ) return false; else { int dir = data.compareTo( n.getData() ) { if( dir == 0 ) return n.isPresent(); else if( dir < 0 ) return helper( data, n.getLeft() ); else return helper( data, n.getRight() ); } } } A. fall off tree case 3 pts B. found and is present case 3 pts C. found and is not present case 3 pts D. go left case 3 pts E. go right case 3 pts 3. public Comparable getMin() { return minHelper(myRoot, null); } private Comparable minHelper( RBTreeNode n, Comparable minSoFar ) { if( n != null ) { minSoFar = minHelper( n.getLeft(), minSoFar ) //if minSoFar == null I fell off tree or all nodes so far are not present if( minSoFar == null && n.isPresent() ) minSoFar = n.getData(); // if minSoFar == null, still have not found a value that is present if( minSoFar == null ) minSoFar = minHelper( n.getRight(), minSoFar ); } return minSoFar; } okay if do complete traversal A. base case, off tree, 4 pts (look ahead okay) B. go left, 4 pts C. go right, 4 pts D. find correct min based on node is present and continue until found, attempt 4 pts E. find correct min based on node is present and continue until found, correct 4 pts 4. public void add(Object item) { int index = item.hashCode() % myCon.length; while( myCon[index] == null ) { index = ( index + 1 ) % myCon.length; } myCon[index] = item; iMySize++; //rehash? if( 1.0 * iMySize / myCon.length > dMyLoadFactorLimit ) rehash(); } A. calculate initial index, attempt 1 pts B. calculate initial index, correct 1 pts C. linear probe for first open spot, attempt 3 pts D. linear probe for first open spot, correct 4 pts E. increment iMySize, 1 pt F. check if above load limit and call rehash, attempt 3 pts G. check if above load limit and call rehash, attempt 2 pts (double) 5. public String encrypt(String[] clearMessage, Map encryptKey) { String result = ""; for(int i = 0; i < clearMessage.length; i++) { if( encryptKey.containsKey( clearMessage[i] ) result += encryptKey.get( clearMessage[i] ) else { for(int c = 0; c < clearMessage[i].length(); c++) { if( encryptKey.containsKey( clearMessage[i].charAt{c) + "" ) result += encryptKey.get( clearMessage[i].charAt(c) + "" ); } } } return result; } A. iterate through clearMessage, attempt 2 pts B. iterate through clearMessage, correct 2 pts C. handle case when whole element is key, attempt 4 pts D. handle case when whole element is key, correct 4 pts E. handle case when must iterate through each char in string, attempt 4 pts E. handle case when must iterate through each char in string, correct 4 pts