Вы находитесь на странице: 1из 9

York University AS/AK/ITEC 2620 3.0 Section M INTRODUCTION TO DATA STRUCTURES Winter 2003 Midterm Test Examiner: Prof.

. S. Chen Duration: One Hour and 15 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:

___________________________________

" #!  

  

Surname:_____________

First name:______________ Student #: _______________

Question 1 (15 marks) Short Answer (maximum 20 words): Answer all five parts below. Part A (3 marks): What is the worst case time complexity for binary search on an array with n elements? Explain.

Part B (3 marks): Quicksort has a worst case time complexity of O(n2). Yet, it is preferred over mergesort which has a wosrt case time complexity of O(n n). Explain why.

Part C (3 marks): Your co-worker has implemented binary search on a sorted linked-list. Explain why this will be slower on average than using linear search. What is the average time complexity of their algorithm?

Part D (3 marks): An O(n) algorithm is always faster than an O(n2) algorithm for all n > 1. True or false? Explain.

Part E (3 marks): You have a binary tree with n elements that is not in sorted order. What is the time complexity to find the smallest value? Explain.

  

Surname:_____________

First name:______________ Student #: _______________

Question 2 (15 marks) Complexity Analysis/Estimation: Answer both parts below. Part A (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/2)

Surname:_____________

First name:______________ Student #: _______________

Part B (10 marks): The following code processes A which is an n-by-n matrix. What is the complexity of the given code as a function of the problem size n? Show the details of your analysis. for (int i=0; i < n; i++) { // note: A[i] is an array with n elements if (Math.random() > 0.5) mergesort(A[i]); else selectionSort(A[i]); }

Surname:_____________

First name:______________ Student #: _______________

Question 3 (15 marks) Linked Data Structures: Answer all four parts below. Tree T shown below is a BST.

Part A (3 marks): Starting with the original tree T, insert 3. Draw the resulting tree below.

Part B (3 marks): Starting with the tree developed in Part A, delete 6. Draw the resulting tree below.



       

   

Surname:_____________

First name:______________ Student #: _______________

Part C (3 marks): Starting with the tree developed in Part B, insert 6. Draw the resulting tree below.

Part D (6 marks): Starting with the original tree T, delete 14. Using two distinctly different deletion strategies (i.e. like those presented in programming project 1), draw two possible resulting trees below.

Question 4 (15 marks) Recursion: One day while taking a break in industry, you notice a random pattern of blue and green tiles on the floor. Reminiscing about happier times in ITEC2620, you decide to write a recursive function that will determine how many more blue tiles there are than green tiles. To develop this function, assume that the color of each tile is stored in a fully populated character array. In this array, a B will represent a blue tile, and a G will represent a green tile. For example, calling your moreBlue() function on the following array

tileArray

will return 2. In particular, the following statements would lead to the underlined output: Example 1: York.println( moreBlue( tileArray, tileArray.length-1, 0 )); -2 Example 2: char[] ar = new char[] {B, B, G, G, B}; York.println( moreBlue( ar, ar.length-1, 0 )); 1

Please write your program on the following page. You may use this page for rough work, but anything on this page will not be graded.

Surname:_____________

First name:______________ Student #: _______________

public static int moreBlue (char[] tile, int highIndex, int lowIndex) {

This blank page may be detached and used for rough work.

Вам также может понравиться