Last Updated : 21 Dec, 2018

Which of the following option is correct?
(A) Inserting into an AVL tree with n nodes requires Θ(logn) rotations.
(B) The depths of any two leaves in a max heap differ by at most 1.
(C) The height of any binary search tree with n nodes is O(logn).
(D) All of the above.


Answer: (B)

Explanation:

  • I. False: 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).
  • II. 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(logn) 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.

  • III. False: In the best case, the height of a BST is O(logn) if it is balanced. In the worst case, however, it can be Θ(n).

So, option (B) is correct.

Quiz of this Question


Share your thoughts in the comments