Test 3 Review Sheet for Csc227 Fall 2006

Total Page:16

File Type:pdf, Size:1020Kb

Test 3 Review Sheet for Csc227 Fall 2006

Final Exam Review Sheet C Sc 227 Summer 2013

 Wednesday 3-July 1:00 pm to 3:50 pm in our lecture room, GS 906 o Or at the arranged time with your test proctor for students who cannot attend the final in person  The final is not comprehensive, about 9 pages, 20 questions, 200pts  Use the Practice Final for the scope and type of questions. o It is linked under Monday and Tuesday Announcements  It is worth 20% of your final grade  Closed book, closed notes, closed neighbors  No calculators or other electronic devices allowed  Use pencil, not pen (please)  Main topics: The Singly-Linked Structure, Recursion, and Binary Trees  Review session: Tuesday, 2-July 2:30-4:00 pm in 906 Gould Simpson  Material: Chapters 15-19 and JavaCollectionFrameWork.pdf

Topics

The Singly-Linked Structure  We will use the same private inner Node class shown on the practice final inside of class LinkedList> Know how to use compareTo! o It stores these 2 things 1. The parameter E type as a reference to the data. Could match any java reference type, including Integer, Double, Character 2. A reference to another Node of the same type o It will have two constructors, so feel free to use either one (or use both).

private class Node {

private E data; private Node next;

public Node(E objectReference) { data = objectReference; next = null; }

public Node(E objectReference, Node nextReference) { data = objectReference; next = nextReference; } }

o Then you can declare Node objects like this

Node ref2 = new Node(element); // element must be of type E Node ref3 = new Node(element, ref1);

 Add two methods to the same LinkedList class shown on the practice final  Algorithms with linked structures might include o traverse the linked structure o find an element at a given index o find an element using compareTo o remove elements from a linked structure o insert elements into a linked structure o insert an element into a linked structure to maintain a natural ordering list.get(0) <= list.get(1) < list.get(2),..., list.get(n-2) <= list.get(n-1)  Code Demo: Make addLast(E) run O(1) with an addition

Recursion  Show output from recursive methods  Determine return values from recursive methods  Write recursive methods with integers, Strings, and arrays.  Code Demo: Change two methods on CodingBat from an iterative to a recursive solution o From String-2 http://codingbat.com/prob/p109637 . Iterative and Recursive solutions are at end of this document o From Array-2 http://codingbat.com/prob/p105771 . Iterative and Recursive solutions are at end of this document

Trees  Tree Terminology: root, leaf, internal node, level, parent, child  Binary Trees o Tree traversals: in-, pre-, and post-order o The ordering property of Binary Search Trees o Tree algorithms in Java, both iterative and recursive o Add two methods to OrderedSet . Like the two on the practice final or those from OrderedSet  You will not be asked to remove elements!  Code Demo: isFull() ANSWER AT END OF DOC

ArrayList and HashMap usage and TreeMap  Topics are not in the book, see beginning of presentation JavaCollectionFrameWork.pdf  Some of the methods from interface java.util.List o boolean add(E) E get(int) in size() boolean contains(E) boolean remove(E)  Some of the methods from interface java.util.Map o V put(K V) V get(K) boolean containsKey(K) o keyset() values() keys in order for TreeSet  Polymorphic methods from class Collections o sort binarySearch max min  Code Demo: setupMap for probabilistic text generation ANSWER AT END OF DOC Not on the Final Hash table implementations or collision resolution, just know 5 methods

Code Demo Solutions from today http://codingbat.com/prob/p109637 Iterative Recursive public String repeatSeparator(String word, public String repeatSeparator(String word, String sep, String sep, int count) { int count) { if(count == 0) if(count == 0) return ""; return ""; else { else if(count == 1) String result = ""; return word; for(int c = 1; c < count; c++ ) { else result = result + word + sep; return word + sep + repeatSeparator(word, sep, } count - 1); return result + word; } } }

http://codingbat.com/prob/p105771 Recursive Iterative public int[] evenOdd(int[] nums) { public int[] evenOdd(int[] nums) { evensLeft(nums, 0, -1); int spot = -1; return nums; for(int i = 0; i < nums.length; i++) { } if(nums[i] % 2 == 0) { spot++; private void evensLeft(int[] a, int temp = nums[spot]; int index, nums[spot] = nums[i]; int insertionSpot) { nums[i] = temp; if (index == a.length) } return; } else if (a[index] % 2 != 0) return nums; evensLeft(a, index + 1, insertionSpot); } else { // pull the even int to insertSpot insertionSpot++; int temp = a[insertionSpot]; a[insertionSpot] = a[index]; a[index] = temp; evensLeft(a, index + 1, insertionSpot); } }

public boolean isFull() { return isFull(root); }

private boolean isFull(TreeNode t) { if(t == null) return true; else if(t.left == null && t.right != null) return false; else if(t.left != null && t.right == null) return false; else return isFull(t.left) && /* AND */ isFull(t.right);

} private void setMap() { allnGrams = new HashMap>(); for (int i = 0; i < theText.length() - nGram.length() - 1; i++) { String key = theText.substring(i, i + nGram.length()); if(! allnGrams.containsKey(key)) { ArrayList value = new ArrayList(); // Dolok=[h, h, h] // olokh=[o, o, o] value.add(theText.charAt(i + nGram.length())); allnGrams.put(key, value); } else { allnGrams.get(key).add(theText.charAt(i + nGram.length())); } }

Recommended publications