0% found this document useful (0 votes)
15 views

AVL Tree

about avl trees

Uploaded by

Keerthi Rishitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

AVL Tree

about avl trees

Uploaded by

Keerthi Rishitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

AVL Tree

• AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right
subtrees cannot be more than one for all nodes.
• The technique of balancing the height of binary trees was developed by Adelson, Velskii, and Landi and hence
given the short form as AVL tree or Balanced Binary Tree.

• Definition: Let T be a non-empty binary tree with TL and TR as its left and right subtrees. The tree is height
balanced if:

➢ TL and TR are height balanced


➢ hL - hR <= 1, where hL - hR are the heights of TL and TR
• Balance Factor (k) = height (left(k)) - height (right(k))
• The Balance factor of a node in a binary tree can have value 1, -1, 0, depending on whether the height of its
left subtree is greater, less than or equal to the height of the right subtree.

• Since AVL trees are height balance trees, operations like insertion and deletion have low time complexity.
Bf = 1 Bf = 2

Bf = 1 Bf = 1 Bf = 2 Bf = 1

Bf = 1 Bf = 0
Bf = 1 Bf = 0
Bf = 0 Bf = 0
Bf = 0
Bf = 0 Bf = 1

Bf = 0
AVL Tree
Not an AVL Tree

Why AVL Trees?


Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of
the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height
of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of
O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is the number of nodes
in the tree.
AVL Tree Examples
If you have the following tree having keys 1, 2, 3, 4, 5, 6, 7 and then the binary tree will be like the second figure:

To insert a node with a key 10 in the binary tree, the algorithm requires seven comparisons, but if you insert the
same key in AVL tree, from the above 1st figure, you can see that the algorithm will require three comparisons.
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.
• Rotation is the process of moving nodes either to left or to right to make the tree balanced.
Single Right Rotation (LL Rotation)

• When BST becomes unbalanced, due to inserting a new node into the left subtree of the left subtree of a given
node, then we perform LL rotation (single right rotation), LL rotation is clockwise rotation, which is applied on
the edge below a node having balance factor 2.

5
1 0
0 1
Insert 4 5 Insert 3 4
4 LL Rotation
5 0 0 0
0
4 3 5
3 Make it root
balanced balanced balanced
Not balanced

In such case just make median element as root and other two nodes will be the children.
Single Left Rotation (RR Rotation)

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 (single Left), RR rotation is an anticlockwise rotation, which is applied on the edge below a
node having balance factor -2

-2

-1 5 0
0 5 -1 6
Insert 6 Insert 7 RR Rotation
5 0 6 0 0
6 0
5 7
7
balanced Make it root
balanced balanced
Not balanced

The node whose balance factor is other than (1, 0, -1) is termed as “critical node”. We always check from the leaf
node to root, which node is critical node.
Double Rotation (RL Rotation)

Double rotations are bit tougher than single rotation which has already explained before. RL rotation = Right
rotation + Left rotation, i.e., first Right rotation is performed on subtree and then Left rotation is performed on
full tree, i.e. the first node from the path of inserted node whose balance factor is other than -1, 0, or 1.

-2
-2
-1 5 0
5
0 7
Insert 8 5 Insert 7 1
Right Rotation -1 Left Rotation
5 0 8 0 0
7
8 0 5 8
0
7 Make it root 8
balanced balanced balanced
Not balanced Not balanced
Double Rotation (LR Rotation)

LR rotation = Left rotation + Right rotation, i.e., first Left rotation is performed on subtree and then Right
rotation is performed on full tree. Full tree means the first node from the path of inserted node whose balance
factor is other than -1, 0, or 1.

2 2

1 5 5 0
0 1 4
Insert 3 5 Insert 4 -1
Left Rotation Right Rotation
5 0 3 4 0 0
3 0 0 3 5
4 3
balanced balanced balanced
Not balanced Not balanced
Make it root
AVL Tree Construction

Construct an AVL tree using following keys.


14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20
1 0

14 14
1 0
0 1 0 1 -1
14 14
14 11 17 11 17
0 0 0
0 0 0
17 11 17
7 7 53
Insert 14
Insert 17
Insert 11
Insert 7 Insert 53
1
0
14
14
2 -1
0 -1
11 17
Insert 4 7 17
1 0
LL Rotation
0 0 0
7 53
4 11 53 2
0 14
4 1
-2 -1
14
7 17
-1 -1
0 -2 0
7 17
4 11 53
Insert 13 0 -1 0 Insert 12
1
R
4 11 53
13
0 0 L
13
RL Rotation
12
2 2
1
14 14
14
-2 -1 -2 -1
-1 -1
7 17 7 17
7 17
0 -2 0 0 -2 0
0 0 0
4 11 53 4 11 53
Left Rotation 4 12 53
1 Right Rotation -1
R
0 0
13 12
0 R0 11 13

12 13
2
1
Insert 8 14
14
-2 -1
0 -1
7 17
11 17
0 1 0 Right Rotation
R 0 1 0
4 12 53
7 12 53
1 0
L 0 0 0
11 13
4 8 13
0

RL Rotation

Here find out the median element of 7, 12, and 11. It is 11, so it will be the root (parent) and 7 and 11 will
be the children.
1
1
Insert 60 14
14
0 -2
0 -2
11 17
11 17
0 1 1
0 1 R 1
7 12 53
7 12 53
0 0 0 0
0 0 0 R 0
4 8 13 60
4 8 13 60
0
1

14 14
1
0 0
0
53 53
11 11
Left Rotation Insert 19
0 -1 -1 0
0 -1 0 0

60 7 12 17 60
7 12 17
0 0 0
0 0 0
4 13 4 8 19
8
0
0
14
1 14
0
1
53 0
11
53
-1 -1 0 Insert 16 11
0
60 0 -1 0 0
7 12 17
7 12 17 60
0 0
0
4 0 0
8 19 0 0
4 8 19
16
-1
-1
14
14
2
2 1
1
53
53 11
Insert 20 11 L
0 0 -1 0
0 0 -1 0
7 12 17 60
7 12 17 60
0 0 R -1
0 0 1 0
0 4
4 8 19
8 19 16
16
0
0
20
20

again find out the median element of 53, 17, and 19. It is 19, so it will be the root (parent) and 17 and 53 will be
the children.
-1
0
14 14
2
1 0
1
53 19
11 11
L
0 0 -1 0 RL Rotation 0
0 0 1
7 12 17 60 53
7 12 17
0 0 R -1 0 0 0
0 0 0
4 8 19 4
16 8 20 60
16
0

20
Construct an AVL tree using the following keys
H, I, J, B, A, E, C, F, D, G

Step 1. Insert H, I, J
Step 2. Insert B, A

On inserting the above elements, especially in case of A, the BST becomes unbalanced as the Balance Factor of H and
I is 2, we consider the first node from the last inserted node i.e. H. Since the BST from H is left-skewed, we will
perform LL Rotation on node H.
Step 3. Insert E

• On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel from E to I we find
that it is inserted in the left subtree of right subtree of I, we will perform LR Rotation on node I. LR = RR + LL
rotation.
• We first perform RR rotation on node B.
• Now perform LL rotation on the node I.
Step 4. Insert C, F, D

• On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2, since if we travel from D
to B we find that it is inserted in the right subtree of left subtree of B, we will perform RL Rotation on node I. RL
= LL + RR rotation.
• We first perform LL rotation on node E.
• Now perform RR rotation on node B
Step 5. Insert G

• On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we travel from G to H, we find
that it is inserted in the left subtree of right subtree of H, we will perform LR Rotation on node I. LR = RR + LL
rotation.
• We first perform RR rotation on node C.
• We then perform LL rotation on node H.
AVL Tree Construction/Insertion

Insert the following elements in an AVL tree and write down the preorder, inorder, and postorder traversals for
the resultant tree.

a. 5, 6, 7, 1, 3, 4, 9, 7, 2

b. 6, 4, 10, 8, 5, 15, 2, 12, 0, 1, 3

c. 9, 17, 20, 5, 2, 4, 18, 3, 6, 0, 7

d. 7, 15, 3, 10, 6, 4, 2, 30, 5, 8,0,1

e. 8,3, 5, 2, 10, 7, 0, 4, 19, 9, 6, 11


Insert the following elements in an AVL tree.

You might also like