Red Black Tree
Red-black trees are a variation of binary search trees to ensure that the tree is balanced. In a Red Black Tree, every node is colored either RED or BLACK. The root node and the leaf must be black. Every new node must be inserted with red color. The children of Red colored node must not Red, i.e. should be black. A Red Black tree insertion operation is similar to that of a binary search tree.
Summary
Red-black trees are a variation of binary search trees to ensure that the tree is balanced. In a Red Black Tree, every node is colored either RED or BLACK. The root node and the leaf must be black. Every new node must be inserted with red color. The children of Red colored node must not Red, i.e. should be black. A Red Black tree insertion operation is similar to that of a binary search tree.
Things to Remember
- Red-black trees are a variation of binary search trees to ensure that the tree is balanced.
- In a Red Black Tree, every node is colored either RED or BLACK.
- The root node and the leaf must be black.
- Every new node must be inserted with red color.
- The children of Red colored node must not Red, i.e. should be black.
- A Red Black tree insertion operation is similar to that of a binary search tree.
MCQs
No MCQs found.
Subjective Questions
No subjective questions found.
Videos
No videos found.

Red Black Tree
Red Black Trees
Red-black trees are a variation of binary search trees to ensure that the tree is balanced. In a Red Black Tree, every node is colored either RED or BLACK. The color of the node is decided based on the properties defined below:
- Red Black Tree must be a Binary Search Tree.
- The ROOT node must be colored BLACK.
- The children of Red colored node must be colored BLACK. (There should be no consecutive RED nodes.)
- In all the paths of the tree, there must be same number of BLACK colored nodes.
- Every new node must be inserted with RED color.
- Every leaf (i.e. NULL node) must be colored BLACK.
Example:
For conveinence, we use a sentinel NIL[T] to represent all the NIL nodes at the leafs.
- NIL[T] has the same fields as an ordinary node
- Color[NIL[T]] = BLACK
- The other fields may be set to arbitrary values
Insertion into a RED BLACK Tree
In a Red Black Tree, every new node inserted is colored RED. The insertion operation in Red Black Tree is similar to insertion operation in Binary Search Tree with the additional color property. After every insertion operation, we need to check all the properties of Red Black Tree. If all the properties are satisfied then we go to next operation, otherwise we need to perform following operations to make it Red Black Tree:
- Recolor
- Rotation followed by Recolor
The insertion operation in Red Black Tree is performed using the following steps:
- Check whether tree is EMPTY
- If the tree is Empty then insert the newNode as Root node with colorBlack and exit from the operation
- If tree is not Empty then insert the newNode as a leaf node with Red color
- If the parent of newNode is Black then exit from the operation
- If the parent of newNode is Red then check the color of parent node's sibling of newNode
- If it is Black or NULL node then make a suitable Rotation and Recolor it
- If it is Red colored node then perform Recolor and Recheck it. Repeat the same until tree becomes Red Black Tree
Example:
Create a RED BLACK Tree by inserting following sequence of numbers: 8, 18, 5, 15, 17, 25, 40 and 80.
1. Insert (8)
Tree is Empty. So, insert newNode as Root node with blackcolor.
2. Insert (18)
Tree is not Empty. So, insert newNode with red color.
3. Insert (5)
Tree is not Empty. So insert newNode with red color.
4. Insert (15)
Tree is not Empty. So, insert newNode with red color.
5. Insert (17)
Tree is not Empty. So, insert newNode with red color.
6. Insert (25)
Tree is not Empty. So, insert newNode with red color.
7. Insert (40)
Tree is not Empty. So, insert newNode with red color.
8. Insert (80)
Tree is not Empty. So, insert newNode with red color.
Finally, above tree is satisfying all the properties of a Red Black Tree. So, it is a perfect Red Black Tree.
References
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, “Introduction toAlgorithms”
- G. Brassard and P. Bratley, “Fundamentals of Algorithms”
- R. L. Kruse, B. P. Leung, C. L. Tondo, “Data Structure and Program designin C”
Lesson
Trees
Subject
Computer Engineering
Grade
Engineering
Recent Notes
No recent notes.
Related Notes
No related notes.