What is AVL Tree ?
AVL stands for ADELSON, VELSKI AND LANDIS. It is a tree representation commonly known as ‘AVL TREE’.Here we have explained an avl tree example in the figure. AVL tree is just like a binary search tree(BST) but it is a balanced tree in data structure. Here the the term balanced is used in context of height it means AVL Tree is a height balanced tree.in data structure. It is heught balanced binary search tree.If in binary search tree, at every node avl tree balance factor is 1 or 0 or -1, then it is AVL tree.
Balancing factor of a node in VAL tree is calculated using this formula
An avl tree example is given below
In the above avl tree example the balance factor of node A is 1 and balance factor of node B, C,Dand E is 0.
Why do we need AVL Trees?
Let us take tree representation as
In the first representation, tree is balanced as it has left subtree height and right subtree same. In second representation node A and node B is balanced as they have avl tree balance factor as 0 and 1 respectively whereas, node C is not balanced, Hence complete tree is not balanced. Similarly, in case of third representation it is not a balanced tree in data
AVL Tree Rotations with Example
First and second rotations are single rotations and the next two rotations are double rotations.
When a node is inserted into the right subtree of the right sub tree then it become unbalanced. Then this problem is known as “RR- PROBLEM” and to make it balanced we perform a left rotation. An avl tree rotation example for left rotation is explained below.
When a node is inserted in the left subtree of the left subtree of Root Node then it become unbalanced. This problem is known as “LL-PROBLEM” and to balance this tree we perform a right rotation. An avl tree rotation example for right rotation is explained below.
LEFT RIGHT ROTATION(LR)
If we insert a node in right of left subtree of root node then it become unbalanced then we performed Left Right rotation. Here is avl tree rotation example for left right rotation.