AVL Tree
AVL Tree
• Advantages of BST
• Disadvantages of BST
• Calculate Height of a BST
• Height of left sub tree
• Height of right sub tree
• Balance Factor
• AVL Tree
• AVL Tree Operations
• AVL Tree Rotations
Advantages of Binary Search Tree
1. Binary Search only works for a fixed data set,
but Binary Search Trees can be used for
dynamic data sets also.
2. It can be used to find the sorted order of a
dynamic data set.
3. If elements are inserted/deleted randomly , it
is actually faster to do it in a binary search
tree than linked lists and arrays.
Disadvantages of Binary Search Trees
• The overall shape of the tree depends on the order
of insertion. If data is inserted in a sorted order or
the tree becomes heavier in one direction, the
insert/search operations become expensive.
• For storing left and right child of each node a lot
of space is required.
• The implementation of Binary Search Tree is not
much used since it cannot guarantee logarithmic
complexity. Hence, we have to use self balancing
implementations.
Calculating Height of a Tree
• Example:
8
10 15
20 25 30 45
40
• The height of a node is the number of edges present in
the longest path connecting that node to a leaf node.
• The Height of node 25 is 1.
• The Height of node 10 is 2.
• The Height of node 8 is 3.
Height of left sub tree
• Root of this tree is 8.
• Height of the left sub
Tree for node 8 is 3.
• Height of the left sub
Tree for node 10 is 1.
Height of right sub tree
• Root node 8.
• Height of right sub tree
For node 8 is 2.
• Height of right sub-tree
For node 15 is 1.
• Height of right sub-tree
For node 10 is 2.
Balanced Binary Search Tree
• Balanced BST, i.e. height of left and right sub trees are equal or not
much differences at any node.
• Example: A full binary tree of n nodes is balanced tree.
• The search in can be done in
log(n) time, O(log n).
Depth of recursion is O(log n)
Time complexity O(log n)
Space complexity O(log n)
45
45
36 63
36
63
27
39 54 72
27 39
54 72
18
45
36 63
27 72
39 54
70
Height Balanced Tree
• A binary search tree in which every node has a
balance factor of -1, 0 or 1 is said to be height
balanced.
In another word: at any node, the difference of heights
of two sub-trees is at most 1
45
36 63
36
63
27
39 54 72
27 39
54 72
18
45
45
36 63
36 63
27
27 72 39
39 54
42
70
AVL Trees
• AVL tree is a self-balancing binary search tree in which the
heights of the two sub-trees of a node may differ by at most
one.
• AVL tree is a height-balanced tree
• Self-balancing : re-balance after BST inserting or deleting
• AVL is named by its inventor G.M.Adelson-Velskii and
E.M.Landis
– provided the height balance property and insertion and deletion
operations that maintain the property in time complexity of
O(log n)
Operations on AVL Tree
45 45
Critical node
36 63
36 63
27
27 39 54 72
39 54 72
18
18
9
Example of AVL Tree insert
• Insert 50,25,10,5,7,3,30,20,8,15 into AVL tree.
AVL Tree Rotations
• In AVL tree, after performing operations like
insertion and deletion we need to check
the balance factor of every node in the tree.
• If every node satisfies the balance factor
condition then we conclude the operation
otherwise we must make it balanced.
• Whenever the tree becomes imbalanced due to
any operation we use rotation operations to
make the tree balanced.
AVL Tree Rotations
• Rotation operations are used to make the tree
balanced.
• Rotation is the process of moving nodes either to left
or to right to make the tree balanced.
• There are four rotations and they are classified into
two types.
LL rotation
• The new node is inserted in the left sub tree of the left sub-tree of
the critical node.
RR Rotation
• The new node is inserted at right sub-tree of the right sub-tree of
the critical node.
LR rotation
• The new node is inserted in the right sub-tree of the left sub-tree
of the critical node.
RL rotation
• The new node is inserted in the left sub-tree of the right sub-tree
of the critical node.
Deleting a Node from an AVL Tree
• Deleting algorithm
Step 1. delete node as BST
Step 2. if not height balanced, rebalance by rotation
• If the resulting BST of step 1 is not height-
balanced, find the critical node. There are four
possible cases similar to the inserting. Then do
corresponding rotation by the case
Deleting a Node from an AVL Tree
• Case 1. R-rotation
2
1
45
45
0
36 63 0
36 63 -1
1
1
27 -1 27 39
72
39 0
-1
0 0 0
18 40
18 40 0
-1
36
1
27 45 1
0
-1
18 0
39 63
0
40
Deleting a Node from an AVL Tree
§ Consider the AVL tree given below and delete 72 from it.
§ Case 2. L-R-rotation
2
1
45
-1 45
-1
36 63 0
36 63
0 0 0
0 0
27
39 72 27
39
0 0
0 0
37 41 1
39 37 41
1
36 45 1
1 0 0
27 37 41 63
0
AVL implementation node design
struct Node
{
int data;
int height; // or using balance factor
int data; // application data associated with data
struct Node *left, *right;
};
AVL right-rotate
node *right_rotate(node *y) {
node *x = y->left; y x
node *T2 = x->right; / \ Right Rotation / \
x T3 – – – – – – – > T1 y
// perform rotation /\ <------- /\
T1 T2 Left Rotation T2 T3
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
return x;
}
AVL left-rotate
node *left_rotate(node *x)
{ y x
node *y = x->right; / \ Right Rotation / \
x T3 – – – – – – – > T1 y
node *T2 = y->left; /\ <------- /\
// perform rotation T1 T2 Left Rotation T2 T3
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
return y;
}
AVL insert
node* insert(node* root, int data) {
if (root == NULL) return(new_node(data));
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
else return root;
// do rotation by cases
AVL implementation insert
if (balance > 1 && balance_factor(root->left) >=0 )
return right_rotate(root);
if (balance < -1 && balance_factor(root->left) <=0 )
return left_rotate(root);
if (balance > 1 && balance_factor(root->left) < 0) {
root->left = left_rotate(root->left);
return right_rotate(root);
}
if (balance < -1 && balance_factor(root->left) > 0) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}