CS 315 Data Structures Spring 2012 Final examination Total Points: 80 Name This is an open-book/open-notes exam. Write the answers in the space provided. Answer for a total of 80 points, including at least THREE of the last FOUR questions (17 to 20). 1) The best expression for the worst-case number of operations involved in inserting a key into a binary search tree of size n: 2 points (a) O(1) (b) O(log n) (c) O(n) (d) O(n log n) (e) O(n 2 ) 2) Which of the following operations can be performed efficiently in a binary heap in the worst-case? (Efficient means O(log n) or faster.) 3 points 1. find-min 2. delete-min 3. delete(x) for a given key x 4. insert(x) for a given key x Choose the correct answer: (a) all except (4) (b) all except (3) (c) all except (1) (d) all except (2) (e) all of them 3) Sum of the series 1 + 3 + 5 + 7 +... added to N terms is 66049. What is the value of N? 5 points
4) Which of the following are requirements of a good hash function? 3 points (a) it should be easy to compute. (b) it should use all the digits of the input. (c) it should allow the searching for a key to be done in O(log n) in the worst-case. (A) (a) only (B) (a) and (b) (C) (a) and (c) (D) (c) only (E) all of them. 5) What is F(100) where F is defined by the following recursive function? 3 points int F(int x) { if (x == 1) return 1; else return 2*F(x-1); } 6) Which of the following operations can be performed efficiently (i.e., in time O(log n) or constant time) in an AVL tree in the worst-case? 3 points (a) insert (b) deletemin (c) successor (d) findmin Circle the correct answer. (1) (a) and (b) (2) all of them (3) all except (c) (4) (a) and (d) (5) none of them 7) How many AVL trees are there with keys 1, 2 and 3? 2 points 8) The reason for doubling a hash table size when the load factor becomes ~ 0.5 is: 3 points (a) The hash function h becomes unusable when > 0.5 (b) the cost of search becomes high when > 0.5 (c) lazy deletion is not possible for > 0.5 Circle the correct answer: (A) (a) only (B) (b) only (C) (a) and (b) (D) (b) and (c) (E) none of the above.
9) State two reasons for choosing the hash-table size to be a prime number. 3 points 10) Name the two sorting algorithms with worst-case time complexity O(n log n). 3 points 11) Consider the partitioning step of Quicksort using the key 9 as pivot. What will be the contents of A after the partition step? The algorithm to be used is the first partition algorithm, not the one that uses median-of-three partition. 5 points Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 A 3 12 7 4 10 6 13 19 5 4 9 3 10 1 left = 4, right = 10 ANSWER: Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 A 12) What is the exact number of key comparisons performed by insertion sorting on the following input? 4 points 12 3 5 8 7 9 4 1 Answer: 13) The reason quick sort switches to insertion sort for small input size are the following: a. Otherwise, the output will be wrong b. Otherwise the pivot will be chosen incorrectly. c. Insertion sort is faster than quick sort when the array size is small.
Choose the correct answer: 3 points (1) (a) and (b) (2) all of them (3) all except (c) (4) (c) only (5) none of them 14) Show the result of inserting the key 64 into the AVL tree shown: 5 points
15) Exhibit the resulting heap if DELETEMIN operation is performed on the heap below: (Assume the algorithm presented in class is used to perform DELETEMIN.) Show the current heap as well as the resulting heap as tree. 5 points index 1 2 3 4 5 6 7 8 9 10 11 12 Heap-array 5 8 6 11 14 7 9 11 14 18 15 8 16) Which of the following operations can be performed efficiently (i.e., in time O(log n) or constant time) on a sorted array A? n denotes the size of the array. 3 points 1) Search(A,x) 2) findmin(a) 3) delete(a,x) Circle the correct answer: (a) 1 and 3 (b) 1 and 2 (c) 2 and 3 (d) all of them (e) 1 only
17) Consider a balanced parenthesis string such as ((a(bc)a)d) that contains alphabetical symbols in addition to parentheses. Write a function calculate_depth that takes such an input string and computes the depth of each alphabetical symbol. For example, the output for the input string x above should be the vector [2 3 3 2 1]. 8 points vector<int> calculate_depth(string x)
18) Consider the following augmented binary search tree in which each node has an extra field that stores the size of the subtree rooted at that node. An example of such a tree is shown below. The first key in the node is the usual key, and the second one is the size. Rewrite the function delete to delete a key from an augmented binary search tree. (Your algorithm should have time complexity O(h)). Assume the fields of a BinaryNode are Left, Right, Key and Size.) 12 points This function returns a pointer to the deleted node. BinaryNode* delete(const Comparable & x, BinaryNode * & t) // delete x from the tree rooted at t
18) Write a function test_heap that takes an array A of size n as input, and outputs k such that k is the largest integer such that the first k keys form a min-heap. For example, if the input array is [8, 11, 19, 20, 9, 23], the output should be 4 since A[1..4] forms a min-heap, but A[1:5] is not a minheap. Obtain the worst-case time complexity of your algorithm in terms of n. 8 points int test_heap(int[] A, int n)
19) Write a function level_print that prints all the nodes of a binary tree in the following order: level 0 (root), level 1 (children of level 0) from left to right, then level 2 (children of level 1) from left to right etc. Thus for the tree shown below, level_print will output the sequence: 15 10 30 5 20 40 4 7. Assume the usual tree representation with each node having a key and pointers to left and right children. 8 points (Hint: Use a queue.) void level_print(tree* T)
20) A closed hash-table is shown below. The table was built using double hashing, with primary hash function h(x) = x % 13, and secondary hash function s(x) = 5 (x % 5). Index key status 0 26 1 1 0 2 0 3 42 1 4 17 1 5 0 6 172 1 7 33 2 8 21 1 9 35 1 10 0 11 0 12 0 12 points a. What is the expected number of probes for a successful search in this hash table? (Calculate the number of probes for each key in the table and average it.) b. Suggest a key that requires the maximum number of probes to insert into the hash table. c. What is the position in which the key 32 will get inserted? d. Is there any problem with using s (x) = x % 5 as the secondary hash function? Explain.