Docsity

2-3 Trees - Algorithms and Abstract Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download 2-3 Trees - Algorithms and Abstract Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 21:

Red-black tree

2-3 trees

Today …

• Red-Black Trees (cont.)

• 2-3 Trees (if time) [Ch 12: 651-670]

CPSC 223 -­‐-­‐ Fall 2010

Balance Constraints

1. Every node is assigned a color (either Red or Black )

2. The root is always Black

3. The children of a Red node are always Black

4. Every path from the root to a NULL node has the

same number of Black nodes

CPSC 223 -­‐-­‐ Fall 2010

B
A C

NULL NULL NULL NULL

Black Nodes (B) = 2

F
D H
B E

NULL NULL

A

G

NULL NULL

C

NULL NULL

NULL NULL

I

NULL NULL

Inserting Nodes (Bottom Up)

If the inserted node’s parent is Red …

  • We have to consider several cases
    • Let …

X = inserted node

P = parent

S = sibling of P

G = grandparent of X

  • Note that G must be Black!
  • If S is Red
  • If S is Black
  • If X is an outside grandchild
  • If X is an inside grandchild

CPSC 223 -­‐-­‐ Fall 2010

G
P S
X C D E

S could be

Red or Black

B
A

NOTE: Because we are possibly

propagating up, we consider the

general case where X may have

subtrees A and B

Inside Grandchildren

If S is Black and X is an inside grandchild

  • “ Inside ” grandchild means X is left-right or right-left
  • Do a double rotation
  • Change X to Black and G to Red
  • We are done rebalancing

CPSC 223 -­‐-­‐ Fall 2010

X
G
C
P
S
E
D
G
P S
D E
C
B
A B
X
G
C
P
S
E
D
A B
A X

rota)ons

re-­‐color

Inserting Nodes Top Down

  • We want to avoid having to rotate “up” the tree
    • We don’t need to when S is Red
  • To avoid iterating back up the tree, we make sure that

when we insert a node, the sibling S is not Red

  • One the way down
    • As we see a node X with two Red children …
    • we change X to Red and its children to Black (color flip)
    • If X and X’s parent are Red, we apply single or double Rotation
    • While this increases the total number of Black nodes … it does

not change the number of Black nodes on a path

CPSC 223 -­‐-­‐ Fall 2010

Inserting Nodes Top Down

  • For example, let’s insert “9” into this tree

CPSC 223 -­‐-­‐ Fall 2010

Start Here
Traverse Here

Inserting Nodes Top Down

  • For example, let’s insert “9” into this tree

CPSC 223 -­‐-­‐ Fall 2010

Now we need to do a
Single Rotation at “30” …
Traverse
Here
“10” is Black and both
children are Red
So we flip colors

Removing Nodes

  • Simple cases:
    • If the node is Red, we can just remove it
    • If it is Black with a Red child … remove it and change the

child’s color to Black

CPSC 223 -­‐-­‐ Fall 2010

X
N
P
Remove X
NOTE: P must be Black
N
P
X
N
P
N
P
Remove X
Change N to Black

Removing Nodes

  • The complicated case
    • The node X (to be deleted) and its child are Black
    • In this case, we end up with one fewer Black node on a path …

and so the tree must be re-balanced

  • First, delete X and replace with inorder successor N
  • Now we need to rebalance N

CPSC 223 -­‐-­‐ Fall 2010

X
N
P
Remove X
(the inorder successor)
S
A B
N
P
A B
L R
S
L R

Removing Nodes – Bottom Up

  • Case A : If N is the new root of the tree we are done
  • Case B : The sibling S is Red
    • Flip colors of P and S
    • Rotate left at P
    • Now rebalance N with Cases D, E, or F

CPSC 223 -­‐-­‐ Fall 2010

N
P
A B
S
L R
S
L
R
N
A B
P
Now N has a Black sibling
and Red parent … so we
go to cases 3-5 at N

Removing Nodes – Bottom Up

  • Case C : if parent P and sibling S and S’s children are Black
    • Change S to Red
    • All paths through P now have one less Black node
    • So we now have to re-balance at P !!!

CPSC 223 -­‐-­‐ Fall 2010

N
P
A B
S
L R
N
P
A B
S
L R

Removing Nodes – Bottom Up

  • Case F : If the sibling S is Black and S’s right child is Red
    • Change S to the color of P
    • Change P to Black
    • Change the right child to Black
    • Rotate P to the left
    • N now has an additional Black node as an ancestor

CPSC 223 -­‐-­‐ Fall 2010

N
P
A B
S
L R
N
P
A B
S
L
R
We are done
rebalancing …