Honors Computer Science I – Exam #2 Date: 4/1/10 Name: _________________________________ 1) (7 pts) What is the worst-case run-time of the function below in terms of the input parameters m and n? Justify your answer. int function1(int array1[], int m, int array2[], int n) { int i, sum=0; for (i=0; i<m; i++) { int j = n-1; while (j > 0 && array1[i] < array2[j]) { sum++; j = j/2; } } return sum; } ___________ 2) (8 pts) What is the worst-case run-time of the function below in terms of the input parameters m and n? Justify your answer. int function2(int n) { int i = 1, sum = 0; while (i < n) { int j; for (j=0; j<i; j++) sum++; i = 2*i; } } _______________ 3) (20 pts) Consider a binary search on a sorted array of n = 2k – 1 elements for a positive integer k. Assume that each element in the array has an equal probability of being searched for, and that only elements in the array are being searched for. Determine the average number of comparisons that the binary search will conduct, in terms of n. 4) (10 pts) Transform the following infix expression into its equivalent postfix expression using a stack. Show the contents of the stack at the indicated points 1, 2 and 3 in the infix expression. 1 2 P / ( B + S – T ) + M * 3 – A + F N / C A B C Resulting Postfix Expression : 5) (6 pts) Consider implementing a queue with a regular linked list, with only a pointer to the front of the queue and no pointer to the back of the queue. In this situation how much time would the following operations take, in terms of n, the number of items in the queue: a) Enqueue ___________ b) Dequeue ___________ 6) (4 pts) Consider running a Merge Sort on the array below. Show the contents of the array right before the very last call to the Merge function: Index Value Answer 0 12 1 3 2 9 3 5 4 13 5 19 6 2 7 6 7) (5 pts) Consider partitioning the array below utilizing the algorithm shown in class, using the element in index 0 as the partition element. Show the result of this partition: Index Value Answer 0 8 1 3 2 9 3 11 4 5 5 13 6 19 7 2 8 6 8) (10 pts) Write a recursive function that takes in a pointer to a node in a binary search tree and returns the length of the path to the minimum value stored in the tree rooted at that node. If the node itself is the answer, the function should return 0. You will be guaranteed that the pointer passed to the function is not NULL and is pointing to a valid treenode. Fill in the prototype given below: struct treenode { int data; struct treenode* left; struct treenode* right; }; int path_to_min(struct treenode* root) { } 9) (10 pts) Write a recursive function that takes in a pointer to a node in a binary tree (not necessarily a binary search tree) and an integer value, threshold, and returns the total number of nodes in the tree rooted by that node that store a value greater than threshold. (Use the same struct as the previous question.) int numGreaterThan(struct treenode* root, int threshold) { } 10) (10 pts) The following struct and function are taken from the program htablelinear.c which implements the strategy of linear probing when a collision occurs. Rewrite this insertTable function for a hash table that uses quadratic probing when a collision occurs. Assume that a spot for the inserted item will eventually be found. #define MAX_SIZE 29 #define TABLE_SIZE 59999 struct htable { char entries[TABLE_SIZE][MAX_SIZE+1]; }; void insertTable(struct htable *h, char word[]) { int hashval; hashval = hashvalue(word); while (strcmp(h->entries[hashval], "") != 0) hashval = (hashval+1)%TABLE_SIZE; strcpy(h->entries[hashval], word); } // Rewrite the quadratic probing version here. void insertTable(struct htable *h, char word[]) { } 11) (10 pts) Name (first and last names) ten people in the class. (Yes, you can name yourself and that counts for one of the ten.) Please clearly mark any work on this page you would like graded.