MIDTERM EXAM (HONORS SECTION)

Similar documents
HOMEWORK FILE SOLUTIONS

Midterm solutions. n f 3 (n) = 3

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Section 1: True / False (2 points each, 30 pts total)

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

CSE332 Summer 2010: Midterm Exam Sample Solutions

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE373 Fall 2013, Midterm Examination October 18, 2013

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

Second Examination Solution

ECE250: Algorithms and Data Structures Midterm Review

Data Structure and Algorithm Midterm Reference Solution TA

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Midterm Exam (REGULAR SECTION)

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

CS 315 Data Structures mid-term 2

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Algorithms. AVL Tree

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

CMSC th Lecture: Graph Theory: Trees.

Computer Science Foundation Exam

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Lesson 21: AVL Trees. Rotation

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Balanced Search Trees. CS 3110 Fall 2010

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

INF2220: algorithms and data structures Series 1

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

CS171 Midterm Exam. October 29, Name:

MIDTERM EXAMINATION Douglas Wilhelm Harder EIT 4018 x T09:30:00P1H20M Rooms: RCH-103 and RCH-302

Computer Science Foundation Exam

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Recursion and Structural Induction

Search Trees. Undirected graph Directed graph Tree Binary search tree

Section 05: Solutions

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Recursion Chapter 3.5

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

CSE 373 Spring Midterm. Friday April 21st

Algorithms and Data Structures

Chapter 4: Trees. 4.2 For node B :

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Solution for Homework set 3

Q1 Q2 Q3 Q4 Q5 Q6 Total

Computer Science Foundation Exam

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Analysis of Algorithms

Part 2: Balanced Trees

Design and Analysis of Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Problem Set 5 Due: Friday, November 2

CSE 2320 Section 002, Fall 2015 Exam 2 Time: 80 mins

Practice Midterm Exam Solutions

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

c) A perfect ternary tree of height h? (In a ternary tree, each node may have up to three children.)

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Solution to CSE 250 Final Exam

INSTITUTE OF AERONAUTICAL ENGINEERING

CS61B Spring 2016 Guerrilla Section 3 Worksheet. 12 March 2016

CPS 231 Exam 2 SOLUTIONS

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Problem Set 4 Solutions

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Choice of C++ as Language

Recursively Defined Functions

CS 216 Exam 1 Fall SOLUTION

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Lecture 6: Analysis of Algorithms (CS )

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Lecture 15 Binary Search Trees

Section 05: Solutions

Prelim One Solution. CS211 Fall Name. NetID

CSCI2100B Data Structures Trees

Exam Data structures DAT036/DAT037/DIT960

CS 10, Fall 2015, Professor Prasad Jayanti

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

UNIVERSITY REGULATIONS

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

TREES Lecture 10 CS2110 Spring2014

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

Writeup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees

Friday Four Square! 4:15PM, Outside Gates

Transcription:

Data Structures Course (V22.0102.00X) Professor Yap Fall 2010 MIDTERM EXAM (HONORS SECTION) October 19, 2010 SOLUTIONS Problem 1 TRUE OR FALSE QUESTIONS (4 Points each) Brief justification is required for credit. (a) 100n 2 = O(n 3 ). Hint: recall the definition of the big-oh notation which involves the existence of two constants, C > 0 and n 0. SOLUTION TRUE: We must show that there is a C > 0 and n 0 such that 100n 2 Cn 3 forn n 0. But100n 2 n 3 foralln 100. SowecanchooseC = 1 and n 0 = 100. Other natural choices include C = 100,n 0 = 1 or C = 10,n 0 = 10. REMARK: to get full credit, you must explicitly indicate the two numerical constants n 0 and C. Any big-oh statement says that there exists two implicit constants C > 0 and n 0. (b) e n = O(n 8 ). Hint: recall that e n = k 0 nk k!. SOLUTION FALSE: from the series for e n, we conclude that e n > n9 9! > Cn 8 for all n > 9!C. OK, this argument is slick, and you might miss many details involved. So, let us do this more slowly: First, observe that e n > n 9 /9!. WHY? Because we have omitted positive terms from the infinite series of e n, and retained the term corresponding to k = 9. Now are are ready to get a contradiction: suppose for some choice of C and n 0, we have that, whenever n > n 0, then e n Cn 8. This means that n 9 /9! < Cn 8. This means n 9 < (9!C)n 8. This means n < 9!C. BUT THIS IS NONSENSE: I can choose any n greater than n 0 and greater than 9!C to get a contradiction. REMARK: Of course, 8 is not particularly special here. The same argument shows that e n O(n k ) for ANY k. Many students just use generalities like e n is growing expoentially fast, and so grows much faster than any polynomial. That is not precise enough again, you must get down to the constants like C and n 0. Remember that C,n 0 is to Computer Science what delta-epsilon is to Calculus. (c) The rotation operation rotate(u) applied to a binary tree node u is an O(1) operation. SOLUTION TRUE: you only need a constant number (at most 6) of pointer statements. REMARK: some students think that 6 = O(1) is false. Of course, this is a grave error. Others think that rotate(u) may involve Ω(n) work in a tree with n nodes but that is not true. 1

