Left-right Double Rotation
(ignore the caption in the img, it is not consistent with the lecture notes).
Right-left Double Rotation
C++ Code for AVL Insert Method
/* This is the function used to insert nodes satisfying the AVL condition.*/
TreeNode* avlInsert(TreeNode* root, int info) {
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())
//outside insertion case
root = singleRightRotation(root);
else //inside insertion case
root = doubleLeftRightRotation(root);
}
}
else if(info > root->getInfo()) {
root->setRight(avlInsert(root->getRight(),info));
int htdiff = height(root->getRight()) – height(root->getLeft());
if(htdiff == 2) {
if(info > root->getRight()->getInfo())
root = singleLeftRotation(root);
else
root = doubleRightLeftRotation( root );
}
}
// else a node with info is already in the tree. In // case, reset the height of this root node.
int ht = Max(height(root->getLeft()), height(root->getRight()));
// new height for root.
root->setHeight(ht + 1);
return root;
}