Computing Science 115 Final Examination April 23, 2002 Section: B2 BASU Last Name: First Name: Please put student id on last page Instructions: The time for this test is 3 hrs. No references or calculators are allowed. Write all answers in this booklet and do not hand in any other work. The mark total for this exam is 100. Only one answer per question is allowed.
#1 (A) [10 marks ] Consider four sorts: SelectionSort, InsertionSort, MergeSort, and QuickSort(standard version with left element as pivot). For each of the following situations, pick ONE of these four sorts that you think would be best for the situation, assuming that you want the fastest sort possible that will work. Then say why it is best in 20 words or less, and give the exact number of comparisons in the worst case where required. Do not simply say that it is the best because it is the fastest. Indicate why it is the fastest. Assume that you have as much memory as necessary to use any of the sorts, except in those situations where memory availability is specifically mentioned. a)start with a Vector of Strings that has 1,000,000 elements and is already sorted in ascending order. Add 5 more Strings to the end of the Vector. Now sort the elements in ascending order. Which Sort? Why? Exact Number of comparisons: b)start with one Vector of Strings whose 200,000 elements are already sorted in ascending order and a second Vector of Strings whose 200,000 elements are also sorted in ascending order. Start with a third Vector that is empty. Add all elements from the first vector to the third Vector in order. Then append all the elements from the second Vector to the third vector in order. Now sort the elements of the third vector in ascending order. Which Sort? Why? c)start with one Vector of Strings whose 10,000 elements are already sorted in descending order. Now sort the elements of the vector in ascending order. Which Sort? Why? d)start with one Vector of Persons whose N elements, N > 100,000 are in random order. Assume that you have enough memory to store this vector, plus enough memory for as many stack frames as any of the sorts would require. However, assume that you do not have enough memory to store more than 1,000 extra Person elements. Now sort the elements of the vector in ascending order. Which Sort? Why? Number of comparisons in O-notation: #1 (B) [10 marks ] Consider the four search scenarios listed below (a, b, c, and d). For each of the scenarios, circle the word binary if it is possible to do a binary search for that scenario or circle sequential if a binary search is not possible. Then, use a binary search if possible and a sequential search if a binary one is not possible. Count the number of element comparisons that will be performed during the search and write this number in the space provided. a)searching for a String in an unsorted Vector of 512 Strings and not finding it. b)searching for a String in a sorted Vector of 256 Strings and finding it at the last location searched.
c)searching for a String in an unsorted DoublyLinkedList of 1,024 Strings and not finding it. d)searching for a String located at position 64 of a sorted SinglyLinkedList of 1,024 Strings. e)searching for an Integer in a sorted SinglyLinkedList of 2,048 Integers and not finding it. Assume the List contains the Integers: 1, 3, 5, and assume you are looking for the Integer 512. #2 (A) [8 marks ] Consider the following program. What is the output? Ignore any syntax errors that may be in the program. import structure.*; public class StacksAndQueues { public static void main(string args[]) { /* Program statements go here. */ String[] mystrings = { "fred", Wilma, "barney", "pebbles"; Stack stack1; Stack stack2; Queue myqueue; int index; Object element; stack1 = new StackList(); stack2 = new StackList(); myqueue = new QueueVector(); for (index = 0; index < mystrings.length; index++) stack1.add(mystrings[index]); System.out.println(stack1); for (index = 0; index < mystrings.length / 4; index++) { element = stack1.remove(); myqueue.add(element); stack2.add(element); System.out.println("Stack 1: " + stack1); while (!stack1.isempty()) { element = stack1.remove(); myqueue.add(element); element = myqueue.remove(); stack2.push(element); System.out.println("Stack 2: " + stack2); System.out.println("Queue: " + myqueue); while (!myqueue.isempty()) { element = myqueue.remove(); stack2.add(element); System.out.println("Stack 2: " + stack2); OUTPUT:
#2 (B) [12 marks] Consider the following problems; clearly justify in the space provided why you will use a Stack or a Queue in each case. (Your choice should be based on lowering cost where both data structures can be used. No marks for guessing without proper justification.) a) You start at an initial position S in a maze. Then mark all positions that can be reached in 1 step from S by 1,, all positions that be reached with a minimum of K steps from S are marked by K, and so on, until all positions are marked. Circle choice: Stack Queue Justification: b) Starting from the root of a tree find the shortest path to a leaf; where the length of a path equals the number of nodes (ignoring the root itself) from the root to a leaf. Circle choice: Stack Queue Justification: c) Starting for the root of a tree, find a leaf in the tree. Circle choice: Stack Queue Justification: #3 [30 marks] Recall the code segments for defining a binary tree node. public class BinaryTreeNode { protected Object data; protected BinaryTreeNode parent; protected BinaryTreeNode left; protected BinaryTreeNode right; Consider adding the functionality of keeping track the height of a binarytreenode in the node itsel (a) [3] Write the 1 line definition code to define nodeheight to keep track of height of a node. (b) [3] Write the code to initialize nodeheight when a new node is inserted at a leaf.
(c) [8] Write the MOST EFFICIENT code to modify nodeheight at certain intermediate nodes when a new element is inserted at a leaf. (d) [6] Given that H is the height of a binary tree: What is worst case cost of finding the height of a node in an usual binary tree (discussed in class)? What is the worst case cost of updating nodeheight of relevant intermediate nodes in (c)? What is the worst case cost of finding the height of a node in the modified binary tree described in this question? [Express your answer as a constant x (a function of H). Specifically, I need the function of H.] Consider the following code: public static boolean isfull(binarytreenode anode) { //post: returns true iff subtree rooted at the node full. // full iff size == 2^(h+1) - 1 int size; int height; int value; int i; if (anode == null) return true; size = size(anode); height = height(anode); value = 2; for (i = 1; i < height + 1; i++) value = 2 * value; value--; return size == value;
(e) [6] Given that H is the height of the binarytreenode anode: What is worst case cost of the method isfull? In 2-3 lines clearly describe how you can significantly reduce the cost of checking if a subtree is full, following parts (a) to (c). What is the worst case cost of checking if a subtree is full following the 2-3 line description you propose? [Again, express your answer as a constant x (a function of H). Specifically, I need the function of H. Full marks only for most efficient solution.] (f) [2] Write the code segment to modify the BinaryTreeNode for efficient checking if a subtree is full. (g) [2] Write the code segment to efficiently test for isfull given the modified data structure in (a) & (f).
4. (a) [7] Consider a hash function that adds the alphabetical order (1 26) of the first and the last letters of a string and computes the remainder of the sum divided by 7. Show how the strings dog, good god, gold, bold, bald, bad will be stored in the hashtable assuming string are inserted from left to right. (b) [6] Define a new hash function that avoids most of the collisions in part (a) by taking all characters & their relative positions in a string into account, and increases the size of the hash table to 14. (c) [7] Enter the strings in part (a), in left to right order, into the hash table below using the improved hash function in part (b). [No marks for this part if hash function in part (b) does not satisfy given requirements.]
5. [10] Consider the Binary Search Tree (BST) above: (a) List the elements in preorder traversal. (b) List the elements in inorder traversal. (c) List the elements in postorder traversal. (d) How many comparisons are required to find 20 in the BST above? (e) Draw the new BST after 20 is deleted from the tree. Student #: #1 #2 #3 #4 #5 Total /20 /20 /30 /20 /10 /100