5 Rules 1 Red Black Tree Properties - A 1. Every Node Is Either - - PowerPoint PPT Presentation

5 rules
SMART_READER_LITE
LIVE PREVIEW

5 Rules 1 Red Black Tree Properties - A 1. Every Node Is Either - - PowerPoint PPT Presentation

Red Black Tree 5 Rules 1 Red Black Tree Properties - A 1. Every Node Is Either RED or BLACK 2. Every NILL Node Is BLACK sometimes referred to as a "Null Leaf" Red Black Tree Properties - B 3. Every RED Node Has Two Black Child


slide-1
SLIDE 1

Red Black Tree 5 Rules

1

slide-2
SLIDE 2

Red Black Tree Properties - A

  • 1. Every Node Is Either RED or BLACK
  • 2. Every NILL Node Is BLACK  sometimes referred to as a "Null Leaf"
slide-3
SLIDE 3

Red Black Tree Properties - B

  • 3. Every RED Node Has Two Black Child Nodes (Or Is Childless)  There

Can't Be Two RED Nodes In A Row

  • 4. Every Distance Path From Node X Down To A Leaf Node Has The Same

Number Of BLACK Nodes

  • 5. The Root Node Is Always BLACK
slide-4
SLIDE 4

Rotation Generalization Right Rotation

4

slide-5
SLIDE 5

Right Rotation - 1/2

  • There Exists Some Square Node

(somewhere in the tree)

  • There Exists Some Circle Node

(Where Value In Circle Is Less Than The Value In Square)

  • There Exists Some Subtrees:

A, B, & C A B C

  • Putting Values In Nodes Is Not

Necessary, But Since I Did It To Demonstrate The AVL Rotations, I Will Do It For The Red Black.

slide-6
SLIDE 6

Right Rotation - 2/2

  • Simple Way To Rotate The Valid Binary

Search Tree Without Altering The Search Capabilities.

  • Single Rotate Right  AVL

A B C 100 90 90 A C 100 B

slide-7
SLIDE 7

Rotation Generalization Left Rotation

7

slide-8
SLIDE 8

Left Rotation - 1/2

  • There Exists Some Square Node

(somewhere in the tree)

  • There Exists Some Circle Node

(Where Value In Circle Is Greater Than The Value In Square)

  • There Exists Some Subtrees:

A, B, & C A B C

  • Putting Values In Nodes Is Not

Necessary, But Since I Did It To Demonstrate The AVL Rotations, I Will Do It For The Red Black.

slide-9
SLIDE 9

Left Rotation - 2/2

  • Simple Way To Rotate The Valid Binary Search

Tree Without Altering The Search Capabilities.

  • Single Rotate Left  AVL

A 90 100 C B A B C 90 100

slide-10
SLIDE 10

Violation 1L RED UNCLE Case

(On Left)

10

slide-11
SLIDE 11

Easy Case 1L - RED UNCLE Case - 1 (P On Left)

  • The Color Distance Will Not

Change The Number of Black Down Any Branch

  • If We Make The New Node RED, It

Minimizes The Number Of Bad Things That Can Happen:

  • Only Thing That Can Go Wrong Is

Two Red Nodes In A Row

50 150 125 100

  • Label P  Pf  Pg  Pu

P Pf Pg Pu

  • This RED UNCLE Case Is

Occurring As We Walk Our Way Up The Tree.

  • Label The Distance Paths

c d e f b

a

slide-12
SLIDE 12

Easy Case 1L - RED UNCLE Case - 2 (P On Left)

  • Easy Case 1L: UNCLE is RED  No Rotation Required!

50 150 125 100

c

  • ac = 1

d

  • ad = 1

e

  • ae = 1

f

  • af = 1
  • ab = 1

b

a

  • Record The Relative Distances Down

The Various Paths In The Tree:

slide-13
SLIDE 13

Easy Case 1L - RED UNCLE Case - 3 (P On Left)

  • Easy Case 1L: UNCLE is RED  No Rotation Required!

50 150 125 100

c d e f b

a

  • How Fix?

P Pf Pg Pu

  • ab = 1
  • ac = 1
  • ad = 1
  • ae = 1
  • af = 1
  • Pf  Color = BLACK Pu  Color = BLACK Pg  Color = RED

50 150 125 100

c d e f b

a

P Pf Pg Pu

  • Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1  YEA!
