Red-Black Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Red-Black Trees

Description:

Red-Black Trees x : x ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 45
Provided by: GLAB3
Category:
Tags: black | red | tree | trees

less

Transcript and Presenter's Notes

Title: Red-Black Trees


1
Red-Black Trees
  • ??????? ???? ?? ? ???????? ??

2
Red-Black Trees (RBT)
  • ??????? ???? ? ???? (Red/Black Trees) ???? ????
    ?????? ?????? (Binary Search Tree) ??? ?? ?? ???
    ?? ????? ?? ??????? ????? ?? ??? ??? ??? ????
  • ??? ??? ??????? (???? ?? ????) ???
  • ??? ?? ??? ???? ????? ????? ???? ???? ???
  • ?? ???? ?????? ??????? ?????? ????? ? ?????? ?
    ??? ??? ????? ?? ?????? ?? ??? ???? ?????.
  • ??? ???? ?????? ?? N ??? ?? ????? O(log N) ???.
  • ??? ???? ???????? ?? N ? ??? ?? ????? O(N) ????.

3
Red-black Tree(RBT)
  • RBT BST 1 color bit
  • ???? ?????? ?????? BST ???
  • ??? ??? Inheritance
  • key, left, right, p.
  • ?? ??? ???? ????? ?? ????? ????
  • ?? ??? ????? ??????? null ??????? ?? ????
  • ???? ?????? ??? ( ???????? ?? ???? ???????) ??
    ??? ???? ?????
  • ???? ???? ????? ?????? ??? ? ?? ??? ???? nil ??
    ????? ? ?? ?? ?? ??? ???? ??????? ??? ????
    ??????? ?? ????.
  • ????? ??? first ? ?? ??????? ??????

4
Red-black Properties
  1. ?? ??? ???? ?? ???? ??? ?? ????
  2. ???? ???? ???? ???
  3. ?? ??? ???(null) ???? ???
  4. ??? ???? ???? ????? ?? ?? ????? ?? ???? ?????
  5. ??? ???????? ?? ?? ?? ??? ???? ??? ? ?? ??? ??
    ????? ????? ????? ????? ????? ??? ???? ?????.

5
???? RBT
???? ?? ??? ???? ????? ?? ????? ????? ?? ???
????? ??????? null ??????? ?? ????
26
17
41
30
47
38
50
6
?????? (???) ???? RBT
  • ?????? ??? h(x) ?????? ?? ??? ????? ?? ???
    ???????? ???? ??? ?? ?????? ???? ???.
  • ?????? ???? ??? x bh(x) ????? ?????? ???? ??
    ?? ?? ?? ???????? ??? ?? ?? ??? ??? ???? ?? ????
    ? ?? ??? null(T) ??? ?? ????
  • ??? null(T) ??? ???? ????? ?? ???
  • ??? ??? x ??? ???? ????? ??? ???
  • ?????? ???? ?? ???? RBT ????? ?? ?????? ???? ????
    ?? ???

7
Height of a RBT
h4 bh2
26
h3 bh2
h1 bh1
  • h ??????
  • bh?????? ????
  • bh(x) h(x) 2 bh(x)
  • ??? ???????? ???? ?? ?? ??? ?? ???? ?? ?????
    ?????? ?? ????? ??? ????????? ???? ?? ???? ??? ??
    ?????? ???? ???.

17
41
h2 bh1
h2 bh1
30
47
h1 bh1
38
50
h1 bh1
nullT
8
??1 ?????? ???? RBT
  • ??1 ??? ???????? ???? ?? ?? ??? ?? ??? ?????? ??
    ????? ??? ????????? ???? ???
  • ?????
  • ????? ??????? ???? ??? ??????? ?? ?? ??? x?????
    ??? ? ?? ???? ?? ???? ????? bh(x) ??? ( ????? 5)
  • ??? ??? ????????? ???? ?? ?? s(x)????? ?????
    bh(x) lt s(x)
  • ????? ????? ?????? ??? ??????? ????? ?????? ????
    ?????? ???? ?????? ? ??? ?????? ?? ?? ??? ????
    ???? ?? ????. ???????? ??? ???????? ????? ?????
    ?????? ???? ? ???? ?????? ?? ?? ???? ? ?? ?????
    ????? ??? h(x) lt 2bh(x)
  • ??????? h(x) lt 2s(x)

