AVL Tree Building Example
If the balance factor
of each node in the tree is 0
then it is called a perfectly balanced tree
.
As we keep inserting nodes, we eventually reach a point like this.
We need to perform a rotation
.
Cases for Rotation
There are 4
cases of violation:
1. An assertion into the left subtree of the left child of alpha
.
2. An assertion into the right subtree of the right child of alpha
.
3. An assertion into the left subtree of the right child of alpha
.
4. An assertion into the left subtree of the right child of alpha
.
For case 1
and 2
, the single rotation is enough to balance the tree but for other cases, it is not.