AVL Trees
AVL Trees
AVL TREES
● An AVL tree is a binary search tree with a
balance condition
● Introduced by Andelson-Velskii and E.M.
Landis in 1962.
○ To ensure that the height of the tree is O(logN)
○ For every node in the AVL tree, the height of left
subtree and the height of the right subtree can
differ by at most one.
○ We are concerned about minimizing worst case
behavior. We wish to consider trees that have a
AVL TREES
● The height of the left subtree minus the
height of the right subtree of a node is
called the balance of the node. For an
AVL tree, the balances of the nodes are
always -1, 0 or 1.
○ The height of an empty tree is defined to be
-1.
● Given an AVL tree, if insertions or
deletions are performed, the AVL tree
AVL TREES
5 7
2 8 2 8
1 4 7 1 4
3 3 5
- double rotation
○ For case 2 and 3
AVL TREES: SINGLE ROTATION
i.e. LL case
AVL TREES: SINGLE
ROTATION
i.e.RR case
AVL TREES: SINGLE
ROTATION
● Example: 3 2 1 4 5 6 7
● Construct AVL tree (height balanced)
AVL TREES: SINGLE
ROTATION
Insert 4, 5
Insert 6
AVL TREES: SINGLE ROTATION
Insert 7
● Example:
AVL TREES: DOUBLE
ROTATION
● Insert 14
AVL TREES: DOUBLE ROTATION
● Insert 12
AVL TREES: DOUBLE ROTATION
● Inserting 9
Right Rotation of node Q Left Rotation of node P
Let P be Q's left child. Let Q be P's right child.
Set P to be the new root. Set Q to be the new root.
Set Q's left child to be P's right Set P's right child to be Q's left
child. child.
AVL Trees: Double Rotation
● Left-Right Rotation (LR) (Right followed
by a Left)
a b
a
b
c a c
b
c
AVL Trees: Double Rotation
● Right-Left Rotation (RL) (Left followed by
a Right)
c b
c
b c
a a
b
a
AVL TREES
IMPLEMENTATION
AVL TREES:
IMPLEMENTATION
● After insertion, if the tree is still balanced =>
done
● Otherwise, if imbalance appears, then
determine the appropriate action: single or
double rotation
● LH: The left subtree of the current node has
more levels
● EH: The left and right subtrees have equal
height
●RH: The right subtree has more levels
AVL TREES:
IMPLEMENTATION
struct AvlNode;
if ( X < T->Left->Element )
T = SingleRotateWithLeft( T );
else
T = DoubleRotateWithLeft( T );
}
else
AVL TREES: IMPLEMENTATION
if ( X > T->Element )
{ T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T = SingleRotateWithRight( T );
else
T = DoubleRotateWithRight( T );
}
/* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T>Right ) ) + 1;
return T;
}
AVL TREES: IMPLEMENTATION
K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
K2->Height = Max( Height(K2->Left), Height(K2->Right) ) +
1;
K1->Height = Max( Height(K1->Left), K2->Height ) + 1;
return K1; /* New root */
CASE 1: RIGHT IS HIGHER
● Left rotation is performed
● Add value 60
20
20
10 40
10 40
30 50
30 50
60
CASE 1: RIGHT IS HIGHER
● Resulting Tree after left Rotation
40
20 50
10 30 60
AVL TREES: IMPLEMENTATION
Position SingleRotateWithRight( Position K1 )
{
Position K2;
K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;
K1->Height = Max( Height(K1->Left), Height(K1->Right)
) + 1;
K2->Height = Max( Height(K2->Right), K1->Height ) + 1;
return K2; /* New root */
}
CASE 2:LEFT SUBTREE IS
HIGHER
.
40
20 50
10 30 60
25 35
CASE 2:LEFT SUBTREE IS
HIGHER
● Adding node 22
40 40
20 50 30 50
10 30 60 20 35 60
10
25 35 25
22 22
CASE 2:LEFT SUBTREE IS
HIGHER
● AVL Balanced Tree
30
20 40
10 25
35 50
22
60
AVL TREES: IMPLEMENTATION