Report On Red-Black Tree
Report On Red-Black Tree
Submitted To,
Tanni Mittra
Senior Lecturer
Department of Computer Science and Engineering
East West University
Submitted By,
Adnan mahmud
ID: 2018-3-60-023
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.
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.
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:
return r;
}
Then the insertRBTree function check the rules of the Red-Black Tree and
Do changes according to the above cases:
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;
}
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.
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.
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.
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.
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.
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.
z = NULL;
node *x;
while (n != NULL){
if (n->data == key) {
z = n;
}
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);
}
}
s->colour = 0;
x->parent->colour = 1;
rotateL(x->parent);
s = x->parent->right;
}
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;
}
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;
}
This is All the Function and Discussion about the Red-Black Tree.