GATE / CS ARCHIVE

GATE CSE 2020 / QUESTION 6

GATE CSE 2020 Q6

Data Structures · Binary Search & Balanced Trees · Full GATE CSE 2020 paper

GATE 2020, Q6 1 mark · MCQ
Report issue

Binary Search & Balanced Trees

Question 6, GATE 2020: What is the worst-case time to insert n² elements into an AVL tree initially containing n elements? A Θ(n⁴); B Θ(n²); C Θ(n² log n); D Θ(n³).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Worked explanation

With m nodes, an AVL tree has height Θ(log m); locating an insertion position and propagating height changes take O(log m), while rotations do not introduce a larger order. During the sequence, m=n+i for 0≤i<n². Thus the total upper bound is Σ O(log(n+i))≤n² O(log(n+n²))=O(n² log n). For ordinary comparison-based insertion of distinct keys, an AVL tree’s external search paths have logarithmic depth; in particular an adversarial sequence can repeatedly force logarithmic work. A constant fraction of n² operations therefore contributes Ω(n² log n). Do not multiply by an extra n for rebalancing.

Derivation and answer

Throughout the n² insertions the AVL size lies between n and n+n², so each logarithmic insertion is O(log n). Their total is O(n²log n), and worst-case searches over a constant fraction of the later insertions require Ω(log n) each. Hence Θ(n²log n), C.

Original paper, page 5 · Answer key