Question
What is the worst case time complexity of inserting elements into an AVL-tree with elements initially?
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 nodes is in the worst case (involving comparisons to find the insertion position, plus at most two tree rotations taking time).
When inserting elements into an AVL tree that already contains elements:
- For the first inserted element, the tree has elements, taking time.
- For the -th inserted element (), the tree contains elements, taking time.
- The total time across all insertions is:
To establish the asymptotic bound:
- Upper bound: Each term satisfies . Therefore:
- Lower bound: Considering the final insertions where the tree size is at least :
- Combining both bounds yields .
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.