0% found this document useful (0 votes)
47 views6 pages

Insertion in Red

The document discusses Red Black Trees, which are self-balancing binary search trees. It covers insertion in Red Black Trees, which involves adding new nodes as red and then recoloring and/or rotating nodes to maintain the Red Black Tree properties. The rules of insertion are presented, which involve checking colors during insertion and performing suitable rotations. An example of inserting elements into an empty tree is also provided. Advantages of Red Black Trees are given as maintaining a height of log n during insertions and deletions, making operations efficient.

Uploaded by

Afra Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views6 pages

Insertion in Red

The document discusses Red Black Trees, which are self-balancing binary search trees. It covers insertion in Red Black Trees, which involves adding new nodes as red and then recoloring and/or rotating nodes to maintain the Red Black Tree properties. The rules of insertion are presented, which involve checking colors during insertion and performing suitable rotations. An example of inserting elements into an empty tree is also provided. Advantages of Red Black Trees are given as maintaining a height of log n during insertions and deletions, making operations efficient.

Uploaded by

Afra Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Table of content:

1. Why do we need Red Black Tree?


2. Insertion in Red Black Tree
i) Recoloring
ii) Rotation
3. Rules of Insertion
4. Example of Insertion
5. Advantages of Red Black tree

Insertion in Red-Black Tree:


In a Red-Black Tree, each new node should be embedded with the color RED. The insertion
operation in Red Black Tree is same as insertion operation in Binary Search Tree. Yet, it is
inserted with a color property. After each insertion operation, we need to check every one of
the properties of Red-Black Tree. Assuming every one of the properties are fulfilled, we go to
next operation else we perform the following operation to make it Red Black Tree.  
1. Recoloring
2. Rotation
Recoloring:
Recoloring is the adjustment of color of the node for example if the node it is red, change it to
black and if it is black change it to red. It should be noticed that the color of the NULL node is
consistently black. Besides, we generally have a go at recoloring first, in the event that recoloring
doesn't work, we go for rotation. It has essentially two cases relying on the color of the parent
sibling. In the event that the parent sibling is red, we do recolor. If the uncle is black, we do
rotation as well as recoloring.
Rotation:
If the parent sibling node is black then there are four possible types of rotations.
LL rotation (Left Left Case):
RR rotation (Right Right Case):

RL rotation (Right Left Case):


LR rotation (Left Right Case):

Rules of Insertion:
1. If tree is empty create a node as root node with color black.
2. If tree is not empty create a leaf node with color Red.
3. If parent of new node is black then exit.
4. If parent of new node is Red then check the color of parent’s sibling of new node.
A) If color is black or null then do suitable rotation
and coloring.
B) If color is Red then recolor & also check if its
parent’s parent of new node is not root node then
recolor it and recheck.

Example:
Creating a red-black tree with elements 3, 21, 32 and 17 in an empty tree.
Why do we need Red Black Tree?
A BST may have a height of n( total number of nodes) in the most worst case if the elements are
in increasing or decreasing order. This would consign it to O(n) time for searches, insertions and
deletions. This is the reason we require a red-black tree. It keeps the BST balanced with a height
log n.

A red-black tree is a sort of elf-balancing binary search tree where every node has an additional
bit, and that bit is often interpreted as the color red or black. These colors are utilized to
guarantee that the tree remains balanced during insertions and deletions.

Advantages of Red Black Tree


1. Red black tree are valuable when we need frequent insertion and deletion relatively .
2. Red-black trees are self-balancing so these operations are destined to be O(log n).
3. They have moderately low constants in a wide variety of situations.

You might also like