



The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains an integer ‘N’ which represents the number of nodes in the tree.
The next line of each test case contains ‘N’ single space-separated integers, representing the value of each node.
For each test case, print the ‘AVL_TREE’ after inserting all the nodes in level order.
Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^4
1 <= ‘DATA’ <= 10^5
Time Limit: 1 second
We have to implement ‘AVL_TREE’ from scratch and we know ‘AVL_TREE’ follows the ‘BST’ properties. So first, we insert a new node into ‘AVL_TREE’ according to the ‘BST’ properties.
‘BST’ property:
DATA(left) < DATA(root) < DATA(right) for every node.
And the difference between heights of left and right subtrees cannot be more than one for all nodes.
After inserting the new node in our tree according to the ‘BST’ properties our tree may be unbalanced (i.e the difference between the height of left and right subtree more than one for a node).
So for making this tree balance we have to perform some operations:
Here, T1, T2, and T3 are the subtrees of the trees.
For re-balancing the tree by performing appropriate rotations we have 4 possible cases that need to be handled.
Left-Left case:
Left-Right case:
Right-Right case:
Right-Left case:
Here is the algorithm: