Question

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

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2020 answer key

Solution

Sum the logarithmic costs of inserting n^2 elements sequentially into an AVL tree of initial size n.

An AVL tree is a height-balanced binary search tree. The time complexity of inserting a single element into an AVL tree containing kk nodes is Θ(log⁡k)\Theta(\log k) in the worst case (involving O(log⁡k)O(\log k) comparisons to find the insertion position, plus at most two tree rotations taking O(1)O(1) time).

When inserting n2n^2 elements into an AVL tree that already contains nn elements:

  • For the first inserted element, the tree has nn elements, taking Θ(log⁡n)\Theta(\log n) time.
  • For the ii-th inserted element (1≤i≤n21 \le i \le n^2), the tree contains n+i−1n + i - 1 elements, taking Θ(log⁡(n+i))\Theta(\log(n + i)) time.
  • The total time T(n)T(n) across all n2n^2 insertions is:
    T(n)=∑i=1n2Θ(log⁡(n+i))T(n) = \sum_{i=1}^{n^2} \Theta(\log(n + i))

To establish the asymptotic bound:

  • Upper bound: Each term satisfies log⁡(n+i)≤log⁡(n+n2)=log⁡(n(n+1))=Θ(log⁡n)\log(n + i) \le \log(n + n^2) = \log(n(n+1)) = \Theta(\log n). Therefore:
    T(n)≤∑i=1n2clog⁡(n+n2)=c⋅n2⋅Θ(log⁡n)=O(n2log⁡n)T(n) \le \sum_{i=1}^{n^2} c \log(n + n^2) = c \cdot n^2 \cdot \Theta(\log n) = O(n^2 \log n)
  • Lower bound: Considering the final n2/2n^2 / 2 insertions where the tree size is at least n+n2/2n + n^2 / 2:
    T(n)≥∑i=n2/2n2c′log⁡(n+i)≥n22⋅c′log⁡(n+n22)=Ω(n2log⁡n)T(n) \ge \sum_{i=n^2/2}^{n^2} c' \log(n + i) \ge \frac{n^2}{2} \cdot c' \log\left(n + \frac{n^2}{2}\right) = \Omega(n^2 \log n)
  • Combining both bounds yields T(n)=Θ(n2log⁡n)T(n) = \Theta(n^2 \log n).
Final answer

C

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2020 Computer Science and Information Technology (CS)
Question
16 of 65, printed as CS Q.6
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.