Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?)

What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?)
def balance_case(order):
    a, b, c = order
    first = 'L' if b < a else 'R'
    second = 'L' if c < b else 'R'
    return first + second
print(balance_case((30, 10, 20)), balance_case((10, 20, 30)), balance_case((30, 20, 10)))
  1. A. LL RR LR
  2. B. LR RL LL
  3. C. RL RR LL
  4. D. LR RR LL

Correct answer: D. LR RR LL

30 → 10 goes left (L), then 20 goes right of 10 (R): an LR case, fixed by a left rotation at 10 then a right rotation at 30 (double rotation), making 20 the root. 10, 20, 30 is RR (single left rotation); 30, 20, 10 is LL (single right rotation).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic