0% found this document useful (0 votes)
56 views19 pages

Report On Red-Black Tree

1. The document describes a red-black tree data structure, which is a self-balancing binary search tree where each node is colored red or black. 2. It outlines the rules that red-black trees follow to maintain balance during insertions and deletions and allows searching, minimum/maximum values, and black height to be found in O(log n) time. 3. Algorithms for insertion, searching, finding minimum/maximum values, calculating black height, and deletion are provided, with the deletion algorithm recoloring nodes and performing rotations to maintain the red-black tree properties.
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)
56 views19 pages

Report On Red-Black Tree

1. The document describes a red-black tree data structure, which is a self-balancing binary search tree where each node is colored red or black. 2. It outlines the rules that red-black trees follow to maintain balance during insertions and deletions and allows searching, minimum/maximum values, and black height to be found in O(log n) time. 3. Algorithms for insertion, searching, finding minimum/maximum values, calculating black height, and deletion are provided, with the deletion algorithm recoloring nodes and performing rotations to maintain the red-black tree properties.
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/ 19

Report

Course Name: Data Structure


Course Code: CSE207
Section: 04
Semester: Summer 2021

Submitted To,
Tanni Mittra
Senior Lecturer
Department of Computer Science and Engineering
East West University

Submitted By,
Adnan mahmud
ID: 2018-3-60-023

Firoz Mahmud Rizvi


ID: 2019-2-60-076

Mst.Fateha Islam Shorna


ID: 2019-1-60-195
Red-Black Tree

A red-black tree is a kind of self-balancing binary search tree where each


node has an extra bit, and that bit is often interpreted as the colour (red or
black). These colours are used to ensure that the tree remains balanced
during insertions and deletions. Although the balance of the tree is not
perfect, it is good enough to reduce the searching time and maintain it
around O(log n) time, where n is the total number of elements in the tree.
This tree was invented in 1972 by Rudolf Bayer. 

It must be noted that as each node requires only 1 bit of space to store
the colour information, these types of trees show identical memory
footprint to the classic (uncoloured) binary search tree.

Rules That Every Red-Black Tree Follows: 

1. Every node has a colour either red or black.


2. The root of the tree is always black.
3. There are no two adjacent red nodes
4. Every path from a node to any of its descendants NULL nodes
has the same number of black nodes.
Nodes:
Each nodes contains 5 properties:
1. Value of the node.
2. Address of Parent node.
3. Address of Left node.
4. Address of Right node.
5. Colour of the value.

class node{
public:
int data;
node *parent;
node *left;
node *right;

int colour;
node(int d){
data = d;
colour = 1; {// o means Black, 1 means red, 2 means double black}
left = NULL;
right = NULL;

};
Insertion:
The insertion is like Binary Search Tree and have to add extra red-
black colour to node and keep rotation & recolouring to maintain it’s rules.

For insertion there are some certain rule:


Case:
1. If tree is empty, create new node as root node with colour black;
2. If tree is not empty, create newnode as leaf node with colour
red.
3. If parent of new node is black then exit.
4. If parent of new node is red then check the colour of parent’s
sibling and
a. If colour is black or Null then do suitable rotation and
recolour.
b. If colour is red then recolour & also check if grandparent
of new node is not root then recolour it and repeat those
step.

void insert(int n) {
current = new node(n);
root = BST(root, current);
root->colour=0;
root->parent==NULL;
insertRBTree(current);
}

After that the BST function add those numbers just Like Normal BST:

node *BST(node *r, node *ptr) {


if (r == NULL)
return ptr;

if (ptr->data < r->data) {


r->left = BST(r->left, ptr);
r->left->parent = r;
}
else if (ptr->data > r->data) {
r->right = BST(r->right, ptr);
r->right->parent = r;
}

return r;
}

Then the insertRBTree function check the rules of the Red-Black Tree and
Do changes according to the above cases:

void insertRBTree(node *x){


p = NULL;
gp = NULL;

while(x!=root && x->colour==1 && x->parent->colour ==1){

p = x->parent;
gp = p->parent;
if(p == gp->left){

unc = gp->right;

if(unc->colour==1){
unc->colour=0;
p->colour = 0;
gp->colour = 1;
x= gp;
}

else{

if(x == p->right){
rotateL(p);
x = p;
p =x->parent;
}
rotateR(gp);
int swapC = p->colour;
p->colour = gp->colour;
gp->colour = swapC;
x = p;
}
}

else{
unc = gp->left;
if(unc->colour ==1){
unc->colour = 0;
p->colour = 0;
gp->colour = 1;
x = gp;
}

else{

if(x == p->left){
rotateR(p);
x = p;
p = x->parent;
}
rotateL(gp);
int swapC = p->colour;
p->colour = gp->colour;
gp->colour = swapC;
x = p;
}
}
}

root->colour = 0;
}

rotateL and rotateR function work as like AVL tree.

void rotateL(node *ptr) {


node *right_child = ptr->right;
ptr->right = right_child->left;

if (ptr->right != NULL)
ptr->right->parent = ptr;

right_child->parent = ptr->parent;

if (ptr->parent == NULL)
root = right_child;
else if (ptr == ptr->parent->left)
ptr->parent->left = right_child;
else
ptr->parent->right = right_child;

right_child->left = ptr;
ptr->parent = right_child;
}
void rotateR(node *ptr) {
node *left_child = ptr->left;
ptr->left = left_child->right;

if (ptr->left != NULL)
ptr->left->parent = ptr;

left_child->parent = ptr->parent;

if (ptr->parent == NULL)
root = left_child;
else if (ptr == ptr->parent->left)
ptr->parent->left = left_child;
else
ptr->parent->right = left_child;

left_child->right = ptr;
ptr->parent = left_child;

}
Searching:
We can search value from this Tree faster than any normal Binary Search
Tree. This Search Value can search value from the tree & print it.

node *searchTree(int k){


z = NULL;
node *n = root;
while (n != NULL){
if (n->data == k) {

z = n;
break;
}

if (n->data <= k) {
n = n->right;
} else {
n = n->left;
}
}

if (z == NULL) {
cout<<"Couldn't find key in the tree"<<endl;
}
else{
cout<<"Found key in the tree\nNode: "<<z->data;
if(z->colour==0)
cout<<"black"<<endl;
else
cout<<"(red)"<<endl;
}

Minimum Value:
We can easily find minimum value from this tree. This minValue function can
easily find this minimum value from the Tree.

node *minValue(node *ptr){


z = ptr;
while(z->left !=NULL){
z = z->left;
}

return z;
}
Maximum Value:
We can easily find maximum value from this tree. This maxValue function can
easily find this minimum value from the Tree.

node *maxValue(node *ptr){


z = ptr;
while(z->left !=NULL){
z = z->right;
}
return z;

Black Height:
We can Find the number of black node from any path as all path have the same
number of black nodes in a Red-black Tree. This blackHeight function can find &
print the value of black height.

void blackHeight(node *ptr){


int b = 0;
while(ptr !=NULL){
if(ptr->colour == 0)
b++;
ptr = ptr->left;
}

cout<<"Black Height : "<<b;


}

Deletion:
We can also delete any nodes from the Trees like any normal Binary Search
Tree and we have do some recolour and rotation so that it can fullfill Red-Black
Tress’s condition.

For insertion there are some certain rule:


1. Save the colour of the node .
2. If the left child of the is NULL.
a. Assign the right child of the node to the to a node x.
b. Transplant the node with x.
3. If the right child of the is NULL.
a. Assign the left child of the node to the to a node x.
b. Transplant the node with x.

4. Otherwise
a. Assign the minimum of right subtree of the node into y.
b. Save the colour of y.
c. Assign the right child of y into x;
d. If y is a child of the node , then set the parent of x as y.
e. Else transplant y with right child of y.
f. Transplant the node with y.
g. Set the colour of y with the node’s colour.

5. If the node’s colour is black , Call deleteFix().


void deleteNode(int data) {
deleteNodeHelper(root, data);
}
This function takes the value of the node to be deleted.
void deleteNodeHelper(node *n, int key) {

z = NULL;
node *x;
while (n != NULL){
if (n->data == key) {
z = n;
}

if (n->data <= key) {


n = n->right;
} else {
n = n->left;
}
}

if (z == NULL) {
cout<<"Couldn't find key in the tree"<<endl;
return;
}

y = z;
int y_original_color = y->colour;
if (z->left == NULL) {
x = z->right;
rbTransplant(z, z->right);
} else if (z->right == NULL) {
x = z->left;
rbTransplant(z, z->left);
} else {
y = minValue(z->right);
y_original_color = y->colour;
x = y->right;
if (y->parent == z) {
x->parent = y;
} else {
rbTransplant(y, y->right);
y->right = z->right;
y->right->parent = y;
}

rbTransplant(z, y);
y->left = z->left;
y->left->parent = y;
y->colour = z->colour;
}
delete z;
if (y_original_color == 0){
fixDelete(x);
}
}

void fixDelete(node *x) {


node *s = NULL;
while (x != root && x->colour == 0) {
if (x == x->parent->left) {
s = x->parent->right;
if (s->colour == 1) {

s->colour = 0;
x->parent->colour = 1;
rotateL(x->parent);
s = x->parent->right;
}

if (s->left->colour == 0 && s->right->colour == 0) {

s->colour = 1;
x = x->parent;
} else {
if (s->right->colour == 0) {
// case 3.3
s->left->colour = 0;
s->colour = 1;
rotateR(s);
s = x->parent->right;
}

s->colour = x->parent->colour;
x->parent->colour = 0;
s->right->colour = 0;
rotateL(x->parent);
x = root;
}
} else {
s = x->parent->left;
if (s->colour == 1) {

s->colour = 0;
x->parent->colour = 1;
rotateR(x->parent);
s = x->parent->left;
}

if (s->right->colour == 0 && s->right->colour == 0) {


// case 3.2
s->colour = 1;
x = x->parent;
} else {
if (s->left->colour == 0) {

s->right->colour = 0;
s->colour = 1;
rotateL(s);
s = x->parent->left;
}

s->colour = x->parent->colour;
x->parent->colour = 0;
s->left->colour = 0;
rotateR(x->parent);
x = root;
}
}
}
x->colour = 0;
}

void rbTransplant(node *u, node *v){


if (u->parent == NULL) {
root = v;
} else if (u == u->parent->left){
u->parent->left = v;
} else {
u->parent->right = v;
}
v->parent = u->parent;

This is All the Function and Discussion about the Red-Black Tree.

You might also like