0% found this document useful (0 votes)
38 views71 pages

Red Black Tree

A red-black tree is a self-balancing binary search tree where each node is colored either red or black, following specific rules to maintain balance during insertions and deletions. The document outlines the properties, rules, and operations of red-black trees, including examples of inserting nodes and handling color conflicts through recoloring and rotations. It also provides a step-by-step guide on creating a red-black tree using a set of node values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views71 pages

Red Black Tree

A red-black tree is a self-balancing binary search tree where each node is colored either red or black, following specific rules to maintain balance during insertions and deletions. The document outlines the properties, rules, and operations of red-black trees, including examples of inserting nodes and handling color conflicts through recoloring and rotations. It also provides a step-by-step guide on creating a red-black tree using a set of node values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 71

UNIT-III

Red- Black Tree

SK. YAKOOB
Associate Professor
Red-Black Tree
 A red-black tree is a kind of self-balancing binary search
tree where each node has an extra bit, and that bit is
often interpreted as the colour (red or black)
 These colours are used to ensure that the tree remains
balanced during insertions and deletions.
 This tree was invented in 1972 by Rudolf Bayer.
 It must be noted that as each node requires only 1 bit of
space to store the colour information
Red-Black Tree
RULES:
 Every node is either Red or Black.
 Root of tree is always Black.
 There are no two adjacent red nodes (A red node cannot
have a red parent or red child)
 Every path from a node (including root) to any of its
descendant NULL node has the same number of black
nodes.
 NULL node is also Black.
 Newly inserted node is Red.
Red-Black Tree
 Example:

R
B
10 10
B B
15
5 R 11 15 B
R B B
2 12 20 B 4 B R
5 25
Not Red – Black Tree Not Red – Black Tree
(Root is always Black) B (BST property violated)
10
B B
5 15
B B B
2 7 20

Not Red – Black Tree


(No same no. of black nodes in every path)
Red-Black Tree
 Example:

B
30
B R
20 50
R B B
10 40 60
R
70

Red – Black Tree


Red - Black Tree Operations
 Searching a Node/ Element

 Inserting a Node/ Element

 Deleting Node/ Element


Inserting in Red- Black Tree
Process:
1. Read the desired element which is red node from the user to insert.
2. Insert a new node using the BST insertion Algorithm.
3. If Tree is empty, create new node as Root make it colour with
Black.
4. If Tree is not empty ,create new node as leaf node.
5. If parent of new node is black, then exit.
6. If parent of new node is Red, then check the colour of parents sibling
of new node
a) If Colour is Black or NULL, then do suitable Rotation or Recolor.
b) If Colour is Red then Recolor, and also check if Grand parent of new node is not
root node then Recolor it and Recheck.
Red - Black Tree
Choosing Recoloring:
Insertion of new node , the tree become unbalance then we choose
Recoloring as follows.
 Newly inserted node color is Red
 If the new node parent colour is black then don’t change the
colour. But if it is not i.e. it is red then check the colour of the new
node’s uncle.
 If the node’s uncle has a red colour then change the colour of the
node’s parent and uncle to black and that of grandfather to red
colour and repeat the same process for him (i.e. grandfather).
 If Grand Parent is Root node ,change the colour to Black.
Red – Black Tree (Recoloring)
Red - Black Tree
Choosing a Tree Rotation:
Insertion of new node , the tree become unbalance then we choose Recoloring
as follows.
 Newly inserted node color is Red
 If the new node parent colour is black then don’t change the colour. But
if it is not i.e. it is red then check the colour of the new node’s uncle.
 If the node’s uncle has a Black colour then apply the suitable Rotation.
 Rotations may be as AVL Tree Rotations i.e LL ,RR ,LR ,RL
 LL and RR Rotations are said to be Zig – Zig Rotation, Where as LR and
RL Rotations are said tobe Zig – Zag Rotation
Red – Black Tree (Zig – Zig Rotation)
1. LL Rotation 2. RR Rotation
Red – Black Tree (Zig – Zag Rotation)
3. LR Rotation 4. RL Rotation
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 10: 10 is the only node so make it as root, make the color to Black.

R
10
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 10: 10 is the only node so make it as root, make the color to Black.

