Project ForgeSign up free
Data Structures
Trees
GATE 2020
Medium

What is the worst case time complexity of inserting n² elements into an AVL-tree with n elements initially?

  • AΘ(n⁴)
  • BΘ(n²)
  • Θ(n² log n)
  • DΘ(n³)

Explanation

An AVL tree stays balanced, so each insertion into a tree of current size m costs O(log m). Inserting n² elements grows the tree from n to about n²+n ≈ n² elements, and every single one of those n² insertions costs O(log(n²)) = O(log n) in the worst case (using the final, largest size as an upper bound on every insertion's cost). Total cost = n² × O(log n) = Θ(n² log n).

Source: GATE CS 2020 (2020)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free