What is the worst case time complexity of inserting elements into an AVL-tree with elements initially?
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…
- A
- B
- CCorrect answer
- D
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.