AVL TREE
AVL TREE
Description:-
Adelson-Velskii and Landis are the people who discovered it, so the name came from their names i.e., AVL. It is commonly referred to as a hei ght binary tree.
An AVL tree is one that has one of the following characteristics at each of its nodes.
If a node's longest path in its left subtree is longer than its longest path in its right subtree, the node is said to be "left heavy."
If the longest path in a node's right subtree is one more long than the longest path in its left subtree, the node is said to be "right heavy."
If the longest paths in the right and left subtrees are equal, a node is said to be balanced.
LL Rotation: When a new node is added as the left child of the unbalanced node's left subtree.
RR Rotation: When a new node is added as the right child of the right subtree of an unbalanced node, this process is known as RR Rotation.
LR Rotation: When a new node is added as the left child of an unbalanced node's right subtree.
RL Rotation: When a new node is added as the right child of an unbalanced node's left subtree.
Program:-
/*
* AVL Tree Program in C
*/
#include<stdio.h>
#include<stdlib.h>
// function prototyping
struct node* create(int);
struct node* insert(struct node*, int);
struct node* delete(struct node*, int);
struct node* search(struct node*, int);
struct node* rotate_left(struct node*);
struct node* rotate_right(struct node*);
int balance_factor(struct node*);
int height(struct node*);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
int main()
{
int user_choice, data;
char user_continue = 'y';
struct node* result = NULL;
switch(user_choice)
{
case 1:
printf("\nEnter data: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("\nEnter data: ");
scanf("%d", &data);
root = delete(root, data);
break;
case 3:
printf("\nEnter data: ");
scanf("%d", &data);
result = search(root, data);
if (result == NULL)
{
printf("\nNode not found!");
}
else
{
printf("\n Node found");
}
break;
case 4:
inorder(root);
break;
case 5:
preorder(root);
break;
case 6:
postorder(root);
break;
case 7:
printf("\n\tProgram Terminated\n");
return 1;
default:
printf("\n\tInvalid Choice\n");
}
return 0;
}
if (root == NULL)
{
return NULL;
}
if (x > root->data)
{
root->right = delete(root->right, x);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else if (x < root->data)
{
root->left = delete(root->left, x);
if (balance_factor(root) == -2)
{
if (balance_factor(root->right) <= 0)
{
root = rotate_left(root);
}
else
{
root->right = rotate_right(root->right);
root = rotate_left(root);
}
}
}
else
{
if (root->right != NULL)
{
temp = root->right;
while (temp->left != NULL)
temp = temp->left;
root->data = temp->data;
root->right = delete(root->right, temp->data);
if (balance_factor(root) == 2)
{
if (balance_factor(root->left) >= 0)
{
root = rotate_right(root);
}
else
{
root->left = rotate_left(root->left);
root = rotate_right(root);
}
}
}
else
{
return (root->left);
}
}
root->ht = height(root);
return (root);
}
if(root->data == key)
{
return root;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
/*
Sample output:-
------- AVL TREE --------
1. Insert
2. Delete
3. Search
4. Inorder
5. Preorder
6. Postorder
7. EXIT
Enter Your Choice: 1
Enter data: 2
Do you want to continue? y
Node found
Result:-
AVL tree for a given set of elements which are stored in a file. And implement insert and delete
operation on the constructed tree. Write contents of tree into a new file using in-order is successfully
executed.