# CS350: Data Structures Red-Black Trees · Red-Black Tree Insertion (Bottom-Up) • Insert nodes...

date post

21-Aug-2020Category

## Documents

view

1download

0

Embed Size (px)

### Transcript of CS350: Data Structures Red-Black Trees · Red-Black Tree Insertion (Bottom-Up) • Insert nodes...

CS350: Data Structures © James Moscola

College Catalog2009–2011

!"#$%&'())*+,-.)/.&01234546708.9:;*&&[email protected]/**/")*&&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI&[email protected]/**/")*&&1015?2D50633

05?3352775?30?EEEF+C:F(A;

!""#$%%&'$#()*$&+$,-$%.$"

'GHI

CS350: Data Structures

Red-Black Tree

• An alternative to AVL trees

• Insertion can be done in a bottom-up or a top-down fashion - An AVL tree uses a pass down the tree for an insertion and a second

pass back up the tree to update node heights and potentially rebalance the tree

- A top-down insertion into a red-black tree requires only a single pass down the tree

• We’ll focus on bottom-up insertion

2

CS350: Data Structures

Red-Black Tree (Cont.)

• A red-black tree is a binary search tree that has the following properties: (1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null node must contain the same

number of black nodes

• These properties must be maintained after each insertion or deletion operation

• A null node is considered black

3

CS350: Data Structures

Example Red-Black Tree

4

80 9050

40 55

655

85602010

15 70

30

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

CS350: Data Structures

• When inserting a new leaf node, what color should it be? insert(24)

Red-Black Tree Insertion

5

80 9050

40 55

655

85602010

15 70

30

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

24

CS350: Data Structures

• Inserting as a black node violates property #4

Red-Black Tree Insertion

6

80 9050

40 55

655

85602010

15 70

30

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

24

CS350: Data Structures

• Inserting as a red node satisfies all four properties (in this case)

Red-Black Tree Insertion

7

80 9050

40 55

655

85602010

15 70

30

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

24

CS350: Data Structures

• Inserting as a red may not always satisfy the four properties insert(99)

Red-Black Tree Insertion

8

80 9050

40 55

655

85602010

15 70

30

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

24

99

CS350: Data Structures

Red-Black Tree Insertion

• Easier to fix a violation of property #3 than it is to fix a violation of property #4 - So, insert all nodes as red nodes

• Must repair violations of property #3 and any new violations that occur as a result of the tree modification - Operations for repairing the tree include:

• Single and Double Rotations (similar to AVL trees)

• Color changes

9

CS350: Data Structures

Red-Black Tree Insertion (Bottom-Up)

• Insert nodes into a red-black tree using the standard binary search tree insertion - Make the newly inserted node red

- If the parent of the newly inserted node is black, then no violations

have occurred and the insertion is complete

- If the parent of the newly inserted node is red, then property #3 has

been violated and must be fixed through rotations and recoloring

• Four different cases must be considered

10

CS350: Data Structures

Red-Black Tree Insertion -- Violations

• The four cases to consider when property #3 is violated (i.e. when a red node is inserted as the child of another red node) (1) Parent’s sibling is red and new node is inserted as an outside

grandchild

(2) Parent’s sibling is red and new node is inserted as an inside

grandchild

(3) Parent’s sibling is black and new node is inserted as an outside

grandchild

(4) Parent’s sibling is black and new node is inserted as an inside

grandchild

• There is a different approach to fix each of these cases

11

CS350: Data Structures

P

X

Insert Violation -- Case #1

12

G

S

ED

BA

C

(1) Parent’s sibling is red and new node is inserted as an outside grandchild

CS350: Data Structures

G

Fixing the Insertion Violation -- Case #1

13

P

X

S

ED

BA

C

P

X

G

S

ED

BA

C

No rotation necessary Make Parent and Sibling black, Grandparent becomes red

Since G becomes red, more work might be

needed further up the tree

CS350: Data Structures

X

P

Insert Violation -- Case #2

14

G

S

ED

(2) Parent’s sibling is red and new node is inserted as an inside grandchild

A

CB

CS350: Data Structures

G G

Fixing the Insertion Violation -- Case #2

15

X

P S

EDA

CB

X

P S

EDA

CB

No rotation necessary Make Parent and Sibling black, Grandparent becomes red

Since G becomes red, more work might be

needed further up the tree

CS350: Data Structures

P

X

Insert Violation -- Case #3

16

G

S

ED

BA

C

(3) Parent’s sibling is black and new node is inserted as an outside grandchild

CS350: Data Structures

X

BAS

ED

P

X

Fixing the Insertion Violation -- Case #3

17

G

S

ED

BA

C

P

G

C

Perform a single rotationP becomes new root of subtree

Nodes P and G get a color change

CS350: Data Structures

X

P

Insert Violation -- Case #4

18

G

S

ED

(4) Parent’s sibling is black and new node is inserted as an inside grandchild

A

CB

CS350: Data Structures

S

G

X

P

CBA

G

Fixing the Insertion Violation -- Case #4

19

X

P S

EDA

CB

Perform a double rotation

ED

X becomes new root of subtree

Nodes X and G get a color change

CS350: Data Structures

Red-Black Tree Insertion Example

20

80 9050

40 55

655

85602010

15 70

30

24

99

insert(99)

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

CS350: Data Structures

Red-Black Tree Insertion Example

21

80 9050

40 55

655

85602010

15 70

30

24

99

Case #1: Parent’s sibling is red and new node is inserted as an outside grandchild

insert(99)

CS350: Data Structures

Red-Black Tree Insertion Example

22

80 9050

40 55

655

85602010

15 70

30

24

99

Case #1: Parent’s sibling is red and new node is inserted as an outside grandchild

Push black down from the grandparent

insert(99)

CS350: Data Structures

Red-Black Tree Insertion Example

23

80 9050

40 55

655

85602010

15 70

30

24

99

Push black down from the grandparent

insert(99)

CS350: Data Structures

Red-Black Tree Insertion Example

24

80 9050

40 55

655

85602010

15 70

30

24

99

Balanced

insert(99)

CS350: Data Structures

Red-Black Tree Insertion Example

25

80 9050

40 55

655

85602010

15 70

30

24

9923

(1) Every node is colored either red or black

(2) The root node is black

(3) If a node is red, its children must be black

(4) Every path from a node to a null link must contain the same number of black nodes

insert(23)

CS350: Data Structures

Red-Black Tree Insertion Example

26

80 9050

40 55

655

85602010

15 70

30

24

9923

Case #4: Parent’s sibling is black and new node is inserted as an inside grandchild

Null

insert(23)

CS350: Data Structures

Red-Black Tree Insertion Example

27

80 9050

40 55

655

85602010

15 70

30

24

9923

Null

Perform a double rotation: First, rotate between nodes 23 & 24, then, rotate between nodes 23 & 20 and change their colors

insert(23)

CS350: Data Structures

Red-Black Tree Insertion Example

28

80 9050

40 55

655

85602010

15 70

30

23

99

Null

24

HEY, LOOK who showed up after the first rotation!! It’s case #3: Parent’s sibling is black and new node is inserted as an outside grandchild

Perform a double rotation: First, rotate between nodes 23 & 24, then, rotate between nodes 23 & 20 and change their colors

insert(23)

CS350: Data Structures

Red-Black Tree Insertion Example

29

80 9050

40 55

655

85602310

15 70

30

24

99

20

Perform a double rotation: First, rotate between nodes 23 & 24, then, rotate between nodes 23 & 20 and change their colors

insert(23)

CS350: Data Structures

Red-Black Tree Insertion Example

30

80 9050

40 55

655

85602310

15 70

30

24

99

20

Finally, recolor nodes 23 and 20

insert(23)

CS350: Data Structures

Red-Black Tree Insertion Example

31

80 9050

40 55

655

85602310

15 70

30

24

99

20

Balanced

insert(23)

CS350: Data Structures

Red-Black Tree Deletion

• Start with standard BST deletion - In BST deletion, when deleting a node with two children, the node

was not actually deleted -- its contents were replaced

(1) Contents replaced with contents from successor or predecessor

(2) Then the successor/predecessor node was deleted

- Successor/predecessor can have 0 or 1 child (if the node had two children, then one of its children would be the successor/predecessor)

- Replacing the contents of a node do not affect the coloring of the node, therefore the properties of the red-black tree are not altered

- Removal of the node with 0 or 1 child needs consideration as it can potentially cause violations in the red-black tree

32

CS350: Data Structures

Red-Black Tree Deletion

• If the node removed is red, then there are no problems and the 4 properties of the red-black tree will still be true -- there is nothing to do in this case

• If the node removed is black, then there is a new violation in the red-black tree -- violation of property #4

- It is necessary to work back up the tree and account for the missing black node

33

(4) Every path from a node to a null node must contain the same number of black nodes