Data Structures and Algorithms Final Exam Spring 2003 Potential exam questions. 1. (15 pts) Specify the best-case running time for each of the following algorithms (assume n elements to be sorted). a). Bubble Sort b). Simple Sort c). Selection Sort d). Quicksort e). Mergesort 2. (10 pts) The following states two tasks and four data structures for each task. Each data structure contains N unique members. a). checking if x is one of the N numbers i. a sorted array ii. a balanced binary search tree iii. a hash table with a perfect hash function iv. a heap b). finding the largest number i. a sorted array ii. a balanced binary search tree iii. a hash table with a perfect hash function iv. a heap Briefly state/name your algorithm to accomplish the two tasks using each of the three data structures and discuss its worst case time complexity in big-O (only counting the number of comparisons between x and an element in a data structure or between two elements in a data structure). That is, 8 algorithms (briefly) and 8 worst case analyses. 3. C, C++ or pseudocode with sufficient details can be used for this two part question: a) (25 pts.) Two trees are isomorphic if they have the same structure independent of the values stored in each node. White a function that compares two binary trees and tells whether they are isomorphic. If you want, you may assume that a reference for both trees are passed to the function. b) (25 pts.) Write a recursive function that returns the sum of the digits of a given integer. As an example sumOfDigits(538) = 5 + 3 + 8 = 16 4. (15 Pts.) Hashing a) What are the best-case and worst-case time complexities (in big-O) of location an item in a hash table? Explain. b) What is a collision in hashing? c) Describe two collision resolution strategies and illustrate with examples. 5. (40 Pts.) Using C, C++ or pseudocode with sufficient details: a) write a procedure that performs selection sort in ascending order on an array of integer elements. b) Use class/struct to represent a singly linked list of integer items and write a recursive function that prints a singly linked list in reverse order. 6. C, C++ or pseudocode with sufficient details can be used for your answer where appropriate. However in each case be sure to specify which language you are using. a) (25 pts.) Suppose that an array is used to store a set of integers (positive and negative) where for a set of size n, the n integers are stored in array locations 1 thru n. i. Give a function that will have two parameters: an array A containing a set of integers, and an integer x. The function will return true if the array contains two elements that sum to x, and false otherwise. ii. What is the worst case running time of your function in part a. b) (25 pts.) Give a function that will have two arrays as parameters, each containing a set of integers. The function will return true if the sets are disjoint and false otherwise ( Recall an important definition of sets). c) What is the worst case running time of your function in part (c )? For this question you can assume that the sizes of the sets are m and n. 7. (50 pts.) C, C++, or pseudocode with sufficient details can be used for this two part question: a) (20 pts.) Write a program to find the k smallest elements in an array of length n. For what value of k does it become advantageous to sort the array? Explain your answer. b) (30 pts.) Write a recursive function that compares the greatest common divisor of two positive integers, long gcd(long m, long n), using Euclids algorithm. As originally formulated by Euclid (circa 300 BC), it says to subtract repeatedly the smaller number “n” from a larger number “m” until the resulting difference “d” is smaller than “n”. Then repeat the same steps with “d” in place of “n” and with “n” in place of “m”. Continue until the two numbers are equal. Then that number will be the greatest common divisor (gcd) of the original two numbers. 8. (25 pts.) Given an array with these numbers: 9, 2, 4, 7, 8, 1, 6, 5: a) Perform Quicksort; show your steps. b) In terms of the number of comparisons In Quicksort: i. ii. iii. iv. When does the best case occur? Explain What is the time complexity of the best case in big-O? Explain When does the worst case occur? Explain What is the time complexity of the worst case in big-O? Explain 9. (25 pts.) Given an array with these numbers: 9, 2, 4, 7, 8, 1, 6, 5: a) Perform Mergesort; show your steps. b) In terms of the number of comparisons In Mergesort: v. vi. vii. viii. When does the best case occur? Explain What is the time complexity of the best case in big-O? Explain When does the worst case occur? Explain What is the time complexity of the worst case in big-O? Explain 10. (20 pts.) Sorting a) Given an array of these integers: 4, 9, 7, 3, 2, 2, 8, perform Insertion Sort and show your steps. Shoe the array after each pass of the array (after one more item is sorted). b) For counting the number of comparisons in Mergesort, when does the worst case occur and what is the complexity in big-O. 11. (20 Pts.) Trees a) b) c) d) How many nodes in a complete binary tree of height h? How many leaves are there in a complete binary tree of height h? How many edges are in a tree with n nodes? Give a lower bound on the height of a binary tree with n leaf nodes. 12. (20 Pts.) Trees a) In any tree, is the number of internal nodes always fewer than the number of leaf nodes? Explain. b) What is the maximum number of nodes in a tree with degree d and height h? Explain. c) Describe two ways of representing a binary tree in a program. d) Starting from a empty AVL tree, insert nodes in this order: 5, 6, 1, 2, 3, 4,. Please show tour steps and the AVL tree after each insertion. 13. (50 Pts.) (50 pts.) C, C++, or pseudocode with sufficient details can be used for this two part question: a) Give a function for calculating the height of a given binary tree. For this question you may assume that an empty tree (one containing no nodes) has height 0, and a tree containing a single node has height 1. b) Give a function for determining the total number of nodes in a tree. 14. (50 Pts.) C, C++, or pseudocode with sufficient details can be used for this two part question: a) (20 Pts.) Write a modified version of Bubblesort that can terminate early if the remaining part of the array is sorted. b) (30 Pts.) The coefficients that result from the expansion of a binomial expression of the form (x+1)m are called binomial coefficients. The French mathematician Blaise Pascal discovered a recursive relationship among the binomial coefficients. By arranging them in a triangle, he found that each interior number is the sum of the two directly above it: 1 1 1 1 1 2 1 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 79 56 28 8 1 1 3 1 4 3 6 For example, 21 = 15+6. Let’s denote pascal(n, k) the coefficient in row number n and column number k (counting from 0). Write a recursive function pascal(n, k) that returns the coefficient at position (n, k) in the triangle. For example, pascal(7, 2) returns 21. 15. (40 Pts.) A palindrome is a word that reads the same forward and backward. For example, “1991”, “level”, “Bob”, and “ABBA”. Write a function (in C, C++ or pseudocode in sufficient details) that checks if a character string is a palindrome. a) Iteratively (no recursion) b) Recursively 16. (10 Pts.) For the following AVL-tree, show yopur steps (intermediate trees) for the following two successive operations: 8 15 4 3 2 11 5 10 16 12 a) Insert 13 and b) Then insert 1. 17. (40 Pts.) Binary Search a) What property does an array have to have before a binary search can be performed? b) Using C, C++ or pseudocode with sufficient details, write a function that performs a binary search for a key on an array of integer elements and returns the array index of the location of the key. The function returns –1 if the key is not in the array. Provide two versions (be sure to specify all parameters). i. Iterative (non-recursive) version ii. Recursive version c) For the iterative version, describe when the “best” case occurs and analyze the run time complexity using big-O notation (assuming n is the number of elements in the array). Explain your answer. d) For the iterastive version, describe when the “worst” case occurs and analyze the run time complexity using big-O notation (assuming n is the number of elements in the array). Explain your answer. e) If the array does not satisify the property in part (a), describe and analyze the bestcase and worst-case run time complexities that include both the worst-case run- time complexity to achieve the property in part (a) and binary search. Explain your answer. 18. (20 Pts.) Given this DigitSet class // set elements can be between 0 and 9 const int SET_SIZE = 10; class DigitSet { bool bitStr[SET_SIZE]; // true if the element is in the set public: void insert (int someDig); // inserts an element into the set bool subset(DigitSet set2); // returns TRUE if the set is a subset of set2 }; a) inplement insert in C++ b) implement subset in C++ 19. (10 pts.) Using the big-O notation, estimate the worst case running time of the following function. Explain your answer. void proc(int x) { int i, j, k; for(i = 0; i < 10; i++)if (x 1) for(j = 0; j < x; j++) for(k = x; k > 0; k = k/2) /* constant time operation */ } 20. (45 Pts.) Answer the following questions about the following binary tree: B1 m x c a s f z p a) Give the preorder traversal of this tree b) Give the inorder traversal of this tree c) Give the postorder traversal of this tree d) Give the preorder traversal of this tree e) Is B1 a binary search tree? Explain. f) Show B1 after inserting node n (call the resulting tree B2). g) Show B1 after deleting node m (call the resulting tree B3). h) Which is more difficult in a search tree: insertion or deletion? Explain. i) Is B1 balanced (AVL)? Explain. If your answer is “not balanced”, convert into a balanced tree. Explain your methodology. j) Is B2 balanced (AVL)? Explain. If your answer is “not balanced”, convert into a balanced tree. Explain your methodology. k) Why is a complete balanced search tree preferable to a random search tree? 21. (25 points) Write a function that searches for an element in a binary search tree. Your function should stop as soon as the element is found. What is the complexity of your function (use the big-O notation)? 22. (15 points) hashing is a very important technique in computer science: a) Shortly describe a case where you might use hashing b) Describe two techniques for handling collision in hash functions 23. (25 points) When used to sort an array, selection sort works by selecting the element with the largest key value and placing it last in the list. In the next step of the algorithm chooses the largest element but does not consider the last element since it is already in its correct position. The algorithm terminates when all the elements have been placed in their correct positions Write a recursive algorithm for selection sort that uses the idea described above. 24. (20 points) Answer the following questions about Quicksort a) Describe a way in which the partitioning part of the algorithm can be implemented so that the algorithm will more likely perform in O(n log n) b) Write a Quicksort function using the partitioning you have described above. You should also provide the implementation of the partition function.