Red - Black Trees

21
Red-Black Trees Acknowledgment Many thanks to “erm” from Purdue University for this very interesting way of presenting this course material. 1

description

Red - Black Trees. Acknowledgment Many thanks to “ erm ” from Purdue University for this very interesting way of presenting this course material. Red -Black Tree. A red -black tree is a binary search tree with the following properties: • edges are colored red or black - PowerPoint PPT Presentation

Transcript of Red - Black Trees

Page 1: Red - Black  Trees

1

Red-Black Trees

AcknowledgmentMany thanks to “erm” from Purdue University for this very interesting way of presenting this

course material.

Page 2: Red - Black  Trees

2

A red-black tree is a binary search tree with the following properties:• edges are colored red or black• no two consecutive red edges on any root-leaf path• same number of black edges on any root-leaf path

(= black height of the tree)• edges connecting leaves are black

Red-Black Tree

Page 3: Red - Black  Trees

3

Correspondence between2-3-4 and Red-Black trees

Notice that red-black trees are just a way of representing 2-3-4 trees!

Page 4: Red - Black  Trees

4

More Red-Black TreeProperties

Page 5: Red - Black  Trees

5

Insertion into Red-Black Trees1.Perform a standard search to find the leaf where the key should be added2.Replace the leaf with an internal node with the new key3.Color the incoming edge of the new node red4. Add two new leaves, and color their incoming edges black5.If the parent had an incoming red edge, we have two consecutive red edges!

We must reorganize tree to remove that violation. What must be done depends on the sibling of the parent.

Page 6: Red - Black  Trees

6

Page 7: Red - Black  Trees

7

Page 8: Red - Black  Trees

8

Page 9: Red - Black  Trees

9

Page 10: Red - Black  Trees

10

Page 11: Red - Black  Trees

11

Page 12: Red - Black  Trees

12

Page 13: Red - Black  Trees

13

Insertion: an example

Start by inserting “REDSOX” into an empty tree

Page 14: Red - Black  Trees

14

Page 15: Red - Black  Trees

15

Page 16: Red - Black  Trees

16

Page 17: Red - Black  Trees

17

Page 18: Red - Black  Trees

18

Page 19: Red - Black  Trees

19

Page 20: Red - Black  Trees

20

Page 21: Red - Black  Trees

21