C Program to Implement AVL Tree
Last Updated :
18 Jun, 2024
In C, AVL trees are self-balancing binary search trees. They maintain a balance by ensuring that the difference between the heights of their left subtrees and the right subtrees can be either 0, 1 or -1 and whenever this height property is violated, the tree balances itself using the different rotations. This balancing allows for efficient search, insertion, and deletion operations with a time complexity of O(log n) for every case. In this article, we will learn how to implement AVL tree in C programming language
AVL Tree in C
An AVL tree is a self-balancing binary search tree that was created by Adelson-Velsky and Landis, hence the name AVL. It is a height balanced tree that keeps the difference between the height of the left and right subtrees in the range [-1, 0, 1]. This difference is called balanced factor and tree is said to be unbalanced when this balance factor is outside the specified range. Unbalanced tree is balanced through specific rotation operations during insertions and deletions.

Representation of AVL Tree in C
In C, an AVL tree node is typically defined using a struct. Each node contains the data, pointers to its left and right children, and an integer to store its height.
struct AVLNode {
int key;
struct AVLNode* left;
struct AVLNode* right;
int height;
};
AVL Tree Rotations in C
Rotations are the most important part of the working of the AVL tree. They are responsible for maintaining the balance in the AVL tree. There are 4 types of rotations based on the 4 possible cases:
- Right Rotation (RR)
- Left Rotation (LL)
- Left-Right Rotation (LR)
- Right-Left Rotation (RL)
Right Rotation (RR)
The RR rotation is applied when a node becomes unbalanced due to an insertion into the right subtree of its right child. This type of imbalance is called Left Imbalance. The solution to it is to take the unbalanced node, and rotate the top edge (that is connected with parent) 90° to the right (clockwise).
Right Rotation - RR in AVL TreeLeft Rotation (LL)
The LL rotation is used in an AVL tree to balance a node that becomes unbalanced due to an insertion into the left subtree of its left child. It is called Left Imbalance. The solution to it is to take the unbalanced node, and rotate the top edge (that is connected with parent) 90° to the left(anti-clockwise).
Left Rotation - RR in AVL TreeLeft-Right Rotation (LR)
A right-left rotation is used when a left child of a node is right-heavy. It helps in balancing the tree after a double imbalance, which is another specific insertion case. We first perform a left rotation on the left child and follow it up with a right rotation on the original node.
Left Right Rotation - LR in AVL TreeRight-Left Rotation (RL)
The right left rotation is performed when a right child of a node is left-heavy. We perform a right rotation on the right child and follow it up with a left rotation on the original node.
Right Left Rotation - RL in AVL TreeImplementation of AVL Tree in C
We will implement the AVL tree along with its basic operations. Following are the basic operations of AVL tree:
Operation | Description | Time Complexity | Space Complexity |
---|
Search | Finds a specific value in the tree. | O(log n) | O(log n) |
---|
Insert | Adds a new value to the tree while maintaining balance. | O(log n) | O(1) |
---|
Delete | Removes a specific value from the tree and maintains balance. | O(log n) | O(1) |
---|
Search Operation Implementation
We implement the search operation on AVL tree using the following algorithm:
- If root is null or key is present at root, return root.
- If key is smaller than root’s key, search in left subtree.
- If key is larger than root’s key, search in right subtree.
Insert Operation Implementation
Insert operation may create an imbalance in the tree which should be handled by the program according to the case.
- If the current node is
NULL
, create a new node with the given key. - If the key is less than the current node’s key, recursively insert the key in the left subtree.
- If the key is greater than the current node’s key, recursively insert the key in the right subtree.
- After insertion, update the height of the current node.
- Calculate the balance factor of the current node.
- If the balance factor is greater than 1 (left-heavy), check for Left-Left (LL) or Left-Right (LR) case:
- LL Case: If the left child’s key is greater than the inserted key, perform a right rotation.
- LR Case: If the left child’s key is less than the inserted key, perform a left rotation on the left child, then a right rotation on the current node.
- If the balance factor is less than -1 (right-heavy), check for Right-Right (RR) or Right-Left (RL) case:
- RR Case: If the right child’s key is less than the inserted key, perform a left rotation.
- RL Case: If the right child’s key is greater than the inserted key, perform a right rotation on the right child, then a left rotation on the current node.
Delete Operation Implementation
- If the node to be deleted is not found, return
NULL
. - If the key to be deleted is less than the current node’s key, recursively delete the key in the left subtree.
- If the key to be deleted is greater than the current node’s key, recursively delete the key in the right subtree.
- If the key to be deleted is equal to the current node’s key:
- If the node has only one child or no child, replace the node with its child.
- If the node has two children, find the in order successor (smallest in the right subtree), copy its key to the node, and delete the inorder successor.
- After deletion, update the height of the current node.
- Calculate the balance factor of the current node.
- Check and fix imbalance just like in insertion.
- Return the node after performing necessary rotations to balance the tree.
C Program to Implement AVL Tree
C
// C program to implement the avl tree
#include <stdio.h>
#include <stdlib.h>
// AVL Tree node
struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};
// Function to get height of the node
int getHeight(struct Node* n)
{
if (n == NULL)
return 0;
return n->height;
}
// Function to 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;
}
// Utility function to get the maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }
// Function to get balance factor of a node
int getBalanceFactor(struct Node* n)
{
if (n == NULL)
return 0;
return getHeight(n->left) - getHeight(n->right);
}
// Right rotation function
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(getHeight(y->left), getHeight(y->right)) + 1;
x->height
= max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}
// Left rotation function
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(getHeight(x->left), getHeight(x->right)) + 1;
y->height
= max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
// Function to insert a key into AVL tree
struct Node* insert(struct Node* node, int key)
{
// 1. Perform standard 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 // Equal keys are not allowed in BST
return node;
// 2. Update height of this ancestor node
node->height = 1
+ max(getHeight(node->left),
getHeight(node->right));
// 3. Get the balance factor of this ancestor node to
// check whether this node became unbalanced
int balance = getBalanceFactor(node);
// 4. If the node becomes unbalanced, then there are 4
// cases
// 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;
}
// Function to perform preorder traversal of AVL tree
void inOrder(struct Node* root)
{
if (root != NULL) {
inOrder(root->left);
printf("%d ", root->key);
inOrder(root->right);
}
}
// Main function
int main()
{
struct Node* root = NULL;
// Inserting nodes
root = insert(root, 1);
root = insert(root, 2);
root = insert(root, 4);
root = insert(root, 5);
root = insert(root, 6);
root = insert(root, 3);
// Print preorder traversal of the AVL tree
printf("Inorder traversal of AVL tree: ");
inOrder(root);
return 0;
}
Output
Inorder traversal of AVL tree: 1 2 3 4 5 6
Related Articles:
Similar Reads
C++ Program to Implement AVL Tree
AVL Tree, named after its inventors Adelson-Velsky and Landis, is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one, which ensures that the tree remains approximately balanced, providing efficient search, insertion, and delet
11 min read
C++ Program to Implement B+ Tree
A B+ Tree is an advanced data structure that extends the B-tree by adding a linked list of leaf nodes. This structure is widely used in databases and file systems to allow efficient insertion, deletion, and search operations. In this article, we will learn how to implement the B+ Tree in C++ along w
10 min read
Java Program to Implement B+ Tree
The B+ tree is a self-balancing tree data structure commonly used in database and file systems applications. It is an extension of B-Tree and maintains sorted data in a manner that allows us for efficient insertion, deletion and search operations. The B+Trees stores all data in a leaf node while int
6 min read
C++ Program to Implement Binomial Heap
A binomial heap is a collection of the binomial trees that are linked together. Each binomial tree in the heap has a node structure where the key of a node is greater than or equal to the key of its parent. It is an extension of Binary Heap that allows us to perform union or merge operation faster m
9 min read
C Program to Implement Binomial Heap
Binomial Heap is a specific type of heap data structure that finds its roots in the Binomial Tree. Itâs a collection of binomial trees, each adhering to the min-heap property, where the parent nodeâs key is less than or equal to its childrenâs keys. This structure is particularly efficient for imple
4 min read
C++ Program to Implement Binary Heap
A binary heap is a complete binary tree that satisfies the heap property. The heap property states that for a max-heap, every parent node has a value greater than or equal to its children, and for a min-heap, every parent node has a value less than or equal to its children. Binary heaps are commonly
8 min read
C++ Program to Implement Minimum Spanning Tree
Given a weighted graph, the objective is to find a subset of edges that forms a tree including every vertex, where the total weight of all the edges in the tree is minimized. In this article, we will explore various algorithms through which we can implement a Minimum Spanning Tree in C++. What is Mi
7 min read
C Program to Implement Minimum Spanning Tree
A Minimum Spanning Tree is a subset of edges from a undirected graph that connects all vertices with minimum total weight and contains no cycle. The most common algorithms to generate Minimum Spanning Tree are Kruskal's algorithm and Prim's algorithm. In this article we explain about implement Minim
5 min read
Implement a Stack in C Programming
Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate
7 min read
B-Tree Implementation in C++
In C++, B-trees are balanced tree data structures that maintain sorted data and allow searches, sequential access, insertions, and deletions in logarithmic time. B-trees are generalizations of binary search trees (BST) in that a node can have more than two children. B-trees are optimized for systems
13 min read