The Wayback Machine - https://web.archive.org/web/20230527151558/https://www.geeksforgeeks.org/gate-gate-cs-2020-question-16/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

GATE | GATE CS 2020 | Question 16

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

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

My Personal Notes arrow_drop_up
Last Updated : 11 Aug, 2021
Like Article
Save Article
Similar Reads