Computer Science 240 Section 01 Name: KEY EXAM 03 April 04, 2008 This exam has 4 pages, including this cover page. Please make sure you have all 4 pages. You have 55 minutes to complete this exam. You must show your work for full credit. Page Number Possible Points 2 40 3 30 4 30 Total 100 Score Honor Pledge I am familiar with the policy for Academic Integrity as outlined in the Pathfinder: (intranet.juniata.edu/policies/pathfinder/ acadhonesty.html). I have not discussed the contents of this exam before it has been administered to me, and I will not discuss the contents of this exam before it is administered to someone else. I understand that failure to comply with this agreement will constitute cheating and subject me to a charge of academic dishonesty. _______________________________ Signature [1 pt. extra-credit] What is the CS 240 version of cool? “Queue-ll!” _______________________________ Date 1. [4 pts] Consider a Binary Search Tree constructed with all the integers from 1 to 1,000,000 (as the key values), and where these integers are inserted in random order, what would the Inorder traversal of the tree give? An in-order traversal of the BST will give the integers in sorted order, 1 to 1,000,000. 2. Answer the following: [3 pts] Give an example of a Binary Search Tree, with 3 levels, which would be searched in O (log( n)) . 10 5 15 2 7 12 20 [3 pts] Give an example of a Binary Search Tree, with 3 levels, which would be searched in O (n ). 10 5 2 3. Consider the Binary Search Tree at the right, with alphabetic key values. L [3 pts] Label the predecessor and successor of node L. Predeccessor of L = J, Successor of L = M H P [3 pts] Label the predecessor and successor of node P. Predeccessor of P = N, Successor of P = Q D J N [4 pts] Draw the tree resulting when C is inserted into the original tree at the right. D I B M B C [4 pts] Draw the tree resulting when node H is deleted from the original tree. L D P B J [4 pts] Draw the tree resulting when node L is deleted from the original tree. J H P D I [4 pts] Draw the tree resulting when node W is deleted from the original tree. P N S M Q 4. [8 pts] Give the Binary Tree (this tree is NOT a Binary Search Tree [there are NO key values], and it is not necessarily full or complete) whose inorder traversal is 9 3 2 1 4 7 preorder traversal is 1 2 9 3 7 4 1 2 9 7 4 3 S Q W 5. [12 pts] Fill in the blanks to implement an array-based recursive binary search algorithm. protected void recFind( Comparable target, int fromLoc, int toLoc ) { if ( fromLoc > toLoc ) found = false; else { int compareResult; location = ( fromLoc + toLoc ) / 2; compareResult = target.compareTo(list[location]); if ( compareResult == 0 ) found = true; else if (compareResult < 0 ) recFind( target , fromLoc recFind (target, location+1 , location-1 ); toLoc ); else , } } [4 pts] The above recursive method is a protected method in a class. Briefly describe how it would be called from an application class. A non-recursive helper method, probably named find, would be called from the application class. find would then call recFind [3 pts] The above recursive method is a void method. If location contains the index of the found element, how is this information used in the class? location would be an instance variable in the class. 6. Answer the following: [2 pts] True or False Linked-based implementations of Lists use the LLObjectNode class. [2 pts] True or False Array-based implementations of Lists use the LLObjectNode class. [2 pts] True or False A binary search algorithm can be used on an Array-based implementation of a List. [2 pts] True or False A binary search algorithm can be used on a Linked-based implementation of a List. [3 pts] (Circle one) (a) public void add (Object element) (b) public void add (Comparable element) is the method header for the add method in an unsorted linked list. [ 12 pts] 7. The following code segment is a count-controlled loop going from 1 through 5. At each iteration, the loop counter is either printed or inserted into a queue, depending on the boolean result returned by the method random (assume that random randomly returns either true or false). At the end of the loop, the items on the queue are removed and printed. Because of the logical properties of a queue, this code segment cannot print certain sequences of the values of the loop counter. for ( count = 1; count <= 5; count++) { if (random()) System.out.println(count); else queue.enqueue(count); } while (!queue.isEmpty()) { number = queue.dequeue(); System.out.println(number); } Now, consider each of the following output sequences. Which of them could be generated by the above code segment (the output on separate lines, via println, was not shown for space considerations)? (Circle one) True or False 1 3 5 2 4 Is a possible output . (Circle one) True or False 1 3 5 4 2 Is a possible output . (Circle one) True or False 5 4 3 2 1 Is a possible output . 8. [15 pts] Give the output for the follow list-based code: UnsortedListInterface list1 = new ArrayUnsortedList(); list1.add("Wirth"); list1.add("Dykstra"); list1.add("Dahl"); list1.add("Nygaard"); SortedListInterface list2 = new ArraySortedList(); list2.add("Wirth"); list2.add("Dykstra"); list2.add("Dahl"); list2.add("Nygaard"); IndexedListInterface list3 = new ArrayIndexedList(); list3.add(0, "Wirth"); list3.add(0, "Dykstra"); list3.add(2, "Dahl"); list3.add(1, "Nygaard"); System.out.println("Unsorted"); System.out.println(list1); System.out.println("Sorted"); System.out.println(list2); System.out.println("Indexed"); System.out.println(list3); // Point A The output would be two increasing sequences, the first from the counting “up,” the second from the de-queueing. Since these are array based implementations, the insertion for an unsorted list is at the end of the list Wirth Dykstra Dahl Nygaard Dahl Dykstra Nygaard Wirth Dykstra Nygaard Wirth Dahl // Point B // Point C [3 pts] There is a method , that is contained in each of the corresponding List classes (ArrayUnsortedList, ArraySortedList, ArrayIndexedList), which is called by default at Point A, Point B, and Point C. What is it? toString()