AVL Tree
AVL Tree
AVL tree
Invented By:
1962
Invented
O(log n) O(log n)
Insert
O(log n) O(log n)
Delete
Definition:
An AVL tree is a self-balancing binary search tree in which heights of the two child sub-trees of
any node differ by at most one.
struct avl_node {
int data;
struct avl_node *left;
struct avl_node *right;
};
int find_height(struct avl_node *node)
{
int height=0;
if (node !=NULL){
int left_height= find_height(node left);
int right_height= find_height(node right);
int max=(left_height > right_height) ? left_height : right_height;
height = 1+ max;
}
return height;
}
int get_diff(struct avl_node *node)
{
int left_height=find_height(node left);
int right_height=find_height(node right);
int b_factor= left_height - right_height;
return b_factor;
}
struct avl_node* right_right_rotation(struct avl_node *parent)
{
struct avl_node *node1;
node1=parent right;
parentright = node1 left;
node1 left= parent;
return node1;
}
struct avl_node* right_left_rotation(struct avl_node *parent)
{
struct avl_node *node1;
node1=parent right;
parentright = left_left_rotation(node1);
return right_right_rotation(parent);
}
struct avl_node* left_left_rotation(struct avl_node *parent)
{
struct avl_node *node1;
node1 = parent left;
parent left = node1 right;
node1 right = parent;
return node1;
}
struct avl_node* left_right_rotation(struct avl_node *parent)
{
struct avl_node *node1;
node1= parent left;
parent left = right_right_rotation(node1);
return left_left_rotation(parent);
}
struct avl_node* balancing(struct avl_node *node)
{
int b_f= get_diff(node);
if (b_f >1) {
if (get_diff(nodeleft) >0)
node=left_left_rotation(node);
else
node=left_right_rotation(node);
}
else if(b_f < -1) {
if(get_diff(node right) >0)
node=right_left_rotation(node);
else
node=right_right_rotation(node);
}
return node;
}
struct avl_node* insert(struct avl_node *root,int val)
{
if (root==NULL) {
root = (struct avl_node*) malloc(sizeof(struct avl_node));
root data = val;
root left = NULL;
root right = NULL;
return root;
}
else if (val < rootdata) {
rootleft = insert(rootleft, val);
root=balancing(root);
}
else if (val > rootdata) {
rootright = insert(rootright, val);
root=balancing(root);
}
return root;
}
void inorder(struct avl_node *tree)
{
if(tree == NULL)
return;
else {
inorder(tree left);
printf("%d\t",tree data);
inorder(tree right);
}
}
void preorder(struct avl_node *tree)
{
if(tree == NULL)
return;
else {
printf("%d\t", treedata);
preorder(treeleft);
preorder(treeright);
}
}
void postorder(struct avl_node *tree)
{
if(tree == NULL)
return;
else {
postorder(treeleft);
postorder(treeright);
printf("%d\t",treedata);
}
}
There are four cases which need to be considered. Let P be the root of the unbalanced subtree,
with R and L denoting the right and left children of P respectively.
1. Right-Right case
If the balance factor of P is -2 then the right subtree outweights the left subtree of the given node,
and the balance factor of the right child (R) must be checked. If the balance factor of R is -1,
a single left rotation (with P as the root) is needed.
2. Right-Left case
If the balance factor of P is -2 then the right subtree outweights the left subtree of the given node,
and the balance factor of the right child (R) must be checked.
If the balance factor of R is +1, two different rotations are needed. The first rotation is
aright rotation with R as the root. The second is the left rotation with P as the root (Right-Left
case).
3. Left-Left case
If the balance factor of P is +2, then the left subtree outweighs the right subtree of the given
node, and the balance factor of the left child (L) must be checked.
If the balance factor of L is +1, a single right rotation (with P as the root) is needed.
4. Left-Right case
If the balance factor of P is +2, then the left subtree outweighs the right subtree of the given
node, and the balance factor of the left child (L) must be checked. If the balance factor of L is -1,
two different rotations are needed. The first rotation is a left rotation with L as the root. The
second is a right rotation with P as the root.