How can you rotate a Binary Tree? Explain right and left rotations by taking an example.
If after we have inserted a node in a Binary search tree, the balancing factor (height of left subtree - height of right subtree) of each node remains 0, 1 and -1 then there is no need of modification or alteration of original tree. If balancing factor of any node is either +2 or -2, the tree becomes unbalanced. It can be balanced by using left rotation or right rotation or a combination of both the rotations.
1. In the tree drawn below, after inserting node 70 , the balance factor of tree at the root node (2) so the tree is rotated left to have a height balanced tree shown by (a) to (b):
2. In the tree drawn below, after inserting 10 the balance factor of tree at root node is +2 so the tree is rotated right to have height balanced tree as shown by(a) to (b) below: