[fibonacci]: https://upload.wikimedia.org/wikipedia/commons/thumb/a/ab/Fibonacci_Tree_as_Red-Black_Tree.svg/2000px-Fibonacci_Tree_as_Red-Black_Tree.svg.png "Fibonacci example of red black trees"
### Why Red-Black Trees?
Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations. The height of a Red Black tree is always O(Logn) where n is the number of nodes in the tree.
A node is initially inserted into a Red-Black Tree just like any Binary Search Tree. The new node is then given a color of red. After that node has been inserted, the tree must be validated to ensure none of the five properties have been violated. If a property has been violated, there are three potential cases requiring either a left-rotation, right-rotation, and/or a recoloring of the nodes. The cases are dependent on the "uncle" of the current node. Specifically, whether the "uncle" node is black or red. For more info on inserting, the three cases can be found [here](https://www.geeksforgeeks.org/red-black-tree-set-2-insert/).
The AVL trees are more balanced compared to Red Black Trees, but they may cause more rotations during insertion and deletion. So if your application involves many frequent insertions and deletions, then Red Black trees should be preferred. And if the insertions and deletions are less frequent and search is more frequent operation, then AVL tree should be preferred over Red Black Tree.
### Left-Leaning Red–Black Tree
A left-leaning red–black (LLRB) tree is a type of self-balancing binary search tree. It is a variant of the red–black tree and guarantees the same asymptotic complexity for operations, but is designed to be easier to implement.
### Properties of Left Leaning Red-Black Trees
All of the red-black tree algorithms that have been proposed are characterized by a worst-case search time bounded by a small constant multiple of log N in a tree of N keys, and the behavior observed in practice is typically that same multiple faster than the worst-case bound, close to the optimal log N nodes examined that would be observed in a perfectly balanced tree.
Specifically, in a left-leaning red-black 2-3 tree built from N random keys:
->A random successful search examines log2 N − 0.5 nodes.
if (sibling.getLeft().getColor() == 1 && sibling.getRight().getColor() == 1) {
sibling.setColor(0);
x = x.getParent();
} else {
if (sibling.getRight().getColor() == 1) {
sibling.getLeft().setColor(1);
sibling.setColor(0);
rotateRight(sibling);
sibling = x.getParent().getRight();
}
sibling.setColor(x.getParent().getColor());
x.getParent().setColor(1);
sibling.getRight().setColor(1);
rotateLeft(x.getParent());
x = this.root;
}
} else {
sibling = x.getParent().getLeft();
if (sibling.getColor() == 0){
sibling.setColor(1);
x.getParent().setColor(0);
rotateRight(x.getParent());
sibling = x.getParent().getLeft();
}
if (sibling.getLeft().getColor() == 1 && sibling.getRight().getColor() == 1){
sibling.setColor(0);
x = x.getParent();
} else {
if (sibling.getLeft().getColor() == 1) {
sibling.getRight().setColor(1);
sibling.setColor(0);
rotateLeft(sibling);
sibling = x.getParent().getLeft();
}
sibling.setColor(x.getParent().getColor());
x.getParent().setColor(1);
sibling.getLeft().setColor(1);
rotateRight(x.getParent());
x = this.root;
}
}
}
x.setColor(1);
}
public Node findMinimum(){
return findMinimum(this.root);
}
private Node findMinimum(Node rootNode){
while (rootNode.getLeft() != nil)
rootNode = rootNode.getLeft();
return rootNode;
}
private Node getSuccessor(Node x){
if (x.getRight() != nil)
return findMinimum(x.getRight());
Node parent = x.getParent();
while (parent != nil && x == parent.getRight()){
x = parent;
parent = parent.getParent();
}
return parent;
}
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void traverseInorder (Node rootNode){
if(rootNode != nil){
traverseInorder(rootNode.getLeft());
System.out.println(rootNode.getData() + " color " + rootNode.getColor());
traverseInorder(rootNode.getRight());
}
}
}
```
- Note: most of this code is brought from "Introduction to Algorithms" book written by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein.