Spring10_Final_SuggesteSolution

Spring10_Final_SuggesteSolution - CS307 Fall 2009 Final...

This preview shows pages 1–3. Sign up to view the full content.

CS307 Fall 2009 Final Exam Suggested Solution and Criteria: 1 1.5 Each, differences in capitalization and spacing okay. On Big O no O() okay. A: 37 / \ 13 42 / \ -12 101 B: 17, 19, 25, 10, 20, 30, 7, 15 C: 19, 10, 25, 17, 20, 7, 30, 15 D: 10, 25, 19, 7, 15, 30, 20, 17 E: No. F: valid binary search tree using values between 1 and 10. No repeats. G: 130 H: ANI I: O(N^2) J: O(N) K: O(N) L: O(N^2) M: O(NlogN) N: O(N^2) O: O(N^2) P: Answers in range 30 - 50 inclusive are correct. Q: 9 11 R: 6 S: HashSet Add, access, and remove are average case O(1). With TreeSet they are average case O(logN). (-1 if HashSet is answer, but bad explanation.) T: Variables must have a box and arrow.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2. Suggested Solution: Using look ahead public boolean removeFirst(E obj) { int oldSize = size; if(first != null) { // special case, first contains obj if(obj.equals(first.getValue()){ first = first.getNext(); size--; } else { // general case Node<E> temp = first; while(temp.getNext() != null temp = temp.getNext(); if(temp.getNext() != null){ // found it! temp.setNext(temp.getNext().getNext()) size--; } } } return oldSize != size; } Using trailer public boolean removeFirst(E obj) { int oldSize = size; if(first != null) { // special case, first contains obj if(obj.equals(first.getValue()){ first = first.getNext(); size--;
This is the end of the preview. Sign up to access the rest of the document.

Spring10_Final_SuggesteSolution - CS307 Fall 2009 Final...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online