This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 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. 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 ....
View Full Document
This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.
- Fall '08