S307 - Final - Spring 2007 1. 2 points each. Big O answers with out the O() are okay. A. 25 B. EMCOTUP / \ C. CMOEUPT -5 39 D. COMPUTE \ E. No 12 F. 935141 / G. O(NlogN) 7 H. Must be able to test if an object is less than, equal to or greater than objects already in the tree. (Or words to that effect.) -1 if appropriate I. 2^16 J. No, can't accept ArrayLists unless specifically parameterize with String; ArrayList is a syntax error. -1 if appropriate (Or words to that effect.) K. O(K) accept O(K-N) L. O(N) M. Instance variable for the number of elements (Or words to that effect.) N. O(logN) O. Unless the new encoding scheme is agreed upon, the key / new encoding scheme must be sent as well and for small files that is a large amount of overhead (Or words to that effect.) 2. Special cases: 1. adding to empty list. 2. adding at position 0 in none empty list. public boolean insert(Object item, int pos){ boolean added = false; // empty list or position == 0? if( pos == 0){ added = true; head = new Node(item, head) } else if(head != null) { Node temp = head; int moved = 0; //try to move to the node before the position to insert at while( moved < (pos -1) && temp.getNext() != null) { temp = temp.getNext(); moved++: } //if able to get to correct spot, insert new node if( moved == (pos - 1) ){ added = true; temp.setNext( new Node(item, temp.getNext() ); } } return added; } Criteria: empty case : 1 pt. pos 0 case : 1 pt general case temp node 2 pt move to correct position attempt : 3 pts move to correct position correct : 3 pts add new node if possible attempt: 2 pts add new node if possible correct: 2 pts return result : 1 pts 3. private int numIncorrectNodes(){ return helper(root); } private int helper(TreeNode n){ if( n == null ) return 0; int numBad = 0; boolean left = true; if( n.getLeft() != null ) left = n.getValue().compareTo( n.getLeft().getValue() ) > 0; boolean right = true; if( n.getRight() != null ) right= n.getValue().compareTo( n.getRight().getValue() ) > 0; if( !(left && right ) numBad++; return numBad + helper(n.getLeft()) + helper(n.getRight());; } helper method : 2 base case : 5 current node check attenpt : 3 current node check correct : 4 recurse left and right check attempt : 2 recurse left and right check correct : 3 return : 1 4. private int numBadLinks(){ int numBad = 0; //get an Iterator for the nodes Iterator nodes = myLinks.keySet().iterator(); while(nodes.hasNext()){ Iterator links = myLinks.get(nodes.next).iterator(); while( links.hasNext() ){ int currentlink = links.next(); if( !myLinks.containsKey(currentLink) ) numBad++ else if(!myLinks.get(currentKey).contains(link.next)) numBad++; // could shorten this to // if( !(myLinks.containsKey(currentLink) && myLinks.get(currentKey).contains(link.next) ) // numBad++; // must do the containsKey first or we'll get a NPE } } } return numBad; } //version to with enhanced for. Much simpler public int numBadLinks2(){ int numBad = 0; //get an Iterator for the nodes for(int currentKey : myLinks.keySet() ){ for(int link : myLinks.get(currentKey) ){ if( !myLinks.containsKey(link) ) numBad++; else if(!myLinks.get(currentKey).contains(link)) numBad++; } } return numBad; } iterate through keys attempt : 3 iterate through keys correct : 3 for a given key iterate through set attempt : 3 for a given key iterate through set correct : 3 for each key check if assoctiated link exists attempt : 3 for each key check if assoctiated link exists correct : 3 track num bad : 1 return num bad : 1 5. // many to 1 answer public boolean isSubset(Stack s, Queue q){ boolean result = true; Stack tempStack = new Stack(); Queue tempQueue = newQueue(); //look for all items. Stop if no more or not a match while( result && !s.isEmpty ){ tempStack.push(s.pop()); //look for tempStack item boolean found = false while( !q.isEmpty() ){ if it matches set found to true and keep it that way if( q.front().equals( tempStack.top() ) ) found = true; tempQueue.enqueue( q.dequeue() ); } assert q.isEmpty; result = found; //restore original queue while(! tempQueue.isEmpty() ) q.enqueue( tempQueue() ); } //restore stack while( !tempStack.isEmpty() ) s.push(temStack.pop() ); return result; } temp stacks and queues 1 pt look for items attempt 3 pts look for particular item attempt 3 pts look for particular item correct 3 pts restore stack 2 pts restore queue 2 pts return result 1 pt none queue / stack methods -3 // 1 to 1 answer public boolean isSubset(Stack s, Queue q){ boolean result = true; Stack tempStack = new Stack(); Queue tempQueue = newQueue(); //make a copy of the original queue int qSize = 0; Queue original = new Queue(); while( !q.isEmpty() ){ original.enqueue( q.dequeue ); size++; } //copy back into q for(int i = 0; i < size; i++){ q.enqueue( original.front() ); original.enqueue( orginal.dequeue() ); } while( result && !s.isEmpty ){ tempStack.push( s.pop() ); //look for tempStack item boolean found = false while( !found && !q.isEmpty() ){ found = q.front().equals( tempStack.top() ); //not matched put back, otherwise throw away so can't use again if( !found ) tempQueue.enqueue( q.dequeue() ); } result = found; //restore original queue while( !q.isEmpty() ) tempQueue.enqueue( q.dequeue() ); while(! tempQueue.isEmpty() ) q.enqueue( tempQueue() ); } //restore stack while( !tempStack.isEmpty() ) s.push(temStack.pop() ); return result; }