SlideShare a Scribd company logo
1 of 81
Download to read offline
1
Red-Black Trees
by Thomas A. Anastasio
2
A Red-Black Tree with NULLs shown
Black-Height of the tree = 4
3
Red-Black Trees
• Definition: A red-black tree is a binary
search tree where:
– Every node is either red or black.
– Each NULL pointer is considered to be a black node
– If a node is red, then both of its children are black.
– Every path from a node to a leaf contains the same
number of black nodes.
• Definition: The black-height of a node, n, in
a red-black tree is the number of black nodes
on any path to a leaf, not counting n.
4
A valid Red-Black Tree
Black-Height = 2
5
6
7
Theorem 1 – Any red-black tree with root x,
has at least n = 2bh(x)
– 1 internal nodes,
where bh(x) is the black height of node x.
Proof: by induction on height of x.
8
Theorem 2 – In a red-black tree, at least half
the nodes on any path from the root to a leaf
must be black.
Proof – If there is a red node on the path,
there must be a corresponding black node.
9
Theorem 3 – In a red-black tree, no path from any
node, N, to a leaf is more than twice as long as any
other path from N to any other leaf.
Proof: By definition, every path from a node to any
leaf contains the same number of black nodes. By
Theorem 2, a least ½ the nodes on any such path
are black. Therefore, there can no more than twice
as many nodes on any path from N to a leaf as on
any other path. Therefore the length of every path
is no more than twice as long as any other path
10
Theorem 4 –
A red-black tree with n internal nodes has
height h <= 2 lg(n + 1).
Proof: Let h be the height of the red-black tree
with root x. By Theorem 2,
bh(x) >= h/2
From Theorem 1, n >= 2bh(x)
- 1
Therefore n >= 2 h/2
– 1
n + 1 >= 2h/2
lg(n + 1) >= h/2
2lg(n + 1) >= h
11
Bottom –Up Insertion
• Insert node as usual in BST
• Color the Node RED
• What Red-Black property may be violated?
– Every node is Red or Black
– Leaf nodes are Black NULLS
– If node is Red, both children must be Black
– Every path from node to descendant leaf must
contain the same number of Blacks
12
Bottom Up Insertion
• Insert node; Color it RED; X is pointer to it
• Cases
0: X is the root -- color it black
1: Both parent and uncle are red -- color parent and uncle
black, color grandparent red, point X to grandparent,
check new situation
2 (zig-zag): Parent is red, but uncle is black. X and its
parent are opposite type children -- color grandparent
red, color X black, rotate left on parent, rotate right on
grandparent
3 (zig-zig): Parent is red, but uncle is black. X and its
parent are both left or both right children -- color parent
black, color grandparent red, rotate right on
grandparent
13
X
P
G
U
P
G
U
Case 1 – U is Red
Just Recolor and move up
X
14
X
P
G
U
S X
P G
S
U
Case 2 – Zig-Zag
Double Rotate
X around P; X around G
Recolor G and X
15
X
P
G
U
S P
X G
S U
Case 3 – Zig-Zig
Single Rotate P around G
Recolor P and G
16
11
14
15
2
1 7
5 8
Black node Red node
Insert 4 into this
R-B Tree
17
Insertion Practice
Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an
initially empty Red-Black Tree
18
Asymptotic Cost of Insertion
• O(lg n) to descend to insertion point
• O(1) to do insertion
• O(lg n) to ascend and readjust == worst
case only for case 1
• Total: O(log n)
Red-Black Trees
Bottom-Up Deletion
Recall “ordinary” BST Delete
• If vertex to be deleted is a leaf, just delete
it.
• If vertex to be deleted has just one child,
replace it with that child
• If vertex to be deleted has two children,
replace the value of by it’s in-order
predecessor’s value then delete the in-
order predecessor (a recursive step)
Bottom-Up Deletion
1. Do ordinary BST deletion. Eventually a
“case 1” or “case 2“ will be done (leaf or
just one child). If deleted node, U, is a
leaf, think of deletion as replacing with
the NULL pointer, V. If U had one child,
V, think of deletion as replacing U with V.
2. What can go wrong??
Which RB Property may be
violated after deletion?
1. If U is red?
Not a problem – no RB properties violated
2. If U is black?
If U is not the root, deleting it will change
the black-height along some path
Fixing the problem
• Think of V as having an “extra” unit of
blackness. This extra blackness must be
absorbed into the tree (by a red node), or
propagated up to the root and out of the
tree.
• There are four cases – our examples and
“rules” assume that V is a left child. There
are symmetric cases for V as a right child
Terminology
• The node just deleted was U
• The node that replaces it is V, which has an
extra unit of blackness
• The parent of V is P
• The sibling of V is S
Black Node
Red Node
Red or Black and don’t care
Bottom-Up Deletion
Case 1
• V’s sibling, S, is Red
– Rotate S around P and recolor S & P
• NOT a terminal case – One of the other
cases will now apply
• All other cases apply when S is Black
Case 1 Diagram
P
S
V+
P
S
V+
Rotate
P
V+
S
Recolor
Bottom-Up Deletion
Case 2
• V’s sibling, S, is black and has two black
children.
– Recolor S to be Red
– P absorbs V’s extra blackness
• If P is Red, we’re done
• If P is Black, it now has extra blackness and
problem has been propagated up the tree
Case 2 diagram
P
S
V+
P+
S
V
Recolor and absorb
Either extra black absorbed by P or
P now has extra blackness
Bottom-Up Deletion
Case 3
• S is black
• S’s RIGHT child is RED (Left child either
color)
– Rotate S around P
– Swap colors of S and P, and color S’s Right
child Black
• This is the terminal case – we’re done
Case 3 diagrams
P
S
V+
P
S
V
Rotate
P
S
V
Recolor
Bottom-Up Deletion
Case 4
• S is Black, S’s right child is Black and S’s
left child is Red
– Rotate S’s left child around S
– Swap color of S and S’s left child
– Now in case 3
Case 4 Diagrams
P
S
V+
P
S
V+
Rotate
P
S
V+
Recolor
65
50 80
10 60 70 90
62
Perform the following deletions, in the order specified
Delete 90, Delete 80, Delete 70
Red Black Trees
Top-Down Insertion
Review of Bottom-Up Insertion
• In B-Up insertion, “ordinary” BST insertion
was used, followed by correction of the tree
on the way back up to the root
• This is most easily done recursively
– Insert winds up the recursion on the way down
the tree to the insertion point
– Fixing the tree occurs as the recursion unwinds
Top-Down Insertion Strategy
• In T-Down insertion, the corrections are
done while traversing down the tree to the
insertion point.
• When the actual insertion is done, no
further corrections are needed, so no need
to traverse back up the tree.
• So, T-Down insertion can be done
iteratively which is generally faster
Goal of T-D Insertion
• Insertion is always done as a leaf (as in
ordinary BST insertion)
• Recall from the B-Up flow chart that if the
uncle of a newly inserted node is black, we
restore the RB tree properties by one or two
local rotations and recoloring – we do not
need to make changes further up the tree
Goal (2)
• Therefore, the goal of T-D insertion is to
traverse from the root to the insertion point
in such a way that RB properties are
maintained, and at the insertion point, the
uncle is Black.
• That way we may have to rotate and
recolor, but not propagate back up the tree
Possible insertion configurations
X (Red or Black)
Y Z
If a new node is inserted as a child of Y or Z, there is no
problem since the new node’s parent is black
Possible insertion configurations
X
Y Z
If new node is child of Z, no problem since Z is black.
If new node is child of Y, no problem since the new node’s
uncle (Z) is black – do a few rotations and recolor…. done
Possible insertion configurations
X
Y Z
If new node is inserted as child of Y or Z, it’s uncle will
be red and we will have to go back up the tree. This is the
only case we need to avoid.
Top-Down Traversal
X
Y Z
As we traverse down the tree and
encounter this case, we recolor and
possible do some rotations.
There are 3 cases.
Remember the goal – to create an insertion point at which the
parent of the new node is Black, or the uncle of the new node
is black.
Case 1 – X’s Parent is Black
X
Z
Y
P
X
Z
P
Just recolor and continue down the tree
Y
Case 2
• X’s Parent is Red (so Grandparent is Black)
and X and P are both left/right children
– Rotate P around G
– Color P black
– Color G red
• Note that X’s uncle, U, must be black
because it (a) was initially black, or (b)
would have been made black when we
encountered G (which would have had two
red children -- X’s Parent and X’s uncle)
Case 2 diagrams
X
Z
Y
P
G
U
S
X
Z
Y
P
G
U
S
Rotate P around G. Recolor X, Y, Z, P and G
Case 3
• X’s Parent is Red (so Grandparent is Black)
and X and P are opposite children
– Rotate P around G
– Color P black
– Color G red
• Again note that X’s uncle, U, must be black
because it (a) was initially black, or (b)
would have been made black when we
encountered G (which would have had two
red children -- X’s Parent and X’s uncle)
Case 3 Diagrams (1 of 2)
X
Z
Y
P
G
U
S
X
Y
S
P
G
U
Z
Step 1 – recolor X, Y and Z. Rotate X around P.
Case 3 Diagrams (2 of 2)
X
Y
S
P
G
U
Z
P
Y
S
X
G
U
Z
Step 2 – Rotate X around G. Recolor X and G
An exercise – insert F
D
T
W
Z
V
L
J P
E K
Top-Down Insert Summary
P
X
Y Z
Case 1
P is Black
Just Recolor
P
X
Y Z
Case 2
P is Red
X & P both left/right
P
X
Y Z
G
P
X
Y Z
G
Recolor
X,Y,Z
P
X
Y Z
G
Rotate P
around G
Recolor P,G
Case 3
P is Red
X and P are
opposite children
P
X
Y Z
G
Recolor X,Y,Z
Rotate X
around P
X
P
Y Z
G
Rotate X
around G
Recolor X, G
Recolor
X,Y,Z
X
P
Y Z
G
Red Black Trees
Top-Down Deletion
Recall the rules for BST deletion
• If vertex to be deleted is a leaf, just delete
it.
• If vertex to be deleted has just one child,
replace it with that child
• If vertex to be deleted has two children,
replace the value of by it’s in-order
predecessor’s value then delete the in-
order predecessor (a recursive step)
What can go wrong?
1. If the delete node is red?
Not a problem – no RB properties violated
2. If the deleted node is black?
If the node is not the root, deleting it will
change the black-height along some path
The goal of T-D Deletion
• To delete a red leaf
• How do we ensure that’s what happens?
– As we traverse the tree looking for the leaf to
delete, we change every node we encounter to
red.
– If this causes a violation of the RB properties,
we fix it
Bottom-Up vs. Top-Down
• Bottom-Up is recursive
– BST deletion going down the tree (winding up
the recursion)
– Fixing the RB properties coming back up the
tree (unwinding the recursion)
• Top-Down is iterative
– Restructure the tree on the way down so we
don’t have to go back up
Terminology
• Matching Weiss text section 12.2
– X is the node being examined
– T is X’s sibling
– P is X’s (and T’s) parent
– R is T’s right child
– L is T’s left child
• This discussion assumes X is the left child of P.
As usual, there are left-right symmetric cases.
Basic Strategy
• As we traverse the tree, we change every
node we visit, X, to Red.
• When we change X to Red, we know
– P is also Red (we just came from there)
– T is black (since P is Red, it’s children are
Black)
Step 1 – Examine the root
1. If both of the root’s children are Black
a. Make the root Red
b. Move X to the appropriate child of the root
c. Proceed to step 2
2. Otherwise designate the root as X and
proceed to step 2B.
Step 2 – the main case
As we traverse down the tree, we continually
encounter this situation until we reach the
node to be deleted
X is Black, P is Red, T is Black
We are going to color X Red, then recolor
other nodes and possibly do rotation(s)
based on the color of X’s and T’s children
2A. X has 2 Black children
2B. X has at least one Red child
P
T
X
Case 2A
X has two Black Children
2A1. T has 2 Black Children
2A2. T’s left child is Red
2A3. T’s right child is Red
** if both of T’s children are Red,
we can do either 2A2 or 2A3
Case 2A1
X and T have 2 Black Children
P
T
X
P
T
X
Just recolor X, P and T and move down the tree
Case 2A2
P
T
X
L
X has 2 Black Children and T’s Left Child is Red
Rotate L around T, then L around P
Recolor X and P then continue down the tree
L1 L2
P T
X
L
L1 L2
Case 2A3
P
T
X
X has 2 Black Children and T’s Right Child is Red
Rotate T around P
Recolor X, P, T and R then continue down the tree
R1 R2
P R
X
T
R2
R1
R
L L
Case 2B
X has at least one Red child
Continue down the tree to the next level
If the new X is Red, continue down again
If the new X is Black (T is Red, P is Black)
Rotate T around P
Recolor P and T
Back to main case – step 2
Case 2B Diagram
P
X T
Move down the tree.
P
X T
P
T X
If move to Black child (2B2)
Rotate T around P; Recolor P and T
Back to step 2, the main case
If move to the Red child (2B1)
Move down again
Step 3
Eventually, find the node to be deleted – a leaf or a node with
one non-null child that is a leaf.
Delete the appropriate node as a Red leaf
Step 4
Color the Root Black
Example 1
Delete 10 from this RB Tree
15
17
16
20
23
18
13
10
7
12
6
3
Step 1 – Root has 2 Black children. Color Root Red
Descend the tree, moving X to 6
Example 1 (cont’d)
15
17
16
20
23
18
13
10
7
12
6
3
One of X’s children is Red (case 2B). Descend down the
tree, arriving at 12. Since the new X (12) is also Red (2B1),
continue down the tree, arriving at 10.
X
Example 1 (cont’d)
15
17
16
20
23
18
13
10
7
12
6
3
Step 3 -Since 10 is the node to be deleted, replace it’s value
with the value of it’s only child (7) and delete 7’s red node
X
Example 1 (cont’d)
15
17
16
20
23
18
13
7
12
6
3
The final tree after 7 has replaced 10 and 7’s red node
deleted and (step 4) the root has been colored Black.
Example 2
Delete 10 from this RB Tree
15
17
16
20
13
10
12
6
3
4
2
Step 1 – the root does not have 2 Black children.
Color the root red, Set X = root and proceed to step 2
Example 2 (cont’d)
15
17
16
20
13
10
12
6
3
4
2
X
X has at least one Red child (case 2B). Proceed down
the tree, arriving at 6. Since 6 is also Red (case 2B1),
continue down the tree, arriving at 12.
Example 2 (cont’d)
15
17
16
20
13
10
12
6
3
4
2
X
X has 2 Black children. X’s sibling (3) also has 2 black children.
Case 2A1– recolor X, P, and T and continue down the tree, arriving
at 10.
P
T
Example 2 (cont’d)
15
17
16
20
13
10
12
6
3
4
2
P
X T
X is now the leaf to be deleted, but it’s Black, so back to step 2.
X has 2 Black children and T has 2 Black children – case 2A1
Recolor X, P and T.
Step 3 -- Now delete 10 as a red leaf.
Step 4 -- Recolor the root black
Example 2 Solution
15
17
16
20
13
12
6
3
4
2
Example 3
Delete 11 from this RB Tree
15
13
11
12
10
5
7
3
6 9
2 4
Valid and
unaffected
Right subtree
Step 1 – root has 2 Black children. Color Root red.
Set X to appropriate child of root (10)
Example 3 (cont’d)
15
13
11
12
10
5
7
3
6 9
2 4
X
X has one Red child (case 2B)
Traverse down the tree, arriving at 12.
Example 3 (cont’d)
15
13
11
12
10
5
7
3
6 9
4
X
Since we arrived at a black node (case 2B2) assuring T is red
and P is black), rotate T around P, recolor T and P
Back to step 2
P
T
2
Example 3 (cont’d)
15
13
11
12
10
5
7
3
6 9
4
X
P
T
2
Now X is Black with Red parent and Black sibling.
X and T both have 2 Black children (case 2A1)
Just recolor X, P and T and continue traversal
Example 3 (cont’d)
15
13
11
12
10
5
7
3
6 9
4
X
P
T
2
Having traversed down the tree, we arrive at 11, the leaf to
be deleted, but it’s Black, so back to step 2.
X and T both have two Black children. Recolor X, P and T.
Step 3 -- delete 11 as a red leaf.
Step 4 -- Recolor the root black
Example 3 Solution
13
12
10
5
7
3
6 9
4
2
15

