2. AVL Tree
The AVL stands for Adelson-
Velskii and Landis, who are the
inventors of the AVL tree.AVL tree is a
self-balancing binary search tree in
which heights of the left sub trees and
right sub trees of any node differ by at
most one.
3. Balancing Factor
Balance factor=height of left sub tree –
height of the right sub tree.
The value of balance factor should always
be -1, 0 or +1.
4. Need for height balanced
trees
If the binary search tree is either left skewed or
right skewed(not balanced) , the searching time
will be increased. Because searching time
depends upon the height of the tree. h=log(n)
where h is the height of the tree and n is the
number of nodes in the binary tree.It takes
log(n) for searching.
6. Need for height balanced
trees
The disadvantage of a binary search tree
is that its height can be as large as N-1
This means that the time needed to
perform insertion and deletion and many
other operations can be O(N) in the worst
case
We want a tree with small height
A binary tree with N node has height at
least Q(log N)
Thus, our goal is to keep the height of a
binary search tree O(log N)
Such trees are called balanced binary
7. How to balanace AVL tree (Rotation)
An insertion or deletion may cause an
imbalance in an AVL tree.
An AVL tree causes imbalance when
any of following condition occurs:
i. An insertion into Right child’s right
subtree.
ii. An insertion into Left child’s left subtree.
iii. An insertion into Right child’s left
subtree.
iv. An insertion into Left child’s right
subtree.
8. How to balanace AVL tree (Rotation)
Whenever the tree becomes imbalanced due to
any operation we use rotation operations to
make the tree balanced.
Rotation is the process of moving nodes
either to left or to right to make the tree
balanced.
9. Single Left Rotation (LL Rotation)
If a tree becomes unbalanced, when a node is
inserted into the right subtree of the right
subtree, then we perform a single left rotation .
If BF(node) = +2 and BF(node -> left-child) = +1, perform LL
rotation.
10. Single Right Rotation (RR Rotation)
AVL tree may become unbalanced, if a node
is inserted in the left subtree of the left
subtree. The tree then needs a right rotation.
If BF(node) = -2 and BF(node -> right-child) = 1, perform RR
rotation.
12. Left Right Rotation (LR Rotation)
(Double Rotation)
AVL tree may become unbalanced, if a node is inserte
the left subtree of the right subtree .The tree needs
Rotation is a sequence of single left rotation followed
single right rotation.
If BF(node) = -2 and BF(node -> right-child) = +1, perform RL rotation.
13. Right Left Rotation (RL Rotation)
(Double Rotation)
AVL tree may become unbalanced, if a node is
inserted in the right subtree of the left subtree
.The tree needs The RL Rotation is sequence
of single right rotation followed by single left
rotation.
If BF(node) = +2 and BF(node -> left-child) = -1, perform LR
rotation.
14. node * RR(node *T) //insert right subtree of right
{
T=rotateleft(T);
return(T);
}
node * LL(node *T) //insert left subtree of left
{
T=rotateright(T);
return(T);
}
node * LR(node *T) // insert left subtree of right
{
T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
node * RL(node *T) //insert right subtree of left
{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
18. Operations on an AVL Tree
The following operations are performed
on AVL tree...
Search
Insertion
Deletion
search operation is similar to Binary
search tree.
19. Insertion Operation in AVL Tree
Step 1 - Insert the new element into the
tree using Binary Search Tree insertion
logic.
Step 2 - After insertion, check
the Balance Factor of every node.
Step 3 - If the Balance Factor of every
node is 0 or 1 or -1 then go for next
operation.
Step 4 - If the Balance Factor of any
node is other than 0 or 1 or -1 then that
tree is said to be imbalanced. In this
case, perform suitable Rotation to make
it balanced and go for next operation.
20. Rotating conditions
If BF(node) = +2 and BF(node -> left-child) =
+1, perform LL rotation.
If BF(node) = -2 and BF(node -> right-child) =
1, perform RR rotation.
If BF(node) = -2 and BF(node -> right-child) =
+1, perform RL rotation.
If BF(node) = +2 and BF(node -> left-child) =
1, perform LR rotation.
25. Operations on an AVL Tree
The following operations are performed
on AVL tree...
Search
Insertion
Deletion
search operation is similar to Binary
search tree.
28. Deletion in AVL Trees
Step 1: Find the element in the tree.
Step 2: Delete the node, as per the
BST Deletion.
Step 3: Two cases are possible:-
Case 1: Deleting from the right subtree.
Case 2: Deleting from left subtree.
29. Case 1: Deleting from the right
subtree.
1A. If BF(node) = +2 and BF(node ->
left-child) = +1, perform LL rotation.
1B. If BF(node) = +2 and BF(node ->
left-child) = -1, perform LR rotation.
1C. If BF(node) = +2 and BF(node ->
left-child) = 0, perform LL rotation.
30. Case 2: Deleting from left subtree.
2A. If BF(node) = -2 and BF(node ->
right-child) = -1, perform RR rotation.
2B. If BF(node) = -2 and BF(node ->
right-child) = +1, perform RL rotation.
2C. If BF(node) = -2 and BF(node ->
right-child) = 0, perform RR rotation.
42. Applications of AVL Tree
AVL trees are used for frequent insertion.
It is used in Memory management
subsystem of linux kernel to search
memory regions of processes during
preemption.
Sort the following database:
Dictioonary
Google search engine
some sites to take data faster
sites which have large amount of data
example is Facebook.
43. In Class Exercises
Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25