0% found this document useful (0 votes)
11 views

10.SearchTrees 3 RBtree

Uploaded by

Dhruv Dengada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

10.SearchTrees 3 RBtree

Uploaded by

Dhruv Dengada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Red-Black Trees

6
v
3 8
z
4

1
From (2,4) to Red-Black Trees
A red-black tree is a representation of a (2,4) tree by means of a binary tree
whose nodes are colored red or black
In comparison with its associated (2,4) tree, a red-black tree has
n same logarithmic time performance
n simpler implementation with a single node type

4 3 5 2 6 7

4 5 3 6

3 OR 5 2 7

2
Red-Black Trees
A red-black tree can also be defined as a binary search tree that
satisfies the following properties:
n Root Property: the root is black
n External Property: every leaf is black
n Internal Property: the children of a red node are black (red rule)
n Depth Property: all the leaves have the same black depth (path rule)
n (Question) How is balancing enforced here?

4 15

2 6 12 21

3
Red Black Tree?
19

12 35
50
0
75
-10
-5 135

-8 100

-6 80

4
Red Back Tree?

19

12 35

3 16

0
What if we attach a child
to node 0?

5
Implications
Root Property: the root is black
External Property: every leaf is black
Internal Property: the children of a red node are black (red rule)
Depth Property: all the leaves have the same black depth (path rule)

1. If a red node has any children, it must have two children and they must be
black
n Why? Depth property

2. If a black node has only one “real” child then it must be a ”last” red node
n If the child is black?
n If the child is not the last red?

(Question) How is balancing enforced in R-B tree?


6
Intuition about “rough balancing”
The longest path <= 2 * the shortest path
n Rough balancing à guarantees log(n) height

Why?
n From “red rule” and “path rule”
shortest path = only black nodes
longest path = inserting a red node between two black nodes

Root Property: the root is black


External Property: every leaf is black
Internal Property: the children of a red node are black (red rule)
Depth Property: all the leaves have the same black depth (path rule)

7
Height of a Red-Black Tree
Theorem: A red-black tree storing n entries has height O(log n)
Proof:
n Omitted

The search algorithm for a binary search tree is the same as that
for a binary search tree

By the above theorem, searching in a red-black tree takes O(log


n) time

8
Insertion

9
Insertion
To perform operation put(k, o), we execute the insertion algorithm for binary
search trees and color red the newly inserted node z unless it is the root
n We preserve the root, external, and depth properties
n If the parent v of z is black, we also preserve the internal property and we are done
n Else (v is red ) we have a double red (i.e., a violation of the internal property),
which requires a reorganization of the tree
n Goal: Removing double read without breaking the depth property

Example where the insertion of 4 causes a double red:

6 6
v v
3 8 3 8
z z
4

10
Remedying a Double Red
Consider a double red with child z and parent v, and let w be the sibling of v

Case 1: w is black Case 2: w is red


n Viewpoint n Viewpoint
The double red is an incorrect The double red corresponds
replacement of a 4-node to an overflow
n Restructuring: we change the n Recoloring: we perform the
4-node replacement equivalent of a split

w 4 v 4 v
w
2 7 2 7
z z
6 6

4 6 7 2 4 6 7

.. 2 ..

11
Restructuring
A restructuring remedies a child-parent double red when the parent red node
has a black sibling
It is equivalent to restoring the correct replacement of a 4-node
The internal property is restored and the other properties are preserved

z
4 6
w v v
2 7 4 7
z w
6 2

4 6 7 4 6 7

.. 2 .. .. 2 ..
12
Restructuring (cont.)
There are four restructuring configurations depending on whether the double
red nodes are left or right children

2 6 6 2
6 2 4 4
4 4 2 6

2 6

13
Recoloring
A recoloring remedies a child-parent double red when the parent red node has
a red sibling
The parent v and its sibling w become black and the grandparent u becomes
red, unless it is the root
It is equivalent to performing a split on a 5-node
The double red violation may propagate to the grandparent u

4 v 4 v
w w
2 7 2 7
z z
6 6

… 4 …
2 4 6 7
2 6 7

14
w v

RST

w v

RCL

w v

RST

w v

RCL 15
w v

RST

w v

RCL RST

16
Analysis of Insertion

Algorithm put(k, o) Recall that a red-black tree has


