Top Banner
Red-Black Trees
52
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Red Black Trees

Red-Black Trees

Page 2: Red Black Trees

Red-Black Trees

• “Balanced” binary search trees guarantee an O(lgn) running time

• Red-black-tree– Binary search tree with an additional attribute for

its nodes: color which can be red or black– Constrains the way nodes can be colored on any

path from the root to a leaf: Ensures that no path is more than twice as long as any

other path the tree is balanced2

Page 3: Red Black Trees

Example: RED-BLACK-TREE

• The black rectangles represent the external (leaf) nodes. Note that the external nodes are always black. They cannot be red.

3

26

17 41

30 47

38 50

Page 4: Red Black Trees

Red-Black-Trees Properties(**Satisfy the binary search tree property**)

1. Every node is either red or black

2. The root is black

3. Every leaf (external node) is black

4. If a node is red, then both its children are black• No two consecutive red nodes on a simple path from the root to a

leaf

5. For each node, all paths from that node to descendant leaves contain the same number of black nodes

4

Page 5: Red Black Trees

Black-Height of a Node

• Height of a node: the number of edges in the longest path to a leaf

• Black-height of a node x: bh(x) is the number of black nodes (including external node) on the path from x to a leaf,

not counting x5

26

17 41

30 47

38 50

NIL NIL

NIL

NIL NIL NIL NIL

NIL

h = 4bh = 2

h = 3bh = 2

h = 2bh = 1

h = 1bh = 1

h = 1bh = 1

h = 2bh = 1 h = 1

bh = 1

Page 6: Red Black Trees

6

• Every path from the root to a leaf contains the same number of black nodes. This number is called the black-height of the tree.

Page 7: Red Black Trees

7

A valid Red-Black Tree

Black-Height = 2

Page 8: Red Black Trees

8

Page 9: Red Black Trees

9

Page 10: Red Black Trees

10

Converting a 2-3-4 Tree to a Red-Black Tree

• A red-black tree is a binary tree representation of a 2-3-4 tree.• The child pointer of a node in a red-black tree are of two types: red

and black.• If the child pointer was present in the original 2-3-4 tree, it is a black

pointer. Otherwise, it is a red pointer.• A node in a 2-3-4 is transformed into its red-black representation as

follows:– A 2-node p is represented by the RedBlackNode q with both its color data

members black, and data = dataL; q->LeftChild = p->LeftChild, and q->RightChild = p ->LeftMidChild.

– A 3-node p is represented by two RedBlackNodes connected by a red pointer. There are two ways in which this may be done.

– A 4-node is represented by three RedBlackNodes, one of which is connected to the remaining two by red pointers.

Page 11: Red Black Trees

11

More Red-Black Tree Properties

N # of internal nodesL # leaves (=N+1)H heightB black height

Property 1: 2B ≤ N+1 ≤ 4BProperty 2: ½ log(N+1) ≤ B ≤ log(N+1) Property 3: log(N+1) ≤ H ≤ 2log(N+1)

Page 12: Red Black Trees

Operations on Red-Black-Trees• The non-modifying binary-search-tree operations

MINIMUM, MAXIMUM, SUCCESSOR,

PREDECESSOR, and SEARCH run in O(h) time

– They take O(lgn) time on red-black trees

• What about TREE-INSERT and TREE-DELETE?

– They will still run on O(lgn)

– We have to guarantee that the modified tree will still be

a red-black tree 12

Page 13: Red Black Trees

INSERTINSERT: what color to make the new node?• Red? Let’s insert 35!

– Property 4 is violated: if a node is red, then both its children are black

• Black? Let’s insert 14!

– Property 5 is violated: all paths from a node to its leaves contain the same number of black nodes

13

26

17 41

30 47

38 50

Page 14: Red Black Trees

14

Insertion Algorithm• We can detect a 4-node simply by looking for nodes q for which both

color data members are red. Such nodes, together with their two children, form a 4-node.

• When such a 4-node q is detected, the following transformations are needed:

