Unit-4 3
Unit-4 3
Unit-4 3
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two
subtrees of any node differ by at most one; therefore, it is also said to be height-balanced.
Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases,
where n is the number of nodes in the tree prior to the operation. The AVL tree is named after
its two inventors, G.M. Adelson-Velskii and E.M. Landis, who published it in their 1962 paper
"An algorithm for the organization of information."
Definition: An empty binary search tree is an AVL tree. If T is a non empty binary search tree
with TL and TR as its left and right subtrees, then T is an AVL tree iff
(1) TL and TR are AVL Trees and
(2) | hL – hR | <=1 where hL and hR are the heights of TL and TR respectively.
In other words, an AVL tree is a binary search tree in which the balance factor for each
and every node of the tree should be 0,1 or -1.
The balance factor of a node is the height of its right subtree minus the height of its left
subtree, and a node with balance factor 1, 0, or -1 is considered balanced.
A node with any other balance factor is considered unbalanced and requires rebalancing the
tree. The balance factor is either stored directly at each node or computed from the heights of
the subtrees.
Inserttion : the process of inserting a node into the binary search tree.
Deletion : The process of deleting a node from Binary Search Tree.
Search: The process of verifying whether a particular node exist in the BST or not.
Traversal: The process of visiting each and every node once.
Insertion:
The process of inseting a node into an AVL tree is same as inserting a node into a Binary
Search Tree. i.e. Compare the node to be inserted with the root. If the node value is less than
the root insert into the left subtree , if the node value is greater than the root then insert into
the right subtree. In this procees a new node will be always inserted as the leaf node. Then,
Insert operation may cause balance factor to become 2 or –2 for some node
only nodes on the path from insertion point to root node have possibly changed in
height
So after the Insert, go back up to the root , node by node, updating heights
If a new balance factor (the difference hleft-hright) of a node in this path is 2 or –2,
adjust tree by doing rotation around the node.
Let the node that needs rebalancing be .
The rebalancing is performed through any of four rotation algorithms.
Thus once we perform a rotation at node , then we do not require to perform any
rotation at any ancestor of .
Some modifications done on AVL tree in order to rebalance it are called rotations of AVL
tree.
Rotations are mainly classified into two types. They are :
(If the node to be rebalanced is
o Outside Cases (require single rotation) :
Insertion into left subtree of left child of .( LL Rotation)
Insertion into right subtree of right child of . (RR Rotation)
o Inside Cases (require double rotation) :
Insertion into right subtree of left child of .(RL Rotation)
Insertion into left subtree of right child of . (LR Rotation)
When BST becomes unbalanced, due to a node is inserted into the left subtree of the left
subtree of C, then we perform LL rotation, LL rotation is clockwise rotation, which is applied
on the edge below a node having balance factor 2.
b) RR Rotation: ( Right Right Case )
When BST becomes unbalanced, due to a node is inserted into the right subtree of the
right subtree of A, then we perform RR rotation, RR rotation is an anticlockwise rotation,
which is applied on the edge below a node having balance factor -2
Right Rotation
b) RL Rotation ( Right Left case): A node is inserted in the Right Subtree of the
left child of unbalanced node
z z x
/ \ / \ / \
y T4 Right Rotate (y) x T4 Left Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
d) LR Rotation (Left Right Case) : A node is inserted in the Left Subtree of the
Right child of unbalanced node
z z x
/ \ / \ / \
T1 y Left Rotate (y) T1 x Right Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
x T4 T2 y T1 T2 T3 T4
/ \ / \
T2 T3 T3 T4
Deletion:
The procedure for deleting a node in AVL tree is:
/* DISPLAY OPERATION*/
display(struct AvlNode *temp)
{
if (temp!=NULL)
{
printf("%5d",temp->element);
preorder(temp->left);
preorder(temp->right);
}
}
/* SEARCH OPERATION*/
void find(int key, struct AvlNode *temp)
{
if(temp==NULL)
{
printf("\nElement Not found");
return;
}
else if(temp->element==key)
{
printf("\nElement found");
return;
}
else if(key < temp->element)
find(key, temp->left);
else
find(key, temp->right);
}