CS 201, Spring 2007 — April 30th — Midterm 2. Name: Question 1. [8 points]
Implement the following method. It takes an integer parameter n and recursively ...
CS 201, Spring 2007 — April 30th — Midterm 2
Name:
Question 1. [8 points] Implement the following method. It takes an integer parameter n and recursively computes the number of even digits in n’s decimal representation. For example, when called with the argument value 29564, the method should return 3, because there are three even digits. Note: treat the number 0 as having no digits, meaning that the method will return 0 when called with n equal to 0. public static int numEvenDigits(int n) {
} Question 2. [8 points] Consider a hypothetical variant of Quick Sort which, in each recursive call (excluding base cases), chooses two pivot values, p1 and p2 , to create three partitions:
All elements in the left partition are less than p1 , all elements in the middle partition are greater than p1 and less than p2 , and all elements in the right partition are greater than p2 . Each partition is sorted recursively. Assuming partitioning is an O(N ) operation for N elements, what are the average-case and worst-case big-O running times of this algorithm? Explain briefly.
Question 3. [16 points] Implement a generic dictionary class that uses a singly-linked list as its underlying data structure. The type parameters K and V should represent the key type and the value type, respectively. The dictionary class should support the following methods: • public V find(K key): Return the value associated with the given key. Return null if the key does not exist in the dictionary. • public boolean add(K key, V val): Add a key/value pair to the dictionary. Return true if the pair is added successfully, or false if the key already exists in the dictionary. You will need to define two classes: one class to represent the singly-linked list nodes and the other to represent the entire dictionary. (Use the other side of the page if needed.)
Question 4. [5 points] What is the worst-case big-O running time of the add method of the dictionary class from the previous question, where N is the number of keys in the dictionary? Explain briefly.
Question 5. [10 points] Sylvester’s sequence S0 , S1 , S2 , . . . is defined by the following recurrence: S0 = 2 Sn = [Sn−1 · (Sn−1 − 1)] + 1
for all n ≥ 1
Write a static method that takes a parameter n whose type is long and returns a long value equal to the nth Sylvester number Sn . Use dynamic programming to avoid recomputing previously computed subproblems. (Use a helper method if needed.)
Questions 6–9 use the following generic class and static methods: public class BSTNode { public K key; public V value; public BSTNode left; public BSTNode right; }
public static boolean allKeysLessThan( BSTNode node, K key, Comparator cmp) { ... } public static boolean allKeysGreaterThan( BSTNode node, K key, Comparator cmp) { ... }
The methods on the right return true if all nodes in the search tree whose root is given have keys that are less than (allKeysLessThan) or greater than (allKeysGreaterThan) the key given as the parameter key. Question 6. [15 points] Complete the following method. It should return true if every node in the binary tree whose root node is given has the binary search tree property, or false if any node in the tree violates the binary search tree property. Use the given Comparator object to compare key values. (Hint: use recursion, and use the allKeysLessThan/allKeysGreaterThan methods.) public static boolean isBST(BSTNode node, Comparator cmp) {
Question 7. [4 points] Complete the following method. It should return true if the BSTNode given as the parameter has exactly one child, and return false otherwise. public static boolean exactlyOneChild(BSTNode node) {
} Question 8. [10 points] Complete the following method. It should print to System.out the values of each key in the binary search tree whose root node is given, in sorted order. (Hint: in-order traversal.) public static void printKeysSorted(BSTNode node) {
}
Question 9. [14 points] Write a generic method that takes a BSTNode and a key comparator as parameters and returns true if the tree rooted at that node is an AVL tree, false if it is not an AVL tree. Hints: • An AVL tree is a binary search tree where every node has the AVL property • You can call the isBST method you wrote in question 6 • Define helper methods as needed
Question 10. [10 points] Consider the following binary tree. (Assume the number inside each node is a search key.)
Explain why the tree is not an AVL tree. What operation is needed to fix the tree so that it becomes an AVL tree? Draw the resulting tree.
Bonus Question. [5 points] A perfect binary tree is one where every non-leaf node has two children, and all leaf nodes are at the bottom-most level of the tree. For example:
Use induction to prove that a perfect binary tree of height h has 2h leaves.