{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

quiz1-s2011-sol

# quiz1-s2011-sol - Introduction to Algorithms Massachusetts...

This preview shows pages 1–3. Sign up to view the full content.

Introduction to Algorithms March 9, 2011 Massachusetts Institute of Technology 6.006 Spring 2011 Professors Erik Demaine, Piotr Indyk, and Manolis Kellis Quiz 1 Solutions Quiz 1 Solutions Problem 1. True or False [21 points] (7 parts) For each of the following questions, circle either T (True) or F (False). Explain your choice. (Your explanation is worth more than your choice of true or false.) (a) T F The height of any binary search tree with n nodes is O (log n ) . Explain: Solution: False. In the best case, the height of a BST is O (log n ) if it is bal- anced. In the worst case, however, it can be Θ( n ) . (b) T F Inserting into an AVL tree with n nodes requires Θ(log n ) rotations. Explain: Solution: False. There were two ways you can show this. 1. There are cases where inserting into an AVL tree requires no rotations. Θ(log n ) rotations implies Ω(log n ) rotations. Since we have insertions that require no rotations, this means that inserting into an AVL tree does not require Ω(log n ) rotations and thus it does not require Θ(log n ) rotations. 2. Inserting into an AVL tree may look at O (log n ) nodes, but it only needs to perform at most 2 rotations to fix the imbalance. Thus inserting into an AVL tree requires O (1) rotations, which is not Θ(log n ) . Both of these were acceptable. Common mistakes included thinking that rotations needed to be made for each node in an inserted node’s ancestry line and misunderstanding the problem to think that we were asking for the runtime of insertion and not the number of rotations required.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6.006 Quiz 1 Solutions Name 2 (c) T F The depths of any two leaves in a max heap differ by at most 1. Explain: Solution: True. A heap is derived from an array and new levels to a heap are only added once the leaf level is already full. As a result, a heap’s leaves are only found in the bottom two levels of the heap and thus the maximum difference between any two leaves’ depths is 1. A common mistake was pointing out that a heap could be arbitrarily shaped as long as the heap property (parent greater than its children in the case of a max- heap) was maintained. This heap is not a valid heap, as there would be gaps if we tried to express it in array form, heap operations would no longer have O (log n ) running time, and heap sort would fail when using this heap. Another common mistake was simply justifying this statement by saying a heap is balanced. An AVL tree is also balanced, but it does not have the property that any two leaves have depths that differ by at most 1. (d) T F A tree with n nodes and the property that the heights of the two children of any node differ by at most 2 has O (log n ) height. Explain: Solution: True. Using the same approach as proving AVL trees have O (log n ) height, we say that n h is the minimum number of elements in such a tree of height h .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern