PRESENTED BY-
AKASH KUMAR CHAUBEY
CLASS – MCA(1)
SEMESTER : 2
ROLL NO. –MCA/25014/18
AVL TREE
WHAT IS BINARY SEARCHTREE
• Every node contain only two
child.
• Small value at left child node
and large value at right child
nodeas compare to Root node.
• The main advantage of BST is
that it is easy to perform
like inserting,
traversing and
operations
searching,
deleting.
 The disadvantageof skewed binary search tree is that
theworstcase timecomplexityof a search is O(n).
50
40 60
30 45 55 70
Symmetric
40
50
30
20
10
Skewed
 There is a need to maintain the binary search tree to
be of the balanced height, so that it is possible to
obtained for the search option a time complexity of
O(log N) in the worst case.
 One of the most popular balanced tree was introduced
by
A binary tree is said to be an AVL tree if T is
a root of tree and T(L) is its left sub tree
and T(R) is its right sub-tree of tree T and
H(T(L)) and H(T(R)) are the heights of the
left and
right sub-trees of T respectively, and
|H(T(L))
- H(T(R))|<= 1 Then we called T is AVLtree.
Height of left sub-tree minus height of
Right leftsub-tree
[H(T(L)) - H(T(R))]
Note:
 An emptybinary tree isan AVLTree
 Balance Factorshouldbe
-1 0 1
T
T(L) T(R)
T
T(L) T(R)
40
50
30
 If after the insertion of the element the, the
balance factor of any node is affect this
problem is overcome by using rotation.
 Rotation is use to restore the balance of
search tree.
 Toperform the rotation
it is necessary toidentify
a specific node A whose
balance factor (BF) is
neither 0, -1 0r 1 and
which is the nearest
ancestor to the inserted
node on the path from
the inserted node to the
root.
40
50
30
 Single rotation switches the roles of the parent and
child while maintaining the searchorder.
 Werotatea nodeand itschild, child becomes parent
 Parent becomes Right child in LLRotation.
 Parent becomes Left child in RR Rotation.
 Inserted node is in the
left sub-tree of the left
sub-tree of A.
 For LL Rotations identify
A and B, where B is a
Left child of A because
insertion is on leftside.
 Then make A as a child
of B.
40
50
30
40
50
30
50
40
30
Parentbecomes
Rightchild
50
40
30
 Inserted node is in the
right sub-tree of the
Right sub-tree of A.
 For RR Rotations
identify A and B,
where B is a Right
child of A because
insertion is on Right
side.
 Then make A as a child
of B.
60
50
70
60
50
70
70
60
50
Parentbecomes
Left child
50
60
70
 Single rotationdoes not fix the LR rotationand RL
rotation.
 LR and RL rotationsrequireadoublerotation,
involving three node.
 Doublerotation is equivalent toa sequenceof two
single rotation.
 1st rotation on original tree
2nd rotation on the newtree
 Inserted node is inthe
right sub-tree of the left
sub-tree of A.
 For LR Rotations identify A
B and C, where B is a Left
child of A and C is right
child of B because Inserted
node is in the right sub-
tree of the left sub-tree of
A.
 Then make A and B as a
child of C.
40
50
45
40
50
45
50
45
40
 Inserted node is in theleft
sub-tree of the Right sub-
tree of A .
 For RL Rotations identifyA
B and C, where B is aRight
child of A and C is Left
child of B because Inserted
node is in the left sub-tree
of the Right sub-tree of A.
 Then make A and B as a
child of C.
60
50
55
60
50
55
60
55
50
50
60
55
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}
 // If this node becomes unbalanced, then there are 4 cases





















// Right Left Case
if (balance < -1 && key < node-
>right->key)
{
node->right = rightRotate(node-
>right); return leftRotate(node);
}
5
5
7
5
7
19
5
7
19
19
7
5
19
7
5
19
7
5
12
19
7
5
12
10
19
7
5
12
10
7
5
19
12
10
7
5
19
12
10
7
5
19
12
10
15
7
5
19
12
10
15
12
7 19
155 10
12
7 19
155 10
12
7 19
155 10
18
12
7 19
155 10
18
12
7
5 10
18
15 19
12
7
5 10
18
15 19
12
7
5 10
18
15 19
20
12
7
5 10
18
15 19
20
25
12
7
5 10
18
15 19
20
25
12
7
5 10
18
15 20
19 25
12
7
5 10
18
15 20
19 25
23
12
7
5 10
18
15 20
19 25
23
12
7
5 10
20
18 25
2315 19
www.tutorialspoint.com
www.geeksforgeeks.com
Data Structure by ABDUL BARI
AVL Tree in Data Structure

AVL Tree in Data Structure

  • 1.
    PRESENTED BY- AKASH KUMARCHAUBEY CLASS – MCA(1) SEMESTER : 2 ROLL NO. –MCA/25014/18 AVL TREE
  • 2.
    WHAT IS BINARYSEARCHTREE • Every node contain only two child. • Small value at left child node and large value at right child nodeas compare to Root node. • The main advantage of BST is that it is easy to perform like inserting, traversing and operations searching, deleting.
  • 3.
     The disadvantageofskewed binary search tree is that theworstcase timecomplexityof a search is O(n). 50 40 60 30 45 55 70 Symmetric 40 50 30 20 10 Skewed
  • 4.
     There isa need to maintain the binary search tree to be of the balanced height, so that it is possible to obtained for the search option a time complexity of O(log N) in the worst case.  One of the most popular balanced tree was introduced by
  • 5.
    A binary treeis said to be an AVL tree if T is a root of tree and T(L) is its left sub tree and T(R) is its right sub-tree of tree T and H(T(L)) and H(T(R)) are the heights of the left and right sub-trees of T respectively, and |H(T(L)) - H(T(R))|<= 1 Then we called T is AVLtree. Height of left sub-tree minus height of Right leftsub-tree [H(T(L)) - H(T(R))] Note:  An emptybinary tree isan AVLTree  Balance Factorshouldbe -1 0 1 T T(L) T(R)
  • 6.
  • 7.
  • 8.
     If afterthe insertion of the element the, the balance factor of any node is affect this problem is overcome by using rotation.  Rotation is use to restore the balance of search tree.
  • 9.
     Toperform therotation it is necessary toidentify a specific node A whose balance factor (BF) is neither 0, -1 0r 1 and which is the nearest ancestor to the inserted node on the path from the inserted node to the root. 40 50 30
  • 10.
     Single rotationswitches the roles of the parent and child while maintaining the searchorder.  Werotatea nodeand itschild, child becomes parent  Parent becomes Right child in LLRotation.  Parent becomes Left child in RR Rotation.
  • 11.
     Inserted nodeis in the left sub-tree of the left sub-tree of A.  For LL Rotations identify A and B, where B is a Left child of A because insertion is on leftside.  Then make A as a child of B. 40 50 30
  • 12.
  • 13.
     Inserted nodeis in the right sub-tree of the Right sub-tree of A.  For RR Rotations identify A and B, where B is a Right child of A because insertion is on Right side.  Then make A as a child of B. 60 50 70
  • 14.
  • 15.
     Single rotationdoesnot fix the LR rotationand RL rotation.  LR and RL rotationsrequireadoublerotation, involving three node.  Doublerotation is equivalent toa sequenceof two single rotation.  1st rotation on original tree 2nd rotation on the newtree
  • 16.
     Inserted nodeis inthe right sub-tree of the left sub-tree of A.  For LR Rotations identify A B and C, where B is a Left child of A and C is right child of B because Inserted node is in the right sub- tree of the left sub-tree of A.  Then make A and B as a child of C. 40 50 45
  • 17.
  • 18.
     Inserted nodeis in theleft sub-tree of the Right sub- tree of A .  For RL Rotations identifyA B and C, where B is aRight child of A and C is Left child of B because Inserted node is in the left sub-tree of the Right sub-tree of A.  Then make A and B as a child of C. 60 50 55
  • 19.
  • 20.
    // Left LeftCase if (balance > 1 && key < node->left->key) return rightRotate(node); // Right Right Case if (balance < -1 && key > node->right->key) return leftRotate(node); // Left Right Case if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); }  // If this node becomes unbalanced, then there are 4 cases                      // Right Left Case if (balance < -1 && key < node- >right->key) { node->right = rightRotate(node- >right); return leftRotate(node); }
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.