0% found this document useful (0 votes)
9 views

BCA-2 Data Structure Using C - Tree L12 by Anant Kumar

The document discusses Red-Black Trees, which are a type of self-balancing binary search tree. Red-Black Trees have properties related to the coloring of nodes red or black, and operations like insertion must be followed by recoloring or rotations to maintain the properties. The document provides details on the properties of Red-Black Trees and the steps to perform insertion while maintaining a valid Red-Black Tree structure.

Uploaded by

Mihir Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

BCA-2 Data Structure Using C - Tree L12 by Anant Kumar

The document discusses Red-Black Trees, which are a type of self-balancing binary search tree. Red-Black Trees have properties related to the coloring of nodes red or black, and operations like insertion must be followed by recoloring or rotations to maintain the properties. The document provides details on the properties of Red-Black Trees and the steps to perform insertion while maintaining a valid Red-Black Tree structure.

Uploaded by

Mihir Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA STRUCTURES

USING “C”

(Red - Black Tree)

For

BCA Part-II Students

ANANT KUMAR
Faculty Member
Department of Computer Science
J. D. Women’s College, Patna
Red - Black Tree
Red - Black Tree is another variant of Binary Search Tree in which every node is colored
either RED or BLACK. We can define a Red Black Tree as follows

Red Black Tree is a Binary Search Tree in which every node is colored either RED or
BLACK.

Properties of Red Black Tree


Property #1: Red - Black Tree must be a Binary Search Tree.
Property #2: The ROOT node must be colored BLACK.
Property #3: The children of Red colored node must be colored BLACK. (There
should not be two consecutive RED nodes).
Property #4: In all the paths of the tree, there should be same number of BLACK
colored nodes.
Property #5: Every new node must be inserted with RED color.
Property #6: Every leaf (e.i. NULL node) must be colored BLACK.

Every Red Black Tree is a binary search tree but every Binary Search Tree need not
be Red Black tree.

Insertion into RED BLACK Tree


In a Red-Black Tree, every new node must be inserted with the color RED. The insertion
operation in Red Black Tree is similar to insertion operation in Binary Search Tree. But it
is inserted with a color property. After every insertion operation, we need to check all the
properties of Red-Black Tree. If all the properties are satisfied then we go to next operation
otherwise we perform the following operation to make it Red Black Tree.

 Recolor
 Rotation
 Rotation followed by Recolor

The insertion operation in Red Black tree is performed using the following steps.
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty then insert the newNode as Root node with color Black and exit
from the operation.
Step 3 - If tree is not Empty then insert the newNode as leaf node with color Red.
Step 4 - If the parent of newNode is Black then exit from the operation.
Step 5 - If the parent of newNode is Red then check the color of parentnode's sibling of
newNode.
Step 6 - If it is colored Black or NULL then make suitable Rotation and Recolor it.
Step 7 - If it is colored Red then perform Recolor. Repeat the same until tree becomes Red
Black Tree.
Example

You might also like