Lecture 20 - AVL Trees
Lecture 20 - AVL Trees
20-AVL 1
Balanced and Unbalanced BST
1 4
2 2 5
3
1 3
4
4 Is this “balanced”?
5
2 6 6
1 3 5 7 7
20-AVL 2
Balanced Tree
6 5
Insert 2 &
4 9 complete tree 2 8
1 5 8 1 4 6 9
20-AVL 3
AVL Trees – Good but not Perfect Balance
• Recall:
– An empty tree has height –1
– A tree with a single node has height 0
20-AVL 4
AVL Trees
20-AVL 5
AVL Trees – Example
5 7
2 8 2 8
1 4 7 1 4
3 3 5
Height of node =h
Balance Factor (BF) = hleft - hright
Empty height = -1
Tree A (AVL) Tree B (AVL)
height=2 BF=1-0=1
1
6 6
4 9 0 4 9
0 0
1 5 -1 -1 1 5 8 -1
-1 -1 -1 -1 20-AVL -1 -1 -1 -1 -1 -1 7
AVL Trees – Example
20-AVL 8
AVL Trees – Example
20-AVL 12
AVL Trees – Example
20-AVL 13
AVL Trees – Example
20-AVL 14
AVL Trees – Example
20-AVL 15
AVL Trees
20-AVL 17
Transformation (Rotation) of AVL Trees
• Only nodes on the path from insertion point to root node have
possibly change in height
• Follow the path up to the root, find the first node (i.e., deepest)
whose new balance violates the AVL condition
– Call this node “a”
20-AVL 18
Balancing AVL Trees – Example
20-AVL 19
Balancing AVL Trees – Example
20-AVL 20
Balancing AVL Trees – Example
• The heights of each of the sub-trees from the insertion point to the
root are increased by one
20-AVL 21
Balancing AVL Trees – Example
20-AVL 22
Balancing AVL Trees – Example
20-AVL 23
Fixing Imbalance By Rotation
20-AVL 24
Single Rotation in an AVL Tree
2 2
6 6
1 2 1
1
4 9 4 8
0 0 1 0 0 0 0
1 5 8 1 5 7 9
0
7
20-AVL 25
Right Rotation (RR) in an AVL Tree
a
b
b
c a
c
20-AVL 26
Right Rotation (RR) – Example
• Consider adding 6
20-AVL 27
Right Rotation (RR) – Example
• Height of each of the trees in the path back to the root are
increased by one
20-AVL 28
Right Rotation (RR) – Example
• Height of each of the trees in the path back to the root are
increased by one
– Only root node (i.e., 36) violates the balancing factor
20-AVL 29
Right Rotation (RR) – Example
17 27
20-AVL 30
When to Perform Right Rotation (RR)?
• Case RR
– Insertion into left subtree of left child of node “a”
– Left tree is heavy (i.e., hleft > hright )
20-AVL 31
When to Perform Right Rotation (RR)
• Case RR
– Insertion into left subtree of left child of node a (RR)
– Left tree is heavy (i.e., hleft > hright )
20-AVL 32
Right Rotation (RR) – Examples
20-AVL 33
Left Rotation (LL) in an AVL Tree
a
b
b
a c
c
20-AVL 34
Left Rotation (LL) – Example
• Consider adding 67
– To fix the imbalance, we perform left rotation of root (i.e., 36)
20-AVL 35
When to Perform Left Rotation (LL)
• Case LL
– Insertion into right subtree of right child of node a
– Right tree is heavy (i.e., hleft < hright )
20-AVL 36
When to Perform Left Rotation (LL)
• Case LL
– Insertion into right subtree of right child of node a (LL)
– Right tree is heavy (i.e., hleft < hright )
20-AVL 37
Single Rotation May Be Insufficient
20-AVL 38
Right-Left Rotation (RL) or "Double Right"
c c
b
a b
Right
a c
b Left
a
20-AVL 40
When to Perform Right-Left Rotation (RL)
• Case RL
– Insertion into right subtree of left child of node “a”
20-AVL 41
When to Perform Right-Left Rotation (RL)
• Case RL
– Insertion into right subtree of left child of node a (RL)
Left heavy
Right heavy
20-AVL 42
Left-Right Rotation (LR) or "Double Left"
a a
b
c b Left
a c
b Right c
20-AVL 43
Left-Right Rotation (LR) or "Double Left" – Example
• Consider adding 67
– To fix the imbalance, we perform left-right (LR) rotation of root
20-AVL 44
When to Perform Left-Right Rotation (LR)
• Case LR
– Insertion into left subtree of right child of node a
20-AVL 45
When to Perform Left-Right Rotation (LR)
• Case LR
– Insertion into left subtree of right child of node a (LR)
Right heavy
Left heavy
20-AVL 46
Summary: How And When To Rotate?
Case 2: LL
20-AVL 47
Summary: How And When To Rotate?
Case 3: RL
4. Insertion into left subtree of
right child of a (case LR)
Case 4: LR
20-AVL 48
Summary: How And When To Rotate?
if tree is right heavy {
if tree's right subtree is left heavy {
Perform Left-Right rotation
}
else {
Perform Single Left rotation
}
}
…
…
else if tree is left heavy {
if tree's left subtree is right heavy {
Perform Right-Left rotation
}
else {
Perform Single Right rotation
}
}
20-AVL 49
AVL Tree – Complete Example
Insert 4, 5
20-AVL 50
AVL Tree – Complete Example
Insert 6
20-AVL 51
AVL Tree – Complete Example
Insert 7
20-AVL 52
AVL Tree – Complete Example
20-AVL 53
AVL Tree – Complete Example
Insert 14
20-AVL 54
AVL Tree – Complete Example
Insert 13
20-AVL 55
AVL Tree – Complete Example
Insert 12
20-AVL 56
AVL Tree – Complete Example
Insert 11, 10
20-AVL 57
AVL Tree – Complete Example
20-AVL 58
AVL Tree Implementation
20-AVL 59
AVL Trees: Implementation
struct AvlNode {
ElementType Element;
AvlTree Left;
AvlTree Right;
int Height;
};
typedef struct AvlNode *Position;
typedef struct AvlNode *AvlTree;
AvlTree MakeEmpty( AvlTree T );
Position Find( ElementType X, AvlTree T );
Position FindMin( AvlTree T );
Position FindMax( AvlTree T );
AvlTree Insert( ElementType X, AvlTree T );
AvlTree Delete( ElementType X, AvlTree T );
ElementType Retrieve( Position P );
20-AVL 60
AVL Trees: Implementation
int Height(Position P )
{
if( P == NULL )
return -1;
else
return P->Height;
}
20-AVL 61
AVL Trees: Insert Function
AvlTree Insert( ElementType X, AvlTree T ) {
if ( T == NULL ) { /* Create and return a one-node tree */
T = new AvlNode;
T->Element = X;
T->Left = T->Right = NULL;
}
else if( X < T->Element ) {
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T ); // RR rotation
else
T = DoubleRotateWithLeft( T ); // RL rotation
}
else 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 ); // LL rotation
else
T = DoubleRotateWithRight( T ); // LR rotation
} /* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
} 20-AVL 62
AVL Trees: Insert Function
AvlTree Insert( ElementType X, AvlTree T ) {
if ( T == NULL ) { /* Create and return a one-node tree */
T = new AvlNode;
T->Element = X;
T->Left = T->Right = NULL;
}
else if( X < T->Element ) {
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T ); // RR rotation
else
if ( TT == NULL ) { /* Create
= DoubleRotateWithLeft( T ); // and return a
RL rotation one-node tree */
} T = new AvlNode;
else T->Element = )X;{
if( X > T->Element
T->Right = Insert( X, T->Right );
T->Left = T->Right = NULL;
if( Height( T->Right ) - Height( T->Left ) == 2 )
} if( X > T->Right->Element )
T = SingleRotateWithRight( T ); // LL rotation
else
T = DoubleRotateWithRight( T ); // LR rotation
} /* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
} 20-AVL 63
AVL Trees: Insert Function
AvlTree Insert( ElementType X, AvlTree T ) {
if ( T == NULL ) { /* Create and return a one-node tree */
T = new AvlNode;
T->Element = X;
T->Left = T->Right = NULL;
}
else if( X < T->Element ) {
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T ); // RR rotation
else
T = DoubleRotateWithLeft( T ); // RL rotation
}
else if( X > T->Element ) {
else if( = XInsert(
T->Right < T->Element
X, T->Right );) {
T->Left
if( = Insert(
Height( T->Right X, T->Left
) - Height( T->Left ) ==);
2 )
if( X > T->Right->Element )
if( Height( T->Left ) - Height( T->Right ) == 2 )
T = SingleRotateWithRight( T ); // LL rotation
if( X < T->Left->Element )
else
T = SingleRotateWithLeft(
T = DoubleRotateWithRight( T ); //
T ); // LR rotation RR rotation
} /* Else else
X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
T = DoubleRotateWithLeft( T ); // RL rotation
return T;
}
} 18-AVL 64
AVL Trees: Insert Function
AvlTree Insert( ElementType X, AvlTree T ) {
if ( T == NULL ) { /* Create and return a one-node tree */
else if( X > T->Element ) {
T = new AvlNode;
T->Right
T->Element = X; = Insert( X, T->Right );
if( =Height(
T->Left T->Right = T->Right
NULL; ) - Height( T->Left ) == 2 )
} if( X > T->Right->Element )
else if( X < T->Element ) {
T = SingleRotateWithRight( T ); // LL rotation
T->Left = Insert( X, T->Left );
elseT->Left ) - Height( T->Right ) == 2 )
if( Height(
T = DoubleRotateWithRight(
if( X < T->Left->Element ) T ); // LR rotation
} T = SingleRotateWithLeft( T );
else
/* Else X is in the tree already; we'll do nothing */
T = DoubleRotateWithLeft( T );
}
else 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 ); // LL rotation
else
T = DoubleRotateWithRight( T ); // LR rotation
} /* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
} 20-AVL 65
AVL Trees: Insert Function
AvlTree Insert( ElementType X, AvlTree T ) {
if ( T == NULL ) { /* Create and return a one-node tree */
T = new AvlNode;
T->Element = X;
T->Left = T->Right = NULL;
}
else if( X < T->Element ) {
T->Left = Insert( X, T->Left );
if( Height( T->Left ) - Height( T->Right ) == 2 )
if( X < T->Left->Element )
T = SingleRotateWithLeft( T ); // RR rotation
else
T = DoubleRotateWithLeft( T ); // RL rotation
}
else if( X > T->Element ) {
T->Right = Insert( X, T->Right );
if( Height( T->Right ) - Height( T->Left ) == 2 )
if( X > T->Right->Element )
T->Height = Max( Height( T->Left ), Height(
T = SingleRotateWithRight( T ); // LL rotation
T->Right ) ) + 1;
returnelseT;
T = DoubleRotateWithRight( T ); // LR rotation
} /* Else X is in the tree already; we'll do nothing */
T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
return T;
} 20-AVL 66
AVL Trees: LL Rotation
Position SingleRotateWithRight( Position K1 ) {
Position K2;
K2 = K1->Right; // K1: node whose balance factor is violated
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 */
}
K1 K2
20
K2 40
Left Rotation
K1
10 40
20 50
30 50
10 30 60
60
20-AVL 67
AVL Trees: RR Rotation
Position SingleRotateWithLeft( Position K1 ) {
Position K2;
K2 = K1->Left; // K1: node whose balance factor is violated
K1->Left = K2->Right;
K2->Right = K1;
K1->Height = Max( Height(K1->Left), Height(K1->Right) ) + 1;
K2->Height = Max( Height(K2->Left), K1->Height ) + 1;
return K2; /* New root */
}
20-AVL 68
AVL Trees: LR Rotation
Position DoubleRotateWithRight( Position K1)
{
/* RR rotation between K3 and K2 */
K1->Right = SingleRotateWithLeft( K1->Right );
/* LL rotation between K1 and K2 */
return SingleRotateWithRight( K1 );
} Single Right Rotation at K3
Single Left rotation at K1
20-AVL 69
LR Rotation
• Adding node 22
– Requires double rotation!
40 40
20 50 20 50
10 30 60 10 30 60
25 35 25 35
22
20-AVL 70
LR Rotation
40 40
K1 K1
20 50 20 50
K3 K2
10 30 60 10 25 60
K2 K3
25 35 22 30
22
35
20-AVL 71
LR Rotation
40 40
K1 K2
20 50 25 50
K2 K1 K3
10 25 60 20 30 60
K3
22 30 10 22 35
35
20-AVL 72
AVL Trees: RL Rotation
Position DoubleRotateWithLeft( Position K3 )
{
/* LL rotation between K1 and K2 */
K3->Left = SingleRotateWithRight( K3->Left );
/* RR rotation between K3 and K2 */
return SingleRotateWithLeft( K3 );
}
Single Left Rotation at K1
Single Right rotation at K3
20-AVL 73
AVL Tree Deletion
20-AVL 74
AVL Tree: Deletion
20-AVL 75
AVL Tree: Deletion
7 7
Delete(9)
4 9 4
3 3
20-AVL 76
AVL Tree: Deletion
1 7 0 1 7
Delete(3)
1 4 0 9 01 4 0 9
0 3
20-AVL 77
AVL Tree: BST Deletion
• Case 2: Node to be deleted has degree 1 (i.e., node with one child)
– Consider deleting node containing 84
20-AVL 79
AVL Tree: BST Deletion
20-AVL 80
AVL Tree: BST Deletion
20-AVL 81
AVL Tree Deletion
20-AVL 82
AVL Tree Deletion: Example
20-AVL 83
AVL Tree Deletion: Example
• The balance factor changes at only nodes between the root and
the parent node of the physically deleted node
– Starting at the action position find the first imbalanced node
20-AVL 84
AVL Tree Deletion: Example
c
b
20-AVL 85
AVL Tree Deletion: Example
20-AVL 86
AVL Tree Deletion: Multiple Imbalance
• Deleting a node may cause more than one AVL imbalance !!!
20-AVL 87
AVL Tree Deletion: Example
20-AVL 88
AVL Tree Deletion: Example
20-AVL 89
AVL Tree Deletion: Example
20-AVL 90
AVL Tree Deletion: Example
20-AVL 91
AVL Tree Deletion: Example
20-AVL 92
AVL Tree Deletion: Example
20-AVL 93
AVL Tree Deletion: Example
20-AVL 94
AVL Tree Deletion: Example
20-AVL 95
AVL Tree Deletion: Example
20-AVL 96
AVL Tree Deletion: Example
20-AVL 97
Any Question So Far?
20-AVL 98