• No results found

Lecture Notes for Chapter 13: Red-Black Trees

Chapter 13 overview

Red-black trees

A variation of binary search trees.

Balanced: height is O(lgn), wherenis the number of nodes.Operations will take O(lgn)time in the worst case.

[These notes are a bit simpler than the treatment in the book, to make them more amenable to a lecture situation. Our students Þrst see red-black trees in a course that precedes our algorithms course. This set of lecture notes is intended as a refresher for the students, bearing in mind that some time may have passed since they last saw red-black trees.

The procedures in this chapter are rather long sequences of pseudocode. You might want to make arrangements to project them rather than spending time writing them on a board.]

Red-black trees

Ared-black treeis a binary search tree + 1 bit per node: an attributecolor, which is either red or black.

All leaves are empty (nil) and colored black.

We use a single sentinel,nil[T], for all the leaves of red-black treeT.color[nil[T]] is black.

The root’s parent is alsonil[T].

All other attributes of binary search trees are inherited by red-black trees (key,left,

right, and p). We don’t care about the key innil[T].

Red-black properties

13-2 Lecture Notes for Chapter 13: Red-Black Trees

1. Every node is either red or black. 2. The root is black.

3. Every leaf (nil[T]) is black.

4. If a node is red, then both its children are black. (Hence no two reds in a row on a simple path from the root to a leaf.)

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

Example: 26 17 41 30 38 47 50 nil[T] h = 4 bh = 2 h = 1 bh = 1 bh = 2h = 3 h = 2 bh = 1 bh = 1h = 2 h = 1 bh = 1 bh = 1h = 1

[Nodes with bold outline indicate black nodes. Don’t add heights and black-heights yet. We won’t bother with drawingnil[T]any more.]

Height of a red-black tree

Height of a nodeis the number of edges in a longest path to a leaf.

Black-heightof a nodex: bh(x)is the number of black nodes (includingnil[T])

on the path from x to leaf, not countingx. By property 5, black-height is well deÞned.

[Now label the example tree with heighthand bh values.]

Claim

Any node with heighthhas black-height≥h/2.

Proof By property 4,≤ h/2 nodes on the path from the node to a leaf are red. Hence≥h/2 are black. (claim)

Claim

Lecture Notes for Chapter 13: Red-Black Trees 13-3

Proof By induction on height ofx.

Basis:Height of x =0⇒x is a leaf⇒bh(x) =0. The subtree rooted atx has 0 internal nodes. 20−1=0.

Inductive step: Let the height of x be h and bh(x) = b. Any child of x has heighth−1 and black-height eitherb(if the child is red) orb−1 (if the child is black). By the inductive hypothesis, each child has≥ 2bh(x)−11 internal nodes.

Thus, the subtree rooted atx contains≥2·(2bh(x)−11)+1=2bh(x)1 internal

nodes. (The+1 is forx itself.) (claim)

Lemma

A red-black tree withn internal nodes has height≤2 lg(n+1).

Proof Lethandbbe the height and black-height of the root, respectively. By the above two claims,

n ≥2b−1≥2h/2−1.

Adding 1 to both sides and then taking logs gives lg(n+1)h/2, which implies thath ≤2 lg(n+1). (theorem)

Operations on red-black trees

The non-modifying binary-search-tree operations MINIMUM, MAXIMUM, SUC- CESSOR, PREDECESSOR, and SEARCH run in O(height) time. Thus, they take

O(lgn)time on red-black trees. Insertion and deletion are not so easy.

If we insert, what color to make the new node?

Red? Might violate property 4.Black? Might violate property 5.

If we delete, thus removing a node, what color was the node that was removed?

Red? OK, since we won’t have changed any black-heights, nor will we have

created two red nodes in a row. Also, cannot cause a violation of property 2, since if the removed node was red, it could not have been the root.

Black? Could cause there to be two reds in a row (violating property 4), and