(d) n 1/lgn = 2. Hint: take logs on both side. SOLUTIONTRUE: let x = n 1/lgn. Taking logs to base 2, lgx = lgn(1/lgn) = 1. So x = 2. Problem 2 SHORT QUESTIONS (a) (2 Points) Name a superclass of the class Integer in Java. SOLUTION Object class. (b) (3 Points) What is wrong with the statement: MyList<int> il = new MyList<int>( ); SOLUTION You cannot instantiate the type of a generic class with a primitive type like int. You must use a class type such as Integer instead. REMARK: Toget fullcredit, itis NOTenoughtojustsay that wecannot useint or that you must use Integer instead. You must indicate the general principle involved, namely, class types (or Object types) must be used instead of primitive types. (c) (5 Points) Name a concrete data structure in which sentinels is a useful concept. What are the pros and cons of using sentinels in your example? SOLUTION Sentinels are useful in the doubly linked list data structure: here, we have a special head and tail nodes to act as sentinels. Pros: sentinels allow you to have uniform code that allows you to avoid many special cases. Cons: you waste a bit of extra space. Every doubly linked list, has at least two nodes, even if empty. Some non-reasons students give: Pro sentinels allow you to insert at both two ends of the list. Con you have to manage dummy nodes the the ends of the list. OTHER COMMENTS: A linked list class will have a reference to the first node in the list. With sentinels, you never need to change this reference (which always point to the head sentinel). Some students say nothing about the cons, so lose 1 point. (d) (5 Points) If u is the node of a BST, let h(u) denote the height of the subtree rooted at u. Write the recursive formula for h(u). Hint: use u.left and u.right. 2

