AVL.ppt
AVL.ppt
CHAPTER 10 OF TEXTBOOK
1. BINARY SEARCH TREE (BST)
2. THREADED TREE
3. AVL TREE
4. RED BLACK TREE
5. SPLAY TREE
Balanced BST
Balanced BST, i.e. height of left and right subtrees are
equal or not much differences at any node
• If the balance factor of a node is -1, then it means that the left
sub-tree of the tree is one level lower than that of the right
sub-tree. Such a tree is called Right-heavy tree.
36 63
0 1
0 0
27 72
39 54
0
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.
2
-1 YES -1
45 NO
45
0 36 63 0
-1
0
36 63
0
27 -1
1
0 0 39
27 72
39 54
-1
42
0
70
3. 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 Adelson-Velskii and Landis
– provided the height balance property and insertion and
deletion operations that maintain the property in time
complexity of O(log n)
Features of AVL Trees
• The key advantage of using an AVL tree is that it takes O(log n)
time to perform search, insertion and deletion operations in
average case as well as worst case (because the height of the tree
is limited to O(log n).
• The nodes whose balance factors will change are those which lie
on the path between the root of the tree and the newly inserted
node.
Inserting a Node in an AVL Tree
• The possible changes which may take place in any node on the
path are as follows:
▪ Initially the node was either left or right heavy and after
insertion has become balanced.
▪ Initially the node was balanced and after insertion has
become either left or right heavy.
▪ Initially the node was heavy (either left or right) and
the new node has been inserted in the heavy
sub-tree thereby creating an unbalanced sub-tree.
Such a node is said to be a critical node.
▪ Critical node is the nearest ancestor node on the path from
the inserted node to the root whose balance factor is
neither –1, 0, nor 1
Example of critical node
Cases of critical nodes
• Height of sub-tree where a new node is inserted can
increase at most 1.
• The balance factor can crease by 1 or decrease by 1, so
the balance factor of critical node is either 2 or -2.
• There are four types of rebalancing rotations and
application of these rotations depends on the position of
the inserted node with reference to the critical node.
Cases of critical nodes
• The four categories of rotations are:
• 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 in the 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.
Cases of critical nodes LL rotation
LL rotation
2 0
x y
h+2 h+1
right-rotation
1 0
y T3 T1 x
h+1 h-1 h h
T1 T2 T2 T3
h h-1 h-1 h-1
Cases of critical nodes LL rotation
Cases of critical nodes RR rotation
RR Rotation
-2
x 0
y
h+2
h+1
left-rotation
-1
0
T1 y x T3
h-1 h+1 h h
T2 T3
T1 T2
h-1 h
h-1 h-1
Cases of critical nodes RR rotation
L-R-Rotation
2
x
x
-1
left-rotation z T4 right-rotation z
y T4
y y x
T1 z T3
T1 T2 T3 T4
T2 T3 T1 T2
Cases of critical nodes LR Rotation
Cases of critical nodes LR Rotation
R-L-Rotation
-2
x right-rotation x
z
1 left-rotation
T1 y T1 z x y
z T4
y
T2 T1 T2 T3 T4
T2 T3
T3 T4
Cases of critical nodes LR Rotation
AVL Example
AVL Example
Example of AVL tree insert
Insert 50, 25, 10, 5, 7, 3, 30, 20, 8, 15 into AVL tree
Example of AVL tree insert
Insert 50, 25, 10, 5, 7, 3, 30, 20, 8, 15 into AVL tree
Deleting a Node from an AVL Tree
• Deleting algorithm
Step 1. delete node as BST
Step 2. if not height balanced, rebalance by rotation
Case 1. R-rotation
1 2 -1
45 45 36
0 0 1
-1 0 27 45 1
36 63 36 63
1 1 0
-1
27 -1 0
72 27 39 18
39 0 39 63
0 -1
0 0
0 0
18 40 18 40
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
-1 39
1
36 63 -1 0
36 63 1
36 45
0 0 0 0 0
27
27 1 0 0
39 72 39
0 0 27 37 41 63
0 0
37 41 0
37 41
Deletion in AVL trees
Deletion in AVL trees
• DELETE 4, 8,6,5,2,1,7 FROM FOLLOWING AVL TREE
8
3
2 4 7 10
1 6 9 11