NAME: EE 322C Fall 2006 (Chase) Exam 2: READ THIS FIRST. Please use the back side of each page for scratch paper. For some of the questions you may need to think quite a bit before you write down an answer. If you write while you think, use the back side of the page and copy your final (neatly written) answer to the front side. I will not give credit for illegible answers (and I reserve sole judgment for what is “legible”). The exam is out of 100 points, not all questions are equally hard or take equally long to complete. Some hard questions are not worth very much. Some easy questions are worth a lot. Good luck! 1. (14 pts) Assuming the array x[] is sorted (smallest values first, largest values last), what is the final height of the binary search trees for each of the following. In each case, we start out with “tree” being an empty binary search tree, and the makeTree function is called as follows: “makeTree(tree, x, 0, N-1);” Please assume that N is a power of 2. a. (6 pts) What’s the height when makeTreeA is used? ________________ void makeTreeA(BST tree, Comparable[] x, int start, int last) { for (int k = start; k < last; k += 1) { tree.insert(x[k]); } } b. (4 pts) What’s the height when makeTreeB is used? ________________ void makeTreeB(BST tree, Comparable[] x, int start, int last) { if (start == last) { return; } int mid = (start + last) / 2; tree.insert(x[mid]); makeTreeB(tree, x, start, mid); makeTreeB(tree, x, mid+1, last); } c. (4 pts) What’s the height when makeTreeC is used? ________________ void makeTreeC(BST tree, Comparable[] x, int start, int last) { if (start == last) { return; } int mid = (start + last) / 2; makeTreeC(tree, x, start, mid); tree.insert(x[mid]); makeTreeC(tree, x, mid+1, last); } 2. (17 pts 3 pts for each individual diagram, 5 pts for using the overall approach to this problem) I’ve drawn several binary search trees with most of the values removed. For each of the trees, put an “X” inside every node that could have contained the value “42”. Keep in mind that the original binary search tree (before the values were removed) contained no duplicate values. 41 41 41 43 3. (15 pts) Assume that r points to the root of a binary search tree. The tree may (or may not) be a proper red/black tree. Every red node in the tree will have the “isRed” data member equal to true, and every black node in the tree will have isRed equal to false. Complete the redRed function below so that it returns true if there exists at least one red node in the tree that has a red parent. (obviously, the function should return false, only if every red node has a black parent). You may assume that the root of the tree is black. A solution longer than ten statements will not be graded. class RBNode { // shown for reference… note, there is no parent pointer for this problem RBNode left; RBNode right; Comparable key; boolean isRed; } boolean redRed(RBNode r) { 4. (18 pts) True/false, multiple guess, and really-short answer questions. a. (true or false): The time complexity to insert into a hash table is proportional to the load factor. b. Given two distinct objects, x and y, and assuming that the programmer who implemented the objects is a reasonable person, which of the following are reasonable conclusions i. If I’m told that x.equals(y) is true, then I can safely assume that x.hashCode() == y.hashCode() ii. If I’m told that x.hashCode() == y.hashCode(), then I can safely assume that x.equals(y) is true iii. If x and y are truly distinct, I can safely assume that x.hashCode() is not equal to y.hashCode() iv. All of the above are reasonable v. None of the above are reasonable c. (true or false): Quadratic probing is widely recognized as the best method for resolving collisions in a hash table. d. What is the worst-case time complexity for an in-order traversal of a binary search tree with N keys in the tree? e. What is the worst-case time complexity for an in-order traversal of a redblack tree with N keys in the tree? f. (true or false): Hash functions, such as the “hashCode()” function in Java, should return irrational numbers. 5. (21 pts) I have two sets, S1 and S2. S1 has N1 elements and S2 has N2 elements. We know that N1 ≤ N2. I want to count the number of elements in the intersection of S1 and S2 (let me make this clear, I don’t want to store the intersection anywhere, I just want to know how many elements in S1 are also in S2). I’ve got four reasonable possibilities, and I want you to help me analyze them. The first possibility is to use the SortedSet that you build in Project1. As I’m sure that you recall, if S1 and S2 are both sorted sets, then the intersection can be computed with worst case time complexity of O(N1 + N2). Since we know that N1 ≤ N2, then the worst case time complexity for option 1 is O(N2). Tell me the worst case time complexity for computing the intersection of S1 and S2 in each of the following three possibilities. Please do not include the time complexity for creating S1 and/or S2. Assume the sets are already created using the data structures described below. Just tell me the time complexity (worst case) for computing │S1 ∩ S2 │ under the assumptions given below. a. Posibility 2. The sets are implemented using hash tables (i.e., HashSets), and I also am fortunate enough to get simple-uniform-hashing. b. Possiblity 3. The sets are implemented using hash tables (i.e., HashSets), but I have no reason to expect simple-uniform-hashing. c. Possiblity 4. The sets are implemented using red-black-trees (i.e., TreeSets). 6. (15 pts) Write a function that removes the largest value from a binary search tree. Assume the tree is built from the following node class. class Node { Comparable x; Node left; Node right; } void removeLargest(Node root) {