9
????? ?????? RBT
  • ?? 2 ????? ?????? ????? ?????? ???? ?? ??? x
    ?????? ????? ??? ??2bh(x)1.
  • ????? ( ?? ??????? ?? ?????? ?????)
  • ??? h(x)0 ?? ????? x???? ???.? ? ?????? ???? ??
    ??? ??? ???. ?? ????? ????? ?????? ?????? ?????
    20 -1 0 ???
  • ??? h(x) hgt0 ? ? ??? ???? ?????? ???? ???
    x?????? bh(x) b ????
  • ?? ???? ?? ??????? x ??????? ????? ?? h-1 ??????
    ????. ??? ????? ???? ???? ????? ?????? ???? ?? b
    ? ?? ??? ??? ???? b-1 ?????? ???
  • ????? ??? ?????? ?? ?????? ???? ??? ?? ????? x
    ?????-1 2bh(x) -1 ??? ????? ???? ? ????????
    ?????? ???? ?? x ????? 2(2bh(x)-1 -1 ) 1????
    ????? ????
  • ? 2 (2bh(x) 1 1)1 2bh(x) 1 ????? ??????
    ?????? ???? ?? x

10
?? ?????? RBT
  • ??1 ??? ???????? ???? ?? ?? ??? ?? ??? ?????? ??
    ????? ??? ????????? ???? ??? h(x) lt 2bh(x)
  • ?? 2 ????? ?????? ????? ?????? ???? ?? ??? x
    ?????? ????? ??? ??2bh(x)1.
  • ?? 3 ?????? ?????? ?? ???? RBT ?? n ???? ?????
    ??? ?? 2 log (n1)
  • ?????
  • ?? ??? , n ? 2bh 1
  • ?? ??? bh ?h/2 ? n ? 2h/2 1
  • ? h ? 2 lg(n 1).

11
?????? ??? RBT
  • ?????? BST ? ???? ?????? ?? O (log n)????? ?????
    ?????
  • ?????? ????? ?? ???? ?? ????? ???? ?????? ?????
    ??? BST ????? ?? ?????
  • ?????? ??? ????? ? ?????? ??? ?? ?????? ??????
    ???? ??? ????? RBT ??????? ?????

12
Left Rotation
P(x)
P(x)


A
A
a
a
B
ß
?
13
Left Rotation
P(x)
P(x)


A
A
B
a
a
B
ß
?
ß
?
14
Left Rotation


