Sample Questions for Midterm Exam 2 The following are meant to give you some examples of questions that might be asked on the second midterm exam. The sample exam questions do not represent the length or difficulty of the actual exam. The actual exam will have more questions and cover a broader range of topics than what is presented here. For a list of topics and more information about midterm exam 2, see the Exams page of the course web site. Multiple Choice Choose the one best answer after reading all of the choices. 1) Given the following binary search tree: 20 11 Y X 33 70 Which of the following best represents the possible values for X and Y if duplicates are not allowed? A. X < 11 and 20 < Y < 70 B. 11 < X < 20 and 33 < Y < 70 C. X < 11 and 33 < Y < 70 D. 11 < X < 20 and Y < 20 E. X < 11 and Y > 33 2) You are given an array of size 100 where the value at each index k is k, e.g., A[0] = 0, A[1] = 1, etc. You perform a search for every value in the array, e.g., first you find 0, then 1, etc. The search uses general techniques and not techniques specific for these values. Which of the statements below is true? A. Binary search is not applicable to this problem. B. Sequential search is not applicable to this problem. C. Sequential search is always faster than binary search for every one of the 100 values searched. D. Sequential search is always slower than binary search for every one of the 100 values searched. E. Sequential search is sometimes faster, sometimes slower than binary search depending on which of the 100 values is being searched. Page: 1
The next two questions refer to the following method: public static int compute(int x, int y) { if (x == y) return x; else return compute(x + 1, y - 1); 3) What is returned by the call compute(1, 5)? A. 1 B. 2 C. 3 D. 4 E. No value is returned because an infinite recursion occurs. 4) Which of the following calls leads to an infinite recursion? i. compute (2, 8) ii. compute (8, 2) iii. compute (2, 5) A. i only B. ii only C. iii only D. i and ii E. ii and iii 5) Assume that general trees are implemented using a Treenode class that includes the following fields and methods: private List<Treenode<T>> children; public List<Treenode<T>> getchildren() { return children; and that the following class is used to represent general trees: public class Tree<T> { private Treenode<T> root; // other methods will come here Consider the following methods (assume they are in the Tree class): public int count() { return count(root); private int count(treenode<t> n) { if ((n == null) n.getchildren().isempty()) return 0; int total = 1; Iterator<Treenode<T>> iter = n.getchildren().iterator(); while (iter.hasnext()) total += count(iter.next()); return total; Which of the following best describes what the method count does? A. Always returns 0. B. Returns the number of nodes in the tree. C. Returns the number of leaves in the tree. D. Returns the number of non-leaves in the tree. E. Returns the height of the tree. Page: 2
Written Questions 1) Assume that binary trees are implemented using a BinaryTreenode class that includes the following fields and methods: // fields private T data; private BinaryTreenode<T> left, right; // methods public T getdata() { return data; public BinaryTreenode<T> getleft() { return left; public BinaryTreenode<T> getright() { return right; public void setleft(binarytreenode<t> newl) { left = newl; public void setright(binarytreenode<t> newr) { right = newr; Write the counttwochildren method whose header is given below. The method should return the number of nodes in the tree that have exactly two children. To receive full credit you must use recursion in your solution. You may not add any additional data members or methods to the BinaryTreenode class. public int counttwochildren(binarytreenode<t> N) { Page: 3
2) Given the following binary search tree: 52 33 60 21 57 75 29 64 81 72 Part a) Give the order in which the nodes would be printed using the given traversal. Note: children should be visited from left to right. Pre-order traversal: Post-order traversal: In-order traversal: Level-order traversal: Part b) Show the tree that results from inserting the value 27, deleting the value 33, and deleting the value 60 (in that order). Deletion should use the inorder successor (where appropriate). Page: 4
3) Assume that s is a non-empty string containing only digits. Consider the following recursive implementation to convert the string to the corresponding integer value (e.g., converttoint("1234") returns the integer 1234): public static int converttoint(string s) { // determine the integer value of the last character in s int value = s.charat(s.length() - 1) - '0'; // if s only has one character, return its integer value if (s.length() == 1) return value; // otherwise the integer value of s is // (integer value of s without its last character)*10 // + integer value of the last character in s return converttoint(s.substring(0, s.length() - 1))*10 + value; Analyze the time complexity for the recursive converttoint method above: Part a) Identify the problem size (what affects the method's runtime), N = Part b) Write the recurrence equations for base and recursive cases, T(1) = T(N) = Part c) State your solution that you've guessed by looking for a pattern, and Part d) Verify your solution. Part e) What is the complexity (in big-o notation) of the converttoint method? Page: 5