Algo Assignment-21.1 RedBlack Tree (TH)
Algo Assignment-21.1 RedBlack Tree (TH)
Algo Assignment-21.1 RedBlack Tree (TH)
The red-Black tree is a binary search tree. The prerequisite of the red-black tree is
that we should know about the binary search tree. In a binary search tree, the values
of the nodes in the left subtree should be less than the value of the root node, and the
values of the nodes in the right subtree should be greater than the value of the root
node.
Each node in the Red-black tree contains an extra bit that represents a color to ensure
that the tree is balanced during any operations performed on the tree like insertion,
deletion, etc. In a binary search tree, the searching, insertion and deletion
take O(log2n) time in the average case, O(1) in the best case and O(n) in the worst
case.
Why do we require a Red-Black tree
• Red-Black tree is a self-balanced binary search tree. The Red-Black tree is
used because the AVL tree requires many rotations when the tree is large,
whereas the Red-Black tree requires a maximum of two rotations to balance the
tree. The main difference between the AVL tree and the Red-Black tree is that
the 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 searching.
• Insertion is easier in the AVL tree as the AVL tree is strictly balanced, whereas
deletion and searching are easier in the Red-Black tree as the Red-Black tree
requires fewer rotations.
• As the name suggests that the node is either colored in Red or Black color.
Sometimes no rotation is required, and only recoloring is needed to balance the
tree.
Properties of Red-Black tree
o It is a self-balancing Binary Search tree. Here, self-balancing means that it
balances the tree itself by either doing the rotations or recoloring the nodes.
o This tree data structure is named as a Red-Black tree as each node is either Red
or Black in color. Every node stores one extra information known as a bit that
represents the color of the node. For example, 0 bit denotes the black color
Step 4: The next element is 15, and 15 is greater than 10, but less than 18, so the
new node will be created at the left of node 18. The node 15 would be Red in color as
the tree is not empty.
The above tree violates the property of the Red-Black tree as it has Red-red parent-
child relationship. Now we have to apply some rule to make a Red-Black tree. The rule
Step 5: The next element is 16. As 16 is greater than 10 but less than 18 and greater
than 15, so node 16 will come at the right of node 15. The tree is not empty; node 16
would be Red in color, as shown in the below figure:
In this figure, we can observe that it violates the
property of the parent-child relationship as it has a
red-red parent-child relationship. We have to apply
some rules to make a Red-Black tree. Since the
new node's parent is Red color, and the parent of
the new node has no sibling, so rule 4a will be
applied. The rule 4a says that some rotations and
recoloring would be performed on the tree.
Since node 16 is right of node 15 and the parent of
node 15 is node 18. Node 15 is the left of node 18.
Here we have an LR (Left Right) relationship, so
we require to perform two rotations. First, we will
perform left, and then we will perform the right
rotation. The left rotation would be performed on
nodes 15 and 16, where node 16 will move upward, and node 15 will move downward.
Once the left rotation is performed, the tree looks like as shown in the below figure:
Initially, the 15 is replaced with a nil value. After replacement, the node becomes
double black. Since double black's sibling is Red so color of the node 20 changes to
Red and the color of the node 30 changes to Black.
Once the swapping of the color is completed, the rotation towards the double black
would be performed. The node 30 will move upwards and the node 20 will move
downwards as shown in the below figure.
In the above tree, we can observe that double black situation still exists in the tree. It
satisfies the case 3 in which double black's sibling is black as well as both its children
are black. First, we remove the double black from the node and add the black color to
its parent node. At the end, the color of the double black's sibling, i.e., node 25
changes to Red as shown in the below figure.
First, we replace the value 1 with the nil value. The node becomes double black as
both the nodes, i.e., 1 and nil are black. It satisfies the case 3 that implies if DB's
sibling is black and both its children are black. First, we remove the double black
of the nil node. Since the parent of DB is Black, so when the black color is added to
the parent node then it becomes double black. After adding the color, the double
black's sibling color changes to Red as shown below.
As we can observe in the above tree that double black situation still exists. So, we
need to case 6. Let's first see what is case 6.
Case 6: If double black's sibling is black, far child is Red
o Swap the color of Parent and its sibling node.
o Rotate the parent towards the Double black's direction
o Remove Double black
o Change the Red color to black.
Now we will apply case 6 in the above example to solve the double black's situation.
In the above example, the double black is node 5, and the sibling of node 5 is node
25, which is black in color. The far child of the double black node is node 30, which is
Red in color as shown in the below figure:
In the next step, we will remove double black from node 5 and node 5 will give its
black color to the far child, i.e., node 30. Therefore, the color of node 30 changes to
black as shown in the below figure.
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
Applications:
1. Most of the self-balancing BST library functions like map, multiset, and
multimap in C++ or java packages like java.util.TreeMap and
java.util.TreeSet use Red-Black Trees.
2. It is used to implement CPU Scheduling Linux. Completely Fair
Scheduler uses it.
3. It is also used in the K-mean clustering algorithm in machine learning for
reducing time complexity.
4. Moreover, MySQL also uses the Red-Black tree for indexes on tables in
order to reduce the searching and insertion time.
5. Red Black Trees are used in the implementation of the virtual memory
manager in some operating systems, to keep track of memory pages and
their usage.
Advantages:
1. Red Black Trees have a guaranteed time complexity of O(log n) for basic
operations like insertion, deletion, and searching.
2. Red Black Trees are self-balancing.
3. Red Black Trees can be used in a wide range of applications due to their
efficient performance and versatility.
4. The mechanism used to maintain balance in Red Black Trees is relatively
simple and easy to understand.
Disadvantages:
1. Red Black Trees require one extra bit of storage for each node to store the
color of the node (red or black).
2. Complexity of Implementation.
3. Although Red Black Trees provide efficient performance for basic
operations, they may not be the best choice for certain types of data or
specific use cases.