Deletion In AVL Tree In Java

Deletion in AVL Tree In Java

In this article, we will understand deletion in an AVL Tree in Java, including the approach, rotations, algorithm, implementation, examples, and best practices.

Deletion in an AVL Tree is an extension of BST deletion with an additional step of rebalancing the tree after removing a node. Since AVL Trees are height balanced, every deletion must ensure that the balance factor of each node remains within the allowed range.

deletion in AVL Tree

What is an AVL Tree?

An AVL Tree is a self balancing Binary Search Tree where:

    • Difference between heights of left and right subtree (called Balance Factor) is at most 1
  • Balance Factor = height(left) - height(right)
  • If imbalance occurs, rotations are performed to fix it

Why Do We Need AVL Tree Insertion?

Deletion ensures:

  1. Tree remains balanced after removal
  2. Operations remain efficient → O(log n)
  3. Suitable for real world systems like:
    • Databases
    • Search engines
    • Memory indexing

Basic Idea of AVL Deletion:

  1. Perform normal BST deletion
  2. Update height of nodes
  3. Calculate balance factor
  4. If unbalanced → apply rotations
deletion in avl tree

Cases in AVL Tree Deletion

1. Standard BST Deletion Cases

  • Node with no child → remove directly
  • Node with one child → replace with child
  • Node with two children → replace with inorder successor

2. Rebalancing Cases

After deletion, check balance factor:

  • LL Case → Right Rotation
  • RR Case → Left Rotation
  • LR Case → Left + Right Rotation
  • RL Case → Right + Left Rotation

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Deletion in an AVL Tree

  • Deletion in an AVL tree is similar to that in a BST.
  • Deletion of a node tends to disturb the balance factor. Thus to balance the tree, we again use the Rotation mechanism.
  • Deletion in AVL tree consists of two steps:
    • Removal of the node: The given node is removed from the tree structure. The node to be removed can either be a leaf or an internal node.
    • Re-balancing of the tree: The elimination of a node from the tree can cause disturbance to the balance factor of certain nodes. Thus it is important to re- balance the b_fact of the nodes; since the balance factor is the primary aspect that ensures the tree is an AVL Tree.
Note: There are certain points that must be kept in mind during a deletion process.
  • If the node to be deleted is a leaf node, it is simply removed from the tree.
  • If the node to be deleted has one child node, the child node is replaced with the node to be deleted simply.
  • If the node to be deleted has two child nodes then,
    • Either replace the node with it’s inorder predecessor , i.e, the largest element of the left sub tree.
    • Or replace the node with it’s inorder successor , i.e, the smallest element of the right sub tree.

Algorithm for Deletion in AVL Tree in Java

  1. Perform BST deletion
  2. Update height of current node
  3. Compute balance factor
  4. Check imbalance:
    • If balance > 1:
      1. If left subtree is balanced → LL
      2. Else → LR
    • If balance < -1:
      1. If right subtree is balanced → RR
      2. Else → RL
  5. Apply appropriate rotation
  6. Return updated node

Java Code for Deletion in AVL Tree

Run
class Node {
    int key, height;
    Node left, right;

    Node(int d) {
        key = d;
        height = 1;
    }
}

public class AVLDeletion {

    int height(Node N) {
        if (N == null)
            return 0;
        return N.height;
    }

    int getBalance(Node N) {
        if (N == null)
            return 0;
        return height(N.left) - height(N.right);
    }

    Node rightRotate(Node y) {
        Node x = y.left;
        Node T2 = x.right;

        x.right = y;
        y.left = T2;

        y.height = Math.max(height(y.left), height(y.right)) + 1;
        x.height = Math.max(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 = Math.max(height(x.left), height(x.right)) + 1;
        y.height = Math.max(height(y.left), height(y.right)) + 1;

        return y;
    }

    Node minValueNode(Node node) {
        Node current = node;
        while (current.left != null)
            current = current.left;
        return current;
    }

    Node delete(Node root, int key) {

        // Step 1: BST deletion
        if (root == null)
            return root;

        if (key < root.key)
            root.left = delete(root.left, key);
        else if (key > root.key)
            root.right = delete(root.right, key);
        else {
            // Node with one or no child
            if ((root.left == null) || (root.right == null)) {
                Node temp = (root.left != null) ? root.left : root.right;

                if (temp == null) {
                    root = null;
                } else {
                    root = temp;
                }
            } else {
                // Node with two children
                Node temp = minValueNode(root.right);
                root.key = temp.key;
                root.right = delete(root.right, temp.key);
            }
        }

        // If tree had only one node
        if (root == null)
            return root;

        // Step 2: Update height
        root.height = Math.max(height(root.left), height(root.right)) + 1;

        // Step 3: Get balance
        int balance = getBalance(root);

        // Step 4: Balance the tree

        // LL Case
        if (balance > 1 && getBalance(root.left) >= 0)
            return rightRotate(root);

        // LR Case
        if (balance > 1 && getBalance(root.left) < 0) {
            root.left = leftRotate(root.left);
            return rightRotate(root);
        }

        // RR Case
        if (balance < -1 && getBalance(root.right) <= 0)
            return leftRotate(root);

        // RL Case
        if (balance < -1 && getBalance(root.right) > 0) {
            root.right = rightRotate(root.right);
            return leftRotate(root);
        }

        return root;
    }

    void inorder(Node root) {
        if (root != null) {
            inorder(root.left);
            System.out.print(root.key + " ");
            inorder(root.right);
        }
    }

    public static void main(String[] args) {
        AVLDeletion tree = new AVLDeletion();
        Node root = null;

        int[] values = {10, 20, 30, 40, 50, 25};

        // Insert first (reuse insertion logic if needed)
        AVLInsertion insertHelper = new AVLInsertion();
        for (int val : values) {
            root = insertHelper.insert(root, val);
        }

        System.out.print("Before Deletion: ");
        tree.inorder(root);

        root = tree.delete(root, 40);

        System.out.print("\nAfter Deletion: ");
        tree.inorder(root);
    }
}

Input:

Insert → 10, 20, 30, 40, 50, 25
Delete → 40

Output:

Before: 10 20 25 30 40 50
After: 10 20 25 30 50

Output:

Preorder traversal is : 
3 1 0 2 5 4 6 
Preorder traversal after deletion of [6,5,4] :
1 0 3 2 

Conclusion:

Common Insights:

  • Deletion is more complex than insertion
  • Multiple rotations may be required
  • Balance factor must be checked at every step
  • AVL guarantees optimal height

Best Practices:

  • Always update height after deletion
  • Carefully handle all rotation cases
  • Reuse insertion logic for building tree
  • Test all edge cases
  • Use helper functions for clarity

Frequently Asked Questions

Answer:

Deletion In AVL Tree In Java is the process of removing a node while maintaining the height-balanced property using rotations.

Answer:

Because after deletion, the tree may become unbalanced and requires rotations to restore balance.

Answer:

The four cases are LL, RR, LR, and RL rotations.

Answer:

It is O(log n) because AVL trees maintain balanced height.

Answer:

It is used in systems requiring fast and consistent performance like databases and search systems.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java