Explain AVL tree. Insert the following elements in AVL tree: 50 25 10 5 7 3 30 20 8 15. What are Red Black trees? Discuss the properties of Red Black trees in detail.


Q.) Explain AVL tree. Insert the following elements in AVL tree: 50 25 10 5 7 3 30 20 8 15. What are Red Black trees? Discuss the properties of Red Black trees in detail.

Subject: data structure

AVL Tree:

An AVL tree, named after its inventors Adelson-Velsky and Landis, is a self-balancing binary search tree that maintains a balance factor to ensure efficient searching and insertion/deletion operations. In an AVL tree, the difference between the heights of the left and right subtrees of every node is at most 1.

Insertion in AVL Tree:

To insert a new element into an AVL tree, you first follow the standard binary search tree insertion algorithm:

  1. Compare the new element with the root node.
  2. If the new element is less than the root, recursively insert it into the left subtree.
  3. If the new element is greater than the root, recursively insert it into the right subtree.

After insertion, you need to check whether the AVL tree is still balanced. If the balance factor of the root node (or any of its ancestors) becomes greater than 1 or less than -1, you need to perform a rotation operation to restore balance.

There are four different types of rotation operations in an AVL tree:

  1. Left Rotation: This operation is performed when the balance factor of a node is 2 and the imbalance is caused by the right child of the node.

  2. Right Rotation: This operation is performed when the balance factor of a node is -2 and the imbalance is caused by the left child of the node.

  3. Left-Right Rotation: This operation is a combination of a left rotation and a right rotation. It is performed when the balance factor of a node is 2 and the imbalance is caused by the left child of the right child of the node.

  4. Right-Left Rotation: This operation is a combination of a right rotation and a left rotation. It is performed when the balance factor of a node is -2 and the imbalance is caused by the right child of the left child of the node.

Example:

Let's insert the following elements into an AVL tree: 50, 25, 10, 5, 7, 3, 30, 20, 8, 15.

  1. Insert 50:

    • The tree initially has no nodes.
    • Insert 50 as the root node.
    • The balance factor of the root node is 0.
  2. Insert 25:

    • Compare 25 with the root node (50).
    • Since 25 is less than 50, insert it into the left subtree.
    • The balance factor of the root node is now 1.
  3. Insert 10:

    • Compare 10 with the left child of the root node (25).
    • Since 10 is less than 25, insert it into the left subtree of the left child.
    • The balance factor of the left child of the root node is now 1.
  4. Insert 5:

    • Compare 5 with the left child of the root node (25).
    • Since 5 is less than 25, insert it into the left subtree of the left child.
    • The balance factor of the left child of the left child of the root node is now 2.
  5. Insert 7:

    • Compare 7 with the left child of the root node (25).
    • Since 7 is greater than 5, insert it into the right subtree of the left child.
    • The balance factor of the left child of the root node is now 0.
  6. Insert 3:

    • Compare 3 with the left child of the root node (25).
    • Since 3 is less than 5, insert it into the left subtree of the left child.
    • The balance factor of the left child of the left child of the root node is now 1.
  7. Insert 30:

    • Compare 30 with the root node (50).
    • Since 30 is greater than 50, insert it into the right subtree.
    • The balance factor of the root node is now -1.
  8. Insert 20:

    • Compare 20 with the right child of the root node (30).
    • Since 20 is less than 30, insert it into the left subtree of the right child.
    • The balance factor of the right child of the root node is now 1.
  9. Insert 8:

    • Compare 8 with the left child of the right child of the root node (20).
    • Since 8 is less than 20, insert it into the left subtree of the left child of the right child.
    • The balance factor of the left child of the right child of the root node is now 2.
  10. Insert 15:

    • Compare 15 with the right child of the root node (30).
    • Since 15 is less than 30, insert it into the left subtree of the right child.
    • The balance factor of the right child of the root node is now 0.

The final AVL tree after inserting all the elements is shown below:

               50
              /  \
            25   30
           /  \    /
          10  3   20
         /  \   /  \
        5   7  15  8

Red-Black Tree:

A Red-Black tree is a self-balancing binary search tree that maintains a balance factor to ensure efficient searching and insertion/deletion operations. In a Red-Black tree, every node is either red or black. The following properties must hold for every Red-Black tree:

  1. The root node is always black.
  2. Every red node must have two black children.
  3. Every path from a node to any of its descendant leaves contains the same number of black nodes.

Insertion in Red-Black Tree:

To insert a new element into a Red-Black tree, you first follow the standard binary search tree insertion algorithm:

  1. Compare the new element with the root node.
  2. If the new element is less than the root, recursively insert it into the left subtree.
  3. If the new element is greater than the root, recursively insert it into the right subtree.

After insertion, you need to check whether the Red-Black tree still satisfies all the properties. If any of the properties are violated, you need to perform a rotation operation and/or recolor some nodes to restore balance.

There are four different types of rotation operations in a Red-Black tree:

  1. Left Rotation: This operation is performed when the child of a red node is red.

  2. Right Rotation: This operation is performed when the parent of a red node is red and the right child of the parent is also red.

  3. Left-Right Rotation: This operation is a combination of a left rotation and a right rotation. It is performed when the left child of a red node is black and the right child is red.

  4. Right-Left Rotation: This operation is a combination of a right rotation and a left rotation. It is performed when the right child of a red node is black and the left child is red.

Properties of Red-Black Trees:

  • Height Balance: Red-Black trees are height-balanced, meaning that the height of any node is at most twice the height of its shortest child.

  • Search Time: The worst-case search time in a Red-Black tree is O(log n), where n is the number of elements in the tree.

  • Insertion Time: The worst-case insertion time in a Red-Black tree is O(log n).

  • Deletion Time: The worst-case deletion time in a Red-Black tree is O(log n).

  • Space Overhead: Red-Black trees require additional space to store the color of each node. However, the space overhead is typically small compared to the size of the data stored in the tree.

  • Applications: Red-Black trees are used in a variety of applications, including operating systems, databases, and graphics processors.