AVL Daraxti
AVL Daraxti
AVL trees are rigidly balanced this is why red-black trees are faster to construct because
O(logN) running time is guaranteed they are not as balanced as AVL trees
(it is as fast as a binary search tree can be) (but it is not as fast as AVL trees)
AVL Trees
• all the operations are the same as we have seen with binary search
trees (insertion and removal)
• after every insertion and removal operations we have to check
whether the tree has become imbalanced or not
• if the tree is imbalanced then we have to make rotations
AVL Trees
AVERAGE-CASE WORST-CASE
space complexity O(N) O(N)
insertion O(logN) O(logN)
deletion (removal) O(logN) O(logN)
search O(logN) O(logN)
AVL Trees
height = max( left child’s height , right child’s height ) + 1
5 23
1 the height of a NULL node
is -1 to be consistent
(this is why leaf nodes have height 0)
AVL Trees
height = max( left child’s height , right child’s height ) + 1
12 12
4 4 20
5 23
1 1
in an imbalanced tree the running time of in a balanced tree the running time of
operations can be reduced to even O(N) linear operations are O(logN) always
Running time complexity
AVL Trees
2 2
12 12
-1 1
4 1 NULL 4 1 20
0
0 0 23
1 0 1 5
in an imbalanced tree the running time of in a balanced tree the running time of
operations can be reduced to even O(N) linear operations are O(logN) always
Running time complexity
AVL Trees
• AVL trees are exactly the same as binary search trees
• the only difference is that we track the h heigth parameters of the
nodes in the tree
all subtrees height parameter can not
| hleft – hright | > 1 differ more than 1
(otherwise the tree is imbalanced)
• we have to update the binary search tree and make rotations if it gets
imbalanced
• this is why we have the h height parameters – we just have to check
the differences in height parameters after every operation
AVL Trees
• this is a left-heavy case when the left
subtree contains more nodes
2 • in this case the height of the root node is 2
12
so it is a positive value
-1
4 1 NULL • usually the difference in the height
pramateres is called balance factor
0 0
1 5
hleft – hright
positive balance factors mean
left-heavy cases
AVL Trees
• this is a right-heavy case when the right
subtree contains more nodes
2 • in this case the height of the root node is -2
12
so it is a negative value
-1 1 • usually the difference in the height
NULL 20
pramateres is called balance factor
0
23
hleft – hright
negative balance factors mean
right-heavy cases
AVL Trees
(Algorithms and Data
Structures)
AVL Trees Rotations
• we have to track the h height parameters for all the nodes in the binary
search tree
• we can calculate the balance factors for the nodes
• have to make rotations if necessary to rebalance search trees
D
B
right rotation
B E
A D
A C left rotation C E
AVL Trees Rotations
C
AVL Trees Rotations
ROTATION CASE 1
2
A
-1
NULL
B 1
C A
AVL Trees Rotations
ROTATION CASE 2
E
AVL Trees Rotations
ROTATION CASE 2
2
1
-1
NULL D
0
negative balance factors mean
right-heavy cases E
AVL Trees Rotations
ROTATION CASE 2
B E
AVL Trees Rotations
ROTATION CASE 3
D
C
AVL Trees Rotations
ROTATION CASE 3 2
-1
NULL
B
1
0 C
AVL Trees Rotations
ROTATION CASE 3
D
B
AVL Trees Rotations
ROTATION CASE 3
B D
AVL Trees Rotations
ROTATION CASE 4
E
AVL Trees Rotations
ROTATION CASE 4
2
-1 1
NULL F
0
E
AVL Trees Rotations
ROTATION CASE 4
F
AVL Trees Rotations
ROTATION CASE 4
B F
AVL Trees Rotations
• rotations are extremely fast – we just have to update the references in
O(1) constant running time
• this operation does not change the properties of the tree
• the in-order traversal remains the same as well as the parent-child
relationships in the tree
• THERE MAY BE OTHER ISSUES BECAUSE OF ROTATIONS
• we have to check up to the root node whether to make further
rotations or not – it takes O(logN) running time
AVL Trees
(Algorithms and Data
Structures)
AVL Trees
32
10 55
1 19 41
16
AVL Trees
INSERT(12)
32
10 55
1 19 41
16
AVL Trees
INSERT(12)
32
10 55
1 19 41
16
AVL Trees
INSERT(12)
32
10 55
1 19 41
16
AVL Trees
INSERT(12)
32
10 55
1 19 41
16
AVL Trees
INSERT(12)
32
10 55
1 19 41
16
AVL Trees
INSERT(12)
32
10 55
1 19 41
16
AVL Trees
32
10 55
1 19 41
16
12
AVL Trees
32
10 55
1 19 41
16
0
12
AVL Trees
32
10 55
1 19 41
16 1
0
12
AVL Trees
32
10 55
1 19 2 41
16 1
0
12
AVL Trees
32
10 55
1 19 2 41
-1
16 1 NULL
10 55
1 19 2 41
-1
16 1 NULL
10 55
1 16 41
12 19
AVL Trees
3
32
2
1
10 55
1 16 1 41 0
0
0 0
12 19
AVL Trees
(Algorithms and Data
Structures)
Balanced Binary Trees
Applications
1.) GAME ENGINES
• if deletions and insertions are frequent then AVL tree is not the best
option possible because of the huge number of rotations
• trees support quite a lot operations (huge advantage)
• red-black trees and B-trees are more popular