Skip to main content

GATE CSE 2020Data Structures › Trees & Binary Search TreesVerified

GATE CSE 2020 PYQ: What is the worst case time complexity of inserting n 2 n^{2} n 2 elements into an AVL-tree with n n n…

What is the worst case time complexity of inserting n2n^{2} elements into an AVL-tree with nn elements initially?

  1. A
    Θ(n4)\Theta (n^{4})
  2. B
    Θ(n2)\Theta (n^{2})
  3. C
    Θ(n2log⁡n)\Theta (n^{2}\log n)
    Correct answer
  4. D
    Θ(n3)\Theta (n^{3})

Answer

(C) Θ ( n 2 log ⁡ n ) \Theta (n^{2}\log n) Θ ( n 2 lo g n )

Approach

AVL insertion into a tree of current size m takes Θ(log m) worst-case time because the tree height is O(log m). Insert the $n^2$ new elements one after another, so the cost is the sum of logarithmic costs for tree sizes n, n+1, ..., $n+n^2-1$.

Each AVL insertion costs Θ(log m) where m is the current number of nodes. Over $n^2$ insertions, the total is Θ(Σ log(n+i)) = $Θ(n^2 log n)$, so option C is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.