























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Cases for rotation and the insertion algorithm in avl trees. It explains the conditions for single and double rotations, and the role of height difference in maintaining the balance of the tree. The document also includes examples of avl tree building and insertion using c++ code.
Typology: Slides
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























1
k 1
k 2
Level n
Level n-
Level n-
k 1
k 2
new
new
2
k 1
k 2
4
k 3
k 1
A
new
1
2
New node inserted a either of the
two spots
B C
k 2
D
new’
3
5
k 3
k 1
A
new
1
2
New node inserted a either of the
two spots
B C
k 2
D
new’
7
A
new
B
D
C
A new’
new
B
D
C
new’
k 3
k 2
k 1
Rotate right
k 2
k 3
k 1
8
k 1
k 3
k 2
k 1
k 3
k 2
Rotate right
10
X
(null)
Z
(null)
Y
k 1
k 2
11
k 1
k 3
k 2
Rotate right
13
k 3
k 2
k 1
14
k 3
k 2
k 1
Rotate right
16
k 3
k 2
k 1
17
Rotate left
19
TreeNode
avlInsert(TreeNode
{
if( info < root->getInfo() ){
root->se tLeft(avlInsert(root->getLeft(), info));
int htdiff = height(root->getLeft()) –
height(root->getRight());
if( htdiff == 2 )
if( info < root->getLeft()->getInfo() )
root = singleRightRotation( root );
else
root = doubleLeftRightRotation( root );
}
20
TreeNode
avlInsert(TreeNode
{
if( info < root->getInfo() ){
root->setLeft(avlInsert(root->getLeft(), info));
int htdiff = height(root->getLeft()) –
height(root->getRight());
if( htdiff == 2 )
if( info < root->getLeft()->getInfo() )
root = singleRightRotation( root );
else
root = doubleLeftRightRotation( root );
}