Presentation on – Red-Black Tree
Department of Computer Science and
Applications
Atal Bihari Vajpayee Vishwavidyalaya, Koni, Bilaspur,
Chhattisgarh
Presented to – Dr. Jeetendra Gupta
Assistant Professor, University
Teaching Department
Presented by – Kishan Kumar
Kaushik, MCA 2nd
Semester,
Computer Science.
Roll No. – 29
Session – 2024-25
Table of contents
• Prerequisites: Binary Search Tree properties and
drawbacks.
• What is Red-Black Tree?
• Properties of Red-Black Tree.
• Red-Black Tree (Rotations – Left and Right Rotations)
• Comparison between BST, Red-Black Tree, AVL.
Table of contents
• Insert operation in Red-Black Tree.
• Deletion operation in Red-Black Tree.
• Advantages, Disadvantages and Applications of Red-
Black Tree.
Following are some properties of binary search tree:
• The left sub tree of a node contains only nodes with keys less than the node’s key.
• The right sub tree of a node contains only nodes with keys greater than the node’s
key.
• The left and right sub tree each must also be a binary search tree.
• There must be no duplicate nodes.
• A unique path exists from the root to every other node.
Prerequisites: Binary Search Tree properties and drawbacks:
• Binary Search Tree is fast in insertion and deletion etc. when it is
balanced.
• Time complexity of performing operations (e.g. searching, inserting,
deletion etc.) on the binary search is O(log n) in the best case (when tree
is balanced.)
• When tree is not balanced the performance degrades form O(log n) to
O(n) when tree is not balanced.
Problem with Binary Search Tree:
• The basic binary search tree have three very nasty cases where the
structure stops being logarithmic and becomes glorified linked list.
• The two most common of these degenerate cases are ascending or
descending sorted order (third case would be alternating order.)
Problem with Binary Search Tree:
Problem with Binary Search Tree:
1
2
3
4
6
5
4
3
9
2
7
5
3
Ascending order Descending order Alternating order
• A red-black tree is a balanced binary search tree with one extra bit of
storage per node: its color, which can be either RED or BLACK.
• By constraining the node colors on any simple path from the root to a
leaf, red-black trees ensure that no such path is more than twice as long
as any other, so that the tree is approximately balanced.
• Each node of the tree now contains the attributes color, key, left, right,
and p.
• If a child or the parent of a node does not exist, the corresponding
pointer attribute of the node contains the value NIL.
Red-Black Tree:
A red-black tree is a binary tree that satisfies the
following red-black properties:
1. Every node is either red or black.
2. The root is black.
3. Every leaf (NIL) is black.
4. If a node is red, then both its children are black.
5. For each node, all simple paths from the node to
descendant leaves contain the same number of black
nodes.
Red-Black Tree:
• The search-tree operations TREE-INSERT and TREE-DELETE, when
run on a red black tree with n keys, take O(log n)time. Because they
modify the tree, the result may violate the red-black properties.
• To restore these properties, we must change the colors of some of the
nodes in the tree and also change the pointer structure.
• We change the pointer structure through rotation, which is a local
operation in a search tree that preserves the binary-search-tree property.
• There are two kinds of rotations : left rotations and right rotations.
Red-Black Tree (Rotations – Left and Right Rotations):
Red-Black Tree: Comparison
Red-Black Tree: Insertion
• We can insert a node into an n-node red-black tree in O(logn) time.
• Following is an example of insertion with its required steps and cases:
• 10,20,30,15,25,5,12,35,40,32,50
Red-Black Tree: Insertion
Algorithm for insertion in red-black tree:
• Step 1: Insert new node with color red.
• Step 2:
• Case 1: Node is root: recolor it to black.
• Case 2: Red-black violation:
• If the new node’s parent is red:
• Case 2.1: Uncle is Red: Recolor the parent and the
uncle to black, and the grandparent to red. Then, (repeat
the fix-up process from the grandparent.)
• Case 2.2: Uncle is Black or Null: Perform rotations
to balance the tree, recolor the nodes accordingly.
Insert following: 10,20,30,15,25,5,12,35,40,32,50
20
15
25 35
30
5
12
32 40
10
25
Red-Black Tree: Insertion
Red-Black Tree – Deletion:
• Like the other basic operations on an Red Black tree, deletion of a node takes
time O(logn)
• Deleting a node from a red-black tree is a bit more complicated than inserting a
node.
• Following is an example of deletion in red-black tree with properties and
algorithm:
Red-Black Tree – Deletion:
70
40
20 50
80 110
100
10 30 60
90 120
• Step 1: Perform standard BST deletion.
• If the node has two children, replace it with its in-order successor and
delete the successor.
• Let the node to be deleted (or moved) be replaced by a child node x.
• Step 2:
• Case 1: Deleted node or its replacement is red:
• Simply delete it or replace it, and recolor x to black if needed.
• Case 2: Deleted node is black and replaced by black (or null) node (x):
• This causes a double black problem. Fix it using the following cases:
• Case 2.1: x's sibling is red:
• Recolor sibling to black, parent to red, perform rotation on parent.
(Transforms to Case 2.2/2.3/2.4)
• Case 2.2: x's sibling is black and both of sibling’s children are black or
null:
• Recolor sibling to red. Move the double black up to the parent and repeat
fix-up from there.
• Case 2.3: x's sibling is black, sibling’s near child is red, far child is black:
• Recolor sibling and its near child, perform rotation on sibling to transform
into Case 2.4.
• Case 2.4: x's sibling is black, and sibling’s far child is red:
• Recolor sibling with parent’s color, recolor parent and sibling’s far child
to black, perform rotation on parent to fix the tree.
• Step 3: Ensure root is black.
Delete following: 90, 100, 110, 80, 120, 90
Red-Black Tree – Deletion:
40
20
10 30
• After deleting the items following
will be the red-black tree:
Delete following: 90, 100, 110, 80, 120, 90
60
50 70
Advantages of Red-Black Tree:
• Red-Black Trees maintain balance through color and rotation rules, ensuring
O(log n) time for insertions, deletions, and lookups.
• Search, insert, and delete operations are guaranteed to take logarithmic time
even in the worst case.
• Many standard libraries (like C++ STL's map and set) use Red-Black Trees due
to their performance and balance properties.
Disadvantages of Red-Black Tree:
• The balancing logic (rotations and color flips) makes the implementation more
complex than simpler trees like binary search trees (BSTs).
• In practice, Red-Black Trees may be slower than AVL trees for frequent lookups,
as they are less rigidly balanced.
• Each node needs to store an additional color bit, which adds slight memory
overhead.
Applications of Red-Black Tree:
• Used in programming libraries to implement data structures like maps, sets, and
dictionaries.
• the C++ Standard Template Library (STL) implements its map, multimap, set,
and multiset containers using Red-Black Trees to maintain sorted order and
provide fast insertions, deletions, and lookups.
• In Java, the TreeMap and TreeSet classes from the Java Collections Framework
also use Red-Black Trees internally.
• Useful in maintaining dynamic routing tables where entries change frequently
but fast lookup is essential.
References:
⮚ https://slideplayer.com/slide/13760328/
⮚ https://www.geeksforgeeks.org/introduction-to-red-black-tree/
⮚ https://www.tutorialspoint.com/data_structures_algorithms/red_
black_trees.htm
⮚ https://www.youtube.com/watch?v=3RQtq7PDHog
⮚ https://www.youtube.com/watch?v=lU99loSvD8s
Thank You!
Any questions?

Kishan Kaushik - Red Black Tree Presentation

  • 1.
    Presentation on –Red-Black Tree Department of Computer Science and Applications Atal Bihari Vajpayee Vishwavidyalaya, Koni, Bilaspur, Chhattisgarh Presented to – Dr. Jeetendra Gupta Assistant Professor, University Teaching Department Presented by – Kishan Kumar Kaushik, MCA 2nd Semester, Computer Science. Roll No. – 29 Session – 2024-25
  • 2.
    Table of contents •Prerequisites: Binary Search Tree properties and drawbacks. • What is Red-Black Tree? • Properties of Red-Black Tree. • Red-Black Tree (Rotations – Left and Right Rotations) • Comparison between BST, Red-Black Tree, AVL.
  • 3.
    Table of contents •Insert operation in Red-Black Tree. • Deletion operation in Red-Black Tree. • Advantages, Disadvantages and Applications of Red- Black Tree.
  • 4.
    Following are someproperties of binary search tree: • The left sub tree of a node contains only nodes with keys less than the node’s key. • The right sub tree of a node contains only nodes with keys greater than the node’s key. • The left and right sub tree each must also be a binary search tree. • There must be no duplicate nodes. • A unique path exists from the root to every other node. Prerequisites: Binary Search Tree properties and drawbacks:
  • 5.
    • Binary SearchTree is fast in insertion and deletion etc. when it is balanced. • Time complexity of performing operations (e.g. searching, inserting, deletion etc.) on the binary search is O(log n) in the best case (when tree is balanced.) • When tree is not balanced the performance degrades form O(log n) to O(n) when tree is not balanced. Problem with Binary Search Tree:
  • 6.
    • The basicbinary search tree have three very nasty cases where the structure stops being logarithmic and becomes glorified linked list. • The two most common of these degenerate cases are ascending or descending sorted order (third case would be alternating order.) Problem with Binary Search Tree:
  • 7.
    Problem with BinarySearch Tree: 1 2 3 4 6 5 4 3 9 2 7 5 3 Ascending order Descending order Alternating order
  • 8.
    • A red-blacktree is a balanced binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. • By constraining the node colors on any simple path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other, so that the tree is approximately balanced. • Each node of the tree now contains the attributes color, key, left, right, and p. • If a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value NIL. Red-Black Tree:
  • 9.
    A red-black treeis a binary tree that satisfies the following red-black properties: 1. Every node is either red or black. 2. The root is black. 3. Every leaf (NIL) is black. 4. If a node is red, then both its children are black. 5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes. Red-Black Tree:
  • 10.
    • The search-treeoperations TREE-INSERT and TREE-DELETE, when run on a red black tree with n keys, take O(log n)time. Because they modify the tree, the result may violate the red-black properties. • To restore these properties, we must change the colors of some of the nodes in the tree and also change the pointer structure. • We change the pointer structure through rotation, which is a local operation in a search tree that preserves the binary-search-tree property. • There are two kinds of rotations : left rotations and right rotations. Red-Black Tree (Rotations – Left and Right Rotations):
  • 11.
  • 12.
    Red-Black Tree: Insertion •We can insert a node into an n-node red-black tree in O(logn) time. • Following is an example of insertion with its required steps and cases: • 10,20,30,15,25,5,12,35,40,32,50
  • 13.
    Red-Black Tree: Insertion Algorithmfor insertion in red-black tree: • Step 1: Insert new node with color red. • Step 2: • Case 1: Node is root: recolor it to black. • Case 2: Red-black violation: • If the new node’s parent is red: • Case 2.1: Uncle is Red: Recolor the parent and the uncle to black, and the grandparent to red. Then, (repeat the fix-up process from the grandparent.) • Case 2.2: Uncle is Black or Null: Perform rotations to balance the tree, recolor the nodes accordingly. Insert following: 10,20,30,15,25,5,12,35,40,32,50 20 15 25 35 30 5 12 32 40 10 25
  • 14.
  • 15.
    Red-Black Tree –Deletion: • Like the other basic operations on an Red Black tree, deletion of a node takes time O(logn) • Deleting a node from a red-black tree is a bit more complicated than inserting a node. • Following is an example of deletion in red-black tree with properties and algorithm:
  • 16.
    Red-Black Tree –Deletion: 70 40 20 50 80 110 100 10 30 60 90 120 • Step 1: Perform standard BST deletion. • If the node has two children, replace it with its in-order successor and delete the successor. • Let the node to be deleted (or moved) be replaced by a child node x. • Step 2: • Case 1: Deleted node or its replacement is red: • Simply delete it or replace it, and recolor x to black if needed. • Case 2: Deleted node is black and replaced by black (or null) node (x): • This causes a double black problem. Fix it using the following cases: • Case 2.1: x's sibling is red: • Recolor sibling to black, parent to red, perform rotation on parent. (Transforms to Case 2.2/2.3/2.4) • Case 2.2: x's sibling is black and both of sibling’s children are black or null: • Recolor sibling to red. Move the double black up to the parent and repeat fix-up from there. • Case 2.3: x's sibling is black, sibling’s near child is red, far child is black: • Recolor sibling and its near child, perform rotation on sibling to transform into Case 2.4. • Case 2.4: x's sibling is black, and sibling’s far child is red: • Recolor sibling with parent’s color, recolor parent and sibling’s far child to black, perform rotation on parent to fix the tree. • Step 3: Ensure root is black. Delete following: 90, 100, 110, 80, 120, 90
  • 17.
    Red-Black Tree –Deletion: 40 20 10 30 • After deleting the items following will be the red-black tree: Delete following: 90, 100, 110, 80, 120, 90 60 50 70
  • 18.
    Advantages of Red-BlackTree: • Red-Black Trees maintain balance through color and rotation rules, ensuring O(log n) time for insertions, deletions, and lookups. • Search, insert, and delete operations are guaranteed to take logarithmic time even in the worst case. • Many standard libraries (like C++ STL's map and set) use Red-Black Trees due to their performance and balance properties.
  • 19.
    Disadvantages of Red-BlackTree: • The balancing logic (rotations and color flips) makes the implementation more complex than simpler trees like binary search trees (BSTs). • In practice, Red-Black Trees may be slower than AVL trees for frequent lookups, as they are less rigidly balanced. • Each node needs to store an additional color bit, which adds slight memory overhead.
  • 20.
    Applications of Red-BlackTree: • Used in programming libraries to implement data structures like maps, sets, and dictionaries. • the C++ Standard Template Library (STL) implements its map, multimap, set, and multiset containers using Red-Black Trees to maintain sorted order and provide fast insertions, deletions, and lookups. • In Java, the TreeMap and TreeSet classes from the Java Collections Framework also use Red-Black Trees internally. • Useful in maintaining dynamic routing tables where entries change frequently but fast lookup is essential.
  • 21.
    References: ⮚ https://slideplayer.com/slide/13760328/ ⮚ https://www.geeksforgeeks.org/introduction-to-red-black-tree/ ⮚https://www.tutorialspoint.com/data_structures_algorithms/red_ black_trees.htm ⮚ https://www.youtube.com/watch?v=3RQtq7PDHog ⮚ https://www.youtube.com/watch?v=lU99loSvD8s
  • 22.