(1) Change both the colors of q to black.(2) If q is the left (right) child of its parent, then change the left

(right) color of its parent to red.(3) If we now have two consecutive red pointers, then one is from the grandparent, gp, of q to the parent, p, of q and the other from p(parent) to q. Let the direction of the first of these be X and that of the second be Y.

• Depending on XY = LL, LR, RL, and RR, transformations are performed to remove violations.

Page 15: Red Black Trees

15

Transformation for a Root 4-Node

Page 16: Red Black Trees

16

Transformation for a 4-Node That is the child of a 2-Node (1)

Page 17: Red Black Trees

17

Transformation for a 4-Node That is the child of a 2-Node (2)

Page 18: Red Black Trees

18

Transformation for a 4-Node That is the left child of a 3-Node

Page 19: Red Black Trees

19

Transformation for a 4-Node That is the left child of a 3-Node

Page 20: Red Black Trees

20

Transformation for a 4-Node That is the middle child of a 3-Node

Page 21: Red Black Trees

21

Transformation for a 4-Node That is the middle child of a 3-Node

Page 22: Red Black Trees

Red Black Trees

Top-Down Deletion

Page 23: Red Black Trees

Recall the rules for BST deletion

1. If vertex to be deleted is a leaf, just delete it.

2. If vertex to be deleted has just one child, replace it with that child

3. If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the in-order predecessor (a recursive step)

Page 24: Red Black Trees

What can go wrong?

1. If the delete node is red?

Not a problem – no RB properties violated

2. If the deleted node is black?

If the node is not the root, deleting it will change the black-height along some path

Page 25: Red Black Trees

The goal of T-D Deletion

• To delete a red leaf

• How do we ensure that’s what happens?– As we traverse the tree looking for the leaf to

delete, we change every node we encounter to red.

– If this causes a violation of the RB properties, we fix it

Page 26: Red Black Trees

Bottom-Up vs. Top-Down

• Bottom-Up is recursive– BST deletion going down the tree (winding up

the recursion)– Fixing the RB properties coming back up the

tree (unwinding the recursion)

• Top-Down is iterative– Restructure the tree on the way down so we

don’t have to go back up

Page 27: Red Black Trees

Terminology

• Matching Weiss text section 12.2– X is the node being examined– T is X’s sibling– P is X’s (and T’s) parent– R is T’s right child– L is T’s left child

• This discussion assumes X is the left child of P. As usual, there are left-right symmetric cases.

Page 28: Red Black Trees

Basic Strategy

• As we traverse the tree, we change every node we visit, X, to Red.

• When we change X to Red, we know– P is also Red (we just came from there)– T is black (since P is Red, it’s children are

Black)

Page 29: Red Black Trees

Step 1 – Examine the root

1. If both of the root’s children are Blacka. Make the root Red

b. Move X to the appropriate child of the root

c. Proceed to step 2

2. Otherwise designate the root as X and proceed to step 2B.

Page 30: Red Black Trees

Step 2 – the main caseAs we traverse down the tree, we continually

encounter this situation until we reach the node to be deleted

X is Black, P is Red, T is Black

We are going to color X Red, then recolor other nodes and possibly do rotation(s) based on the color of X’s and T’s children

2A. X has 2 Black children2B. X has at least one Red child

Page 31: Red Black Trees

P

TX

Case 2AX has two Black Children

2A1. T has 2 Black Children

2A2. T’s left child is Red

2A3. T’s right child is Red

** if both of T’s children are Red, we can do either 2A2 or 2A3

Page 32: Red Black Trees

Case 2A1X and T have 2 Black Children

P

TX

P

TX

Just recolor X, P and T and move down the tree

Page 33: Red Black Trees

Case 2A2

P

TX

L

X has 2 Black Children and T’s Left Child is Red

Rotate L around T, then L around PRecolor X and P then continue down the tree

L1 L2

P T

X

L

L1 L2

Page 34: Red Black Trees

Case 2A3

P

TX

X has 2 Black Children and T’s Right Child is Red