can also cause a violation of property 5. Could also cause a violation of prop- erty 2, if the removed node was the root and its child—which becomes the new root—was red.

Rotations

The basic tree-restructuring operation.

Needed to maintain red-black trees as balanced binary search trees.Changes the local pointer structure. (Only pointers are changed.)

13-4 Lecture Notes for Chapter 13: Red-Black Trees

Won’t upset the binary-search-tree property.

Have both left rotation and right rotation. They are inverses of each other.A rotation takes a red-black-tree and a node within the tree.

y x α β γ x y α β γ LEFT-ROTATE(T, x) RIGHT-ROTATE(T, y) LEFT-ROTATE(T,x) yright[x] Set y.

right[x]←left[y] Turny’s left subtree intox’s right subtree.

ifleft[y]=nil[T] then p[left[y]]←x p[y]← p[x] Linkx’s parent toy. if p[x] =nil[T] thenroot[T]←y else ifx =left[p[x]] thenleft[p[x]]← y else right[p[x]]← y

left[y]←x Putx ony’s left.

p[x]← y

[In theÞrst two printings of the second edition, this procedure contains a bug that is corrected above (and in the third and subsequent printings). The bug is that the assignment in line 4 (p[left[y]]←x) should be performed only when y’s left child is not the sentinel (which is tested in line 3). TheÞrst two printings omitted this test.]

The pseudocode for LEFT-ROTATEassumes that

right[x] =nil[T], androot’s parent isnil[T].

Pseudocode for RIGHT-ROTATEis symmetric: exchangeleftandrighteverywhere.

Example: [Use to demonstrate that rotation maintains inorder ordering of keys. Node colors omitted.]

Lecture Notes for Chapter 13: Red-Black Trees 13-5 4 7 11 9 18 14 17 19 22 x y 4 7 18 19 14 17 22 x y 11 9 LEFT-ROTATE(T, x)

Before rotation: keys ofx’s left subtree11keys ofy’s left subtree18

keys ofy’s right subtree.

Rotation makesy’s left subtree intox’s right subtree.

After rotation: keys ofx’s left subtree11keys ofx’s right subtree18

keys ofy’s right subtree.

Time: O(1)for both LEFT-ROTATEand RIGHT-ROTATE, since a constant number

of pointers are modiÞed.

Notes:

Rotation is a very basic operation, also used in AVL trees and splay trees.Some books talk of rotating on an edge rather than on a node.

Insertion

13-6 Lecture Notes for Chapter 13: Red-Black Trees RB-INSERT(T,z) ynil[T] xroot[T] whilex =nil[T] do yx ifkey[z]<key[x] thenxleft[x] else xright[x] p[z]←y ify =nil[T] thenroot[T]←z

else ifkey[z]<key[y]

thenleft[y]←z else right[y]←z left[z]←nil[T] right[z]←nil[T] color[z]←RED RB-INSERT-FIXUP(T,z)

RB-INSERTends by coloring the new nodez red.

Then it calls RB-INSERT-FIXUP because we could have violated a red-black

property.

Which property might be violated? 1. OK.

2. Ifzis the root, then there’s a violation. Otherwise, OK. 3. OK.

4. If p[z] is red, there’s a violation: bothzand p[z] are red. 5. OK.

Lecture Notes for Chapter 13: Red-Black Trees 13-7

RB-INSERT-FIXUP(T,z)

whilecolor[p[z]]=RED

do if p[z]=left[p[p[z]]]

thenyright[p[p[z]]]

ifcolor[y]=RED

thencolor[p[z]]←BLACK Case 1

color[y]←BLACK Case 1

color[p[p[z]]]←RED Case 1

zp[p[z]] Case 1

else ifz =right[p[z]]

thenzp[z] Case 2 LEFT-ROTATE(T,z) Case 2

color[p[z]]←BLACK Case 3

color[p[p[z]]]←RED Case 3 RIGHT-ROTATE(T,p[p[z]]) Case 3

else (same asthenclause

with “right” and “left” exchanged)

color[root[T]]←BLACK

Loop invariant:

At the start of each iteration of thewhileloop, a. zis red.

b. There is at most one red-black violation:

Property 2:z is a red root, orProperty 4:z andp[z] are both red.

[The book has a third part of the loop invariant, but we omit it for lecture.]

Initialization: We’ve already seen why the loop invariant holds initially.

Termination: The loop terminates because p[z] is black. Hence, property 4 is OK. Only property 2 might be violated, and the last lineÞxes it.

Maintenance: We drop out when z is the root (since then p[z] is the sentinel

nil[T], which is black). When we start the loop body, the only violation is of property 4.

There are 6 cases, 3 of which are symmetric to the other 3. The cases are not mutually exclusive. We’ll consider cases in which p[z] is a left child.

13-8 Lecture Notes for Chapter 13: Red-Black Trees Case 1: yis red z y C D A B α β γ δ ε C D A B α β γ δ ε new z y C D B δ ε C D B A α β γ δ ε new z A α β γ z If z is a right child If z is a left child

p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red

and there are no other violations of property 4.

Make p[z] and y blacknowz and p[z] are not both red. But prop-

erty 5 might now be violated.

Make p[p[z]] redrestores property 5.

The next iteration has p[p[z]] as the newz(i.e.,zmoves up 2 levels). Case 2: yis black,z is a right child

C A B α β γ δ Case 2 z y B A α β γ δ Case 3 z y z A B C α β γ δ C

Left rotate around p[z]nowz is a left child, and bothzand p[z] are

red.

Takes us immediately to case 3. Case 3: yis black,z is a left child

Make p[z] black and p[p[z]] red.Then right rotate on p[p[z]].No longer have 2 reds in a row.

p[z] is now blackno more iterations. Analysis

O(lgn)time to get through RB-INSERTup to the call of RB-INSERT-FIXUP. Within RB-INSERT-FIXUP:

Lecture Notes for Chapter 13: Red-Black Trees 13-9

Each iteration takesO(1)time.

Each iteration is either the last one or it moveszup 2 levels.O(lgn)levelsO(lgn)time.

Also note that there are at most 2 rotations overall.

Thus, insertion into a red-black tree takesO(lgn)time.

Deletion

Start by doing regular binary-search-tree deletion: RB-DELETE(T,z)

ifleft[z]=nil[T] orright[z]=nil[T]

thenyz else y ←TREE-SUCCESSOR(z) ifleft[y]=nil[T] thenxleft[y] else xright[y] p[x]← p[y] if p[y]=nil[T] thenroot[T] ←x else if y=left[p[y]] thenleft[p[y]]←x else right[p[y]]←x ify =z

thenkey[z]←key[y]

copyy’s satellite data intoz

ifcolor[y]=BLACK

thenRB-DELETE-FIXUP(T,x)

returny

y is the node that was actually spliced out.x is either

y’s sole non-sentinel child beforeywas spliced out, orthe sentinel, ify had no children.

In both cases, p[x] is now the node that was previouslyy’s parent. Ifyis black, we could have violations of red-black properties:

1. OK.

2. If yis the root andx is red, then the root has become red. 3. OK.

4. Violation if p[y] andx are both red.

5. Any path containing ynow has 1 fewer black node.

13-10 Lecture Notes for Chapter 13: Red-Black Trees

Add 1 to count of black nodes on paths containingx.Now property 5 is OK, but property 1 is not.

xis eitherdoubly black(ifcolor[x] =BLACK) orred & black(ifcolor[x]=

RED).

The attributecolor[x] is still eitherREDorBLACK. No new values forcolor

attribute.

In other words, the extra blackness on a node is by virtue ofxpointing to the

node.

Remove the violations by calling RB-DELETE-FIXUP: RB-DELETE-FIXUP(T,x)