0% found this document useful (0 votes)
17 views7 pages

Exp Avl Trees C

The document outlines the implementation of AVL Trees, which are self-balancing binary search trees that maintain a balance factor of -1, 0, or 1 for efficient operations. It details the algorithms for insertion and deletion, including the necessary rotations to maintain balance. The source code provided demonstrates the insertion, deletion, and in-order traversal of an AVL tree in C.

Uploaded by

snezshanks3005
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)
17 views7 pages

Exp Avl Trees C

The document outlines the implementation of AVL Trees, which are self-balancing binary search trees that maintain a balance factor of -1, 0, or 1 for efficient operations. It details the algorithms for insertion and deletion, including the necessary rotations to maintain balance. The source code provided demonstrates the insertion, deletion, and in-order traversal of an AVL tree in C.

Uploaded by

snezshanks3005
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/ 7

EXPERIMENT NO.

-14

Aim: Implement AVL Trees(insert, print and delete).

Theory:

An AVL tree (Adelson-Velsky and Landis Tree) is a self-balancing binary search


tree where the difference between the heights of left and right subtrees (called the
balance factor) is at most 1 for all nodes. This property ensures that the tree
remains balanced, leading to better time complexities for operations like insertion,
deletion, and searching.

Balance Factor:

 The balance factor of a node in an AVL tree is defined as:

Balance Factor (BF) = Height of Left Subtree - Height of Right Subtree

The balance factor of any node must be one of {-1, 0, 1}. If it goes beyond
this range, the tree is rebalanced.

Rotations:

To maintain the balance of the tree after insertions and deletions, rotations are
performed:

1. Left Rotation (Single Rotation): This is needed when a node becomes


unbalanced due to a right-heavy subtree.
2. Right Rotation (Single Rotation): This is needed when a node becomes
unbalanced due to a left-heavy subtree.
3. Left-Right Rotation (Double Rotation): This is a combination of left and
right rotations.
4. Right-Left Rotation (Double Rotation): This is also a combination of right
and left rotations.
Algorithm:

For Insertion in an AVL Tree:

1. Standard BST Insert: Insert the new node as in a regular binary search tree.
2. Update Heights: After the insertion, update the height of each node starting
from the newly inserted node up to the root.
3. Check Balance: For each node, check the balance factor. If the balance
factor is outside the range {-1, 0, 1}, perform the appropriate rotations.
4. Rebalance the Tree: Perform the necessary rotations (Left or Right
rotations) to restore balance.

For Deletion in an AVL Tree:

1. Standard BST Delete: Delete the node as you would in a regular binary
search tree.
2. Update Heights: After deletion, update the height of the nodes from the
deleted node up to the root.
3. Check Balance: For each node, check the balance factor. If it is outside the
acceptable range, perform the necessary rotations.
Source Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int data;
struct Node* left;
struct Node* right;
int height;
} Node;

int height(Node* node) {


if (node == NULL) return 0;
return node->height;
}

int balanceFactor(Node* node) {


if (node == NULL) return 0;
return height(node->left) - height(node->right);
}

Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;

x->right = y;
y->left = T2;

y->height = (height(y->left) > height(y->right) ? height(y->left) : height(y->right)) + 1;


x->height = (height(x->left) > height(x->right) ? height(x->left) : height(x->right)) + 1;

return x;
}

Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;

y->left = x;
x->right = T2;

x->height = (height(x->left) > height(x->right) ? height(x->left) : height(x->right)) + 1;


y->height = (height(y->left) > height(y->right) ? height(y->left) : height(y->right)) + 1;
return y;
}

Node* insert(Node* node, int key) {

if (node == NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = key;
newNode->left = newNode->right = NULL;
newNode->height = 1;
return newNode;
}

if (key < node->data)


node->left = insert(node->left, key);
else if (key > node->data)
node->right = insert(node->right, key);
else
return node;

node->height = 1 + ((height(node->left) > height(node->right)) ? height(node->left) :


height(node->right));

int balance = balanceFactor(node);

if (balance > 1 && key < node->left->data)


return rightRotate(node);

if (balance < -1 && key > node->right->data)


return leftRotate(node);

if (balance > 1 && key > node->left->data) {


node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->data) {


node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

void inorder(Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

Node* minValueNode(Node* node) {


Node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}

Node* deleteNode(Node* root, int key) {

if (root == NULL) return root;

if (key < root->data)


root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {

if (root->left == NULL) {
Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
Node* temp = root->left;
free(root);
return temp;
}

Node* temp = minValueNode(root->right);


root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}

root->height = 1 + ((height(root->left) > height(root->right)) ? height(root->left) :


height(root->right));

int balance = balanceFactor(root);

if (balance > 1 && balanceFactor(root->left) >= 0)


return rightRotate(root);

if (balance < -1 && balanceFactor(root->right) <= 0)


return leftRotate(root);
if (balance > 1 && balanceFactor(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
}

if (balance < -1 && balanceFactor(root->right) > 0) {


root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}

int main() {
Node* root = NULL;

root = insert(root, 10);


root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 25);
root = insert(root, 5);

printf("In-order traversal of the AVL tree: ");


inorder(root);
printf("\n");

root = deleteNode(root, 10);


printf("In-order traversal after deleting 10: ");
inorder(root);
printf("\n");

return 0;
}

Output:

You might also like