# 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)

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. Note:
```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

Secure upto 50% Scholarship for our newly launched GATE Classes! Register for the Free Scholarship Test today
Rewards-
1. Top 3: 50% Scholarship
2. Rank 4-10: 30% Scholarship
3. Rank 11-50: 10% Scholarship

Contest starting from 17th Feb, 2024. Register today and get ready to smash GATE 2025!

Previous
Next