O(log n) height
1. We search for key k to locate the Step 1 takes O(log n) time
insertion node z because we visit O(log n) nodes
Step 2 takes O(1) time
2. We add the new entry (k, o) at Step 3 takes O(log n) time
node z and color z red
because we perform
n O(log n) recolorings, each
3. while doubleRed(z) taking O(1) time, and
if isBlack(sibling(parent(z))) n at most one restructuring
z ¬ restructure(z) taking O(1) time
return Thus, an insertion in a red-black
else { sibling(parent(z) is red } tree takes O(log n) time
z ¬ recolor(z)

17
RB-Tree: Deletion

18
Deletion: Example 1
To perform operation erase(k), we first execute the deletion algorithm for
binary search trees
10 10
1
5 30 5 35
3
-5 7 20 38 -5 7 20 38
null null null null null null 35 null null null null null null null 35 null

null null null null


delete 30 Copy inorder successor

10
Just delete the copied 35, and
5 35 color the remaining node
in black. Then, we are done.
-5 7 20 38
null null null null null null null null Implication:
If the node to be deleted is red,
removing it is fine
19
Deletion: Example 2
To perform operation erase(k), we first execute the deletion algorithm for
binary search trees
10 10
5 30 5 32
-5 7 20 38 -5 7 20 38
null null null null null null 32 41 null null null null null null 32 41

delete 30 null 35 null null Copy inorder successor null 35 null null

null null null null


10
Just delete the copied 32, and color 35 with
5 32 black.
-5 7 20 38 Implication: For a node (with a red child)
null null null null null null 35 41 to be deleted, delete it and change the red
child’s color.
null null null null
(35: -1 first and +1 second. So no change)
20
Deletion: Example 3
What about deleting a node with a black child?

10 20
-10 30 -10 30
null null
20 38 null null
20 38
null null null null null null null null

Delete 10 Copy inorder successor

20
-10 30 Delete 20.
null null null 38 Problem: A path of only 2 blacks
null null

Regard this as “double black nodes”


21
Deletion
To perform operation erase(k), we first execute the deletion algorithm for
binary search trees
n Enough to consider the removal of an entry at a node with an external child
(To remove a node with both internal children, we first copy the inorder successor,
and then …)
Notations
n v : the internal node removed, Delete 30
w “myself” 10
n w : the external node removed, 5 30
w “my lonely child” x
n r : the sibling of w -5 7 20 38
w “my other child” null null null null null null
v
32 41
n x : the parent of v r
w
w “my father” null 35 null null

null null

22
Questions
l How to handle “double black nodes”

l Are there some cases in handling those? Yes

l Are you ready for ”cases”?

l It’s really, really complex, but if you concentrate, then you can follow it.

23
Deletion: Algorithm Overview (1)
First, remove v and w, and make r a 10 delete 30
child of x 5 30
-5 7 20 38
null null null null null null 35 null
If either of v or r was red, we
null null
color r black and we are done
(Examples 1 and 2) 10
5 35
x
-5 7 20 38
Else (v and r were both black) null null null null null null 35 v null
we color r double black, which null null
r
w
is a violation of the internal
10
property requiring a
5 35
reorganization of the tree
-5 7 20 38
(Examples 3)
null null null null null null null null

24
Deletion: Algorithm Overview (2)
First, remove v and w, and make r a 10
delete 30
child of x 5 30
-5 7 20 38
null null null null null null 32 41
If either of v or r was red, we null 35 null null

color r black and we are done null null

(Examples 1 and 2) 10
5 32 x
-5 7 20 38
Else (v and r were both black) null null null null
v
null null 32 41

we color r double black, which wnull 35 r


null null
null null
is a violation of the internal
10
property requiring a 5 32
reorganization of the tree -5 7 20 38
(Examples 3) null null null null null null 35 41

null null null null

25
Deletion: Algorithm Overview (2)
First, remove v and w, and make r a child of x

If either of v or r was red, we color r black and we are done (Examples 1 and 2)
(Let’s call this Case 0)

Else (v and r were both black) we color r double black, which is a violation of the
internal property requiring a reorganization of the tree (Examples 3)

l Notations after removing v and w


l y: sibling of r
l z: child of y

l We now divide the cases, depending of the color of y and z

26
Recall: Example 3. Notations again!
What about deleting a node with a black child?

10 20
x
-10 30 -10 30
v
null null
20 38 null null
20 38
null null null null null null null null
w r

20 Delete 10 Copy inorder successor


x
-10 30
r y Delete 20.
null null null 38
Problem: A path of only 2 blacks
null null
z
Regard this as “double black nodes”

27
Handling Double Black Nodes: Case 1
Case 1: The sibling y of r is black, and has a red child z
n We perform a restructuring, and we are done

z is the left child

z is the right child

Double black node solved?


28
Handling Double Black Nodes: Case 2
Case 2: The sibling y of r is black, and y’s both children are
black
n We perform a recoloring
n Case 2-1: x (r’s parent) is red

Color x black and color y red

29
Handling Double Black Nodes: Case 2
Case 2: The sibling y of r is black, and y’s both children are
black
n We perform a recoloring
n Case 2-2: x (r’s parent) is black

Color y red (which solves r’s double black),


and make x “double black”
(propagates the double black up),
then reconsider the cases for x

30
Handling Double Black Nodes: Case 3
Case 3: The sibling y of r is red
n We perform adjustment
w If y is the right child of x, then let z be the right child of y
w If y is the left child of x, then let z be the left child of y
n Case 3-1: z is the left child of y

Perform restructuring
Make y be the parent of x
Color y black and x red
(double black not yet solved)
n Case 3-2: z is the right child
à The sibling of r is black (why?)
of y à Similarly, we apply à Case 1 or Case 2 applies
31
Double Black Node Handling: Summary
The algorithm for remedying a double black node r with sibling y considers
three cases
Case 1: y is black and has a red child
n We perform a restructuring, and we are done
Case 2: y is black and its children are both black
n We perform a recoloring, which may propagate up the double black violation
Case 3: y is red
n We perform an adjustment, equivalent to choosing a different representation of a
3-node, after which either Case 1 or Case 2 applies

Deletion in a red-black tree takes O(log n) time

32
Example: Remove 3

w r r

v is red à Case 0 (either v or r is red)


Remove v and w and color r black

33
Example: Remove 12
x
x
v
y
w r r
z

None of v and r is red à Not Case 0


y is black, which has red child
n à Case 1, restructuring

34
Example: Remove 17

v is red àCase 0

35
Example: Remove 18

x x

y v y
r

w r

None of v and r is red à Not Case 0


y is black, having both black children
àCase 2
n x is red àCase 2-1, recoloring
between x and y

36
Example: Remove 15

Case 0 (now you know, right?)

37
Example: Remove 16
x

y x
r
z y r

y is red à Case 3
y is the left child of x, thus z is node 4
(left child of y) à Case 3-1
Adjustment à node 14 becomes double
black à new y (sibling of x)
y has both black children, and x is red
n à Case 2-1, recoloring, then we’re done

38
Questions?

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