Explain AVL tree. Insert the following elements in AVL structure: 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 structure: 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 Trees

An AVL tree is a self-balancing binary search tree, where the difference between heights of left and right subtrees cannot be more than one for all nodes. This difference is called the Balance Factor. The AVL tree was named after its inventors, G.M. Adelson-Velsky and E.M. Landis.

Balance Factor (BF)

The balance factor of a node in an AVL tree is the height of its left subtree minus the height of its right subtree. The balance factor can be -1, 0, or +1 for a balanced AVL tree.

AVL Tree Rotations

To maintain the balance after every insertion or deletion, AVL tree may perform the following rotations:

  1. Single Right Rotation (LL Rotation)
  2. Single Left Rotation (RR Rotation)
  3. Left-Right Rotation (LR Rotation)
  4. Right-Left Rotation (RL Rotation)

These rotations are used to re-balance the tree.

Insertion into AVL Tree

Insertion into an AVL tree is similar to insertion into a regular binary search tree. After insertion, the balance factor of each node is checked, and if a node is found to be unbalanced, one of the four rotations is performed to balance the tree.

Let's insert the given elements into an AVL tree step by step:

  1. Insert 50 50 / \ null null
  2. Insert 25 50 / \ 25 null
  3. Insert 10 (unbalanced, requires LL Rotation) 50 / \ 25 null / 10 After LL Rotation: 25 / \ 10 50
  4. Insert 5 25 / \ 10 50 / 5
  5. Insert 7 (unbalanced, requires LR Rotation) 25 / \ 10 50 / 5 \ 7 After LR Rotation: 25 / \ 7 50 / \ 5 10
  6. Insert 3 25 / \ 7 50 / \ 5 10 / 3
  7. Insert 30 25 / \ 7 50 / \ \ 5 10 30 / 3
  8. Insert 20 25 / \ 7 50 / \ / \ 5 10 30 null / 3 \ 20
  9. Insert 8 (unbalanced, requires LL Rotation on node 7) 25 / \ 7 50 / \ / \ 5 10 30 null / \ 3 8 After LL Rotation on node 7: 25 / \ 8 50 / \ / \ 5 10 30 null / 3 \ 7
  10. Insert 15 (unbalanced, requires LR Rotation on node 25) 25 / \ 8 50 / \ / \ 5 10 30 null / \ 3 15 \ 7 After LR Rotation on node 25: 15 / \ 8 25 / \ / \ 5 10 20 50 / \ 3 30 \ 7 Now the AVL tree is balanced after all insertions.

Red-Black Trees

Red-Black trees are another type of self-balancing binary search tree. Each node in the tree has an extra bit for denoting the color of the node, either red or black. A Red-Black tree ensures that the tree remains balanced during insertions and deletions.

Properties of Red-Black Trees

Property Description
1. Red/Black Nodes Every node is either red or black.
2. Root is Black The root of the tree is always black.
3. Red Node Children The children of a red node are always black (no two red nodes can be adjacent).
4. Black Depth Every path from a node to its descendant NULL nodes has the same number of black nodes.
5. New Insertions are Red Newly inserted nodes are always red.

Balancing Red-Black Trees

To maintain these properties, Red-Black trees use rotations similar to AVL trees, but the rules for when to rotate and change colors are different. The rotations used in Red-Black trees are:

  1. Left Rotation
  2. Right Rotation

These rotations, along with changing the colors of certain nodes, ensure that the Red-Black properties are preserved after every insertion and deletion, keeping the tree balanced.

In summary, both AVL and Red-Black trees are self-balancing binary search trees that ensure O(log n) time complexity for insertions, deletions, and lookups. AVL trees are more rigidly balanced and thus may provide faster lookups at the cost of slower insertion and deletion operations. Red-Black trees provide a good compromise between the complexity of operations and balancing, which is why they are used in many practical applications such as the implementation of associative containers in the C++ STL (Standard Template Library).