INDEX
OVERVIEW
DEFINATION
PROPRETIES
WORKING/OPERATION
ALGORITHM
EXAMPLE
IMPLEMENTATION
ADVANTAGES
DISADVANTAGES
CONCLUSION
3.
OVERVIEW
An AVL treeis a type of self-balancing binary search
tree named after its inventors, Adelson-Velsky and
Landis. It was introduced in 1962. The key feature of
an AVL tree is that it maintains its balance
automatically, ensuring that the height difference
between the left and right subtrees of any node is at
most one.
4.
DEFINITION
An AVL treedefined as a
self-balancing Binary Search
Tree (BST) where the
difference between heights
of left and right subtrees for
any node cannot be more
than one. The difference
between the heights of the
left subtree and the right
subtree for any node is
known as the balance factor
of the node.
5.
PROPERTIES
1.Balance Factor 2.Height3.Rotation
For any node in an AVL
tree, the height difference
between its left and right
subtrees (known as the
balance factor) is at most
This ensures the tree
remains balanced.
Balance Factor = (Height
of Left Subtree - Height of
Right Subtree)
The height of an AVL tree
with ( n ) nodes is ( O(log
n) ).
This property ensures
efficient operations
To maintain balance after
insertions and deletions,
AVL trees may perform
rotations.There are four
types of rotations:
Left Rotation
Right Rotation
Left-Right Rotation
Right-Left Rotation
6.
OPERATIONS
INSERTION
DELETION
SEARCHING
When inserting anode into an AVL tree, you initially
follow the same process as inserting into a Binary
Search Tree.
Deletion in an AVL tree involves removing a node, then
rebalancing the tree by performing necessary rotations to
maintain the AVL property
Searching in an AVL tree is performed similarly to searching in a
standard binary search tree (BST), where you traverse the tree
from the root, comparing the target value to node values.
7.
ALGORITHM
AVL Tree InsertionAlgorithm
1. Insert the node as you
would in a regular binary
search tree.
2. Update the balance
factor of each node.
3. Rebalance the tree if the
balance factor of any node
becomes -2 or +2 by
performing rotations.
IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
//Node structure
struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};
// Get the height of a node
int height(struct Node* node) {
if (node == NULL)
return 0;
return node->height;
}
// Create a new node
struct Node* createNode(int key)
{
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // New node is initially added at leaf
return node;
}
// Right rotate subtree rooted with y
struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
10.
x->height = max(height(x->left),height(x->right)) + 1;
// Return new root
return x;
}
// Left rotate subtree rooted with x
struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node*T2 = y->left;
// Perform rotation
y->left = x; x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// Return new root
return y;
}
// Get the balance factor of a node
int getBalance(struct Node* node) {
if (node == NULL)
return 0;
return height(node->left) - height(node->right);
}
// Utility function to return the maximum of two integers
int max(int a, int b) {
return (a > b) ? a : b;
}
// Insert a node into the AVL tree
struct Node* insert(struct Node* node, int key)
{
// 1. Perform the normal BST insertion
if (node == NULL)
return createNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Duplicate keys are not allowed
return node;
// 2. Update the height of the ancestor node
node->height = 1 + max(height(node->left), height(node->right));
11.
// 3. Getthe balance factor of this node
int balance = getBalance(node);
// 4. Balance the tree if it becomes unbalanced
// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);
// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);
// Left Right Case
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node); }
// Right Left Case
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
// Return the (unchanged) node pointer
return node;
}
// A utility function to print preorder traversal of the
tree
void preOrder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
// Driver code
int main() {
struct Node* root = NULL;
12.
// Inserting valuesinto the AVL tree
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
// Print preorder traversal of the AVL tree
printf("Preorder traversal of the AVL tree is: n");
preOrder(root);
return 0;
}
OUTPUT:-
Preorder traversal of the AVL tree
is:
30 20 10 25 40 50
AVL TREE REPRESENTATION:
30
/
20 40
/
10 25 50
13.
ADVANTAGES
1. Efficient Search:Ensures
O(log n)search time.
2. Balanced Structure:
Automatically maintains
balance after updates.
3. Good for dynamic
Datasets: Great for
systems where frequent
insertions and deletion
happens.
14.
DISADVANTAGES
1.Overhead: Marinating balanceadds
extra complexity.
2.Rotations: Multiple rotations can be
costly for certain operations, especially
if frequent.
3.Not always ideal: In some cases (like
static datasets), simpler structure's like
regular BSTs or hash maps may
perform better.
15.
CONCLUSION
The conclusion ofan AVL tree is that it is a highly efficient data structure
for maintaining sorted data, especially in scenarios with frequent insertions
and deletions. By automatically balancing itself through rotations, an AVL
tree ensures that the height remains logarithmic relative to the number of
nodes, resulting in consistently fast search, insertion, and deletion
operations.This makes AVL trees particularly useful in real-time systems
and applications where maintaining balanced data is crucial for
performance.