slide-14
SLIDE 14

Case 1L Rule Summary

50 150 125 100

c d e f b

a

P Pf Pg Pu

50 150 125 100

c d e f b

a

P Pf Pg Pu

slide-15
SLIDE 15

Violation 1R RED UNCLE Case

(On Right)

15

slide-16
SLIDE 16

Easy Case 1R - RED UNCLE Case - 1 (P On Right)

  • The Color Distance Will Not

Change The Number of Black Down Any Branch

  • If We Make The New Node RED, It

Minimizes The Number Of Bad Things That Can Happen:

  • Only Thing That Can Go Wrong Is

Two Red Nodes In A Row

50 200 150 125

  • Label P  Pf  Pg  Pu

P Pf Pg Pu

  • This RED UNCLE Case Is

Occurring As We Walk Our Way Up The Tree.

  • Label The Distance Paths

c d e f b

a

slide-17
SLIDE 17

Easy Case 1R - RED UNCLE Case - 2 (P On Right)

  • Easy Case 1R: UNCLE is RED  No Rotation Required!
  • ac = 1
  • ad = 1
  • ae = 1
  • af = 1
  • ab = 1
  • Record The Relative Distances Down

The Various Paths In The Tree:

50 200 150 125

  • Label P  Pf  Pg  Pu

P Pf Pg Pu

  • Label The Distance Paths

c d e f b

a

slide-18
SLIDE 18

Easy Case 1R - RED UNCLE Case - 3 (P On Right)

  • Easy Case 1L: UNCLE is RED  No Rotation Required!
  • How Fix?
  • ab = 1
  • ac = 1
  • ad = 1
  • ae = 1
  • af = 1
  • Pf  Color = BLACK Pu  Color = BLACK Pg  Color = RED
  • Double Check The Distances ab=1, ac=1, ad=1, ae=1, af=1  YEA!

50 200 150 125

c d e f b

a

P Pf Pg Pu

50 200 150 125

c d e f b

a

P Pf Pg Pu

slide-19
SLIDE 19

Case 1R Rule Summary

50 200 150 125

c d e f b

a

P Pf Pg Pu

50 200 150 125

c d e f b

a

P Pf Pg Pu

slide-20
SLIDE 20

RED Uncle Can Occur With Insertion RED UNCLE Case

(On Left)

20

slide-21
SLIDE 21
  • Distance Property = 2 For All - If Count Null

Easy Case 1L - RED UNCLE Case - 1 (P On Left)

  • Easy Case 1L: UNCLE is RED  No Rotation Required!

200 250

  • Add 50 to Tree - Make it Red
  • Will Not Change The Number of

Black Down Any Branch

  • Minimize The Number Of Bad

Things That Can Happen:

  • Only Thing That Can Go Wrong Is

Two Red Nodes In A Row

50 150 125 100

  • Label P  Pf  Pg  Pu

P Pf Pg Pu

slide-22
SLIDE 22

Easy Case 1L - RED UNCLE Case - 2 (P On Left)

  • Easy Case 1L: UNCLE is RED  No Rotation Required!

200 250 50 150 125 100

  • How Fix?
  • THIS IS EASY CASE - Pu Is Red

(125)

  • Might Become Easier If We Don't

Try To Fix Everything At Once Start With This Portion

  • Make Pg RED

Make Pf BLACK Make Pu BLACK

  • This Shifts The Problem Up Two

Generations! P Pg Pf Pu

slide-23
SLIDE 23

Easy Case 1L - RED UNCLE Case - 3 (P On Left)

200 250 50 150 125 100

  • How Fix?
  • How Fix?
  • Change Pf To BLACK
  • 2 BLACK UNCLE Cases: One Requires A Rotation

One Requies A Preliminary Rotation And A Second Rotation

  • Distance Property = 3 For All

P Pf

slide-24
SLIDE 24

RED Uncle Can Occur With Insertion Add To Tree Violation 1R RED UNCLE Case

(On Right)

24

slide-25
SLIDE 25
  • Distance Property = 2 For All If Count Null

Easy Case 1R - RED UNCLE Case - 1 (P On Right)

  • Easy Case 1R: UNCLE is RED  No Rotation Required!

100 50 150 200

  • Add 225 to Tree - Make it Red
  • Will Not Change The Number of

Black Down Any Branch

  • Minimize The Number Of Bad

