Red Black Tree (Data Structures) - Javatpoint
Red Black Tree (Data Structures) - Javatpoint
DS Tutorial
Advertisement
DS Tutorial Mygraduaid
Asymptotic Analysis
Skip list in DS
DS Stack
DS Stack
Array Implementation
In the above tree, if we want to search the 80. We will first compare
Linked List Implementation 80 with the root node. 80 is greater than the root node, i.e., 10, so
searching will be performed on the right subtree. Again, 80 is
compared with 15; 80 is greater than 15, so we move to the right of
DS Queue the 15, i.e., 20. Now, we reach the leaf node 20, and 20 is not equal
to 80. Therefore, it will show that the element is not found in the
tree. After each operation, the search is divided into half. The above
:
BST will take O(logn) time to search the element.
DS Queue
Types of Queues
Array Representation
Circular Queue
Deque
Priority Queue
DS Tree
DS Tree
Binary Tree
AVL Tree
B Tree
B+ Tree
Red-black tree
DS Graph
The above tree shows the right-skewed BST. If we want to search
the 80 in the tree, we will compare 80 with all the nodes until we
DS Graph
find the element or reach the leaf node. So, the above right-
Graph Implementation skewed BST will take O(N) time to search the element.
BFS Algorithm
In the above BST, the first one is the balanced BST, whereas the
DFS Algorithm second one is the unbalanced BST. We conclude from the above
Spanning Tree two binary search trees that a balanced tree takes less time than
an unbalanced tree for performing any operation on the tree.
a)Prim's Algorithm
b)Kruskal's Algorithm Therefore, we need a balanced tree, and the Red-Black tree is a
self-balanced binary search tree. Now, the question arises that why
do we require a Red-Black tree if AVL is also a height-balanced
tree. The Red-Black tree is used because the AVL tree requires
DS Searching
many rotations when the tree is large, whereas the Red-Black tree
requires a maximum of two rotations to balance the tree. The main
Linear Search difference between the AVL tree and the Red-Black tree is that the
Binary Search AVL tree is strictly balanced, while the Red-Black tree is not
completely height-balanced. So, the AVL tree is more balanced
than the Red-Black tree, but the Red-Black tree guarantees
O(log2n) time for all operations like insertion, deletion, and
DS Sorting
searching.
Bubble Sort Insertion is easier in the AVL tree as the AVL tree is strictly
balanced, whereas deletion and searching are easier in the Red-
Bucket Sort
:
Comb Sort Black tree as the Red-Black tree requires fewer rotations.
Counting Sort As the name suggests that the node is either colored in Red or
Heap Sort Black color. Sometimes no rotation is required, and only recoloring
is needed to balance the tree.
Insertion Sort
Radix Sort balancing means that it balances the tree itself by either
doing the rotations or recoloring the nodes.
Selection Sort
Shell Sort
This tree data structure is named as a Red-Black tree as
each node is either Red or Black in color. Every node
Bitonic Sort
stores one extra information known as a bit that
Cocktail Sort represents the color of the node. For example, 0 bit
Cycle Sort denotes the black color while 1 bit denotes the red color
of the node. Other information stored by the node is
Tim Sort
similar to the binary tree, i.e., data part, left pointer and
right pointer.
Singly Linked List vs Doubly If the node is Red, then its children should be in Black
Linked List color. In other words, we can say that there should be no
Binary vs Binary Search Tree red-red parent-child relationship.
Tree vs Graph Every path from a node to any of its descendant's NIL
Binary Search tree vs AVL tree node should have same number of black nodes.
Stack vs Array
1. If the tree is empty, then we create a new node as a root node with
Trie Data Structure
the color black.
Heap Data Structure 2. If the tree is not empty, then we create a new node as a leaf node
Graph Algorithms
Hash Function in Data Structure We know the second rule of the Red Black tree that if the tree is
Complete Binary Tree not empty then the newly created node will have the Red color.
Therefore, node 18 has a Red color, as shown in the below figure:
Threaded Binary Tree
Diameter of Binary Tree Now we verify the third rule of the Red-Black tree, i.e., the parent of
the new node is black or not. In the above figure, the parent of the
Height of Binary Tree
node is black in color; therefore, it is a Red-Black tree.
Inorder Tree Traversal without
Stack Step 3: Now, we create the new node having value 7 with Red
Semi-Structured Data
Implementation of Deque by
Circular Array
Self-organizing List
Topological Sorting
Inversion count
Expression tree in DS
Garbage Collection in DS
:
Merge Sort on Doubly Linked
List
Sort Stack using Recursion Now we verify the third rule of the Red-Black tree, i.e., the parent of
the new node is black or not. As we can observe, the parent of the
LIFO Approach in data structure
node 7 is black in color, and it obeys the Red-Black tree's
Big O Notation in DS properties.
Binary Tree Traversal in DS
Step 4: The next element is 15, and 15 is greater than 10, but less
Queue Operations in DS than 18, so the new node will be created at the left of node 18. The
What is a Non-Linear Data node 15 would be Red in color as the tree is not empty.
Structure
Maximum area rectangle parent is Red, then we have to check the color of the parent's
created by selecting four sides sibling of a new node. The new node is node 15; the parent of the
from an array new node is node 18 and the sibling of the parent node is node 7.
Maximum number of distinct As the color of the parent's sibling is Red in color, so we apply the
nodes in a root-to-leaf path rule 4a. The rule 4a says that we have to recolor both the parent
Hashing - Open Addressing for and parent's sibling node. So, both the nodes, i.e., 7 and 18, would
Collision Handling be recolored as shown in the below figure.
Introduction to Hashing
Fibonacci Heap node is the root node or not. As we can observe in the above
:
Find whether an array is subset figure, the parent's parent of a new node is the root node, so we do
of another array not need to recolor it.
Check if any anagram of a string In the above figure, we can observe that it violates the property of
is palindrome or not the parent-child relationship as it has a red-red parent-child
Minimum Insertions to form a relationship. We have to apply some rules to make a Red-Black
Palindrome tree. Since the new node's parent is Red color, and the parent of
Allocate Minimum Number of the new node has no sibling, so rule 4a will be applied. The rule 4a
Pages says that some rotations and recoloring would be performed on
the tree.
Assembly Line Scheduling
Breadth First Search or BFS for a Since node 16 is right of node 15 and the parent of node 15 is node
Graph 18. Node 15 is the left of node 18. Here we have an LR relationship,
Find an element in array such so we require to perform two rotations. First, we will perform left,
that sum of the left array is equal and then we will perform the right rotation. The left rotation would
to the sum of the right array be performed on nodes 15 and 16, where node 16 will move
Bridges in a Graph upward, and node 15 will move downward. Once the left rotation is
performed, the tree looks like as shown in the below figure:
Bubble Sort in JavaScript
Line Graph
Application of Binary Tree black, so it will change to a red color as shown in the below figure:
B Tree Visualization
What is the Balance Factor of Step 6: The next element is 30. Node 30 is inserted at the right of
AVL Tree node 18. As the tree is not empty, so the color of node 30 would be
AVL Tree Implementation in red.
Golang
B Tree Insertion
B+ Tree Insertion
B Tree Applications
B Tree Properties
B+ Tree Deletion node, i.e., node 30 is node 16 and node 16 is not a root node, so we
will recolor the node 16 and changes to the Red color. The parent
B+ Tree Insertion
of node 16 is node 10, and it is not in Red color, so there is no Red-
Checking for the Mirror Images red conflict.
in the Binary Trees
Find Level in a Binary Tree with Since 25 is greater than 10, 16, 18 but less than 30; so, it will come at
Max Sum the left of node 30. As the tree is not empty, node 25 would be in
Red color. Here Red-red conflict occurs as the parent of the newly
Find kth Smallest Element in a
Binary Search Tree created is Red color.
Find Next Greater Element in Since there is no parent's sibling, so rule 4a is applied in which
Binary Tree
rotation, as well as recoloring, are performed. First, we will perform
Hashing in Data Structure rotations. As the newly created node is at the left of its parent and
the parent node is at the right of its parent, so the RL relationship
:
Primitive Data Structure is formed. Firstly, the right rotation is performed in which node 25
goes upwards, whereas node 30 goes downwards, as shown in the
Find if Binary Tree Satisfies
Balanced Height Property below figure.
Interpolation Search
Substring Check
Serialize and Deserialize an N-ary Step 8: The next element is 40. Since 40 is greater than 10, 16, 18,
Tree 25, and 30, so node 40 will come at the right of node 30. As the tree
Sum of all elements of N-ary Tree is not empty, node 40 would be Red in color. There is a Red-red
conflict between nodes 40 and 30, so rule 4b will be applied.
The Great Tree-List Recursion
Problem
Majority Element After recoloring, we also have to check the parent's parent of a new
Maximum equilibrium sum in an node, i.e., 25, which is not a root node, so recoloring would be
array performed, and the color of node 25 changes to Red.
MO's Algorithm
Sparse Table
Recaman's Sequence
An Interesting Method to
Generate Binary Numbers from 1
to n
Polish and Reverse Polish When left rotation is performed, node 40 will come upwards, and
Notation
node 30 will come downwards, as shown in the below figure:
Suffix Trees in data structure
Detect and Remove Loop in a After rotation, the recoloring is performed on nodes 30 and 40. The
Linked List
color of node 30 would become Red, while the color of node 40
Difference Between a Tree and a would become black.
Forest in Data Structures
Dependency Graph Initially, we are having the address of the root node. First, we will
apply BST to search the node. Since 30 is greater than 10 and 20,
Diameter of an N-ary tree
which means that 30 is the right child of node 20. Node 30 is a leaf
Height of n-ary tree if parent
node and Red in color, so it is simply deleted from the tree.
array is given
Introduction to Heavy Light If we want to delete the internal node that has one child. First,
Decomposition replace the value of the internal node with the value of the child
node and then simply delete the child node.
LCA in a binary tree using RMQ
Number of Siblings of a given Let's take another example in which we want to delete the
node in n-arr tree internal node, i.e., node 20.
Number of ways to traverse an
N-arr
Callback Queue
Perfect Binary Trees We cannot delete the internal node; we can only replace the value
Reverse Sort of that node with another value. Node 20 is at the right of the root
node, and it is having only one child, node 30. So, node 20 is
Scapegoat Trees
replaced with a value 30, but the color of the node would remain
Stack Organisation the same, i.e., Black. In the end, node 20 (leaf node) is deleted from
Time Complexity of using heap the tree.
Pair of strings having longest If we want to delete the internal node that has two child nodes. In
common prefix of maximum this case, we have to decide from which we have to replace the
length in given array
value of the internal node (either left subtree or right subtree). We
have two ways:
:
Print all Possible Combinations Inorder predecessor: We will replace with the largest
of Words from the Dictionary
value that exists in the left subtree.
using Trie
Segment Tree (Range Maximum Inorder successor: We will replace with the smallest
Query with Node Update) value that exists in the right subtree.
Numbsubarrayer of elements
less than or equal to a given
number in a given
:
Queries to add, remove and
return the difference of
maximum and minimum
COUNT OF ZEROES
Flatten a binary tree into linked black. As the double black's sibling and its children have black so
list it cannot give its black color to neither of these. Now, the double
black's parent node is Red so double black's node add its black
General Tree (Each node can
have arbitrary number of color to its parent node. The color of the node 20 changes to black
children) Level Order Traversal while the color of the nil node changes to a single black as shown
Which Minimum Spanning Tree After adding the color to its parent node, the color of the double
Algorithm is better black's sibling, i.e., node 30 changes to red as shown in the below
Ackermann Function figure.
BINARY TO SYMMETRIX In the above tree, we can observe that there is no longer double
INPLACE MATRIX TRANSPOSE black's problem exists, and it is also a Red-Black tree.
Queue for Competitive Initially, the 15 is replaced with a nil value. After replacement, the
Programming node becomes double black. Since double black's sibling is Red so
:
Recursively remove all adjacent color of the node 20 changes to Red and the color of the node 30
duplicates changes to Black.
MCQs on backtracking
DIFFERENCE BETWEEN
GREEDY AND DIVIDE AND
CONQUER
Largest Number After Digit Swap the color of double black's sibling and the sibling
Swaps By Parity child which is nearer to the double black node.
Minimize Deviation In Array
Rotate the sibling in the opposite direction of the double
Print a given matrix in spiral form black.
Print unique rows in a given
Apply case 6
Boolean matrix
Restrictive Candy Crush Suppose we want to delete the node 1 in the below tree.
SIP Stack
Stack Pointer
The Great Tree-List Recursion As we can observe in the above tree that double black situation
Problem still exists. So, we need to case 6. Let's first see what is case 6.
What Does Big O(N^2) Swap the color of Parent and its sibling node.
Complexity Mean
Rotate the parent towards the Double black's direction
How to handle duplicates in
Binary Search Tree
Remove Double black
Optimal cell in matrix to collect
maximum coins Change the Red color to black.
Interleave the first half of the of node 5 is node 25, which is black in color. The far child of the
queue with the second half double black node is node 30, which is Red in color as shown in
the below figure:
The sum of all elements between
k1'th and k2'th smallest elements
Internal Data Structures and First, we will swap the colors of Parent and its sibling. The parent of
Time Complexity Table of All the node 5 is node 10, and the sibling node is node 25. The colors of
C++ STL Containers both the nodes are black, so there is no swapping would occur.
Advanced DS MCQ
Insertion at beginning
Insertion at end
Deletion at beginning
Deletion at end Now let us implement the Red-black tree Data Structure in the
java programming language.
Deletion after specified node
Traversing
// Each element of the Red Black tree node has four mem
:
Searching bers.
// Out of these four members two variables are of Node_Re
d_Black_Tree class type named left_node_addr and right_no
de_addr storing the left and right nodes of the previous nod
Circular Linked List
e
Node_Red_Black_Tree left_node_addr, right_node_addr;
Insertion at beginning
// The node_data Integer variable is used to store the data
Insertion at end present in that particular node
int node_data;
Deletion at beginning
// The node_data Integer variable is used to store the color
Deletion at the end
of that particular node
int colour_of_node;
d)RR Rotation
}
handleReorient( item );
}
// Insertion fails if already present
if (current_node != node_null)
return;
current_node = new Node_Red_Black_Tree(item, node_
null, node_null);
// Attach to parent_node
if (item < parent_node.node_data)
parent_node.left_node_addr = current_node;
else
parent_node.right_node_addr = current_node;
:
handleReorient( item );
}
private void handleReorient(int item)
{
// Do the colour_of_node flip
current_node.colour_of_node = RED;
current_node.left_node_addr.colour_of_node = BLACK;
current_node.right_node_addr.colour_of_node = BLACK
;
if (parent_node.colour_of_node == RED)
{
// Have to rotate
grand_node.colour_of_node = RED;
if (item < grand_node.node_data != item < parent_nod
e.node_data)
parent_node = rotate( item, grand_node ); // Start d
bl rotate
current_node = rotate(item, great_node );
current_node.colour_of_node = BLACK;
}
// Make root black
header_node.right_node_addr.colour_of_node = BLACK;
}
private Node_Red_Black_Tree rotate(int item, Node_Red_
Black_Tree parent_node)
{
if(item < parent_node.node_data)
return parent_node.left_node_addr = item < parent_n
ode.left_node_addr.node_data ? rotateWithleft_node_addrC
hild(parent_node.left_node_addr) : rotateWithright_node_ad
drChild(parent_node.left_node_addr) ;
else
return parent_node.right_node_addr = item < parent
_node.right_node_addr.node_data ? rotateWithleft_node_ad
drChild(parent_node.right_node_addr) : rotateWithright_no
de_addrChild(parent_node.right_node_addr);
}
/* Rotate binary tree node with left_node_addr child */
private Node_Red_Black_Tree rotateWithleft_node_addr
Child(Node_Red_Black_Tree k2)
{
Node_Red_Black_Tree k1 = k2.left_node_addr;
k2.left_node_addr = k1.right_node_addr;
k1.right_node_addr = k2;
return k1;
}
/* Rotate binary tree node with right_node_addr child */
private Node_Red_Black_Tree rotateWithright_node_add
:
rChild(Node_Red_Black_Tree k1)
{
Node_Red_Black_Tree k2 = k1.right_node_addr;
k1.right_node_addr = k2.left_node_addr;
k2.left_node_addr = k1;
return k2;
}
/* Class Red_Black_Tree_Run */
class Red_Black_Tree_Run
{
public static void main(String[] args)
{
Scanner scannner_object = new Scanner(System.in);
/* Creating object of RedBlack Tree */
Red_Black_Tree red_black_tree_object = new Red_Black
_Tree(Integer.MIN_VALUE);
System.out.println("Red Black Tree Test\n");
char ch;
/* Perform tree operations */
do
{
System.out.println("\nThe options list for Red Black Tr
ee::\n");
System.out.println("1. To add a new node in the Red-
Black Tree");
System.out.println("2. To search the Red-
Black Tree for a node");
System.out.println("3. To get node count of nodes in R
ed Black Tree");
System.out.println("4. To check if the Red_Black_Tree i
s Empty or not?");
System.out.println("5. To Clear the Red_Black_Tree.");
switch (option_list_choice)
{
case 1 :
System.out.println("Enter integer node_data to inser
t");
red_black_tree_object.insert( scannner_object.nextI
nt() );
break;
case 2 :
System.out.println("Enter integer node_data to sear
ch");
System.out.println("Search result : "+ red_black_tree
_object.search( scannner_object.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ red_black_tree_objec
t.countNodes());
:
break;
case 4 :
System.out.println("Empty status = "+ red_black_tre
e_object.isEmpty());
break;
case 5 :
System.out.println("\nTree Cleared");
red_black_tree_object.makeEmpty();
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
Output:
RBT in Post-order: 10B 31R 33B 77R 90R 87B 45R 18B
RBT in Pre-order: 18B 10B 45R 33B 31R 87B 77R 90R
RBT in In-order: 10B 18B 31R 33B 45R 77R 87B 90R
Wanna proceed further(Type y or n)?
RBT in Post-order: 10B 31R 33B 77R 90R 87B 45R 18B
RBT in Pre-order: 18B 10B 45R 33B 31R 87B 77R 90R
RBT in In-order: 10B 18B 31R 33B 45R 77R 87B 90R
Wanna proceed further(Type y or n)?
RBT in Post-order: 10B 31R 33B 77R 90R 87B 45R 18B
RBT in Pre-order: 18B 10B 45R 33B 31R 87B 77R 90R
RBT in In-order: 10B 18B 31R 33B 45R 77R 87B 90R
Wanna proceed further(Type y or n)?
RBT in Post-order: 10B 31R 33B 77R 90R 87B 45R 18B
RBT in Pre-order: 18B 10B 45R 33B 31R 87B 77R 90R
RBT in In-order: 10B 18B 31R 33B 45R 77R 87B 90R
Wanna proceed further(Type y or n)?
Tree Cleared
RBT in Post-order:
RBT in Pre-order:
RBT in In-order:
Wanna proceed further(Type y or n)?
RBT in Post-order:
RBT in Pre-order:
RBT in In-order:
Wanna proceed further(Type y or n)?
C++ Code:
#include<iostream>
struct node_of_the_red_black_tree
{
int key;
node_of_the_red_black_tree *parent;
char color;
node_of_the_red_black_tree *left;
node_of_the_red_black_tree *right;
};
class Red_Black_Tree
{
node_of_the_red_black_tree *root;
node_of_the_red_black_tree *temp_node1;
public :
Red_Black_Tree()
{
temp_node1=NULL;
root=NULL;
}
void insert_a_node_to_rbt();
void insert_a_node_to_rbtfix(node_of_the_red_black_tree
*);
void leftrotate_rbt_node(node_of_the_red_black_tree *);
void rightrotate_rbt_node(node_of_the_red_black_tree *);
void del_rbt_node();
node_of_the_red_black_tree* successor(node_of_the_red_
black_tree *);
void del_rbt_nodefix(node_of_the_red_black_tree *);
:
void disp();
void display( node_of_the_red_black_tree *);
void search_rbt_node();
};
void Red_Black_Tree::insert_a_node_to_rbt()
{
int init_value,i=0;
cout<<"\nKey value for the node_of_the_red_black_tree to
insert_a_node_to_rbt in RBT: "; cin>>init_value;
node_of_the_red_black_tree *temp_node,*temp_node1;
node_of_the_red_black_tree *t=new node_of_the_red_bla
ck_tree;
t->key=init_value;
t->left=NULL;
t->right=NULL;
t->color='r';
temp_node=root;
temp_node1=NULL;
if(root==NULL)
{
root=t;
t->parent=NULL;
}
else
{
while(temp_node!=NULL)
{
temp_node1=temp_node;
if(temp_node->key<t->key)
temp_node=temp_node->right;
else
temp_node=temp_node->left;
}
t->parent=temp_node1;
if(temp_node1->key<t->key)
temp_node1->right=t;
else
temp_node1->left=t;
}
insert_a_node_to_rbtfix(t);
}
void Red_Black_Tree::insert_a_node_to_rbtfix(node_of_the_r
ed_black_tree *t)
{
node_of_the_red_black_tree *u;
if(root==t)
{
t->color='b';
return;
}
:
while(t->parent!=NULL&&t->parent->color=='r')
{
node_of_the_red_black_tree *g=t->parent->parent;
if(g->left==t->parent)
{
if(g->right!=NULL)
{
u=g->right;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->right==t)
{
t=t->parent;
leftrotate_rbt_node(t);
}
t->parent->color='b';
g->color='r';
rightrotate_rbt_node(g);
}
}
else
{
if(g->left!=NULL)
{
u=g->left;
if(u->color=='r')
{
t->parent->color='b';
u->color='b';
g->color='r';
t=g;
}
}
else
{
if(t->parent->left==t)
{
t=t->parent;
rightrotate_rbt_node(t);
}
t->parent->color='b';
g->color='r';
:
leftrotate_rbt_node(g);
}
}
root->color='b';
}
}
void Red_Black_Tree::del_rbt_node()
{
if(root==NULL)
{
cout<<"\nEmpty Tree." ;
return ;
}
int x;
cout<<"\nEnter the key of the node_of_the_red_black_tree
to be del_rbt_nodeeted: "; cin>>x;
node_of_the_red_black_tree *temp_node;
temp_node=root;
node_of_the_red_black_tree *y=NULL;
node_of_the_red_black_tree *temp_node1=NULL;
int found=0;
while(temp_node!=NULL&&found==0)
{
if(temp_node->key==x)
found=1;
if(found==0)
{
if(temp_node->key<x) temp_node=temp_node-
>right;
else
temp_node=temp_node->left;
}
}
if(found==0)
{
cout<<"\nElement Not Found.";
return ;
}
else
{
cout<<"\ndel_rbt_nodeeted Element: "<<temp_node-
>key;
cout<<"\nColour: "; if(temp_node->color=='b')
cout<<"Black\n";
else
cout<<"Red\n"; if(temp_node->parent!=NULL)
cout<<"\nParent: "<<temp_node->parent->key;
else
cout<<"\nno parent node_of_the_red_black_tree pre
:
sent "; if(temp_node->right!=NULL)
cout<<"\nRight Child: "<<temp_node->right->key;
else
cout<<"\nno right child node_of_the_red_black_tree
present. "; if(temp_node->left!=NULL)
cout<<"\nLeft Child: "<<temp_node->left->key;
else
cout<<"\nno left child node_of_the_red_black_tree p
resent. ";
cout<<"\nnode_of_the_red_black_tree del_rbt_nodeete
d."; if(temp_node->left==NULL||temp_node->right==NULL)
y=temp_node;
else
y=successor(temp_node);
if(y->left!=NULL)
temp_node1=y->left;
else
{
if(y->right!=NULL)
temp_node1=y->right;
else
temp_node1=NULL;
}
if(temp_node1!=NULL)
temp_node1->parent=y->parent;
if(y->parent==NULL)
root=temp_node1;
else
{
if(y==y->parent->left)
y->parent->left=temp_node1;
else
y->parent->right=temp_node1;
}
if(y!=temp_node)
{
temp_node->color=y->color;
temp_node->key=y->key;
}
if(y->color=='b')
del_rbt_nodefix(temp_node1);
}
}
void Red_Black_Tree::del_rbt_nodefix(node_of_the_red_blac
k_tree *temp_node)
{
node_of_the_red_black_tree *s;
while(temp_node!=root&&temp_node->color=='b')
{
:
if(temp_node->parent->left==temp_node)
{
s=temp_node->parent->right;
if(s->color=='r')
{
s->color='b';
temp_node->parent->color='r';
leftrotate_rbt_node(temp_node->parent);
s=temp_node->parent->right;
}
if(s->right->color=='b'&&s->left->color=='b')
{
s->color='r';
temp_node=temp_node->parent;
}
else
{
if(s->right->color=='b')
{
s->left->color=='b';
s->color='r';
rightrotate_rbt_node(s);
s=temp_node->parent->right;
}
s->color=temp_node->parent->color;
temp_node->parent->color='b';
s->right->color='b';
leftrotate_rbt_node(temp_node->parent);
temp_node=root;
}
}
else
{
s=temp_node->parent->left;
if(s->color=='r')
{
s->color='b';
temp_node->parent->color='r';
rightrotate_rbt_node(temp_node->parent);
s=temp_node->parent->left;
}
if(s->left->color=='b'&&s->right->color=='b')
{
s->color='r';
temp_node=temp_node->parent;
}
else
{
if(s->left->color=='b')
{
:
s->right->color='b';
s->color='r';
leftrotate_rbt_node(s);
s=temp_node->parent->left;
}
s->color=temp_node->parent->color;
temp_node->parent->color='b';
s->left->color='b';
rightrotate_rbt_node(temp_node->parent);
temp_node=root;
}
}
temp_node->color='b';
root->color='b';
}
}
void Red_Black_Tree::leftrotate_rbt_node(node_of_the_red_
black_tree *temp_node)
{
if(temp_node->right==NULL)
return ;
else
{
node_of_the_red_black_tree *y=temp_node->right;
if(y->left!=NULL)
{
temp_node->right=y->left;
y->left->parent=temp_node;
}
else
temp_node->right=NULL;
if(temp_node->parent!=NULL)
y->parent=temp_node->parent;
if(temp_node->parent==NULL)
root=y;
else
{
if(temp_node==temp_node->parent->left)
temp_node->parent->left=y;
else
temp_node->parent->right=y;
}
y->left=temp_node;
temp_node->parent=y;
}
}
void Red_Black_Tree::rightrotate_rbt_node(node_of_the_red
_black_tree *temp_node)
{
:
if(temp_node->left==NULL)
return ;
else
{
node_of_the_red_black_tree *y=temp_node->left;
if(y->right!=NULL)
{
temp_node->left=y->right;
y->right->parent=temp_node;
}
else
temp_node->left=NULL;
if(temp_node->parent!=NULL)
y->parent=temp_node->parent;
if(temp_node->parent==NULL)
root=y;
else
{
if(temp_node==temp_node->parent->left)
temp_node->parent->left=y;
else
temp_node->parent->right=y;
}
y->right=temp_node;
temp_node->parent=y;
}
}
node_of_the_red_black_tree* Red_Black_Tree::successor(nod
e_of_the_red_black_tree *temp_node)
{
node_of_the_red_black_tree *y=NULL;
if(temp_node->left!=NULL)
{
y=temp_node->left;
while(y->right!=NULL)
y=y->right;
}
else
{
y=temp_node->right;
while(y->left!=NULL)
y=y->left;
}
return y;
}
void Red_Black_Tree::disp()
{
display(root);
:
}
void Red_Black_Tree::display(node_of_the_red_black_tree *t
emp_node)
{
if(root==NULL)
{
cout<<"\nEmpty Tree.";
return ;
}
if(temp_node!=NULL)
{
cout<<"\n\t node_of_the_red_black_tree: ";
cout<<"\n Key: "<<temp_node->key;
cout<<"\n Colour: "; if(temp_node->color=='b')
cout<<"Black";
else
cout<<"Red"; if(temp_node->parent!=NULL)
cout<<"\n Parent: "<<temp_node->parent->key;
else
cout<<"\n no parent node_of_the_red_black_tre
e present "; if(temp_node->right!=NULL)
cout<<"\n Right Child: "<<temp_node->right-
>key;
else
cout<<"\n no right child node_of_the_red_black_
tree present. "; if(temp_node->left!=NULL)
cout<<"\n Left Child: "<<temp_node->left->key;
else
cout<<"\n no left child node_of_the_red_black_tr
ee present. ";
cout<<endl; if(temp_node->left)
{
cout<<"\n\nLeft:\n"; display(temp_node->left);
}
/*else
cout<<"\nNo Left Child.\n";*/ if(temp_node->right)
{
cout<<"\n\nRight:\n"; display(temp_node->right);
}
/*else
cout<<"\nNo Right Child.\n"*/
}
}
void Red_Black_Tree::search_rbt_node()
{
if(root==NULL)
{
cout<<"\nEmpty Tree\n" ;
return ;
}
:
int x;
cout<<"\n Enter key of the node_of_the_red_black_tree to
be search_rbt_nodeed: "; cin>>x;
node_of_the_red_black_tree *temp_node=root;
int found=0;
while(temp_node!=NULL&& found==0)
{
if(temp_node->key==x)
found=1;
if(found==0)
{
if(temp_node->key<x) temp_node=temp_node-
>right;
else
temp_node=temp_node->left;
}
}
if(found==0)
cout<<"\nElement Not Found.";
else
{
cout<<"\n\t FOUND node_of_the_red_black_tree: ";
cout<<"\n Key: "<<temp_node->key;
cout<<"\n Colour: "; if(temp_node->color=='b')
cout<<"Black";
else
cout<<"Red"; if(temp_node->parent!=NULL)
cout<<"\n Parent: "<<temp_node->parent->key;
else
cout<<"\n no parent node_of_the_red_black_tre
e present"; if(temp_node->right!=NULL)
cout<<"\n Right Child: "<<temp_node->right-
>key;
else
cout<<"\n no right child node_of_the_red_black_
tree present. "; if(temp_node->left!=NULL)
cout<<"\n Left Child: "<<temp_node->left->key;
else
cout<<"\n no left child node_of_the_red_black_tr
ee present";
cout<<endl;
}
}
int main()
{
int ch,y=0;
Red_Black_Tree obj;
do
{
:
cout<<"\nThe options list for Red Black Tree::" ;
cout<<"\n1. To add a new node_of_the_red_black_tre
e in the Red-Black Tree";
cout<<"\n2. To search_rbt_node the Red-
Black Tree for a node_of_the_red_black_tree";
cout<<"\n3. To del_rbt_nodeete an existing Red-
Black tree node_of_the_red_black_tree";
cout<<"\n4. To display all the node_of_the_red_black
_trees of the Red-Black Tree ";
cout<<"\n5. To exit the code execution." ;
cout<<"\nInput: ";
cin>>ch;
switch(ch)
{
case 1 : obj.insert_a_node_to_rbt();
cout<<"\nNew node_of_the_red_black_tre
e added.\n";
break;
case 2 :
obj.search_rbt_node();
break;
case 3 :
obj.del_rbt_node();
break;
case 4 : obj.disp();
break;
case 5 : y=1;
break;
default : cout<<"\nEnter a Valid Choice.";
}
cout<<endl;
}while(y!=1);
return 0;
}
Output:
:
The options list for Red Black Tree::
1. To add a new node_of_the_red_black_tree in the Red-
Black Tree
2. To search_rbt_node the Red-Black Tree for a
node_of_the_red_black_tree
3. To del_rbt_nodeete an existing Red-Black tree
node_of_the_red_black_tree
4. To display all the node_of_the_red_black_trees of
the Red-Black Tree
5. To exit the code execution.
Input: 1
node_of_the_red_black_tree:
Key: 23
Colour: Black
Parent: 12
Right Child: 45
Left Child: 12
Left:
node_of_the_red_black_tree:
Key: 12
Colour: Black
Parent: 23
no right child node_of_the_red_black_tree present.
no left child node_of_the_red_black_tree present.
:
Right:
node_of_the_red_black_tree:
Key: 45
Colour: Black
Parent: 23
Right Child: 98
no left child node_of_the_red_black_tree present.
Right:
node_of_the_red_black_tree:
Key: 98
Colour: Red
Parent: 45
no right child node_of_the_red_black_tree present.
no left child node_of_the_red_black_tree present.
FOUND node_of_the_red_black_tree:
Key: 98
Colour: Red
Parent: 45
no right child node_of_the_red_black_tree present.
no left child node_of_the_red_black_tree present
del_rbt_nodeeted Element: 45
Colour: Black
Parent: 23
Right Child: 98
no left child node_of_the_red_black_tree present.
node_of_the_red_black_tree del_rbt_nodeeted.
node_of_the_red_black_tree:
Key: 23
Colour: Black
Parent: 12
Right Child: 98
Left Child: 12
Left:
node_of_the_red_black_tree:
Key: 12
Colour: Black
Parent: 23
no right child node_of_the_red_black_tree present.
no left child node_of_the_red_black_tree present.
Right:
:
node_of_the_red_black_tree:
Key: 98
Colour: Red
Parent: 23
no right child node_of_the_red_black_tree present.
no left child node_of_the_red_black_tree present.
← prev next →
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
Latest Courses
:
: