C Sc 227 Practice Final Section Leader ________ Name ___________________________ 125 pts 0. Upon which day is our final exam? __Wed_____ (4pts) This tension reducer will be easier to answer on the final 1. Use the following binary tree to write out each of the three traversals indicated below. (15pts) Preorder traversal _____1__2__4__3__5__6__________________________ Inorder traversal _____2__4__1__5__3__6__________________________ Postorder traversal _____4__2__5__6__3__1__________________________ 2. Is this a Binary Search Tree? (yes or no)? __NO___ (3pts) 3. Is this a Binary Search Tree? (yes or no)? _NO__ (3pts) 4. What potential benefit is there to using a binary search tree to store a set of elements rather than a sorted array to store the same collection of elements? (3pts) Better runtime for insert and remove: O(log n) rather than O(n) 5. What is required of of the value's type to be stored in a binary search tree? (3pts) The type must implement Comparable or some interface that extends the Comparable interface so it has compareTo 6. Draw a picture of the tree that results from the following code on your OrderedSet class. Be sure to have the instance variable root reference the root node using an arrow. (8pts) OrderedSet<String> set = new OrderedSet<String>(); root ----> Inception set.insert("Inception"); / \ set.insert("Shaft"); Bean Shaft set.insert("Bean"); / \ set.insert("Scream"); set.insert("Titanic"); Scream Titanic 1 7. Complete method maxKey in OrderedSet that returns a reference to the key with the maximum value. Write two different implementations of a private helper method. One method must have a recursive solution. The other method will have an iterative solution. These assertions should pass: @Test public void testMaxKey() { OrderedSet<Integer> intTree = new OrderedSet<Integer>(); assertNull(intTree.maxKey()); intTree.insert(5); assertEquals(new Integer(5), intTree.maxKey()); intTree.insert(8); assertEquals(new Integer(8), intTree.maxKey()); intTree.insert(-3); assertEquals(new Integer(8), intTree.maxKey()); } /////////////////////////////////////////////////////////// public class OrderedSet<E extends Comparable<E>> { private class TreeNode { private TreeNode left; private TreeNode right; private E data; public TreeNode(E element) { data = element; left = null; right = null; } } // end class TreeNode private TreeNode root; // Create an empty tree public OrderedSet() { root = null; } a) Write the public and private methods using a recursive solution. public E maxKey() { if (root == null) return null; else return maxKey(root); } private K maxKey(TreeNode t) { if (t.right == null) return t.key; else return maxKey(t.right); } 2 (8pts) b) Write the complete public method with an iterative solution. (8pts) public E maxKey() { if (root == null) return null; else { TreeNode t = root while(t.right != null) t = t.right; return t.data; } } 8. Complete the output that would be generated by this code. (8pts) System.out.print(list.get(0i) + " "); // 0 should have been i. not being tricky -5 -5 12 23 === true false -5 23 9. Complete combine so it returns a List that only has any and all elements that are contained in both List<Integer> arguments. The following assertions must pass. (12pts) List<Integer> ts1 = new ArrayList<Integer>(); ts1.add(2); ts1.add(4); ts1.add(2); ts1.add(4); List<Integer> ts2 = new LinkedList<Integer>(); ts2.add(2); ts2.add(2); ts2.add(4); ts2.add(6); ts2.add(4); // combine([2, 4, 2, 4], [2, 2, 4, 6, 4]) should return [2, 4] or [4, 2] List<Integer> result = combine(ts1, ts2); assertEquals(2, result.size()); assertTrue(result.contains(2)); assertTrue(result.contains(4)); public List<Integer> combine (List<Integer> s1, List<Integer> s2) { List<Integer> result = new ArrayList<Integer>(); for (int i = 0; i < s1.size(); i++) { Integer current = s1.get(i); if (s2.contains(current) && !result.contains(current)) result.add(current); } return result; } 10. To our OrderedSet class shown earlier, add method valuesGreaterThan(E element) to return a List<E> 3 of all elements in the OrderedSet object that are greater than element. (12pts) OrderedSet<Integer> om = new OrderedSet<Integer>(); om.insert(50); om.insert(60); om.insert(45); om.insert(55); List<Integer> list = om.valuesGreaterThan(50); assertEquals(2, list.size()); assertTrue(list.contains(55)); assertTrue(list.contains(60)); assertFalse(list.contains(45)); assertFalse(list.contains(50)); public List<E> valuesGreaterThan(E element) { List<E> list = new ArrayList<E>(); gatherAllGreaterThan(element, root, list); return list; } private void gatherAllGreaterThan(E element, TreeNode t, List<E> list) { if (t != null) { if (t.data.compareTo(element) > 0) list.add(t.data); gatherAllGreaterThan(element, t.left, list); gatherAllGreaterThan(element, t.right, list); } } 11. To our OrderedSet class, add method isFull() that returns true if all nodes are leaves or have exactly two children. If any node has precisely one child, return false. An empty tree is full. (12pts) public boolean isFull() { public boolean isFull() { return isFull(root); } private boolean isFull(TreeNode t) { if (t == null) return true; else if (t.right == null && t.left != null) return false; else if (t.left == null && t.right != null) return false; else return isFull(t.left) && isFull(t.right); } 12. To our OrderedSet class, add method reverse() that changes the ordering property of such that an in order traversal would visit all elements in descending order. Empty tree and trees with only one node will not change. Trees with two or more nodes will change. (12pts) root \ M becomes root \ M 4 / G root \ 99 / \ 12 555 root \ M / \ G S / \ P V \ G becomes becomes root \ 99 / \ 555 12 root \ M / \ S G / \ V P public void reverse() { public void reverse() { mirror(root); } private void mirror(TreeNode t) { if (t != null) { TreeNode temp = t.right; t.right = t.left; t.left = temp; mirror(t.left); mirror(t.right); } } 13. Complete the output that would be generated by this code. (9pts) HashMap<Integer, String> map = System.out.println(map.put(50, System.out.println(map.put(25, System.out.println(map.put(50, System.out.println(map.put(75, new HashMap<Integer, String>(); "M")); "B")); "C")); "D")); System.out.println(map.size()); 5 System.out.println(map.get(25)); System.out.println(map.get(50)); System.out.println(map.get(75)); System.out.println(map.get(99)); null null M null 3 B C D null 14. Write the output generated by the following program when the input file "short.txt" has the one line. (6 pts) Spain rain in Spain in Spain public class A { public static void main(String[] args) { Scanner scanner = null; try { scanner = new Scanner(new File("short.txt")); } catch (FileNotFoundException e) { } TreeMap<String, Integer> mystery = new TreeMap<String, Integer>(); while (scanner.hasNext()) { String word = scanner.next(); if (mystery.containsKey(word)) { Integer current = mystery.get(word); current++; mystery.put(word, current); } else { mystery.put(word, 1); } } Collection<String> coll = mystery.keySet(); for(String str : coll) System.out.println(str + " " + mystery.get(str)); } } Spain 3 in 2 rain 1 6