Things That Can Happen:

  • Only Thing That Can Go Wrong Is

Two Red Nodes In A Row

125 225

  • Label P  Pf  Pg  Pu

P Pf Pg Pu

slide-26
SLIDE 26

Easy Case 1R - RED UNCLE Case - 2 (P On Right)

100 50 150 200

  • How Fix?
  • THIS IS EASY CASE - Uncle Of P

Is Red (125)

  • Might Become Easier If We Don't

Try To Fix Everything At Once Start With This Portion

  • Make Pg RED

Make Pf BLACK Make Pu BLACK

125 225

  • This Shifts The Problem Up Two

Generations! Pg Pf P Pu

slide-27
SLIDE 27

Easy Case 1R - RED UNCLE Case - 3 (P On Right)

100 50 150 200

  • How Fix?
  • Change Pf To BLACK

125 225

  • 2 BLACK UNCLE Cases: One Requires A Rotation

One Requies A Preliminary Rotation And A Second Rotation

  • Distance Property = 3 For All

Pf P

slide-28
SLIDE 28

Violation 3L BLACK UNCLE Case

With One Rotation

(On Left)

28

slide-29
SLIDE 29

Case 3L - BLACK UNCLE One Rotation Class- 1 (P On Left)

200 250

  • This BLACK UNCLE Case Is

Occurring As We Walk Our Way Up The Tree.

  • We Have Already Established That

The Distance Property Is OK

125 100

  • ab = 1

a b c

  • ac = 1

d

  • ad = 1

e

  • ae = 2

f

  • af = 2
  • GOAL: Fix The RED CHILD With RED

Father Problem Without Messing Up The Distance Properties.

slide-30
SLIDE 30

Case 3L - BLACK UNCLE One Rotation Class- 2 (P On Left)

200 250

  • Solve RED RED Problem?

125 100

a b c d e f

  • ab = 1
  • ac = 1
  • ad = 1
  • ae = 2
  • af = 2
  • Label P  Pf  Pg  Pu

P Pf Pg Pu b c P Pf 125

100

  • Promote Pf Up

a

200 250

e f Pg Pu

  • Hook Pg Right of Pf

d

  • Hook up 'd'
  • Swap Colors Pg Pf
  • Double Check The Distances ab=1, ac=1, ad=1, ae=2, af=2  YEA!
slide-31
SLIDE 31

Case 3L Rule Summary f

200 250 125 100

a b c d e P Pf Pg Pu P Pu b c Pf 125

100

a

250

e f Pg d

200

slide-32
SLIDE 32

Violation 3R BLACK UNCLE Case

With One Rotation

(On Right)

32

slide-33
SLIDE 33

Case 3R - BLACK UNCLE One Rotation Class- 1 (P On Right)

200 250

  • This BLACK UNCLE Case Is

Occurring As We Walk Our Way Up The Tree.

  • We Have Already Established That

The Distance Property Is OK

125 300

  • ab = 2

a b c

  • ac = 2

d

  • ad = 1

e

  • ae = 1

f

  • af = 1
  • GOAL: Fix The RED CHILD With RED

Father Problem Without Messing Up The Distance Properties.

slide-34
SLIDE 34
  • Hook Pg Left of Pf

200 125

b c Case 3R - BLACK UNCLE One Rotation Class- 2 (P On Right)

200

  • Solve RED RED Problem?

125

a b c

250 300

d e f

  • ab = 2
  • ac = 2
  • ad = 1
  • ae = 1
  • af = 1
  • Label P  Pf  Pg  Pu

P Pf Pg Pu a

  • Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1  YEA!
  • Promote Pf Up

250 300

e f P Pf

  • Hook up 'd'

d

  • Swap Colors Pg Pf
slide-35
SLIDE 35

Case 3R Rule Summary

200 125

a b c

250 300

d e f P Pf Pg Pu

200 125

b c a

250 300

e f P Pf

slide-36
SLIDE 36

Violation 2L BLACK UNCLE Case

With Two Rotations

(On Left)

36

slide-37
SLIDE 37

Case 2L - BLACK UNCLE Two Rotations Class- 1 (P On Left)

200 250 125 150

  • Label P  Pf  Pg  Pu

P

Pf Pg Pu

  • This BLACK UNCLE Case Is

Occurring As We Walk Our Way Up The Tree.

  • We Have Already Established That

The Distance Property Is OK c

  • ac = 1

