How to Troubleshoot Apps for the Modern Connected Worker
Red Black Tree (and Examples)
1. RED BLACK TREE
Prof. Manjusha Amritkar
Information Technology Department
International Institute of Information Technology, I²IT
www.isquareit.edu.in
2. Introduction
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
• Most of the BST operations (e.g., search, max, min, insert,
delete.. etc) take O(log n) time
• 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(Log n) after
every insertion and deletion, then we can guarantee an upper
bound of O(Log n) 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.
3. Introduction
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
• Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node
follows following rules.
Property #1: Red - Black Tree must be a Binary Search Tree.
Property #2: The ROOT node must be colored BLACK.
Property #3: The children of Red colored node must be colored
BLACK. (There should not be two consecutive RED nodes).
Property #4: In all the paths of the tree, there should be same
number of BLACK colored nodes.
Property #5: Every new node must be inserted with RED color.
Property #6: Every leaf (e.i. NULL node) must be colored BLACK.
4. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
5. Insertion into a Red Black Tree
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
• In a Red-Black Tree, every new node must be inserted with the
color RED.
• 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 perform the following operation to make it Red
Black Tree.
1. Recolor
2. Rotation
3. Rotation followed by Recolor
6. Insertion into a Red Black Tree
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
• The insertion operation in Red Black tree is performed using the following
steps...
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty then insert the newNode as Root node with color
Black and exit from the operation.
Step 3 - If tree is not Empty then insert the newNode as leaf node with
color Red.
Step 4 - If the parent of newNode is Black then exit from the operation.
Step 5 - If the parent of newNode is Red then check the color of
parent node's sibling of newNode.
Step 6 - If it is colored Black or NULL then make suitable Rotation and
Recolor it.
Step 7 - If it is colored Red then perform Recolor. Repeat the same until
tree becomes Red Black Tree.
7. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
8. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
9. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
10. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
11. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
12. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
13. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
14. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
15. Example
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
16. AVL Tree Vs Red Black Tree
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
• AVL trees provide faster lookups than Red Black Trees because they are
more strictly balanced.
• Red Black Trees provide faster insertion and removal operations than AVL
trees as fewer rotations are done due to relatively relaxed balancing.
• AVL trees store balance factors or heights with each node, thus requires
storage for an integer per node whereas Red Black Tree requires only 1 bit
of information per node.
• Red Black Trees are used in most of the language libraries like map,
multimap, multiset in C++ whereas AVL trees are used in databases
where faster retrievals are required.
17. Thank-You
International Institute of Information Technology, I²IT, P-14, Rajiv Gandhi Infotech Park, Hinjawadi Phase 1, Pune - 411 057
Phone - +91 20 22933441/2/3 | Website - www.isquareit.edu.in | Email - info@isquareit.edu.in
International Institute of Information Technology (I²IT),
P-14, Rajiv Gandhi Infotech Park, MIDC Phase – 1, Hinjawadi, Pune –
411057, India
info@isquareit.edu.in
http://www.isquareit.edu.in/