0% found this document useful (0 votes)
139 views

AVL Tree

An AVL tree is a self-balancing binary search tree where the heights of the left and right subtrees of any node differ by at most one. It provides O(log n) time for search, insert, and delete operations by rebalancing the tree after insertions or deletions using rotations. There are four cases of rotations - right-right, right-left, left-left, left-right - to balance the tree based on the balance factor of each node.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

AVL Tree

An AVL tree is a self-balancing binary search tree where the heights of the left and right subtrees of any node differ by at most one. It provides O(log n) time for search, insert, and delete operations by rebalancing the tree after insertions or deletions using rotations. There are four cases of rotations - right-right, right-left, left-left, left-right - to balance the tree based on the balance factor of each node.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

AVL Tree

AVL tree
Invented By:
1962
Invented

G. M. Adelson-Velskii and E. M. Landis


Invented by

Time com plexity


in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(log n)

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.

An AVL tree has the following properties:


1. Every sub-tree is an AVL tree.
2. The sub-trees of every node differ in height by at most
one.
The balance factor of a node is the height of its left sub-tree minus the height of its right sub-
tree (sometimes opposite) and a node with balance factor 1, 0, or −1 is considered balanced.

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;
parentright = node1  left;
node1  left= parent;
return node1;
}
struct avl_node* right_left_rotation(struct avl_node *parent)
{
struct avl_node *node1;
node1=parent  right;
parentright = 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(nodeleft) >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 < rootdata) {
rootleft = insert(rootleft, val);
root=balancing(root);
}
else if (val > rootdata) {
rootright = insert(rootright, 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", treedata);
preorder(treeleft);
preorder(treeright);
}
}
void postorder(struct avl_node *tree)
{
if(tree == NULL)
return;
else {
postorder(treeleft);
postorder(treeright);
printf("%d\t",treedata);
}
}
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.

You might also like