Read an integer n, then n integer keys, and insert them one by one into an initially empty AVL tree. After each insertion print one line:
Insert K: no rotationif the tree did not need rebalancing,Insert K: C at Zif a rebalancing happened, whereCis the case (LL,RR,LRorRL) andZis the key of the lowest unbalanced node,Insert K: duplicateif K was already in the tree (the tree does not change).
After all insertions print the root key (none for an empty tree) and how many single (LL/RR) and double (LR/RL) rotations were performed.
Input:
7 10 20 30 25 28 27 27
Output:
Insert 10: no rotation
Insert 20: no rotation
Insert 30: RR at 10
Insert 25: no rotation
Insert 28: LR at 30
Insert 27: RL at 20
Insert 27: duplicate
Root: 25
Single: 1, Double: 2
Classify the case from the balance factors: bf(Z) = +2 is LL when Z's left child has bf ≥ 0 and LR otherwise; bf(Z) = −2 is RR when Z's right child has bf ≤ 0 and RL otherwise.