Non-Linear Structures - Advanced Trees

advertisement

CS3424

AVL Trees

Red-Black Trees

Trees

• As stated before, trees are great ways of holding hierarchical data

• Insert, Search, Delete ~ O(lgN)

• But only if they’re balanced!

• So let’s discuss how to assure balance

Rotations

 x

 y

Left-Rotate(x)

Right-Rotate(y)

 x

 y

AVL Trees (1962)

• Uses height property to maintain balance

• Height of left & right differ by at most 1

• Uses rotations to restore balance after insertions and deletions

Balancing in AVL Trees

• Balance is the difference between the heights of the left and right subtrees

– This should range between -1 and 1

• Maintain this balance upon insert and delete

• Balance = H

R

- H

L

Left-Left Imbalance

Right-Rotate(X)

-1

Y

-2

X

Y

0

X

0

Left-Right Imbalance

+1

Y

-2

X

=

+1

Y

-1

-2

X

Left-Right Imbalance (cont)

+1

Y

-2

X

=

+1

Y

+1

-2

X

Left-Right Imbalance (

 left)

+1

Y

-1

-2

X

Left-Right Imbalance (

 left)

+1

Y

-1

-2

X

Left-Rotate(Y)

Y

-2

0

-2

X

Left-Right Imbalance (

 left)

-2

Y

0

-2

X

Right-Rotate(X)

0

Y

0

X

+1

Left-Right Imbalance (

 right)

+1

Y

+1

-2

X

Left-Right Imbalance (

 right)

+1

Y

+1

-2

X

Left-Rotate(Y)

-1

Y

-1

-2

X

Left-Right Imbalance (

 right)

-1

Y

-1

-2

X

Right-Rotate(X)

-1

Y

0 

X

0

Red-Black Trees (1972/1978)

• BST + notion of color (RED / BLACK)

• Every node is either red or black

• Root is black

• Every leaf (NIL) is black

• If node red, both children are black

• For each node, path to NIL children contains same number of black nodes ( black-height )

Possibilities in Growing a Red-

Black Tree

Painting a Tree to be

Red-Black

Painting a Tree to be

Red-Black

Root is black

Painting a Tree to be

Red-Black

Must be black bh=1 bh=1

Painting a Tree to be

Red-Black

Painting a Tree to be

Red-Black

Painting a Tree to be

Red-Black

Must be black

Painting a Tree to be

Red-Black

Must be red

Painting a Tree to be

Red-Black bh = 2 or 3

Inserting & Deleting in Red-

Black Trees

• Insert similar to binary search trees

– “Search” left & right until finding NIL

– But now, we need to worry about coloring

– Requires possible rotations & “cleanup”

• Deleting similar to BSTs as well

– Search left & right until finding item

– If we removed a black node, “cleanup”

Red-Black Insert

• Insert node (z)

• z.Color = red

– This can cause two possible problems:

• Root must be black (initial insert violates this)

• Two red nodes cannot be adjacent (this is violated if parent of z is red)

– Cleanup

Red-Black Cleanup

• y = z’s “uncle”

• Three cases:

– y is red

– y is black and z is a right child

– y is black and z is a left child

Not mutually exclusive

Case 1 – z’s Uncle is red

• y.Color = black

• z.Parent.Color = black

• z.Parent.Parent.Color = red

• z = z.Parent.Parent

• Repeat cleanup

Case 1 Visualized

11

2

1 7

5 8 y z

4

New

Node

14

15 y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent

repeat cleanup

Case 1 Visualized

11

2

1 7

5 8 y z

4

New

Node

14

15 y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent

repeat cleanup

Case 1 Visualized

11

2

1 7

5 8 y z

4

New

Node

14

15 y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent

repeat cleanup

Case 1 Visualized

11

2

1 7

5 8 y z

4

New

Node

14

15 y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent

repeat cleanup

Case 1 Visualized

11

2

1 z

7

5 8

4

New

Node

14 y

15 y.Color = black z.Parent.Color = black z.Parent.Parent.Color = red z = z.Parent.Parent

repeat cleanup

Case 2 – z’s Uncle is black & z is a right child

• z = z.Parent

• Left-Rotate(T, z)

• Do Case 3

• Note that Case 2 is a subset of Case 3

Case 2 Visualized

11

2

1 z

7

5 8

4

14 y

15 z = z.Parent

Left-Rotate(T,z)

Do Case 3

Case 2 Visualized

11 z 2

1 7

5 8

4

14 y

15 z = z.Parent

Left-Rotate(T,z)

Do Case 3

Case 2 Visualized

11

7

1 z 2

5

8

4

14 y

15 z = z.Parent

Left-Rotate(T,z)

Do Case 3

Case 3 – z’s Uncle is black & z is a left child

• z.Parent.Color = black

• z.Parent.Parent.Color = red

• Right-Rotate(T, z.Parent.Parent)

Case 3 Visualized

11

7

1 z 2

5

8

4

14 y

15 z.Parent.Color = black z.Parent.Parent.Color = red

Right-Rotate(T, z.Parent.Parent)

Case 3 Visualized

11

7

1 z 2

5

8

4

14 y

15 z.Parent.Color = black z.Parent.Parent.Color = red

Right-Rotate(T, z.Parent.Parent)

Case 3 Visualized

11

7

1 z 2

5

8

4

14 y

15 z.Parent.Color = black z.Parent.Parent.Color = red

Right-Rotate(T, z.Parent.Parent)

Case 3 Visualized

7

1 z 2

5

4

11

8 14 y

15 z.Parent.Color = black z.Parent.Parent.Color = red

Right-Rotate(T, z.Parent.Parent)

Analysis of Red-Black Cleanup

C New z C

A

 z B

D y

A D

B

 

 

Case 1

,

,

,

, and

 are all black-rooted and all have same black-height

(symmetric L/R on A & B)

Analysis of Red-Black Cleanup

C

A

 z B

 y

Case 2

C

B

 y

A z

 Case 3

Case 2

,

,

, and

 are all black-rooted and all have same black-height

(

 is black or this would be case 1)

Analysis of Red-Black Cleanup

C B

 y

A z

B

A z

 

C

 y

Case 3

,

,

, and

 are all black-rooted and all have same black-height

Questions about

Red-Black Insert

• What is the running time?

• Why are there only at most 2 rotations in the cleanup?

• How many times can the cleanup repeat (i.e. how many times can we have case 1 in a row)?

Red-Black Delete

• Delete as before

• But if you take away a black node:

– Black height is not “balanced”

– Can generate adjacent red nodes

– Must perform cleanup

• 4 cases

• “Beyond the scope of this course…”

FIN

Download