York University AS/AK/ITEC 2620 3.0 INTRODUCTION TO DATA STRUCTURES Midterm Sample I Examiner: S. Chen Duration: One Hour and 30 Minutes This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g. for computing and/or communicating) is NOT permitted. Do not unstaple this test book any detached sheets will be discarded. Answer all questions in the space provided. No additional sheets are permitted a blank page is attached at the back for rough work. Work independently. The value of each part of each question is indicated. The total value of all questions is 60. However, the test is out of 50, so there are 10 extra marks available it is possible to score more than 100% on this test. Write your name and student number in the space below. Do the same on the top of each sheet of this exam where indicated. NOTE: YOU MAY USE PEN OR PENCIL, BUT ANSWERS IN PENCIL WILL NOT BE CONSIDERED FOR REGRADING. Surname: Given Names: Student Number: 1 2 Total 3 4 Page 1 of 8
Question 1 (15 marks) Short Answer (maximum 20 words): Answer all five parts below. Part A (3 marks): What is the time complexity of the best case to insert a new node into a minimum-level BST with n nodes? Explain. (Hint: You may draw a diagram as part of your solution.) Part B (3 marks): The best case to find the smallest value in an unsorted array is O(1) when there is only one element in the array. True or false? Explain. Part C (3 marks): If both have n nodes and are sorted smallest to largest, will it be faster to find the smallest value in a sorted linked list or a minimum-level BST? Explain. Part D (3 marks): Two O(n 2 ) algorithms will always take the same amount of time for a given value of n. True or false? Explain. Part E (3 marks): You have a (large) unsorted set of n records that you wish to store in a BST. The BST will be built by adding one record at a time. Will it be faster to build the BST if the records are first sorted so that they can be added to the BST in sorted order? Yes or no? Explain. Page 2 of 8
Question 2 (15 marks) Complexity Analysis/Estimation: Answer all three parts below. Part A (5 marks): Develop a recurrence relationship for the bizarre method that is called as follows: int result = bizarre(n, n); public static int bizarre (int first, int second) { if (second <= 1) { int temp = 0; for (int i = 0; i < first; i++) temp += i; return temp; } } return bizarre (first, second-1); Page 3 of 8
Part B (5 marks): What is the complexity of the following recurrence relation? Show the details of your analysis. T(1) = 1 T(n) = 2*T(n-1) + 1 Part C (5 marks): What is the complexity of the given code as a function of the problem size n? Show all details of your analysis. for (int i=0; i < n; i++) loops(i); public static void loops (int x) { if (x < 0) return; } for (int i=0; i < x; i++) for (int j=0; j < x; j++) print ( hi ); Page 4 of 8
Question 3 (15 marks) Linked Data Structures: Answer all seven parts below. Tree T shown below is a BST. 9 3 16 1 8 15 22 7 18 Part A (2 marks): Starting with the original tree T, insert 12. Draw the resulting tree below. Part B (2 marks): Starting with the tree developed in Part A, delete 22. Draw the resulting tree below. Page 5 of 8
Part C (2 marks): Starting with the tree developed in Part B, insert 22. Draw the resulting tree below. Part D (4 marks): Starting with the original tree T, delete 9. Use the deletion strategy presented in class. Draw the resulting tree below. Part E (5 marks): Draw the BST that is created by inserting the following nodes/values in the given order into a BST that is initially empty. 6 3 8 4 7 2 5 1 Page 6 of 8
Question 4 (15 marks) Recursion: The modulus operation returns the unit value of a number in a given base. For example: In base 10, the unit value of 237 is 7. (237%10 = 7) In base 2, the unit value of 1023 is 1. (1023%2 = 1) In base 8, the unit value of 6 is 6. (6%8 = 6) Write a recursive function in that will calculate the modulus of two positive inputs (the number and the base). Note: you may only use addition and subtraction you must not use multiplication, division, modulus, or any other methods in your solution, and you will not receive any credit for a non-recursive solution. Hint: use repeated subtraction. For example, the following statements would lead to the underlined output: Example 1: Example 2: Example 3: print ( modulus( 237, 10 )); 7 print ( modulus( 1023, 2 )); 1 print ( modulus( 6, 8 )); 6 Note: you may assume two positive inputs no error checking of inputs is required. Please write your method on the following page. You may use this page for rough work, but anything on this page will not be graded. Page 7 of 8
int modulus (number, base) { } Page 8 of 8