Red-Black Trees:-
In continuation to binary search trees. Searching, deleting and inserting are very quick in a BST given that the BST is balanced or somewhat balanced. But the problem comes when data to be inserted is a sorted data either ascending form or descending form.
For example :- {3,5,7,8,9,12,28,30} — asc sorted
{30,28,12,9,8,7,5,3} — desc sorted
When we try to create a BST out of sorted data, the tree becomes unbalanced and starts growing in either left direction if desc sorted or in right direction if asc sorted. So basically, it will be safe to say that it kind of becomes a linked list and with linked list comes its own issues.
Searching, insertion & deletion becomes O(N) instead of O(LogN).
So for a BST or BT (binary tree) to remain balanced , it is important that data insertion happens in random order instead of data being sorted. Though random insertion can also make a BST/BT unbalanced if a sequence of input data in between is in sequential order — for example :- {6,4,7,5,2,8,9,10,11,34,23,55,43} where 8,9,10,11 is a sequence which will make the tree unbalanced towards right side in some way.
Now to avoid a BST/BT in becoming unbalanced at the time of insertion, there are certain techniques available while doing insert and red-black tree is one such technique.
RB-Tree is basically some set of rules which should be followed while we are doing insert operation on BST/BT. These rules will ensure that remains balanced.
1) Every node is either red or black
2) The root node is always black
3) If a node is red, its children must be black
4) Every path from root to a leaf or to null child must contain same number of black nodes.
5) Every nil node is always black
Some light on the above 4 rules:-
(a) Node being red/black is basically to conceptually understand the technique. To identify a node as red/black we can add a boolean flag in Node class to set it as true or false for red/black.
(b) “Null Child” refereed in point 4 is basically a potential child to non-leaf node.For example potential left child to non-leaf node with right child or the other way round.
Black Height :- Number of black nodes while traversing from root to leaf. Re-enforcing rule 4, black height for root to leaf node should be same.
Duplicate Keys :-
Incase we come across a sequence like 50,50,50 while performing insert , these duplicate records will be insert in the order:-
1) Insert first node
2) Insert first duplicate as right child of the first node.
3) Insert second duplicate as left child of the first node.