d

  • ad = 1

e

  • ae = 2

f

  • af = 2

a b

  • ab = 1
  • Case 2L is going to turn into Case 3L.
  • This Is The Worst Possible Case
slide-38
SLIDE 38

Case 2L - BLACK UNCLE Two Rotations Class- 2 (P On Left)

200 250 100 125

  • ab = 1
  • ac = 1
  • ad = 1
  • ae = 2
  • af = 2

P

Pf Pg Pu c d e f a b

  • Might Become Easier If We Don't

Try To Fix Everything At Once Start With This Portion

  • Hook P To The Left Of Pg

125 P

d

200 250

Pg Pu e f a

1

100

Pf b

  • Hook Pf To The Left Of P

c

  • Hook up 'c'
  • This Is Case 3L
slide-39
SLIDE 39

Case 2L - BLACK UNCLE Two Rotations Class- 3 (P On Left)

  • ab = 1
  • ac = 1
  • ad = 1
  • ae = 2
  • af = 2
  • Swap Pointers Pf  P

1

125 Pf

d

200 250

Pg Pu e f a

100

P b c

slide-40
SLIDE 40

Case 2L - BLACK UNCLE Two Rotations Class- 4 (P On Left)

  • ab = 1
  • ac = 1
  • ad = 1
  • ae = 2
  • af = 2
  • Swap Pointers Pf & P

2

125 Pf

d

200 250

Pg Pu e f a

100

P b c b c P Pf 125

100

  • Do Case 3L Rotation

200 250

e f Pg Pu d a

  • Double Check The Distances ab=1, ac=1, ad=1, ae=2, af=2  YEA!
slide-41
SLIDE 41

Case 2L Rule Summary

200 250 125 150 P

Pf Pg Pu c d e f a b b c P Pf 125

100 200 250

e f Pg Pu d a

slide-42
SLIDE 42

Violation 2R BLACK UNCLE Case

With Two Rotations

(On Right)

42

slide-43
SLIDE 43

Case 2R- BLACK UNCLE Two Rotations Class- 1 (P On Right)

  • This BLACK UNCLE Case Is

Occurring As We Walk Our Way Up The Tree.

  • We Have Already Established That

The Distance Property Is OK c

  • ac = 2

d

  • ad = 1

e

  • ae = 1

f

  • af = 1

a b

  • ab = 2
  • Case 2R is going to turn into Case 3R.
  • This Is The Worst Possible Case

200 125

a

300 250 P

Pf Pg Pu

slide-44
SLIDE 44

Case 2R - BLACK UNCLE Two Rotations Class- 2 (P On Right)

  • ab = 2
  • ac = 2
  • ad = 1
  • ae = 1
  • af = 1
  • Might Become Easier If We Don't

Try To Fix Everything At Once Start With This Portion

1

  • This Is Case 3L

c d e f a b

200 125

a

300 250 P

Pf Pg Pu

  • Hook P To The Right Of Pg

d

250 P

c a b

200 125

Pg Pu

  • Hook up 'e'

e

  • Hook Pf To The Right Of P

f

300

Pf

slide-45
SLIDE 45

Case 2R - BLACK UNCLE Two Rotations Class- 3 (P On Right) d

250 P

c a b

200 125

Pg Pu e f

300

Pf

  • Swap Labels Pf  P
  • This Is Case 3L
  • ab = 2
  • ac = 2
  • ad = 1
  • ae = 1
  • af = 1
slide-46
SLIDE 46

Case 2R - BLACK UNCLE Two Rotations Class- 4 (P On Right)

  • Swap Labels Pf  P

2

  • Double Check The Distances ab=2, ac=2, ad=1, ae=1, af=1  YEA!
  • ab = 2
  • ac = 2
  • ad = 1
  • ae = 1
  • af = 1

d

250 P

c a b

200 125

Pg Pu e f

300

Pf

  • Do Case 3R Rotation

200 125

b c

250 300

e f P Pf d a

slide-47
SLIDE 47

Case 2R Rule Summary c d e f a b

200 125

a

300 250 P

Pf Pg Pu

200 125

b c

250 300

e f P Pf d a

slide-48
SLIDE 48

Rotation Generalization Right Rotation

48

slide-49
SLIDE 49
slide-50
SLIDE 50

Practice Problem #1

50

slide-51
SLIDE 51