SOLUTION Let u L and u R be the left and right children of u. Then { 1 if u = null h(u) = 1+max{h(u L ),h(u R ) else. PITFALLS: Some students provide incomplete analysis of the base cases, giving, for instance: { 0 if ul = null and u h(u) = R = null 1+max{h(u L ),h(u R ) else. One problem with this solution is that it is implicitly assumed that u null. A second problem is that it misses the case where exactly one of u L,u R is null. You can fix this by adding additional cases, of course, but it is clearly inferior to our solution. Hope you see the extreme usefulness of treating the special case of u = null (just as it is useful in counting numbers to have 0). NOTE that many students actually write the Java code to compute the height. That is not necessary, but you do not lose any points for doing it. (e) (6Points)ThepostorderofaBSTT producesthefollowingsequenceofkeys: (1,3,5,4,8,7,9,6,2). Please reconstruct T from this information. 2 1 6 3 4 5 7 8 9 Figure 1: Solution for Postorder tree SOLUTION See Figure 1. As some of you noted, you can construct this tree by inserting into an empty tree starting from the end of the list: i.e., insert 2, then 6, then 9, then 7,... and finally insert 1. (f) (8 Points) Consider the following function: static int strange(int n){ for (int i=0; i< n; i++){ int j = 1<<i; // NOTE: This assigns j the value 2^i (exponentiation) while (j>0) { j--; Give an asymptotic bound for the running time of strange(n). 3

SOLUTION T(n) = n 1 i=0 (1+2i ) 2 n 1 i=0 2i = 2(2 n 1). In short, T(n) = O(2 n ). This sum is the simplest example of a geometric series. COMMENTS: In fact, T(n) = Θ(2 n ). Some students say T(n) = O(n2 n ), since each term in the summation is O(2 n ). This is correct of course, but not tight. I award 3 out of 4 points in this case. (g) (6 Points) I created an AVL tree to store the 26 letters of the alphabet. What is the maximum possible height? Hint: let µ(h) be the minimum number of nodes in an AVL tree of height h what is a recursive formula for µ(h)? SOLUTION Answeris 5. Usingthehint, weseethatµ(0) = 1,µ(1) = 2andfor h 1, µ(h+1) = 1+µ(h)+µ(h 1). Thus we see µ(2,3,4,5,6) = (4,7,12,20,33) (hopefully this notation is clear). Since µ(5) 26 < µ(6), a tree with 26 nodes have height at most 5. Problem 3 AVL TREES. Consider the AVL tree T 0 in Figure 2. All keys in this tree must be integers, and each part of this question is independent of the others. 20 T 1 25 40 55 T 2 35 45 58 Figure 2: Insertion to AVL tree (a) (5 Points) Please show the tree after we insert key 59 into T 0. (b) (5 Points) Please show the tree after we insert key 46 into T 0. (c) (3 Points) What is the largest key k in the range 20 and that you can insert into T 0 which does not cause any rotation? (d) (3 Points) What is the smallest key k in the range 20 and that you can insert into T 0 so that you need to do a double-rotation? SOLUTION (a) and (b): See Figure 3. (c) Largest key to insert without rotation is 54. (d) Smallest key whose insertion causes double-rotation is 24. Problem 4 JAVA PROGRAMMING. (20 Points) To get full credit, you must write Java code that will not generate any compile error. In our BST<Integer> class (recall hw3), we have a method called public String search(integer 4

20 20 20 T1 25 T2 T1 25 T2 T1 40 T2 40 55 40 58 25 35 45 55 35 45 58 35 45 55 59 46 58 (o) original tree (a) Ins(59) (b) Ins(46) Figure 3: Solution for AVL insertion k) which searches for a key k, and returns the String data associated with the key. In this question, we want you to add another method called public Integer searchindex(int i) which returns the i-th smallest key. E.g., if i = 1, you return the smallest key in the BST. If i = n where n is the size of the BST, you return the largest key. Assume that we maintain the size of the subtree rooted at each node u. This value is stored in the field u.size. Write the Java code to implement searchindex(int). HINT: it may be useful to write another helper function. SOLUTION We assume a helper method (also) called searchindex but which (in addition to the index) has a node as an argument: public Integer searchindex(int i){ return searchindex(i, root); private Integer searchindex(int i, BinaryNode<Integer> u){ if (i < 1 i > u.size) throw new RuntimeException("Index out of bounds"); if (i <= u.left.size) return searchindex(i, u.left); if (i == u.left.size+1) return u.key; return searchindex(i-1-u.left.size, u.right); Most students did an inorder listing of the keys, store into an array, and return the i-th item in the array. This is an extremely inefficient solution (you need to copy the entire tree into a new array. It also does not use the u.size information we provided. You get no more than 10 points for this solution. If you do not check for index out of range, I take off one point. Note the simple way to throw exception in Java above. Please learn this syntax. Problem 5 INDUCTION (10 Points) Ulysses in our class derived this interesting relation connecting φ (golden ratio) and F n (the 5

Fibonacci numbers, assuming that F n = n for n = 0,1): φ n = φf n +F n 1, (n 1) Please prove this by induction (note that we ignore the case n = 0). SOLUTION Check that the relation holds for n = 1,2: φ 1 = φ(f 1 ) + F 0 = φ(1)+0 = φ, and φ 2 = φ(f 2 )+F( 1 ) = φ(1)+1 = φ+1 = φ 2. For n > 2, φ n+1 = φ(φf n +F n 1 ) (by induction hypothesis) = F n (φ 2 )+φf n 1 (rearrangement) = F n (1+φ)+φF n 1 (φ 2 = 1+φ) = φ(f n +F n 1 +F n (rearrangement) = φf n+1 +F n (F n+1 = F n +F n 1 ) PITFALLS: some students gave only one base case, for n = 1. Any proof that does not exploit the two relations φ 2 = 1+φ, F n+1 = F n +F n 1 cannot be correct! After all, you must use the properties of φ and F n somewhere. 6