Explain AVL trees. Insert the following elements in AVL tree and show the tree after each insertion: 64, 44, 26, 13, 110, 98, 7


Q.) Explain AVL trees. Insert the following elements in AVL tree and show the tree after each insertion: 64, 44, 26, 13, 110, 98, 7

Subject: Data Structures

AVL Trees:

AVL trees, named after their inventors Adelson-Velsky and Landis, are balanced binary search trees where the difference in the heights of the left and right subtrees for any node is at most one. This property allows for efficient searching, insertion, and deletion operations.

Insertion in AVL Trees:

Inserting a new element into an AVL tree involves the following steps:

  1. Perform a standard binary search tree insertion to find the appropriate location for the new element.

  2. Update the heights of the nodes along the path from the newly inserted node to the root.

  3. Check for imbalanced nodes (nodes where the absolute difference in the heights of the left and right subtrees is greater than one).

  • If an imbalanced node is found, perform appropriate rotations to restore the balance of the tree. There are four possible rotation cases:

    • Left Rotation: If the left child of a node has a greater height than the right child, and the left child's right child has a greater height than the left child's left child, a left rotation is performed.
    • Right Rotation: If the right child of a node has a greater height than the left child, and the right child's left child has a greater height than the right child's right child, a right rotation is performed.
    • Left-Right Rotation: If the left child of a node has a greater height than the right child, and the left child's right child has a greater height than the left child's left child, a left-right rotation is performed.
    • Right-Left Rotation: If the right child of a node has a greater height than the left child, and the right child's left child has a greater height than the right child's right child, a right-left rotation is performed.

Inserting Elements into an AVL Tree:

1. Insertion of 64:

  • Start with an empty tree.

  • Insert 64 as the root node.

  • The tree remains balanced with a height of 1.

2. Insertion of 44:

  • Perform a binary search tree insertion to find the appropriate location for 44.

  • Insert 44 as the left child of 64.

  • The left subtree has a height of 1, while the right subtree has a height of 0.

  • The tree remains balanced with a height of 2.

3. Insertion of 26:

  • Perform a binary search tree insertion to find the appropriate location for 26.

  • Insert 26 as the right child of 44.

  • The left subtree has a height of 2, while the right subtree has a height of 1.

  • The tree is imbalanced at node 44.

  • Perform a right rotation at node 44 to restore balance.

  • The tree is now balanced with a height of 3.

4. Insertion of 13:

  • Perform a binary search tree insertion to find the appropriate location for 13.

  • Insert 13 as the left child of 26.

  • The left subtree has a height of 2, while the right subtree has a height of 0.

  • The tree is imbalanced at node 26.

  • Perform a left rotation at node 26 to restore balance.

  • The tree is now balanced with a height of 3.

5. Insertion of 110:

  • Perform a binary search tree insertion to find the appropriate location for 110.

  • Insert 110 as the right child of 64.

  • The left subtree has a height of 3, while the right subtree has a height of 0.

  • The tree is imbalanced at node 64.

  • Perform a right rotation at node 64 to restore balance.

  • The tree is now balanced with a height of 3.

6. Insertion of 98:

  • Perform a binary search tree insertion to find the appropriate location for 98.

  • Insert 98 as the right child of 110.

  • The left subtree has a height of 3, while the right subtree has a height of 1.

  • The tree is imbalanced at node 110.

  • Perform a left rotation at node 110 to restore balance.

  • The tree is now balanced with a height of 3.

7. Insertion of 7:

  • Perform a binary search tree insertion to find the appropriate location for 7.

  • Insert 7 as the left child of 13.

  • The left subtree has a height of 3, while the right subtree has a height of 0.

  • The tree is imbalanced at node 13.

  • Perform a right rotation at node 13 to restore balance.

  • The tree is now balanced with a height of 3.

The final AVL tree after inserting all the elements looks like this:

                 64
              /       \
             44        110
           /   \       /    \
          26   13     98    7