More Related Content

Similar to anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf

Similar to anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf (20)

Red black tree
Red black treeRed black tree
Red black tree
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
Data structure
Data structureData structure
Data structure
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
 
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED BRED BLACK TREE ADSA UNIT 1 ROTAITON RED B
RED BLACK TREE ADSA UNIT 1 ROTAITON RED B
 
Red black trees1109
Red black trees1109Red black trees1109
Red black trees1109
 
Red black-trees-4
Red black-trees-4Red black-trees-4
Red black-trees-4
 
Red black tree
Red black treeRed black tree
Red black tree
 
Trees
TreesTrees
Trees
 
Red black 1
Red black 1Red black 1
Red black 1
 
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)
 
redblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptxredblacktreeindatastructure-200409083949.pptx
redblacktreeindatastructure-200409083949.pptx
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
 
Red black tree in data structure
Red black tree in data structureRed black tree in data structure
Red black tree in data structure
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
void insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdfvoid insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdf
 
Red black tree
Red black treeRed black tree
Red black tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

Recently uploaded

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 

anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf

  • 2. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4
  • 3. 3 Red-Black Trees • Definition: A red-black tree is a binary search tree where: – Every node is either red or black. – Each NULL pointer is considered to be a black node – If a node is red, then both of its children are black. – Every path from a node to a leaf contains the same number of black nodes. • Definition: The black-height of a node, n, in a red-black tree is the number of black nodes on any path to a leaf, not counting n.
  • 4. 4 A valid Red-Black Tree Black-Height = 2
  • 5. 5
  • 6. 6
  • 7. 7 Theorem 1 – Any red-black tree with root x, has at least n = 2bh(x) – 1 internal nodes, where bh(x) is the black height of node x. Proof: by induction on height of x.
  • 8. 8 Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node.
  • 9. 9 Theorem 3 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem 2, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path
  • 10. 10 Theorem 4 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h
  • 11. 11 Bottom –Up Insertion • Insert node as usual in BST • Color the Node RED • What Red-Black property may be violated? – Every node is Red or Black – Leaf nodes are Black NULLS – If node is Red, both children must be Black – Every path from node to descendant leaf must contain the same number of Blacks
  • 12. 12 Bottom Up Insertion • Insert node; Color it RED; X is pointer to it • Cases 0: X is the root -- color it black 1: Both parent and uncle are red -- color parent and uncle black, color grandparent red, point X to grandparent, check new situation 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children -- color grandparent red, color X black, rotate left on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children -- color parent black, color grandparent red, rotate right on grandparent
  • 13. 13 X P G U P G U Case 1 – U is Red Just Recolor and move up X
  • 14. 14 X P G U S X P G S U Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X
  • 15. 15 X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G
  • 16. 16 11 14 15 2 1 7 5 8 Black node Red node Insert 4 into this R-B Tree
  • 17. 17 Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree
  • 18. 18 Asymptotic Cost of Insertion • O(lg n) to descend to insertion point • O(1) to do insertion • O(lg n) to ascend and readjust == worst case only for case 1 • Total: O(log n)
  • 20. Recall “ordinary” BST Delete • If vertex to be deleted is a leaf, just delete it. • If vertex to be deleted has just one child, replace it with that child • If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the in- order predecessor (a recursive step)
  • 21. Bottom-Up Deletion 1. Do ordinary BST deletion. Eventually a “case 1” or “case 2“ will be done (leaf or just one child). If deleted node, U, is a leaf, think of deletion as replacing with the NULL pointer, V. If U had one child, V, think of deletion as replacing U with V. 2. What can go wrong??
  • 22. Which RB Property may be violated after deletion? 1. If U is red? Not a problem – no RB properties violated 2. If U is black? If U is not the root, deleting it will change the black-height along some path
  • 23. Fixing the problem • Think of V as having an “extra” unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree. • There are four cases – our examples and “rules” assume that V is a left child. There are symmetric cases for V as a right child
  • 24. Terminology • The node just deleted was U • The node that replaces it is V, which has an extra unit of blackness • The parent of V is P • The sibling of V is S Black Node Red Node Red or Black and don’t care
  • 25. Bottom-Up Deletion Case 1 • V’s sibling, S, is Red – Rotate S around P and recolor S & P • NOT a terminal case – One of the other cases will now apply • All other cases apply when S is Black
  • 27. Bottom-Up Deletion Case 2 • V’s sibling, S, is black and has two black children. – Recolor S to be Red – P absorbs V’s extra blackness • If P is Red, we’re done • If P is Black, it now has extra blackness and problem has been propagated up the tree
  • 28. Case 2 diagram P S V+ P+ S V Recolor and absorb Either extra black absorbed by P or P now has extra blackness
  • 29. Bottom-Up Deletion Case 3 • S is black • S’s RIGHT child is RED (Left child either color) – Rotate S around P – Swap colors of S and P, and color S’s Right child Black • This is the terminal case – we’re done
  • 31. Bottom-Up Deletion Case 4 • S is Black, S’s right child is Black and S’s left child is Red – Rotate S’s left child around S – Swap color of S and S’s left child – Now in case 3
  • 33. 65 50 80 10 60 70 90 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
  • 35. Review of Bottom-Up Insertion • In B-Up insertion, “ordinary” BST insertion was used, followed by correction of the tree on the way back up to the root • This is most easily done recursively – Insert winds up the recursion on the way down the tree to the insertion point – Fixing the tree occurs as the recursion unwinds
  • 36. Top-Down Insertion Strategy • In T-Down insertion, the corrections are done while traversing down the tree to the insertion point. • When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. • So, T-Down insertion can be done iteratively which is generally faster
  • 37. Goal of T-D Insertion • Insertion is always done as a leaf (as in ordinary BST insertion) • Recall from the B-Up flow chart that if the uncle of a newly inserted node is black, we restore the RB tree properties by one or two local rotations and recoloring – we do not need to make changes further up the tree
  • 38. Goal (2) • Therefore, the goal of T-D insertion is to traverse from the root to the insertion point in such a way that RB properties are maintained, and at the insertion point, the uncle is Black. • That way we may have to rotate and recolor, but not propagate back up the tree
  • 39. Possible insertion configurations X (Red or Black) Y Z If a new node is inserted as a child of Y or Z, there is no problem since the new node’s parent is black
  • 40. Possible insertion configurations X Y Z If new node is child of Z, no problem since Z is black. If new node is child of Y, no problem since the new node’s uncle (Z) is black – do a few rotations and recolor…. done
  • 41. Possible insertion configurations X Y Z If new node is inserted as child of Y or Z, it’s uncle will be red and we will have to go back up the tree. This is the only case we need to avoid.
  • 42. Top-Down Traversal X Y Z As we traverse down the tree and encounter this case, we recolor and possible do some rotations. There are 3 cases. Remember the goal – to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.
  • 43. Case 1 – X’s Parent is Black X Z Y P X Z P Just recolor and continue down the tree Y
  • 44. Case 2 • X’s Parent is Red (so Grandparent is Black) and X and P are both left/right children – Rotate P around G – Color P black – Color G red • Note that X’s uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- X’s Parent and X’s uncle)
  • 45. Case 2 diagrams X Z Y P G U S X Z Y P G U S Rotate P around G. Recolor X, Y, Z, P and G
  • 46. Case 3 • X’s Parent is Red (so Grandparent is Black) and X and P are opposite children – Rotate P around G – Color P black – Color G red • Again note that X’s uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- X’s Parent and X’s uncle)
  • 47. Case 3 Diagrams (1 of 2) X Z Y P G U S X Y S P G U Z Step 1 – recolor X, Y and Z. Rotate X around P.
  • 48. Case 3 Diagrams (2 of 2) X Y S P G U Z P Y S X G U Z Step 2 – Rotate X around G. Recolor X and G
  • 49. An exercise – insert F D T W Z V L J P E K
  • 50. Top-Down Insert Summary P X Y Z Case 1 P is Black Just Recolor P X Y Z Case 2 P is Red X & P both left/right P X Y Z G P X Y Z G Recolor X,Y,Z P X Y Z G Rotate P around G Recolor P,G Case 3 P is Red X and P are opposite children P X Y Z G Recolor X,Y,Z Rotate X around P X P Y Z G Rotate X around G Recolor X, G Recolor X,Y,Z X P Y Z G
  • 52. Recall the rules for BST deletion • If vertex to be deleted is a leaf, just delete it. • If vertex to be deleted has just one child, replace it with that child • If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the in- order predecessor (a recursive step)
  • 53. What can go wrong? 1. If the delete node is red? Not a problem – no RB properties violated 2. If the deleted node is black? If the node is not the root, deleting it will change the black-height along some path
  • 54. The goal of T-D Deletion • To delete a red leaf • How do we ensure that’s what happens? – As we traverse the tree looking for the leaf to delete, we change every node we encounter to red. – If this causes a violation of the RB properties, we fix it
  • 55. Bottom-Up vs. Top-Down • Bottom-Up is recursive – BST deletion going down the tree (winding up the recursion) – Fixing the RB properties coming back up the tree (unwinding the recursion) • Top-Down is iterative – Restructure the tree on the way down so we don’t have to go back up
  • 56. Terminology • Matching Weiss text section 12.2 – X is the node being examined – T is X’s sibling – P is X’s (and T’s) parent – R is T’s right child – L is T’s left child • This discussion assumes X is the left child of P. As usual, there are left-right symmetric cases.
  • 57. Basic Strategy • As we traverse the tree, we change every node we visit, X, to Red. • When we change X to Red, we know – P is also Red (we just came from there) – T is black (since P is Red, it’s children are Black)
  • 58. Step 1 – Examine the root 1. If both of the root’s children are Black a. Make the root Red b. Move X to the appropriate child of the root c. Proceed to step 2 2. Otherwise designate the root as X and proceed to step 2B.
  • 59. Step 2 – the main case As we traverse down the tree, we continually encounter this situation until we reach the node to be deleted X is Black, P is Red, T is Black We are going to color X Red, then recolor other nodes and possibly do rotation(s) based on the color of X’s and T’s children 2A. X has 2 Black children 2B. X has at least one Red child
  • 60. P T X Case 2A X has two Black Children 2A1. T has 2 Black Children 2A2. T’s left child is Red 2A3. T’s right child is Red ** if both of T’s children are Red, we can do either 2A2 or 2A3
  • 61. Case 2A1 X and T have 2 Black Children P T X P T X Just recolor X, P and T and move down the tree
  • 62. Case 2A2 P T X L X has 2 Black Children and T’s Left Child is Red Rotate L around T, then L around P Recolor X and P then continue down the tree L1 L2 P T X L L1 L2
  • 63. Case 2A3 P T X X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree R1 R2 P R X T R2 R1 R L L
  • 64. Case 2B X has at least one Red child Continue down the tree to the next level If the new X is Red, continue down again If the new X is Black (T is Red, P is Black) Rotate T around P Recolor P and T Back to main case – step 2
  • 65. Case 2B Diagram P X T Move down the tree. P X T P T X If move to Black child (2B2) Rotate T around P; Recolor P and T Back to step 2, the main case If move to the Red child (2B1) Move down again
  • 66. Step 3 Eventually, find the node to be deleted – a leaf or a node with one non-null child that is a leaf. Delete the appropriate node as a Red leaf Step 4 Color the Root Black
  • 67. Example 1 Delete 10 from this RB Tree 15 17 16 20 23 18 13 10 7 12 6 3 Step 1 – Root has 2 Black children. Color Root Red Descend the tree, moving X to 6
  • 68. Example 1 (cont’d) 15 17 16 20 23 18 13 10 7 12 6 3 One of X’s children is Red (case 2B). Descend down the tree, arriving at 12. Since the new X (12) is also Red (2B1), continue down the tree, arriving at 10. X
  • 69. Example 1 (cont’d) 15 17 16 20 23 18 13 10 7 12 6 3 Step 3 -Since 10 is the node to be deleted, replace it’s value with the value of it’s only child (7) and delete 7’s red node X
  • 70. Example 1 (cont’d) 15 17 16 20 23 18 13 7 12 6 3 The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black.
  • 71. Example 2 Delete 10 from this RB Tree 15 17 16 20 13 10 12 6 3 4 2 Step 1 – the root does not have 2 Black children. Color the root red, Set X = root and proceed to step 2
  • 72. Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 X X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.
  • 73. Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 X X has 2 Black children. X’s sibling (3) also has 2 black children. Case 2A1– recolor X, P, and T and continue down the tree, arriving at 10. P T
  • 74. Example 2 (cont’d) 15 17 16 20 13 10 12 6 3 4 2 P X T X is now the leaf to be deleted, but it’s Black, so back to step 2. X has 2 Black children and T has 2 Black children – case 2A1 Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black
  • 76. Example 3 Delete 11 from this RB Tree 15 13 11 12 10 5 7 3 6 9 2 4 Valid and unaffected Right subtree Step 1 – root has 2 Black children. Color Root red. Set X to appropriate child of root (10)
  • 77. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 2 4 X X has one Red child (case 2B) Traverse down the tree, arriving at 12.
  • 78. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P Back to step 2 P T 2
  • 79. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X P T 2 Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X, P and T and continue traversal
  • 80. Example 3 (cont’d) 15 13 11 12 10 5 7 3 6 9 4 X P T 2 Having traversed down the tree, we arrive at 11, the leaf to be deleted, but it’s Black, so back to step 2. X and T both have two Black children. Recolor X, P and T. Step 3 -- delete 11 as a red leaf. Step 4 -- Recolor the root black