38-Implementation of AVL Tree-12-11-2024
38-Implementation of AVL Tree-12-11-2024
BF=0 BF=-1
2 6
TREE is balanced
Node Structure
Struct Node
{
int data;
struct Node *left;
struct Node *right;
int height;
};
LEFT DATA RIGHT HEIGHT
NODE
Height of the Node
Height(Struct Node *Node)
{
if(Node==NULL)
return 0;
return Node->Height;
}
NODE
Creation of Node
Create newNode(int value)
{
Struct Node *newnode;
newnode= malloc(sizeof(Struct node);
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
newnode->height=1;
return newNode;
NULL 10 NULL 1
}
newNODE
Balance Factor of the Node
Balance Factor(Struct Node *Node)
{
if(Node==NULL)
return 0;
return height(Node->left)-height(Node->right) ;
}
NODE
Left Rotate
Struct Node *Left Rotate( Struct node *x)
{
Struct Node *y= x->right;
Struct Node *T= y->left;
y->left =x;
x->right=T;
x->height= max(height(x->left), height(x->right))+1;
y->height= max(height(y->left), height(y->right))+1;
return y;
}
Right Rotate
Struct Node *Right Rotate( Struct node *y)
{
Struct Node *x= y->left;
Struct Node *T= x->right;
x->right=y;
y->left =T;
y->height= max(height(y->left), height(y->right))+1;
x->height= max(height(x->left), height(x->right))+1;
return x;
}
Finding Max
Max( a,b)
{
return(a>b)? a:b;
}