Fall 2002 Final - Key 1A. 195 / \ 156 / \ 20 162 / \ 68 \ 150 B. -42 -5 32 -22 81 135 138 161 C. -5 32 -42 135 81 138 -22 161 D. 32 -5 135 138 82 161 -22 -42 E. No F. O(N) G. O(N^2) H. O(N * logN) I. O(N), As N gets large resizing a lot J. O(1), Resize very infrequently K. O(N^2 * logN) L. 8.5N + 7 or 5/2 * N + 1 M. O(N) N. 48 sec O. 1280 sec P. O(N), must track top or bottom fixed Q. R. some mention of accessing arrays S. O(N), N = number of elements in Set T. 40 2. Clean recursive solution n = helper(n); // note if n is a leaf this won't actually work. Method // would have to return a BTNode to root public BTNode helper(BTNode n) { // n = null -> Base Case, no work if( n != null) { // other base case, n is a leaf if(n.getLeft() == null && n.getRight() == null) n = null; else { // recursive step n.setLeft(helper(n.getLeft()); n.setRight(helper(n.getRight()); } } return n; } Not so clean recursive solution { // base case. check to see if n is a leaf if( n != null ) { if(n.getLeft() == null && n.getRight() == null) n = null; else { // check to see if left node exists and is leaf if( n.getLeft() != null && n.getLeft().getLeft() == null && n.getLeft.getRight() == null) // left child exists and is leaf n.setLeft(null); else removeLeaves(n.getLeft()); // check to see i right node exists and is leaf if( n.getRight() != null && n.getRight().getLeft() == null && n.getRight.getRight() == null) // left child exists and is leaf n.setRight(null); else removeLeaves(n.getRight()); } } } 3. { // container full? if( iMySize == myCon.length ) { Object temp[] = new Object[ iMySize * 2 ]; int pos = iMyFront; for(int i = 0; i < iMySize; i++) { temp[i] = myCon[pos]; pos = (pos + 1)%iMySize; } myCon = temp; } // okay to assume iMyBack and iMyFront adjusted correctly for // empty list. If not would nee following code if( iMySize == 0 ) { myCon[0] = item; iMyFront = iMyBack = 0; } else { iMyFront--; if(iMyFront == - 1) { iMyFront = myCon.length - 1; myCon[iMyFront] = item; } } iMySize++; } 4. { int parts = 0; Queue toCheck = new Queue(); ArrayList used = new ArrayList(); ArrayList inCurrent = new ArrayList(); int numNodes = g.numNodes(); Integer temp1, temp2; for(int currentNode = 1; currentNode <= numNodes; currentNode++) { temp1 = new Integer(currentNode); if( !used.contains(temp) ) { parts++; used.add(temp); toCheck.enqueue(temp); while( toCheck.size() != 0 ) { temp1 = toCheck.dequeue(); for(int otherNode = 1; otherNodes <= numNodes; otherNode++) { temp2 = new Integer(otherNode); if( g.adjacent(temp1.intValue(), otherNode) && !used.contains(temp2) ) { used.add(temp2); toCheck.enqueue(temp2); } } } } } return parts; }