Following article is extension of article discussed here.
In AVL tree insertion, we used rotation as a tool to do balancing after insertion caused imbalance. In Red-Black tree, we use two tools to do balancing.
We try recoloring first, if recoloring doesn’t work, then we go for rotation. Following is detailed algorithm. The algorithms has mainly two cases depending upon the color of uncle. If uncle is red, we do recoloring. If uncle is black, we do rotations and/or recoloring.
Color of a NULL node is considered as BLACK.
Let x be the newly inserted node.
1. Perform standard BST insertion and make the color of newly inserted nodes as RED.
2. If x is root, change color of x as BLACK (Black height of complete tree increases by 1).
3. Do following if color of x’s parent is not BLACK or x is not root.
1. If x’s uncle is RED (Grand parent must have been black from property 4)
1. Change color of parent and uncle as BLACK.
2. color of grand parent as RED.
3. Change x = x’s grandparent, repeat steps 2 and 3 for new x.
2. If x’s uncle is BLACK, then there can be four configurations for x, x’s parent (p) and x’s grandparent (g) (This is similar to AVL Tree)
1. Determine the configuration:
1. Left Left Case (p is left child of g and x is left child of p).
2.Left Right Case (p is left child of g and x is right child of p).
3. Right Right Case (Mirror of case a).
4.Right Left Case (Mirror of case c).
2. Change x = x’s parent, repeat steps 2 and 3 for new x.
Following are operations to be performed in four subcases when uncle is BLACK.
All four cases when Uncle is BLACK
Left Left Case (See g, p and x)
Left Right Case (See g, p and x)
Right Right Case (See g, p and x)
Right Left Case (See g, p and x)
Examples of Insertion
Below is C++ Code.
Inoder Traversal of Created Tree 1 2 3 4 5 6 7 Level Order Traversal of Created Tree 6 4 7 2 5 1 3
This article is contributed by Mohsin Mohammaad. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Left Leaning Red Black Tree (Insertion)
- Red-Black Trees | Top-Down Insertion
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Red Black Tree vs AVL Tree
- Red-Black Tree | Set 3 (Delete)
- Red-Black Tree | Set 1 (Introduction)
- Red-Black Tree | Set 2 (Insert)
- AVL Tree | Set 1 (Insertion)
- ScapeGoat Tree | Set 1 (Introduction and Insertion)
- Threaded Binary Tree | Insertion
- Optimal sequence for AVL tree insertion (without any rotations)
- Binary Search Tree | Set 1 (Search and Insertion)
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Binary Tree to Binary Search Tree Conversion
- Difference between Binary Tree and Binary Search Tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Interval Tree using GNU Tree-based container
- Binary Tree to Binary Search Tree Conversion using STL set
- A program to check if a binary tree is BST or not
- Find the node with minimum value in a Binary Search Tree