red black trees csc 172 spring 2002 lecture 18 red black tree bst with a coloring convention for...

22
Red Black Trees CSC 172 SPRING 2002 LECTURE 18

Post on 22-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Red Black Trees

CSC 172

SPRING 2002

LECTURE 18

Red Black Tree

BST with a coloring convention for each element

Nodes are colored according to rules

One rule involves paths

Specifically, paths from nodes with no children or one child

Example

30

5 45

2 9 40 50

41

Two Rules

Red Rule

If an element is red, all of it’s children are black

Path Rule

The number of black elements must be the same in all paths from the root element to element with no children or with one child

Example

30

5 45

2 9 40 50

41

Height of a Red-Black Tree

Claim: Let y be the root of a subtree of a red-black tree. The number of black elements is the same in a path from y to any one of its descendants with no child or one child.

General case

x

y

z1 z2

b1 b2

b0In general,b0+b1=b0+b2

So,b1=b2

We define blackHeight(z) = bh(z) =The number of black elementsin any path from z to any descendant with 0 or 1 child

Height of a red-black tree

For any nonempty subtree of a red-black tree

12 ))(( trootbhtn

Basis

height(t) = 0

n(t) = 0 if the root is red

n(t) = 1 if the root is black

Either way 1 >= 2 bh(root(t)) - 1

12121 ))((1 trootbhtn

Induction case 1

Let k be any nonnegative integer

BTIH n(t) >= 2 bh(root(t)) - 1 for height(t) <= k

If the root of t has one child,

we must have bh(root) = 1

12121 ))((1 trootbhtn

Induction case 2

The root of t has two children, (v1,v2)

If the root is red bh(root) = bh(v1) = bh(v2)

If the root is black bh(root) = bh(v1) + 1 = bh(v2) + 1

Either way

bh(v1) >= bh(root(t)) – 1

bh(v2) >= bh(root(t)) – 1

Induction case 2

BTIH:

n(leftTree(t)) >= 2 bh(v1) – 1

n(rightTree(t)) >= 2 bh(v2) – 1

The number of elements in t is one more than the number of elements in leftTree(t) + rightTree(t)

Ergo

1))(())(( trightTreentleftTreentn

11212 )()( 21 vbhvbh

11212 1))((1))(( trootbhtrootbh

12*2 1))(( trootbh

12 ))(( trootbh

Finally

For any red-black tree with n elements

height(t) is O(log n)

By the red rule, at most half of the elements in a path from the root to the farthest leaf can be red.

So, at least half those elements must be black

bh(root(t)) >= height(t)/2

n(t) >= 2bh(root(t)) – 1

n(t) >= 2height(t)/2 – 1

So,

height(t) <= 2 log2(n(t)+1)

AVL vs RB

AVL :

height < 1.75 log2(n)

Red-Black

height <= 2 log2(n+1)

So, AVLs are “bushier” that red-black.

However, maintaining a red-black is simpler

Which is why Java’s TreeSet class uses red-black

Helpersprivate static final boolean RED = false;private static final boolean BLACK= true;private static boolean colorOf(Entry p) {

return (p==null?BLACK:p.color);} private static void setColor(Entry p,boolean c) {

if(p!=null) p.color – c;}private static Entry parentOf(Entry p) {

return (p==null?null:p.parent);}

Insertions on red-black trees

FixAfterInsertion

1. Let t be (ref) the parent of the new insertion

2. Create a new Entry object pointer to by t.left or t.right

3. Set new Entry’s fields

4. Recolor and restructure

5. Set the root to BLACK

Insertions on red-black trees

Suppose we insert Enty x

We set x’s color to red

Do we need to recolor & rotate?

If x is root, “no”

If x’s parent is BLACK, “no”

So, loop

while(x!= root && x.parent.color == RED)

Aunt’s & Uncles

Because of rotation we need to consider the color of the sibling of x’s parent.

When x is parent’s left child:

y = x.parent.parent.right; // could be null

Case 1:colorOf(y) == RED

45

40 50

41

45

40 50

41x

y

setColor(parentOf(x),BLACK);setColor(y,BLACK);setColor(parentOf(parentOf(x)),RED)x=parentOf(x); //keep looping

x

Case 2:colorOf(y) = BLACK && x is RC

45

40

41

x=parentOf(x); rotateLeft(x);

45

41

40 xx

Y is null

Case 3:colorOf(y) = BLACK && x is LC

setColor(parentOf(x),BLACK);setColor(parentOf(parentOf(x)),RED);if(parentOf(parentOf(x) )!= null)

rotateRight(parentOf(parentOf(x));45

41

40 x

Y is null

45

41

40 x