Unsatisfied Bhabhi ℂall Girls Ahmedabad Book Esha 6378878445 Top Class ℂall G...
Red Black Tree,Red black tree, Cases-insertion, Deletion
1. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
1
Red Black Tree
By
Amlan Pati
Shivanand Sahu
Under the guidance of
Mr. Ajaya Kumar Das
A123001
A123007
3. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
3
International
Institute
of
Information
of
Technology
• A red-black tree is a binary search tree with one extra bit of storage per node:
its color, which can be either RED or BLACK.
• The longest path from the root to the leaf node cannot be more than twice the
shortest path.
• 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.
• Think of these NILs as pointers to leaves (external nodes) of the binary search
tree and the normal, key-bearing nodes as internal nodes of the tree.
INTRODUCTION
5. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
5
A red-black tree is a binary search 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.
Properties
6. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
6
INSERTION
Cases
1. If tree is empty, create new node as root node with color
black.
2. If tree is not empty, create new node as leaf node with
color red.
3. If parent of new node is black then exit.
4. If parent of new node is red, then check the color of
parent’s sibling of new node.
a. If color is black or null then do suitable rotation and
recolor.
b. If color is red then recolor and also check if parent’s
parents of new node is not root node then recolor it
and recheck.
7. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
7
Insertion
Example- 10,18,7,15,16,30,25,40,60,2,1,70
16
1
25
2
7
40
18
30
70
60
10
15
8. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
8
Analysis of Insertion
• Search for insertion location takes O(log n) time
because we visit O(log n) nodes.
• Addition to the node takes O(1) time.
• Rotation or recoloring takes O(log n) time.
• O(log n) recoloring, each taking O(1) time, and
at most one rotation taking O(1) time.
• Thus, an insertion in a red-black tree takes O(log n)
time.
9. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
9
DELETION
•Deleting a node from a red-black tree is a bit more
complicated than inserting a node.
•If the node is red?
Not a problem – no RB properties violated.
•If the node is black?
deleting it will change the black-height along some path.
10. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
10
Cases
Case-1
•If node to be deleted is red, just delete it.
Case-2
•If root is double black(DB) ,just remove DB.
Case-3
•If DB’s sibling is black and both its children are black.
•Remove DB
•Add black to its parent (P)
•If parent is red it becomes black
•If parent is black it becomes double black
• Make sibling red.
• if still DB exist apply other cases.
11. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
11
Cases
Case-4
• If DB’s sibling red.
• Swap color’s of parent its sibling of double black.
• Rotated parent towards double black direction.
• Reapply cases
Case-5
• DB’s sibling is black, sibling’s child who is far from DB is black, but
near child to DB is red.
• Swap color of DB’s sibling and sibling child who is near to DB.
• Rotate sibling in opposite direction to DB
• Apply case-6
12. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
12
Cases
Case-6
• If DB’s sibling is black ,far child is red.
• Swap color of parent and sibling.
• Rotate parent in DB’s direction.
• Remove DB.
• Change color of red child to black.
13. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
13
Analysis of Deletion
• A red-black tree has O(log n) height
• Search for deletion location takes O(log n) time
• Each rotation or recoloring is O(1).
• Thus, the deletion in a red-black tree takes O(log n) time
14. National
Institute
of
Science
&
Technology
M. Tech 1th Sem (CSE) Batch:2023-25
International
Institute
of
Information
of
Technology
14
CONCLUSION
• Red-Black Trees are preferred in scenarios where there is a
need for faster insertions and deletions, and the tree can
tolerate a slightly less balanced structure.
• AVL Trees may be preferred when a more strictly balanced
tree is required, and the focus is on maintaining a consistent
performance in search operations.
•The choice between the two depends on the specific
characteristics and requirements of the application.