In the previous post, we discussed introduction to Red-Black Trees. In this post, insertion is discussed.
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 and x is not root.
….a) If x’s uncle is RED (Grand parent must have been black from property 4)
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
….b) 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)
……..i) Left Left Case (p is left child of g and x is left child of p)
……..ii) Left Right Case (p is left child of g and x is right child of p)
……..iii) Right Right Case (Mirror of case i)
……..iv) Right Left Case (Mirror of case ii)
Following are operations to be performed in four subcases when uncle is BLACK.
All four cases when Uncle is BLACK
Please refer C Program for Red Black Tree Insertion for complete implementation of above algorithm.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Splay Tree | Set 2 (Insert)
- Insert Operation in B-Tree
- K Dimensional Tree | Set 1 (Search and Insert)
- B-Tree Insert without aggressive splitting
- Insert a node in Binary Search Tree Iteratively
- Binary Search Tree insert with Parent Pointer
- 2-3 Trees | (Search and Insert)
- Trie | (Insert and Search)
- Treap | Set 2 (Implementation of Search, Insert and Delete)
- Efficiently design Insert, Delete and Median queries on a set
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Two Dimensional Binary Indexed Tree or Fenwick Tree
- Tournament Tree (Winner Tree) and Binary Heap
- Order statistic tree using fenwick tree (BIT)
- Build a segment tree for N-ary rooted tree