What is AVL Tree? Discuss various types of nodes required to implement an unbalanced AVL tree. Construct an AVL tree for the following: March, May, November, August, April, June, December, July, February, October, September


Q.) What is AVL Tree? Discuss various types of nodes required to implement an unbalanced AVL tree. Construct an AVL tree for the following: March, May, November, August, April, June, December, July, February, October, September

Subject: Data Structure and Algorithm

What is an AVL Tree?

An AVL tree 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; if at any time they differ by more than one, rebalancing is done to restore this property. AVL trees are named after their inventors, Adelson-Velsky and Landis.

Types of Nodes Required to Implement an Unbalanced AVL Tree

To implement an AVL tree, we need to keep track of the balance factor of each node. The balance factor is the difference between the heights of the left and right subtrees. Based on the balance factor, there are four types of nodes that can cause an AVL tree to become unbalanced:

  1. Left-Left (LL) Heavy: The left subtree is taller than the right subtree, and the balance factor is greater than 1 because of an insertion into the left subtree of the left child.

  2. Left-Right (LR) Heavy: The left subtree is taller than the right subtree, but the balance factor is greater than 1 because of an insertion into the right subtree of the left child.

  3. Right-Right (RR) Heavy: The right subtree is taller than the left subtree, and the balance factor is less than -1 because of an insertion into the right subtree of the right child.

  4. Right-Left (RL) Heavy: The right subtree is taller than the left subtree, but the balance factor is less than -1 because of an insertion into the left subtree of the right child.

Constructing an AVL Tree

Let's construct an AVL tree using the given months. We will insert the months one by one and perform rotations when necessary to maintain the AVL property.

Here are the steps to insert each month and balance the tree if necessary:

  1. March: Start with the first node.
  2. May: Insert to the right of March.
  3. November: Insert to the right of May. Tree is still balanced.
  4. August: Insert to the left of November. May node becomes unbalanced (Right-Left heavy). Perform RL rotation.
  5. April: Insert to the left of March. Tree is still balanced.
  6. June: Insert to the right of April. Tree is still balanced.
  7. December: Insert to the right of November. Tree is still balanced.
  8. July: Insert to the left of August. Tree is still balanced.
  9. February: Insert to the left of March. March node becomes unbalanced (Left-Left heavy). Perform LL rotation.
  10. October: Insert to the left of November. Tree is still balanced.
  11. September: Insert to the left of October. October node becomes unbalanced (Left-Left heavy). Perform LL rotation.

Below is the step-by-step construction of the AVL tree with rotations:

Insertion Tree Structure Action Taken
March March None
May March None
\
May
November March None
\
May
\
November
August March RL Rotation at May
\
August
/ \
May November
April April None
\
August
/ \
May November
June April None
\
August
/ \
May November
\
June
December April None
\
August
/ \
May November
\ \
June December
July April None
\
August
/ \
May November
/ \ \
July June December
February February LL Rotation at April
\
April
\
August
/ \
May November
/ \ \
July June December
October February None
\
April
\
August
/ \
May November
/ \ / \
July June October December
September February LL Rotation at April
\
April
\
August
/ \
May September
/ \ /
July June October
\
November
\
December

Final AVL Tree

After all insertions and rotations, the final AVL tree looks like this:

        April
       /     \
 February    August
       \     /    \
      March May  September
             \    /     \
            June July  November
                          \
                        December

This tree is balanced according to AVL tree properties, with the balance factor of each node being either -1, 0, or 1.