AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.
Insertion in an AVL Tree follows the same basic rules as in a Binary Search Tree (BST):
- A new key is placed in its correct position based on BST rules (left < node < right).
- After the insertion, the balance factor of each node is checked during the path back up to the root. If any node becomes unbalanced (i.e., its balance factor becomes less than -1 or greater than +1), a rotation is required to restore the AVL property.
Example of AVL Tree:

The above tree is AVL tree because the differences between the heights of left and right subtrees for every node are lies in the range -1 to +1.
Example of a Tree that is NOT an AVL Tree:

The above tree is not an AVL tree because the differences between the heights of the left and right subtrees for 8 and 12 are greater than 1.
Why AVL Trees? Most of the BST operations (e.g., search, max, min, insert, delete, floor and ceiling) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that the height of the tree remains O(log(n)) after every insertion and deletion, then we can guarantee an upper bound of O(log(n)) for all these operations. The height of an AVL tree is always O(log(n)) where n is the number of nodes in the tree.
Insertion in AVL Tree:
To make sure that the given tree remains AVL after every insertion, we must augment the standard BST insert operation to perform some re-balancing. Following are two basic operations that can be performed to balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
- Left Rotation
- Right Rotation

Illustration of Insertion at AVL Tree:
Approach: The idea is to use recursive BST insert, after insertion, we get pointers to all ancestors one by one in a bottom-up manner. So we don't need a parent pointer to travel up. The recursive code itself travels up and visits all the ancestors of the newly inserted node.
Follow the steps mentioned below to implement the idea:
- Perform the normal BST insertion.
- The current node must be one of the ancestors of the newly inserted node. Update the height of the current node.
- Get the balance factor (left subtree height - right subtree height) of the current node.
- If the balance factor is greater than 1, then the current node is unbalanced and we are either in the Left Left case or left Right case. To check whether it is left left case or not, compare the newly inserted key with the key in the left subtree root.
- If the balance factor is less than -1, then the current node is unbalanced and we are either in the Right Right case or Right-Left case. To check whether it is the Right Right case or not, compare the newly inserted key with the key in the right subtree root.
Below is the implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
// AVL tree node
class Node {
public:
int key;
Node* left;
Node* right;
int height;
Node(int x) {
key = x;
left = nullptr;
right = nullptr;
height = 1;
}
};
// Get the height of a node
int height(Node* node) {
if (node == nullptr)
return 0;
return node->height;
}
// Get the balance factor of a node
int getBalance(Node* node) {
if (node == nullptr)
return 0;
return height(node->left) - height(node->right);
}
// Perform a right rotation
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* temp = x->right;
x->right = y;
y->left = temp;
// Update heights
y->height = 1 + max(height(y->left), height(y->right));
x->height = 1 + max(height(x->left), height(x->right));
return x;
}
// Perform a left rotation
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* temp = y->left;
y->left = x;
x->right = temp;
// Update heights
x->height = 1 + max(height(x->left), height(x->right));
y->height = 1 + max(height(y->left), height(y->right));
return y;
}
// Insert a key into the AVL tree
Node* insertToAVL(Node* node, int key) {
// Insert the key as in a normal BST
if (node == nullptr)
return new Node(key);
if (key < node->key)
node->left = insertToAVL(node->left, key);
else if (key > node->key)
node->right = insertToAVL(node->right, key);
else
return node;
// Update the height
node->height = 1 + max(height(node->left), height(node->right));
// Get the balance factor
int balance = getBalance(node);
// 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 node;
}
// Print preorder traversal
void preOrder(Node* root) {
if (root == nullptr)
return;
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
}
int main() {
Node* root = nullptr;
root = insertToAVL(root, 10);
root = insertToAVL(root, 20);
root = insertToAVL(root, 30);
root = insertToAVL(root, 40);
root = insertToAVL(root, 50);
root = insertToAVL(root, 25);
// Print the preorder traversal
preOrder(root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// AVL tree node
struct Node {
int key;
struct Node* left;
struct Node* right;
int height;
};
// Create a new node
struct Node* createNode(int x) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = x;
node->left = NULL;
node->right = NULL;
node->height = 1;
return node;
}
// Get the height of a node
int height(struct Node* node) {
if (node == NULL)
return 0;
return node->height;
}
// Get the balance factor of a node
int getBalance(struct Node* node) {
if (node == NULL)
return 0;
return height(node->left) - height(node->right);
}
// Get the maximum of two values
int max(int a, int b) {
return (a > b) ? a : b;
}
// Perform a right rotation
struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* temp = x->right;
x->right = y;
y->left = temp;
// Update heights
y->height = 1 + max(height(y->left), height(y->right));
x->height = 1 + max(height(x->left), height(x->right));
return x;
}
// Perform a left rotation
struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node* temp = y->left;
y->left = x;
x->right = temp;
// Update heights
x->height = 1 + max(height(x->left), height(x->right));
y->height = 1 + max(height(y->left), height(y->right));
return y;
}
// Insert a key into the AVL tree
struct Node* insertToAVL(struct Node* node, int key) {
// Insert the key as in a normal BST
if (node == NULL)
return createNode(key);
if (key < node->key)
node->left = insertToAVL(node->left, key);
else if (key > node->key)
node->right = insertToAVL(node->right, key);
else
return node;
// Update the height
node->height = 1 + max(height(node->left), height(node->right));
// Get the balance factor
int balance = getBalance(node);
// 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 node;
}
// Print preorder traversal
void preOrder(struct Node* root) {
if (root == NULL)
return;
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
int main() {
struct Node* root = NULL;
root = insertToAVL(root, 10);
root = insertToAVL(root, 20);
root = insertToAVL(root, 30);
root = insertToAVL(root, 40);
root = insertToAVL(root, 50);
root = insertToAVL(root, 25);
// Print the preorder traversal
preOrder(root);
return 0;
}
class Node {
int key;
Node left, right;
int height;
Node(int x) {
key = x;
left = null;
right = null;
height = 1;
}
}
// Get the height of a node
class GFG {
static int height(Node node) {
if (node == null)
return 0;
return node.height;
}
// Get the balance factor of a node
static int getBalance(Node node) {
if (node == null)
return 0;
return height(node.left) - height(node.right);
}
// Perform a right rotation
static Node rightRotate(Node y) {
Node x = y.left;
Node temp = x.right;
x.right = y;
y.left = temp;
// Update heights
y.height = 1 + Math.max(height(y.left), height(y.right));
x.height = 1 + Math.max(height(x.left), height(x.right));
return x;
}
// Perform a left rotation
static Node leftRotate(Node x) {
Node y = x.right;
Node temp = y.left;
y.left = x;
x.right = temp;
// Update heights
x.height = 1 + Math.max(height(x.left), height(x.right));
y.height = 1 + Math.max(height(y.left), height(y.right));
return y;
}
// Insert a key into the AVL tree
static Node insertToAVL(Node node, int key) {
// Insert the key as in a normal BST
if (node == null)
return new Node(key);
if (key < node.key)
node.left = insertToAVL(node.left, key);
else if (key > node.key)
node.right = insertToAVL(node.right, key);
else
return node;
// Update the height
node.height = 1 + Math.max(height(node.left), height(node.right));
// Get the balance factor
int balance = getBalance(node);
// 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 node;
}
// Print preorder traversal
static void preOrder(Node root) {
if (root == null)
return;
System.out.print(root.key + " ");
preOrder(root.left);
preOrder(root.right);
}
public static void main(String[] args) {
Node root = null;
root = insertToAVL(root, 10);
root = insertToAVL(root, 20);
root = insertToAVL(root, 30);
root = insertToAVL(root, 40);
root = insertToAVL(root, 50);
root = insertToAVL(root, 25);
// Print the preorder traversal
preOrder(root);
}
}
# AVL tree node
class Node:
def __init__(self, x):
self.key = x
self.left = None
self.right = None
self.height = 1
# Get the height of a node
def height(node):
if node is None:
return 0
return node.height
# Get the balance factor of a node
def getBalance(node):
if node is None:
return 0
return height(node.left) - height(node.right)
# Perform a right rotation
def rightRotate(y):
x = y.left
temp = x.right
x.right = y
y.left = temp
# Update heights
y.height = 1 + max(height(y.left), height(y.right))
x.height = 1 + max(height(x.left), height(x.right))
return x
# Perform a left rotation
def leftRotate(x):
y = x.right
temp = y.left
y.left = x
x.right = temp
# Update heights
x.height = 1 + max(height(x.left), height(x.right))
y.height = 1 + max(height(y.left), height(y.right))
return y
# Insert a key into the AVL tree
def insertToAVL(node, key):
# Insert the key as in a normal BST
if node is None:
return Node(key)
if key < node.key:
node.left = insertToAVL(node.left, key)
elif key > node.key:
node.right = insertToAVL(node.right, key)
else:
return node
# Update the height
node.height = 1 + max(height(node.left), height(node.right))
# Get the balance factor
balance = getBalance(node)
# Left-Left case
if balance > 1 and key < node.left.key:
return rightRotate(node)
# Right-Right case
if balance < -1 and key > node.right.key:
return leftRotate(node)
# Left-Right case
if balance > 1 and key > node.left.key:
node.left = leftRotate(node.left)
return rightRotate(node)
# Right-Left case
if balance < -1 and key < node.right.key:
node.right = rightRotate(node.right)
return leftRotate(node)
return node
# Print preorder traversal
def preOrder(root):
if root is None:
return
print(root.key, end=" ")
preOrder(root.left)
preOrder(root.right)
if __name__ == "__main__":
root = None
root = insertToAVL(root, 10)
root = insertToAVL(root, 20)
root = insertToAVL(root, 30)
root = insertToAVL(root, 40)
root = insertToAVL(root, 50)
root = insertToAVL(root, 25)
# Print the preorder traversal
preOrder(root)
using System;
// AVL tree node
class Node {
public int key;
public Node left;
public Node right;
public int height;
public Node(int x) {
key = x;
left = null;
right = null;
height = 1;
}
}
class GFG {
// Get the height of a node
static int height(Node node) {
if (node == null)
return 0;
return node.height;
}
// Get the balance factor of a node
static int getBalance(Node node) {
if (node == null)
return 0;
return height(node.left) - height(node.right);
}
// Perform a right rotation
static Node rightRotate(Node y) {
Node x = y.left;
Node temp = x.right;
x.right = y;
y.left = temp;
// Update heights
y.height = 1 + Math.Max(height(y.left), height(y.right));
x.height = 1 + Math.Max(height(x.left), height(x.right));
return x;
}
// Perform a left rotation
static Node leftRotate(Node x) {
Node y = x.right;
Node temp = y.left;
y.left = x;
x.right = temp;
// Update heights
x.height = 1 + Math.Max(height(x.left), height(x.right));
y.height = 1 + Math.Max(height(y.left), height(y.right));
return y;
}
// Insert a key into the AVL tree
static Node insertToAVL(Node node, int key) {
// Insert the key as in a normal BST
if (node == null)
return new Node(key);
if (key < node.key)
node.left = insertToAVL(node.left, key);
else if (key > node.key)
node.right = insertToAVL(node.right, key);
else
return node;
// Update the height
node.height = 1 + Math.Max(height(node.left), height(node.right));
// Get the balance factor
int balance = getBalance(node);
// 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 node;
}
// Print preorder traversal
static void preOrder(Node root) {
if (root == null)
return;
Console.Write(root.key + " ");
preOrder(root.left);
preOrder(root.right);
}
static void Main() {
Node root = null;
root = insertToAVL(root, 10);
root = insertToAVL(root, 20);
root = insertToAVL(root, 30);
root = insertToAVL(root, 40);
root = insertToAVL(root, 50);
root = insertToAVL(root, 25);
// Print the preorder traversal
preOrder(root);
}
}
class Node {
constructor(x) {
this.key = x;
this.left = null;
this.right = null;
this.height = 1;
}
}
// Get the height of a node
function height(node) {
if (node === null)
return 0;
return node.height;
}
// Get the balance factor of a node
function getBalance(node) {
if (node === null)
return 0;
return height(node.left) - height(node.right);
}
// Perform a right rotation
function rightRotate(y) {
let x = y.left;
let temp = x.right;
x.right = y;
y.left = temp;
// Update heights
y.height = 1 + Math.max(height(y.left), height(y.right));
x.height = 1 + Math.max(height(x.left), height(x.right));
return x;
}
// Perform a left rotation
function leftRotate(x) {
let y = x.right;
let temp = y.left;
y.left = x;
x.right = temp;
// Update heights
x.height = 1 + Math.max(height(x.left), height(x.right));
y.height = 1 + Math.max(height(y.left), height(y.right));
return y;
}
// Insert a key into the AVL tree
function insertToAVL(node, key) {
// Insert the key as in a normal BST
if (node === null)
return new Node(key);
if (key < node.key)
node.left = insertToAVL(node.left, key);
else if (key > node.key)
node.right = insertToAVL(node.right, key);
else
return node;
// Update the height
node.height = 1 + Math.max(height(node.left), height(node.right));
// Get the balance factor
let balance = getBalance(node);
// 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 node;
}
// Print preorder traversal
function preOrder(root) {
if (root === null)
return;
process.stdout.write(root.key + " ");
preOrder(root.left);
preOrder(root.right);
}
// Driver code
let root = null;
root = insertToAVL(root, 10);
root = insertToAVL(root, 20);
root = insertToAVL(root, 30);
root = insertToAVL(root, 40);
root = insertToAVL(root, 50);
root = insertToAVL(root, 25);
// Print the preorder traversal
preOrder(root);
Output
30 20 10 25 40 50
Time Complexity: O(logn), for Insertion
Auxiliary Space: O(logn), for recursion call stack as we have written a recursive method to insert
The rotation operations (left and right rotate) take constant time as only a few pointers are being changed there. Updating the height and getting the balance factor also takes constant time. So the time complexity of the AVL insert remains the same as the BST insert which is O(h) where h is the height of the tree. Since the AVL tree is balanced, the height is O(logn). So time complexity of AVL insert is O(logn).
Comparison with Red Black Tree:
The AVL tree and other self-balancing search trees like Red Black are useful to get all basic operations done in O(logn) time. The AVL trees are more balanced compared to Red-Black Trees, but they may cause more rotations during insertion and deletion. So if your application involves many frequent insertions and deletions, then Red Black trees should be preferred. And if the insertions and deletions are less frequent and search is the more frequent operation, then the AVL tree should be preferred over Red Black Tree.