GATE | GATE CS 2020 | Question 16

What is the worst case time complexity of inserting n2 elements into an AVL-tree with n elements initially ?
(A) Θ(n4)
(B) Θ(n2)
(C) Θ(n2 log n)
(D) Θ(n3)

Answer: (C)

Explanation: Since AVL tree is balanced tree, the height is O(log n). So, time complexity to insert an element in an AVL tree is O(log n) in worst case.


Every insertion of element:
Finding place to insert = O(log n)
If property not satisfied (after insertion) do rotation = O(log n)

So, an AVL insertion take = O(log n) + O(log n) = O(log n) in worst case. 

Now, given n2 element need to insert into given AVL tree, therefore, total time complexity will be O(n2 log n).

Alternative method: Time complexity in worst case,

1st insertion time complexity = O(log n)
2nd insertion time complexity = O(log(n+1))
n2th insertion time complexity = O(log(n + n2)) 

So, total time complexity will be,

= O(log n) +  O(log n+1)) + .... +  O(log(n + n2))
= O(log n*(n+1)*(n+2)*...(n+n2))
= O(log nn2)
= O(n2 log n) 

Option (C) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.