B
10
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 20: 20 is greater than 10, insert at right sub tree


B
10
R
20
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 30: 30 is greater than 20, insert at right sub tree


B
10
R
20
R
30
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 30: 30 is greater than 20, insert at right sub tree

 Newly inserted node parent is Red and Uncle is Black, hence apply Zig-Zig Rotation(RR).
B
10
R
B
U 20
R
30
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 30: 30 is greater than 20, insert at right sub tree

 Newly inserted node parent is Red and Uncle is Black, hence apply Zig-Zig Rotation(RR).

B
20
R R
10 30
B
U
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 50: 50 is greater than 20,30, insert at right sub tree

 Newly inserted node parent is Red and Uncle is Red, hence apply Recoloring.
B
20
R R
10 30
R
50
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 50: 50 is greater than 20,30, insert at right sub tree

 Make the Root colour to Black.


R
20
B B
10 30
R
50
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 50: 50 is greater than 20,30, insert at right sub tree

 Make the Root colour to Black.


B
20
B B
10 30
R
50
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 40: 40 is greater than 30 and less than 50 so insert at left sub
tree of 50
B
20
B B
10 30
R
50
R
40
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 40: 40 is greater than 30 and less than 50 so insert at left sub tree of 50

 Newly inserted node parent is Red and Uncle is Black, hence apply Zig-Zag Rotation(RL). B
20
B B
10 30
R
B
U 50
R
40
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 40: 40 is greater than 30 and less than 50 so insert at left sub tree of 50

 Newly inserted node parent is Red and Uncle is Black, hence apply Zig-Zag Rotation(RL). B
20
B B
10 40
R R
30 50
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 60: 60 is greater than 50 so insert at right sub tree of 50

 Newly inserted node parent is Red and Uncle is Red, hence apply Recoloring B
20
B B
10 40
R R
30 50
R
60
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 60: 60 is greater than 50 so insert at right sub tree of 50

 Newly inserted node parent is Red and Uncle is Red, hence apply Recoloring B
20
B R
10 40
B B
30 50
R
60
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 60: 60 is greater than 50 so insert at right sub tree of 50

 Check with Grand parent color with its predecessor for color conflict, if not go futher. B
20
B R
10 40
B B
30 50
R
60
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 70: 70 is greater than 60 so insert at right sub tree of 60

 Newly inserted node parent is Red and Uncle is Black(NULL), hence apply Rotation. B
20
B R
10 40
B
B
30 50
R

60 R
70
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 70: 70 is greater than 60 so insert at right sub tree of 60


B
20
B R
10 40
B
B
30 60
R R

50 70
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 80: 80 is greater than 70 so insert at right sub tree of 70

 Newly inserted node parent is Red and Uncle is Red, hence apply Recoloring B
20
B R
10 40
B
B
30 60
R R

50 70
R
80
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 80: 80 is greater than 70 so insert at right sub tree of 70

 Grand parent color conflict with its predecessor B


20
B R
10 40
R
B
30 60
B B

50 70
R
80
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 80: 80 is greater than 70 so insert at right sub tree of 70

 Newly node parent is Red and Uncle is Black, hence apply Rotation B
20
B R
10 40
R
B
30 60 Assume New Node
B B

50 70
R
80
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 80: 80 is greater than 70 so insert at right sub tree of 70

 Newly node parent is Red and Uncle is Black, hence apply Rotation

B
40
R R
B
20 30 60 Assume New Node
B B B
10 70
50
R
80
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 80: 80 is greater than 70 so insert at right sub tree of 70

B
40
R R
20 60 Assume New Node
B B B B
10 70
30 50
R
80
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 4: 4 is smaller than 10 so insert at left sub tree of 10

 No Color Conflict

B
40
R R
20 60
B B B B
10 70
R 30 50
R
4 80
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 8: 8 is smaller than 10,greater than 4 so insert at right sub tree of 4

 Newly inserted node parent is Red and Uncle is Black, hence apply Rotation(Zig –Zag) 40 B
R R
20 60
B B B B
10 70
R 30 50
R
4 80
R
8
Red - Black Tree
 Create a Red - Black Tree using following node values
 10,20,30,50,40,60,70,80,4,8

 Insert 8: 8 is smaller than 10,greater than 4 so insert at right sub tree of 4