#1 Practice Red Black Insertion  Add 10 to Empty Tree Some folks draw in the NULL Leaves - I do not! Some folks code this with an empty node to represent the Leaves

  • I do not!

Label P  Pf  Pg  Pu

P Pf = / Pg = / Pu = /

slide-52
SLIDE 52

Practice Problem #2

52

slide-53
SLIDE 53

#2 Practice Red Black Insertion  Add 5 Tee Is There A Need To Do Anything To Preserve The Red Black Tree? No Label P  Pf  Pg  Pu

P Pf Pg = / Pu = /

slide-54
SLIDE 54

Practice Problem #3

54

slide-55
SLIDE 55

#3 Practice Red Black Insertion  Add 15 Tee Is There A Need To Do Anything To Preserve The Red Black Tree? No Label P  Pf  Pg  Pu

P Pf Pg = / Pu = /

slide-56
SLIDE 56

Practice Problem #4

56

slide-57
SLIDE 57

#4 Practice Red Black Insertion  Add 15 Tree

 Is There A Need To Do Anything To Preserve The Red Black Tree?

No

Label P  Pf  Pg  Pu

P Pf Pg = / Pu = /

slide-58
SLIDE 58

Practice Problem #5

58

slide-59
SLIDE 59

#5 Practice Red Black Insertion  Add 5 Tree

 Is There A Need To Do Anything To Preserve The Red Black Tree?

No

Label P  Pf  Pg  Pu

P Pf Pg = / Pu = /

slide-60
SLIDE 60

Practice Problem #6

60

slide-61
SLIDE 61

#6 Practice Red Black Insertion  Add 3 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

Label P  Pf  Pg  Pu

P Pf Pg Pu = /

Simple Rotate Right [ Pg Around Pf ]

P Pf Pg

What Do Now?

slide-62
SLIDE 62

#6 Practice Red Black Insertion  Add 3 - 2

P Pf Pg

Color Change (Pf & Pg)

Pg Pf P

  • AVL  Simple Rotate Right

What Do Now?

slide-63
SLIDE 63

Practice Problem #7

63

slide-64
SLIDE 64

#7 Practice Red Black Insertion  Add 20 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

Label P  Pf  Pg  Pu

P Pf Pg Pu = /

Simple Rotate Left [ Pg Around Pf ]

P Pf Pg

What Do Now?

slide-65
SLIDE 65

#7 Practice Red Black Insertion  Add 20 - 2

P Pf Pg P Pf Pg

Color Change (Pf & Pg)

  • AVL  Simple Rotate Left

What Do Now?

slide-66
SLIDE 66

Practice Problem #8

66

slide-67
SLIDE 67

#8 Practice Red Black Insertion  Add 8 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree? Yes

Label P  Pf  Pg  Pu

P Pf Pg Pu = /

Rotate Pf & P To The Left Around Pf

Pg Pf P

What Do Now?

slide-68
SLIDE 68

#8 Practice Red Black Insertion  Add 8 - 2 Color Change (Pf & Pg)

Pg Pf P

Simple Rotate Right [ Pg Around Pf ]

Pg Pf P Pg Pf P

What Do Now? What Do Now?

  • AVL  Simple Double Rotate Right

Pg P Pf

Swap Pointers Pf & P What Do Now?

slide-69
SLIDE 69

Practice Problem #9

69

slide-70
SLIDE 70

#9 Practice Red Black Insertion  Add 8 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree? Yes

Label P  Pf  Pg  Pu

P Pf Pg Pu = /

Rotate Pf & P To The Right Around Pf

Pg P Pf

What Do Now?

slide-71
SLIDE 71

#9 Practice Red Black Insertion  Add 8 - 2 Color Change (Pf & Pg) Simple Rotate Left[ Pg Around Pf ]

P Pf Pg P Pf Pg

What Do Now? What Do Now?

  • AVL  Simple Double Rotate Left

Pg P Pf Pg Pf P

What Do Now? Swap Pointers Pf & P

slide-72
SLIDE 72

Practice Problem #10

72

slide-73
SLIDE 73

#10 Practice Red Black Insertion  Add 5 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

Label P  Pf  Pg  Pu

P Pf Pg Pu

What Do Now? Violation 1L

slide-74
SLIDE 74

#10 Practice Red Black Insertion  Add 5 - 2

P Pg Pf Pu

Fix Violation 1L

P Pg Pf Pu

Make Tree Root BLACK What Do Now?

P Pg Pf Pu

slide-75
SLIDE 75

Practice Problem #11

75

slide-76
SLIDE 76

#11 Practice Red Black Insertion  Add 15 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

Label P  Pf  Pg  Pu

P Pf Pg Pu

What Do Now? Violation 1R

slide-77
SLIDE 77

Fix Violation 1R #11 Practice Red Black Insertion  Add 15 - 2

P Pf Pg Pu P Pf Pg Pu

Make Tree Root BLACK

P Pf Pg Pu

What Do Now?

slide-78
SLIDE 78

Practice Problem #12

78

slide-79
SLIDE 79

#12 Practice Red Black Insertion  Add 25

 Is There A Need To Do Anything To Preserve The Red Black Tree?

No

P Pg Pf Pu

Label P  Pf  Pg  Pu

slide-80
SLIDE 80

Practice Problem #13

80

slide-81
SLIDE 81

#13 Practice Red Black Insertion  Add 8 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

P Pg Pf Pu = /

Label P  Pf  Pg  Pu

Pf Pg P

Simple Rotate Right Pg Around Pf What Do Now?

slide-82
SLIDE 82

#13 Practice Red Black Insertion  Add 8 - 2

Pf Pg P Pf Pg P

Swap Pointers Pf & P What Do Now?

slide-83
SLIDE 83

#13 Practice Red Black Insertion  Add 8 - 3 Simple Rotate Right PB Around Pf

Pf Pg P

What Do Now?

Pf Pg P

slide-84
SLIDE 84

#13 Practice Red Black Insertion  Add 8 - 4

Pf Pg P

What Do Now? Color Change (Pf & Pg)

Pf Pg P

slide-85
SLIDE 85

Practice Problem #14

85

slide-86
SLIDE 86

#14 Practice Red Black Insertion  Add 8 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree? Yes P Pg Pf Pu = /

Label P  Pf  Pg  Pu

Pf Pg P

Rotate P & Pf To The Left Around Pf What Do Now?

slide-87
SLIDE 87

#14 Practice Red Black Insertion  Add 8 - 2

Pf Pg P P Pg Pf

Swap Pointers Pf & P What Do Now?

slide-88
SLIDE 88

#14 Practice Red Black Insertion  Add 8 - 3 Simple Rotate Right Pg Around Pf Color Change (Pf & Pg)

P Pg Pf

What Do Now?

P Pg Pf

What Do Now?

P Pg Pf

slide-89
SLIDE 89

Practice Problem #15

89

slide-90
SLIDE 90

#15 Practice Red Black Insertion  Add 9 - 1

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

P Pg Pf Pu

Label P  Pf  Pg  Pu

slide-91
SLIDE 91

#15 Practice Red Black Insertion  Add 9 - 2

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

P Pg Pf Pu

Label P  Pf  Pg  Pu Violation 1R

slide-92
SLIDE 92

Practice Problem #16

92

slide-93
SLIDE 93

#16 Practice Red Black Insertion  Add 13 - 1

P Pg Pf Pu

Label P  Pf  Pg  Pu Violation 1R

 Is There A Need To Do Anything To Preserve The Red Black Tree?

Yes

What Do Now?

slide-94
SLIDE 94

#16 Practice Red Black Insertion  Add 13 - 2

P Pg Pf Pu

Apply Violation 1R

slide-95
SLIDE 95

#16 Practice Red Black Insertion  Add 13 - 3 What Do Now?

P Pg Pf Pu P Pg Pf Pu

Move Potential Problem (focus) Up Two Levels

slide-96
SLIDE 96

#16 Practice Red Black Insertion  Add 13 - 4 What Do Now?

P Pg Pf Pu

Rotate P & Pf To The Left Around Pf

P Pg Pf Pu

slide-97
SLIDE 97

#16 Practice Red Black Insertion  Add 13 - 5 What Do Now? Swap Pointers P & Pf

P Pg Pf Pu P Pg Pf Pu

slide-98
SLIDE 98

#16 Practice Red Black Insertion  Add 13 -6 What Do Now? Fix Violation 3L

P Pg Pf Pu P Pg Pf Pu

Note That Pf-R Goes To Pg-L

slide-99
SLIDE 99
slide-100
SLIDE 100

Rotation Generalization Right Rotation

100