k1 (First Node in k2's Left Subtree) Will Be The New Root Y Moves From k1's Right To k2's Left
k1 (First Node in k2's Left Subtree) Will Be The New Root Y Moves From k1's Right To k2's Left
return k1;
1
}
singleRightRotation
TreeNode<int>* singleRightRotation(TreeNode<int>* k2)
{
if( k2 == NULL ) return NULL; k2
// k1 (first node in k2's left subtree)
// will be the new root
k1
TreeNode<int>* k1 = k2->getLeft(); Z
// Y moves from k1's right to k2's left
k2->setLeft( k1->getRight() ); Y
k1->setRight(k2);
X
return k1;
2
}
singleRightRotation
TreeNode<int>* singleRightRotation(TreeNode<int>* k2)
{
if( k2 == NULL ) return NULL;
// k1 (first node in k2's left subtree) k2
// will be the new root
TreeNode<int>* k1 = k2->getLeft(); k1
Z
// Y moves from k1's right to k2's left
k2->setLeft( k1->getRight() ); Y
k1->setRight(k2);
X
// reassign heights. First k2
int h = Max(height(k2->getLeft()),
height(k2->getRight()));
k1
k2->setHeight( h+1 );
// k2 is now k1's right subtree
k2
h = Max( height(k1->getLeft()),
k2->getHeight());
k1->setHeight( h+1 ); X
Y Z
return k1;
3
}
singleRightRotation
TreeNode<int>* singleRightRotation(TreeNode<int>* k2)
{
if( k2 == NULL ) return NULL; k2
// k1 (first node in k2's left subtree)
// will be the new root
k1
TreeNode<int>* k1 = k2->getLeft(); Z
// Y moves from k1's right to k2's left
k2->setLeft( k1->getRight() ); Y
k1->setRight(k2);
X
return k1;
4
}
singleRightRotation
TreeNode<int>* singleRightRotation(TreeNode<int>* k2)
{
if( k2 == NULL ) return NULL; k2
// k1 (first node in k2's left subtree)
// will be the new root
k1
TreeNode<int>* k1 = k2->getLeft(); Z
// Y moves from k1's right to k2's left
k2->setLeft( k1->getRight() ); Y
k1->setRight(k2);
X
return k1;
5
}
singleRightRotation
TreeNode<int>* singleRightRotation(TreeNode<int>* k2)
{
if( k2 == NULL ) return NULL; k2
// k1 (first node in k2's left subtree)
// will be the new root
k1
TreeNode<int>* k1 = k2->getLeft(); Z
// Y moves from k1's right to k2's left
k2->setLeft( k1->getRight() ); Y
k1->setRight(k2);
X
return k1;
6
}
singleRightRotation
TreeNode<int>* singleRightRotation(TreeNode<int>* k2)
{
if( k2 == NULL ) return NULL; k2
// k1 (first node in k2's left subtree)
// will be the new root
k1
TreeNode<int>* k1 = k2->getLeft(); Z
// Y moves from k1's right to k2's left
k2->setLeft( k1->getRight() ); Y
k1->setRight(k2);
X
return k1;
7
}
height
8
height
9
singleLeftRotation
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 )
{
if( k1 == NULL ) return NULL; k1
// k2 is now the new root
TreeNode<int>* k2 = k1->getRight(); k2
X
k1->setRight( k2->getLeft() ); // Y
k2->setLeft( k1 );
Y
// reassign heights. First k1 (demoted)
int h = Max(height(k1->getLeft()), Z
height(k1->getRight()));
k1->setHeight( h+1 );
// k1 is now k2's left subtree
k2
h = Max( height(k2->getRight()),
k1->getHeight()); k1
k2->setHeight( h+1 );
Z
return k2; X Y
} 10
singleLeftRotation
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 )
{
if( k1 == NULL ) return NULL; k1
// k2 is now the new root
TreeNode<int>* k2 = k1->getRight(); k2
X
k1->setRight( k2->getLeft() ); // Y
k2->setLeft( k1 );
Y
// reassign heights. First k1 (demoted)
int h = Max(height(k1->getLeft()), Z
height(k1->getRight()));
k1->setHeight( h+1 );
// k1 is now k2's left subtree
k2
h = Max( height(k2->getRight()),
k1->getHeight()); k1
k2->setHeight( h+1 );
Z
return k2; X Y
} 11
singleLeftRotation
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 )
{
if( k1 == NULL ) return NULL; k1
// k2 is now the new root
TreeNode<int>* k2 = k1->getRight(); k2
X
k1->setRight( k2->getLeft() ); // Y
k2->setLeft( k1 );
Y
// reassign heights. First k1 (demoted)
int h = Max(height(k1->getLeft()), Z
height(k1->getRight()));
k1->setHeight( h+1 );
// k1 is now k2's left subtree
k2
h = Max( height(k2->getRight()),
k1->getHeight()); k1
k2->setHeight( h+1 );
Z
return k2; X Y
} 12
singleLeftRotation
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 )
{
if( k1 == NULL ) return NULL; k1
// k2 is now the new root
TreeNode<int>* k2 = k1->getRight(); k2
X
k1->setRight( k2->getLeft() ); // Y
k2->setLeft( k1 );
Y
// reassign heights. First k1 (demoted)
int h = Max(height(k1->getLeft()), Z
height(k1->getRight()));
k1->setHeight( h+1 );
// k1 is now k2's left subtree
k2
h = Max( height(k2->getRight()),
k1->getHeight()); k1
k2->setHeight( h+1 );
Z
return k2; X Y
} 13
singleLeftRotation
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 )
{
if( k1 == NULL ) return NULL; k1
// k2 is now the new root
TreeNode<int>* k2 = k1->getRight(); k2
X
k1->setRight( k2->getLeft() ); // Y
k2->setLeft( k1 );
Y
// reassign heights. First k1 (demoted)
int h = Max(height(k1->getLeft()), Z
height(k1->getRight()));
k1->setHeight( h+1 );
// k1 is now k2's left subtree
k2
h = Max( height(k2->getRight()),
k1->getHeight()); k1
k2->setHeight( h+1 );
Z
return k2; X Y
} 14
singleLeftRotation
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 )
{
if( k1 == NULL ) return NULL; k1
// k2 is now the new root
TreeNode<int>* k2 = k1->getRight(); k2
X
k1->setRight( k2->getLeft() ); // Y
k2->setLeft( k1 );
Y
// reassign heights. First k1 (demoted)
int h = Max(height(k1->getLeft()), Z
height(k1->getRight()));
k1->setHeight( h+1 );
// k1 is now k2's left subtree
k2
h = Max( height(k2->getRight()),
k1->getHeight()); k1
k2->setHeight( h+1 );
Z
return k2; X Y
} 15
doubleRightLeftRotation
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1)
{
if( k1 == NULL ) return NULL;
// single right rotate with k3 (k1's right child)
k1->setRight( singleRightRotation(k1->getRight()));
// now single left rotate with k1 as the root
return singleLeftRotation(k1);
}
k1 k1
k3 k2
A A
k2 k3
D B
16
D
B C C
doubleRightLeftRotation
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1)
{
if( k1 == NULL ) return NULL;
// single right rotate with k3 (k1's right child)
k1->setRight( singleRightRotation(k1->getRight()));
// now single left rotate with k1 as the root
return singleLeftRotation(k1);
}
k1 k1
k3 k2
A A
k2 k3
D B
17
D
B C C
doubleRightLeftRotation
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1)
{
if( k1 == NULL ) return NULL;
// single right rotate with k3 (k1's right child)
k1->setRight( singleRightRotation(k1->getRight()));
// now single left rotate with k1 as the root
return singleLeftRotation(k1);
}
k1 k2
k2 k1 k3
A
k3
B C D
B A
D 18
C
doubleRightLeftRotation
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3)
{
if( k3 == NULL ) return NULL;
// single left rotate with k1 (k3's left child)
k3->setLeft( singleLeftRotation(k3->getLeft()));
// now single right rotate with k3 as the root
return singleRightRotation(k3);
}
k3 k3
k1 k2
D D
k2 k1
A C
B C A B 19
doubleRightLeftRotation
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3)
{
if( k3 == NULL ) return NULL;
// single left rotate with k1 (k3's left child)
k3->setLeft( singleLeftRotation(k3->getLeft()));
// now single right rotate with k3 as the root
return singleRightRotation(k3);
}
k3 k3
k1 k2
D D
k2 k1
A C
B C A B 20
doubleRightLeftRotation
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3)
{
if( k3 == NULL ) return NULL;
// single left rotate with k1 (k3's left child)
k3->setLeft( singleLeftRotation(k3->getLeft()));
// now single right rotate with k3 as the root
return singleRightRotation(k3);
}
k3 k2
k2 k1 k3
D
k1
B C D
C A
A B 21
Deletion in AVL Tree
Delete is the inverse of insert: given a
value X and an AVL tree T, delete the
node containing X and rebalance the tree,
if necessary.
Turns out that deletion of a node is
considerably more complex than insert
22
Deletion in AVL Tree
Insertion in a height-balanced tree
requires at most one single rotation or one
double rotation.
We can use rotations to restore the
balance when we do a deletion.
We may have to do a rotation at every
level of the tree: log2N rotations in the
worst case.
23
Deletion in AVL Tree
Here is a tree that causes this worse case number of
rotations when we delete A. At every node in N’s left subtree,
the left subtree is one shorter than the right subtree.
C I
A D G K
E H J L
M
24
Deletion in AVL Tree
Deleting A upsets balance at C. When rotate (D up, C down)
to fix this
C I
A D G K
E H J L
25
Deletion in AVL Tree
Deleting A upsets balance at C. When rotate (D up, C down)
to fix this
C I
D G K
E H J L
26
Deletion in AVL Tree
The whole of F’s left subtree gets shorter. We fix this by
rotation about F-I: F down, I up.
D I
C E G K
H J L
M
27
Deletion in AVL Tree
The whole of F’s left subtree gets shorter. We fix this by
rotation about F-I: F down, I up.
D I
C E G K
H J L
M
28
Deletion in AVL Tree
This could cause imbalance at N.
The rotations propagated to the root.
F K
D G J L
C E H M
29
Deletion in AVL Tree
Procedure
Delete the node as in binary search tree (BST).
The node deleted will be either a leaf or have just
one subtree.
Since this is an AVL tree, if the deleted node has
one subtree, that subtree contains only one node
(why?)
Traverse up the tree from the deleted node
checking the balance of each node.
30
Deletion in AVL Tree
31
Deletion in AVL Tree
Delete on
this side
0
4
2 6 1
1 3 5 7 2
33
Deletion in AVL Tree
0
4 4
2 6 1 2 6
1 3 5 7 2 3 5 7
remove(1)
34
Deletion in AVL Tree
Delete on
this side
Delete on
this side