Rotate T around PRecolor X, P, T and R then continue down the tree

R1 R2

P R

X

T

R2R1R

L L

Page 35: Red Black Trees

Case 2BX has at least one Red child

Continue down the tree to the next level

If the new X is Red, continue down again

If the new X is Black (T is Red, P is Black)

Rotate T around P

Recolor P and T

Back to main case – step 2

Page 36: Red Black Trees

Case 2B Diagram

P

X T

Move down the tree.

P

X

T

P

T X

If move to Black child (2B2)Rotate T around P; Recolor P and TBack to step 2, the main case

If move to the Red child (2B1) Move down again

Page 37: Red Black Trees

Step 3

Eventually, find the node to be deleted – a leaf or a node with one non-null child that is a leaf.

Delete the appropriate node as a Red leaf

Step 4Color the Root Black

Page 38: Red Black Trees

Example 1Delete 10 from this RB Tree

15

17

1620

23181310

7

12

6

3

Step 1 – Root has 2 Black children. Color Root Red

Descend the tree, moving X to 6

Page 39: Red Black Trees

Example 1 (cont’d)

15

17

1620

23181310

7

12

6

3

One of X’s children is Red (case 2B). Descend down the tree, arriving at 12. Since the new X (12) is also Red (2B1), continue down the tree, arriving at 10.

X

Page 40: Red Black Trees

Example 1 (cont’d)

15

17

1620

23181310

7

12

6

3

Step 3 -Since 10 is the node to be deleted, replace it’s value with the value of it’s only child (7) and delete 7’s red node

X

Page 41: Red Black Trees

Example 1 (cont’d)

15

17

1620

2318137

12

6

3

The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black.

Page 42: Red Black Trees

Example 2Delete 10 from this RB Tree

15

17

1620

1310

12

6

3

42

Step 1 – the root does not have 2 Black children.

Color the root red, Set X = root and proceed to step 2

Page 43: Red Black Trees

Example 2 (cont’d)

15

17

1620

1310

12

6

3

42

X

X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.

Page 44: Red Black Trees

Example 2 (cont’d)

15

17

1620

1310

12

6

3

42

X

X has 2 Black children. X’s sibling (3) also has 2 black children.Case 2A1– recolor X, P, and T and continue down the tree, arriving at 10.

P

T

Page 45: Red Black Trees

Example 2 (cont’d)

15

17

1620

1310

12

6

3

42

P

X T

X is now the leaf to be deleted, but it’s Black, so back to step 2.X has 2 Black children and T has 2 Black children – case 2A1

Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf.Step 4 -- Recolor the root black

Page 46: Red Black Trees

Example 2 Solution

15

17

1620

13

12

6

3

42

Page 47: Red Black Trees

Example 3Delete 11 from this RB Tree

15

1311

12

10

5

73

6 9 2 4

Valid and unaffected Right subtree

Step 1 – root has 2 Black children. Color Root red.

Set X to appropriate child of root (10)

Page 48: Red Black Trees

Example 3 (cont’d)

15

1311

12

10

5

73

6 9 2 4

X

X has one Red child (case 2B)

Traverse down the tree, arriving at 12.

Page 49: Red Black Trees

Example 3 (cont’d)

15

1311

12

10

5

73

6 9 4

X

Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P

Back to step 2

P

T

2

Page 50: Red Black Trees

Example 3 (cont’d)

15

1311

12

10

5

73

6 9 4

XP

T

2

Now X is Black with Red parent and Black sibling.X and T both have 2 Black children (case 2A1)Just recolor X, P and T and continue traversal

Page 51: Red Black Trees

Example 3 (cont’d)15

1311

12

10

5

73

6 9 4X

P

T 2

Having traversed down the tree, we arrive at 11, the leaf to be deleted, but it’s Black, so back to step 2.X and T both have two Black children. Recolor X, P and T.Step 3 -- delete 11 as a red leaf. Step 4 -- Recolor the root black

Page 52: Red Black Trees

Example 3 Solution

13

12

10

5

73

6 9 4 2

15