You are on page 1of 4

✌️✌️Lucky Carrot ✌️✌️

Question
RED BLACK TREE

1. Create a Red Black Tree using the following sequence:


41,22,5,51,48,29,18,21,45,3
2. Try implementing (a) Red Black Tree insertion in C Program and
print those datas with InOrder Traversal

Inoder Traversal of Created Tree 3 5 18 21 22 29 41 45 48 51 Process exited after 0.1592 seconds with return value o Press any key to
continue
Show transcribed image text Inoder Traversal of Created Tree 3 5 18 21 22 29 41 45 48 51 Process exited after 0.1592 seconds with return
value o Press any key to continue

Answer
Red Black is a type of binary search tree which is self
balancing and these type of trees have an additional bit to
denote the colour which is
either red or black. The
colours has the significance which tells that the tree is balanced
during insertions or deletions. Searching an
element takes
O(log n) time, where n is length of tree.

Below is the C code snippet:

The code contains lots of comments, please read all the


comments very minutely to get proper understanding of the
code:
#include <stdio.h>

#include <stdlib.h>

struct node { //a structure to hold the red black tree data

struct node* p; // signifies parent

struct node* l; // denotes the left child

struct node* r; // denotes the right child

int d; // used to store data

int c; // used for colouration red is 1 and black is 0

};

struct node* root = NULL; //used to store the root of the tree

struct node* bst(struct node* traverse, struct node* tmp) //a function used for applying BST insertion

if (traverse == NULL) //if the tree is blank, then it is returning a new node

return tmp; //returning tmp pointer

if (tmp->d < traverse->d) //if tree is not empty then traverse and perform addition of value

traverse->l = bst(traverse->l, tmp); //calling bst insertion

traverse->l->p = traverse; // parent of left child of the current data

else if (tmp->d > traverse->d) //if tree is not empty then traverse and perform addition of value

traverse->r = bst(traverse->r, tmp); //calling bst insertion

traverse->r->p = traverse; // parent of right child of the current data

return traverse;// return the created or unchanged node

void right_Rotation(struct node* tmp) //function to perform right rotation of the node passed as parameter

struct node* left = tmp->l; //initialising left pointer to current node left child

tmp->l = left->r; //setting left child of current node to right child of left node

left->p = tmp->p;

if (!tmp->p) //if this does not have value

root = left;

else if (tmp == tmp->p->l) //if tmp is equal to left child of parent of tmp

tmp->p->l = left;
else

tmp->p->r = left;
if (tmp->l) //if this has value

tmp->l->p = tmp;

left->r = tmp;

tmp->p = left;

void inorder(struct node* traverse) // for calculating inorder Traversal

if (traverse == NULL) //if root is null

return;

inorder(traverse->l); ///else traverse

printf("%d ", traverse->d); //print data

inorder(traverse->r); //calling inorder function

void left_Rotation(struct node* tmp) //function to perform left rotation of the node passed as parameter

struct node* right = tmp->r; //initialising right pointer to current node right child

tmp->r = right->l; //setting right child of current node to left child of right node

right->p = tmp->p;

if (!tmp->p) //if this does not have value

root = right;

else if (tmp == tmp->p->l)

tmp->p->l = right;

else

tmp->p->r = right;

if (tmp->r) //if this has value

tmp->r->p = tmp;

right->l = tmp;

tmp->p = right;

void fix(struct node* root, struct node* pt) //function to adjust the violations created by bst insertion

struct node* prnt_pt = NULL;

struct node* grand_prnt_pt = NULL;

while ((pt != root) && (pt->c != 0)

&& (pt->p->c == 1))

prnt_pt = pt->p;

grand_prnt_pt = pt->p->p;

//Case : When pt has a parent which is left child of the grand parent of pt

if (prnt_pt == grand_prnt_pt->l)

struct node* uncle_pt = grand_prnt_pt->r;

// Case : When only recolouring is needed as uncle of pt is also red

if (uncle_pt != NULL && uncle_pt->c == 1)

grand_prnt_pt->c = 1;

prnt_pt->c = 0;

uncle_pt->c = 0;

pt = grand_prnt_pt;

else {

// Case : When pt is right child of its parent and left rotation is needed

if (pt == prnt_pt->r) { //checking pt is equal to right child of parent of pt


left_Rotation(prnt_pt); //if matches the left rotate

pt = prnt_pt; // set pt to parent of pt

prnt_pt = pt->p;

// Case : When pt is left child of its parent and right rotation is needed

right_Rotation(grand_prnt_pt); //perform right rotate

int t = prnt_pt->c;

prnt_pt->c = grand_prnt_pt->c;

grand_prnt_pt->c = t;

pt = prnt_pt; // set pt to parent of pt

//Case : When pt has a parent which is right child of the grand parent of pt

else {

struct node* uncle_pt = grand_prnt_pt->l;

// Case : When only recolouring is needed as uncle of pt is also red

if ((uncle_pt != NULL) && (uncle_pt->c == 1))

grand_prnt_pt->c = 1;

prnt_pt->c = 0;

uncle_pt->c = 0;

pt = grand_prnt_pt;

else {

// Case : When pt is left child of its parent and right rotation is needed

if (pt == prnt_pt->l) { //checking pt is equal to left child of parent of pt

right_Rotation(prnt_pt); //if matches the right rotate

pt = prnt_pt; // set pt to parent of pt

prnt_pt = pt->p;

// Case : When pt is right child of its parent and left rotation is needed

left_Rotation(grand_prnt_pt); //performing left rotate

int t = prnt_pt->c; //setting prnt_pt -> c to t

prnt_pt->c = grand_prnt_pt->c; //setting prnt_pt->c = grand_prnt_pt->c

grand_prnt_pt->c = t;

pt = prnt_pt; //setting parent of pt to pt

root->c = 0;

// main function

int main()

int n = 10;

int a[10] = {41, 22, 5, 51, 48, 29, 18, 21, 45, 3 }; //given data

for (int i = 0; i < n; i++) {

struct node* tmp = (struct node*)malloc(sizeof(struct node)); // allocating memory

tmp->r = NULL; //right child pointer as NULL

tmp->l = NULL; //left child pointer as NULL

tmp->p = NULL; //parent child pointer as NULL

tmp->d = a[i]; //setting data a[i]

tmp->c = 1; //setting color as red

root = bst(root, tmp); //performing bst insertion

fix(root, tmp); //to fix anomalies created during bst insertion

printf("Inoder Traversal of the Created Tree \n");

inorder(root);

return 0;

Note: Please read all comments very


carefully.

Output:

//-------------------------------------------------------------------------------------------------//

Inoder Traversal of the Created Tree 3 5 18 21 22 29 41 45 48 51 5


Comment
Comments

Leave a comment
Leave a comment

Post comment

You might also like