? Left Rotation ? Right Rotation
A
a
B
ß
?
15
Left Rotation Example
16
Insert a node
11
2
14
1
7
15
5
8
4
Insert (4) Tree color violation
17
Insert a node
11
2
14
1
7
15
5
8
yright(p(p(z))
if colory RED then colorpz ?
BLACK colory ? BLACK colorppz ? RED z ?
ppz
4
z
FIX-UP Color violation
18
Insert a node
11
14
15
z
5
if z rightpz then z ? pz LEFT-ROTATE(T,
z) colorpz ? BLACK colorppz ?
RED RIGHT-ROTATE(T, ppz)
4
FIX-UP Color violation
19
Insert a node
11
14
7
15
z
8
5
if z rightpz then z ? pz LEFT-ROTATE(T,
z) colorpz ? BLACK colorppz ?
RED RIGHT-ROTATE(T, ppz)
4
FIX-UP Color violation
20
Insert a node
7
z
11
14
5
8
15
4
if z rightpz then z ? pz LEFT-ROTATE(T,
z) colorpz ? BLACK colorppz ?
RED RIGHT-ROTATE(T, ppz)
FIX-UP Color violation
21
Delete a node
11
2
14
z
1
7
15
5
8
Delete Node 7
RB Tree Coloring violation
22
Delete Node
11
2
14
Delete Node z 7
z
1
7
15
y succssor(z)
5
8
x
nil
23
Delete Node
11
2
14
z
1
7
15
y successor(z)
5
8
x
nil
Copy Node 8 data to node 7
24
Delete Node
11
2
14
z
1
8
15
5
x
nil
If color(y) black, then fix-up color violation
at x
25
Delete Node
11
2
14
z
1
8
15
5
x
nil
??? x ?? ????? ??? ??? ??? ??? ?? ??? nil(T)
26
Delete Node
11
Delete Node z 2
z
2
14
1
7
15
5
8
y succssor(z)
x
nil
27
Delete Node
11
Delete Node z 2
z
2
14
1
7
15
5
8
y succssor(z)
x
nil
28
Delete Node
11
Delete Node z 2
z
5
14
1
7
15
8
x
nil
29
Delete Node
11
Delete Node z 5
z
5
14
y
1
7
15
x
8
nil
30
Delete Node
11
Delete Node z 5
z
7
14
y
1
7
15
x
8
nil
31
Delete Node
11
Delete Node z 5
z
7
14
y
1
7
15
x
8
nil
32
Delete Node
11
Delete Node z 5
z
7
14
y
1
15
x
8
nil
Color Violation
33
??? ????? RBT
  • ??? y (??? ??? ???) ???? ????? ?? ????? ???
    ????? RBT ??? ?? ???( x ???? ??? ?? ???? ?????
    y ???? ???. ??? y ?????? ?????? ????? x ??? nil
    ???? ???.)
  • y ???? ???? ???? ? ????? ??????? ??? ???? ??? ?
    x? ???? ????? ???? ??? ??? (??? 1)
  • p(y)p(x) ???? ??? ? x ,p(x) ?? ?? ?????? ( ???
    4)
  • ?? ????? ?? ???? ???? ??? y ???? ???? ?? ???
    ???? ???? ?? ???? ????( ??? 5)
  • ?? ??? ?? ???? ??? x ????? ?? ???? ???? ?????
    ???
  • ????? 1 ??? ?? ???
  • ?? ?? ??? ?????? ?????? ? ????? ???? ???? ????
    ??? ????? ?? ????

34
????? ????
  • ?????? ??? x ?? ???? ?????? ?? ???? ??
  • x ???? ???? ? ???? ???? ? ????? ???? ?? ??? ??
    ???
  • x ?? ???? ???? ? ????? ???? ????? ??? ?? ???
  • x ?????? ???? ? ???? ???? ? ?? ???? ??? ?????
    ???? ?? ?? ???? ?????? ????

35
Delete Node
  • y ??? ??? ??? ? x ???? ??? ?? ???? ????? y ????
    ???. ??? y ?????? ?????? ????? x ??? nil ????
    ???.
  • RB-DELETE-FIXUP(T, x)
  • while x ? rootT and colorx BLACK
  • do if x leftpx
  • then w ? rightpx
  • //Four different cases
  • else (same as then clause with "right" and "left"
    exchanged)
  • colorx ? BLACK

36
????? ??? ???? ??? ?? ??? ?? ???
  • RB-Delete-Fixup(T, x)
  • while x ? rootT and colorx BLACK
  • do if x leftpx
  • then w ? rightpx
  • if colorw RED
  • then colorw ? BLACK
    // Case 1
  • colorpx ? RED
    // Case 1
  • LEFT-ROTATE(T, px)
    // Case 1
  • w ? rightpx
    // Case 1

37
????? ??? - ?????
  • RB-Delete-Fixup(T, x) (Contd.)
  • / x is still leftpx /
  • if colorleftw BLACK and
    colorrightw BLACK
  • then colorw ? RED
    // Case 2
  • x ? px
    // Case 2
  • else if colorrightw BLACK
  • then colorleftw ?
    BLACK // Case 3
  • colorw ? RED
    // Case 3
  • RIGHT-ROTATE(T,w)
    // Case 3
  • w ? rightpx
    // Case 3
  • colorw ? colorpx
    // Case 4
  • colorpx ? BLACK
    // Case 4
  • colorrightw ? BLACK
    // Case 4
  • LEFT-ROTATE(T, px)
    // Case 4
  • x ? rootT
    // Case 4
  • else (same as then clause with right and
    left exchanged)
  • colorx ? BLACK

38
???? ??? w ???? ???
px
B
D
w
x
B
E
A
D
x
?
?
?
?
new w
A
C
C
E
?
?
?
?
?
?
?
?
  • ??????? w ???? ?????
  • ??? w? ?? p(x) ?? ???? ??? ?? ( ??? ???? p(x)
    ???? ???? ???! (????))
  • left rotate(T , p(x))
  • ????? ???? x ???? ???. ???? ??? ?? ??????? ?????
    w ???
  • ?????? ??? x? ????? ?? ???? ? ??? w ????? ?? ????
    ???
  • ??? ?? ????? 2- 4 ????? ?? ????

39
???? ??? w ? ???????? ???? ?????
px
new x
c
c
B
B
w
x
A
D
A
D
?
?
?
?
C
E
C
E
?
?
?
?
?
?
?
?
  • w ? ???????? ???? ?????
  • ?? ??? x ? w ?? ???? ?? ???? ? ?? ??? ??? ?? ??
    ???? ????? ??
  • ???? ??? x? ????? ?? ???? ??? ? ?? ??? ???? ???
  • p(x) ????? ?? ????? ???? ????? ??? ? ???????? ??
    ?? x ????? p(x) ????? ???
  • ??? p(x) ???? ???? ? ?? ???? ??? ?? ??? ???? ????
    ???
  • ??? p(x) ?? ???? ?? ? ???????? ????? ???? ?? ???

40
???? ??? w ? ????? ???? ???? ????? ? ????? ??
???? ???
c
c
B
B
w
x
new w
x
A
D
A
C
?
?
?
?
D
C
E
?
?
E
?
?
?
?
?
?
  • w ? ????? ???? ???? ????? ? ????? ?? ???? ???
  • ??? w? ? ????? ?? ?? ???? ??? ??
  • right rotate(w)
  • w ???? ??? ? ???? ????? ???? ?? ???? ??? ? ???
    ????? ???? ???? ???? ???!

41
???? ????? w ???? ? ????? ???? ?? ???? ???
c
B
D
w
x
B
E
A
D
x
?
?
?
?
A
C
c
C
E
?
?
?
?
?
?
?
?
  • ???? ????? w ???? ? ????? ???? ?? ???? ???
  • w ?? ????? p(x) ??? ??
  • p(x) ? ????? ???? w ?? ???? ??? ??
  • left rotate px.
  • ???? ????? ??? x ?? ??? ?? ? ????? ????????!
  • ???? ????? ???????? ? x ???? ?? ???? ???? ?????
    ?? ???!

42
?????? ????????
  • ??? ??? O ( log n )
  • ????? ????? ????
  • ???? ???? ??? ???? ??? ????? ???
  • ?? ?? ????? ??? x? ?? ??? ?? ???? ?????? ?? ???
  • ?????? ????? ??? ????? ?? ???????? ?????? ????
    ???
  • ????? ????? O ( log n )

43
??? ????
  • RBT ???? ?????? ?????? ?? ????? ????? ??? ???
  • ?? ?????? ???? RBT??? ???? ???? ?????? ????
    ?????? ?????? ?? ?? ????? O(log n) ????? ??? ??
    ?? ?? n ????? ?????? ???? ???
  • ?????? ??? ? ????? ???? ??? ?? ???? ???? ??? ???
    ??? ???? ???? RBT ????. ?? ??? ???? ???? ???? ??
    ?? ??? ???? ????? ???

44
?????
  • ????????? ??????? ?? ?? ???? ?????? ????? ??? ???
    ?? ????? ? ????? ??? ??? ??? ???? ?? ???? ???? ?
    ???? ??? ?? ??!
  • ??? ?? ?????? ????????? ?? ???? ??? ????? ???? ??
    ?? ???? ??????? ?? ?? ???? RBT ????? ??? ??????
    ?????? ???????? ??? ???? ?
Write a Comment
User Comments (0)
About PowerShow.com