40 B
R R
20 60
B B B B
8 70
R R 30 50
R
4 10 80
Deletion in Red- Black Tree
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red, Just Delete it.

B
10
B B
7 20
R
30
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex1: if we want to delete 30, search for it then simply delete it

B
10
B B
7 20
R
30
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex1: if we want to delete 30, search for it then simply delete it

B
10
B B
7 20
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex2: if we want to delete 20, search for it and perform BST
Type2 deletion

B
10
B B
7 20
R
30
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex2: if we want to delete 20, search for it and perform BST
Type 2 deletion

B
10
B B
7 30
R
20
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex2: if we want to delete 20, search for it and perform BST
Type 2 deletion

B
10
B B
7 30
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex3: if we want to delete 30, search for it and perform BST
Type3 deletion

B
10
R R
5 30
B B B B
2 9 25 40

R
38
Deletion in Red- Black Tree
Process:
STEP1: Perform BST deletion operations.
STEP2:
CASE 1: If node to be deleted is Red , Just Delete it.
Ex3: if we want to delete 30, search for it and perform BST
Type3 deletion( In-order Successor i.e 38)

B
10
R R
5 38
B B B B
2 9 25 40

R
30
Deletion in Red- Black Tree
Deletion in Red- Black Tree
CASE 2: If we want to delete 15,then it becomes Double Black.

B
10
B R
5 20

B B
15 30
Deletion in Red- Black Tree
CASE 2: If we want to delete 15,then it becomes Double Black.

B
10
B R
5 20

DB B
n 30
Deletion in Red- Black Tree
CASE 2: If we want to delete 15,then it becomes Double Black.

B
10
B R
5 20

DB B
n 30
n n
Deletion in Red- Black Tree
CASE 2: If we want to delete 15,then it becomes Double Black.

B
10
B B
5 20
B B
n
30
n n
Deletion in Red- Black Tree
CASE 2: If we want to delete 15,then it becomes Double Black.

B
10
B B
5 20

B R
n
30
n n
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if
deleted node parent color is Black]

B
10
B B
5 20

B B B B
1 7 15 30
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if
deleted node parent color is Black]

B
10
B B
5 20

B B DB B
1 7 n 30
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if
deleted node parent color is Black]

B
10
B B
5 20

B B DB B
1 7 n 30

n n
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if
deleted node parent color is Black]

B
10
B DB
5 20

B B B B
1 n
7 30

n n
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if deleted node parent color is Black]

Still Double Black(DB) exists, try to remove DB

B
10
B DB
5 20

B B B R
1 n
7 30

n n
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if deleted node parent color is Black]

Still Double Black(DB) exists, try to remove DB

DB
10
B B
5 20

B B B R
1 n
7 30

n n
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if deleted node parent color is Black]

Still Double Black(DB) exists, remove DB directly if it is parent.

DB
10
R B
5 20

B B B R
1 n
7 30

n n
Deletion in Red- Black Tree
CASE 2 : If we want to delete 15,then it becomes Double Black.[ if deleted node parent color is Black]

B
10
R B
5 20

B B B R
1 n
7 30

n n
Deletion in Red- Black Tree
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

B
10
B B
5 20

B B B R
1 7 15 30
B B
25 40
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

B
10
B B
5 20

B B DB R
1 7 n 30
B B
25 40
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

B
10
B B
5 20

B B DB R
1 7 n 30
B B
25 40
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

B
10
B R
5 20

B B DB B
1 7 n 30
B B
25 40
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

Still DB exists, then parent Rotate towards DB


B
10
B R
5 20

B B DB B
1 7 n 30
B B
25 40
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

Still DB exists, then parent Rotate towards DB


B
10
B B
5
30
B B B
1 7 R
20 40

B
DB
n 25
Deletion in Red- Black Tree
CASE 3 : If we want to delete 15,then it becomes Double Black

Still DB exists, Reapply cases sibling is Black


B
10
B B
5
30
B B B
1 7 B
20 40

R
B
n 25
Deletion in Red- Black Tree
Deletion in Red- Black Tree
Thank you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy