4. What is AVL tree ?
• AVL tree name after it’s
inventors
Georgy Adelson
Velsky
Evgenii Landis
Adelson
Velsky
Evgenii
Landis
• Normally it is a self-
balancing binary search
tree.
• In an AVL tree the height of
child subtree of any node
differ by at most one
5. WHY AVL Tree?
Advantage of AVL tree
• Better search times for keys
the running time for a findKey(k) operation in a AVL tree is
guaranteed to be O(log(n))
Disadvantage of AVL tree
• Longer running time for insertion and deletion
Every time insertion and deletion operation must
rebalance the avl tree
6. HOW ?
There are three type of operation in AVL tree
1.Insertion
2.Deletion
3.Searching
7. 1. Insertion
For insertion we need to know about three things
those are
• Balance factor
• Rotation
Right Rotation
Left Rotation
• Case
Right-Right
Left-Left
Left-Right
Right-Left
8. Balance factor
T1 T2
B
Left Sub tree Right Sub Tree
H1 H2
Balance factor of B = Difference between left and right sub tree. [|H1-H2|}].
In AVL tree balance factor <=1.
12. Case 1: Right-Right
When we Insert a node into right of a right sub tree of a node, unbalance may
be occur. For solving This case, we need to balance tree by rote unbalanced
node to left.
50
80
90
Rote left
50
80
90
For solving Right of Right imbalance
13. Case 2: Left-Left
When we Insert data into left of a left sub tree of a node, unbalance may be
occur. For solving This case, we need to balance tree by rote unbalanced
node to right.
50
30
20
20
30
50
For solving Left of Left imbalance
Rote right
14. Case 3: Right-Left
When we Insert data into left of a Right sub tree of a node, unbalance may be
occur. For solving This case, we need to balance tree using two steps. Given
below:
50
75
80
For solving Right-Left imbalance
50
80
75
Rote right
50
75
80
Rote left
Step 1 Step 2
15. Case 4: Left-Right
When we Insert data into Right of a left sub tree of a node, imbalance may be
occur. For solving This case, we need to balance tree using two steps. Given
below:
30
35
50
For solving Left of Right imbalance
Step 1 Step 2
50
30
35
50
35
30
Rote left
Rote right
34. Deletion
Case 1
• If we delete the leaf node than there will be no change in rest of
the tree
40 (3,2)
30
(0,0)
50
(1,1)60
70
(0,0)
25
27
(1,1) (0,0)
(1,1)
35. Deletion
Case 1
• If we delete the leaf node than there will be no change in rest of
the tree
40 (3,2)
30
(0,0)
50
(0,1)60
70
(0,0)
25
27
(1,1) (0,0)
(1,1)
Delete : 50
36. Deletion
Case 2
• We can delete internal node in two way
50 (2,1)
45
(0,0)
60
30
(1,1) 40
(0,0)
(0,0)
45 (2,1)
60
30
(1,0)40
(0,0)
(0,0)
Either
We can replaced it with it’s left subtree most bigger node
37. Deletion
Or
We can replaced it with it’s right subtree most smaller node
40
(2,2)
30
(0,0)
50
(1,1) 60
(0,0)
70
(0,0)
25
(1,1) 27
(0,0)
50
(2,2)
30
(0,0)
(1,1)60
70
(0,0)
25
(1,1)27
(0,0)
42. Searching
• Searching for a specific key in an AVL tree can be done the same
way as binary search tree
400
500
600
700200
250
275
Search : 500
(500>400)
43. Searching
• Searching for a specific key in an AVL tree can be done the same
way as binary search tree
400
500
600
700200
250
275
Search : 500
(500>400)
(500<600)
44. Searching
• Searching for a specific key in an AVL tree can be done the same
way as binary search tree
400
500
600
700200
250
275
Search : 500
(500<600)
FOUND
(500=500)