Sunteți pe pagina 1din 7

Red-Black Trees Red-Black Trees (cont)

Every red node must have two black children.


A red-black tree is a binary search tree where Every internal black node must have two children.
1) every node is red or black bh(x)= # of black nodes on paths to xs descendant
leaves (not counting x)
2) the root is black
bh(x) h(x) = height of x
3) every leaf is black
4) no red node has a red child
5) any node x has the same number of black nodes x The subtree rooted at x has
at least 2 bh(x) -1 internal nodes.
on all simple paths to its descendant leaves

1 3

Red-Black Trees (cont) Red-Black Trees (cont)


22

Black Proof: By induction on h(x).


Base: h(x)=0. Then bh(x)=0 and x is a leaf and its
Red 5 29
subtree has 0 internal nodes and 0 = 20 -1 .
Either 0 9 26 35 Step: h(x)>0. x is not a leaf so it has two children, y and z
of lesser height.
nil nil
-3 3 7 12 31 106 Both bh(y) and bh(z) are either bh(x) or bh(x)-1.
nil nil The number of internal nodes in xs subtree is
-8 -2 6 8 11 19 nil nil nil nil
at least
-9 nil nil nil nil nil nil nil nil nil nil nil 1 + 2bh ( y ) 1 + 2bh ( z ) 1 2 2bh ( x ) 1 1 = 2bh ( x ) 1

nil nil
Red-black trees are artificially full by adding
black nil nodes. 2
QED 4
Red-Black Trees (cont) Red-Black Trees (cont)
Insertion - Use normal Insert operation then repair.
If T has n internal nodes then n 2bh ( root ) 1
h(root )
Let z be the inserted node (has 2 black nil children)
lg(n + 1) bh(root)
2 If z is not the root, make z red.
h(T ) = h(root ) 2 lg(n + 1) If p[z] is black then done,
otherwise we have two adjacent red nodes.
So operations which are O(h) on binary trees are
Let y be zs uncle. x
O(lg n) on red-black trees...... provided these
operations preserve the properties of red and black p[z] y
trees.
z
Assume root is black.
nil nil
5 7

Red-Black Trees (cont) Red-Black Trees (cont) Insertion


Rotations Case 1 - y is red.

x x
y Right Rotate x
p[z] y p[z] y
x
Left Rotate y
z z

Preserves binary-search-tree property. And repeat if xs parent is red.


If x is the root, make x black.
6 8
Red-Black Trees (cont) Insertion Red-Black Trees (cont) Insertion Flowchart

Case 2 - y is black and z is a right child. Start


Rotate left at p[z] and go to Case 3.

Case 1 Case 2
Left-Rotate
x x

p[z] y z y Case 3
Right-Rotate
z p[z]

End
Case 1 can repeat at most h/2 = O(lg n) times.
At most 2 rotations will be needed.
9 11

Red-Black Trees (cont) Insertion Red-Black Trees (cont) 22

Example: Insertion
Case 3 - y is black and z is a left child. 5 29

Rotate right at x and exchange colors of p[z] and x. 0 9 26 35

x p[z] nil nil


-3 3 7 12 31 106

p[z] y z x nil nil


-8 -2 6 8 11 19 nil nil nil nil

z y
nil nil nil nil 6.5 nil nil nil nil nil nil
5.5
Case 1
nil
5.3 nil nil nil

DONE nil nil


10 12
Red-Black Trees (cont) 22 Red-Black Trees (cont) 22

Example: Insertion Example: Insertion


5 29
9 Case 3 29
5
0 9 35 12 35
26 26
Case 1 0

nil nil 7 11 19 nil nil


-3 3 7 12 31 106 31 106
-3 3
6 8
-2 nil nil 11 19 nil nil nil nil nil nil nil nil nil nil nil nil
-8 6 8 nil nil
-8 -2
5.5 6.5 nil nil
nil nil nil nil 5.5 6.5 nil nil nil nil nil nil
nil nil nil nil
5.3 nil nil nil
nil
5.3 nil nil nil
nil nil
nil nil
13 15

Red-Black Trees (cont) 22


Red-Black Trees (cont)
Example: Insertion Example: Insertion 9

5 Case 2 29

5 22
0 9 35
26
0
nil nil
-3 3 7 12 31 106 7 12 29
-3 3
nil nil 11 19 nil nil nil nil 6 8 11 19
-8 -2 6 8 26 35
nil nil
-8 -2
nil nil nil nil 6.5 nil nil nil nil nil nil 5.5 6.5 nil nil nil nil nil nil nil nil 106
5.5 31
nil nil nil nil
5.3 nil nil nil 5.3 nil nil nil nil nil nil nil

nil nil nil nil


14 16
Red-Black Trees (cont) Deletion Red-Black Trees (cont) Deletion
Deletion - Assume y is the node to remove.
Case 2 - w and its two children are black.
y has a child x (could be a nil)
If y is red then no change is needed.
If y is black and x is red, then make x black. z z

Otherwise y and x are black.


Let z be ys parent and z x w x w
z
assume y was a left child. y w
y
Make w red and pull a black node above z.
Extra black node needed x x w
If z is red then absorb extra black node by making
to maintain black height.
Before After z black. Done or move up tree.
17 19

Red-Black Trees (cont) Deletion Red-Black Trees (cont) Deletion


Case 1 - z is black and w is red. Case 3 - w and its right child are black while its left
child is red.
z w z
z
z
x w x L
x w
x w
L R
R

Left-Rotate at z and exchange colors of z and w.
Right-Rotate at w and exchange colors of w and
Go to Case 2, 3 or 4.
its left child. Go to Case 4.
18 20
Red-Black Trees (cont) Deletion Red-Black Trees (cont)
Example: Deletion 9
Case 4 - w is black and its right child is red.

5 22
z w
0

z R 7 12 Case 2 29
x w -3 3
6 8 11 19 w
x L 26 35
L R -8 -2
nil nil nil x
5.5 6.5 nil nil nil nil nil nil nil nil 106
31
nil nil nil nil
Left-Rotate at z and exchange colors of z and w. 5.3 nil nil nil nil nil nil nil
Extra black node is absorbed by R. Done.
nil nil
21 23

Red-Black Trees (cont) Deletion Flowchart Red-Black Trees (cont)


Start Example: Deletion 9

Case 1
Left-Rotate Case 4
5 22
Case 2 Case 3 0
Right-Rotate
7 12 x 29 w
Case 4 -3 3
Left-Rotate Case 2 6 8 19
26 35
nil nil nil
-8 -2
5.5 6.5 nil nil nil nil nil nil 106
End 31
nil nil nil nil
Case 2 can repeat at most h = O(lg n) times. 5.3 nil nil nil nil nil nil nil

At most 3 rotations will be needed. nil nil


22 24
Red-Black Trees (cont)
Example: Deletion 9

5 29

0
22
7 35

-3 3 12 26
6 8 31 106
nil nil 19 nil nil
-8 -2 nil
nil nil nil nil nil nil
5.5 6.5
nil nil nil nil nil nil
5.3 nil nil nil

nil nil
25

S-ar putea să vă placă și