CS307 Spring 2009 Final Solution and Grading Criteria. Grading acronyms ABA - Answer by Accident AIOBE - Array Index out of Bounds Exception may occur BOD - Benefit of the Doubt. Not certain code works, but, can't prove otherwise ECF - Error carried forward. Gacky or Gack - Code very hard to understand even though it works or solution is not elegant. (Generally no points off for this.) GCE - Gross Conceptual Error. Did not answer the question asked or showed fundamental misunderstanding NAP - No answer provided. No answer given on test NN - Not necessary. Code is unneeded. Generally no points off NPE - Null Pointer Exception may occur OBOE - Off by one error. Calculation is off by one. 1. As written or -1.5. No partial credit unless stated. On Big O, missing O() is okay. A. 22 / \ 8 63 / / -20 49 B. AGAISNO C. AGANSOI D. AGNOSIA E. AGIASNO F. No G. O(N^2) H. O(NlogN) (base 2 okay) I. SSNI J. O(N) K. O(N) L. open address hasing (or probing) and chaining (or buckets) M. 9 N. 5 bits O. O(logN) P. 5000 * 1000 = 5,000,000 (5 million) +/- 1 million Q. Yes, it is a red black tree. R. No it is not a red black tree. Rule 5 is violated. (The path rule). There are paths with 1, 2, and 3 black nodes. (Or words to that effect. -1 if no explanation or explanation incorrect.) S. LinkedList T. A linked list. 2. Suggested solution: public void enqueue(E value, int priority){ assert value != null && priority > 0; // make use of short circuit. Is new node first node? if( front == null || priority < front.getPriority() ) front = new Node(value, priority, front); else{ Node temp = front; while(temp.getNext() != null && priority >= temp.getNext().getPriority() ) temp = temp.getNext(); temp.setNext( new Node(value, priority, temp.getNext() ); } } grading criteria: O(1) space: 4 pts handle front empty case: 2 pts handle non empty, but new first node case: 2 pts general case, move through list: attempt 2 pts, correct 2pts general case, check priorities correctly: attempt 2 pts, correct 2 pts general case, insert node at correct spot: attempt 2 pts, correct 2ps 3. Suggested Solution public boolean add(Object obj){ int index = obj.hashCode() % con.length; int indexToAdd = -1; boolean looking = true; while( looking ){ if( con[index] == null ){ // if we find a null we have not found an object equal to obj // and we can stop looking looking = false; if(indexToAdd == -1) indexToAdd = index; } else if( con[index] == NOTHING ) // .equals okay // open spot. This is where obj will goif it does not appear later indexToAdd = index; else if( con[index].equals( obj ) // == not okay // obj already present. stop looking, won't add looking = false; index = (index + 1) % con.length; } if( indexToAdd != -1 ){ con[indexToAdd] = obj; size++; if( 1.0 * size / con.length >= loadFactorLimit ) resizeAndRehash(); } return indexToAdd != -1; } grading criteria: determine initial index: 2 points determine if obj already present: attempt 1 pt, correct 1 pt find first null or NOTHING to add: attempt 2 pts, correct 3 pts wrap around and linear probe: attempt 1 pt, correct 1 pt check load factor and resize if necessary: attempt 1 pt, correct 1 pt size update: 1 pt return: 1 pt 4A. Suggested Solution public int numLeaves(){ return numLeavesHelper(root); } private int numLeavesHelper(BSTNode n){ if(n == null) return 0; else if (n.getLeft() == null && n.getRight() == null) return 1; else return numLeavesHelper(n.getLeft()) + numLeavesHelper(n.getRight()); } Grading Criteria: traverse all node: attempt 2 pts, correct 2 pts determine leaves that are node: attempt 2 pts, correct 2 pts sum total leaves: 1 pt return: 1 pt 4B. Suugested Solution: // iterartive solution that uses look ahead public void removeMax(){ assert size > 0; size--; if(root.getRight() == null) root = root.getLeft(); else{ BSTNode n = root; // use look ahead while(n.getRight().getRight() != null) n = n.getRight(); n.setRight( n.getRight().getLeft() ); } } // recursive solution public void removeMax(){ assert size > 0; size--; root = removeMaxHelper(root); } private BSTNode removeMaxHelper(BSTNode n) { if(n.getRight() == null) n = n.getLeft(); else n.setRight( removeMaxHelper(n.getRight())); return n; } Grading criteria: decrement size: 1 pt handle case when root changes: 1 pt traverse to max node: attempt 2 pts, correct 2 pts remove node with max: attempt 2 pts, correct 2 pts 5. Suggested Solution: public boolean wordIsPresent(String word){ TNode n = root; int index = 0; while(n != null && index < word.length()){ n = n.getChild(word.charAt(index++)); } return n != null && n.isWord(); } Grading Criteria: