Professional Documents
Culture Documents
Question
RED BLACK TREE
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.
#include <stdlib.h>
struct node { //a structure to hold the red black tree data
};
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
if (tmp->d < traverse->d) //if tree is not empty then traverse and perform addition of value
else if (tmp->d > traverse->d) //if tree is not empty then traverse and perform addition of value
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;
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;
return;
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;
root = right;
tmp->p->l = right;
else
tmp->p->r = right;
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
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)
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
prnt_pt = pt->p;
// Case : When pt is left child of its parent and right rotation is needed
int t = prnt_pt->c;
prnt_pt->c = grand_prnt_pt->c;
grand_prnt_pt->c = t;
//Case : When pt has a parent which is right child of the grand parent of pt
else {
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
prnt_pt = pt->p;
// Case : When pt is right child of its parent and left rotation is needed
grand_prnt_pt->c = t;
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
inorder(root);
return 0;
Output:
//-------------------------------------------------------------------------------------------------//
Leave a comment
Leave a comment
Post comment