A red-black tree is a form of self-balancing binary seek tree in which each node has a further bit, and that bit is regularly interpreted because of the colouration (crimson or black). Those colours are used to make sure that the tree remains balanced, all through insertions and deletions. Even though the balance of the tree isn't always best, it is ideal to reduce the looking time and hold it round o(log n) time, where n is the total quantity of factors in the tree. This tree was invented in 1972 with the aid of Rudolf Bayer.

As each node requires the handiest one little bit of area to store the colouration records, these kinds of bushes show an equal reminiscence footprint to the conventional (uncoloured) binary seek tree.

Rules for Red-Black Tree:

A node can have Either Red colour or Black Colour.

Root nodes always have Black Colour.

No two Red nodes can be adjacent to each other.

The path from any Node to any descendants null node has the same count of black nodes.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Why do we Use Red Black Trees?

Most of the best operations (e.G., max, min, insert, delete.. Etc) take o(h) time in which (h) is the height of the Bst. The cost of these operations may also emerge as o(n) for a skewed binary tree. If we are sure that the height of the binary tree stays o(log n) after each insertion and deletion, then we can guarantee an upper certain of o(log n) for all these operations. The height of a red-black tree is constantly o(log n), wherein n is the number of nodes inside the tree.

No

Algorithm

Time complexity

1

search

o(log(n))

2

delete

o(log(n))

3

insert

o(log(n))

Here “n” represents the total no of the element that is present in the red Black Tree.

Comparision B/w Red Black tree and AVL tree:

The AVL tree is more balanced than the Red-black tree, but when the deletion of insertion operation is performed, the AVL tree requires more number of rotations. So when an operation requires frequent insertion and deletion then a Red-black tree is preferred more than the AVL tree to reduce time complexity.

How does a Red-Black Tree ensure balance?

The following is not a Red-Black tree because the chain of three-nodes is not possible.

The following is a red-black tree with three keys.

Facts about Red-Black tree:

The black height of the tree is the total count of black nodes on a path from the base node to a leaf node. Leaf nodes also are counted as black nodes. So, the red-black tree of height (h) has the black height >= h/2.

The height of a red-black tree with n nodes is h<= 2 log2(n + 1)

All bottom leaf nodes are black.

The red-Black tree is the remarkable variety of the Binary Tree.

No of Black Nodes is defined as the no of black nodes from the nodes to the given node that was no of an ancestor.

Black Height of Red-Black Tree:

The count of nodes from a node to its farthest descendant leaf is not quite twice because of the number of nodes to the closest descendant leaf.

Height h of red-black Tree is >=h/2;

Note: Red-Black Tree which has a height h and nodes count(n) has height<= 2Log2(n+1);

Searching in Red Black Tree:

Since the red-black tree is a special case of the binary tree, searching is similar to binary tree searching.