Red-Black Trees ! Motivation: a binary search tree that is guaranteed - - PowerPoint PPT Presentation

red black trees
SMART_READER_LITE
LIVE PREVIEW

Red-Black Trees ! Motivation: a binary search tree that is guaranteed - - PowerPoint PPT Presentation

Red-Black Trees ! Motivation: a binary search tree that is guaranteed to be balanced (operations take time in the ( ) O lg n worst-case). Red-Black Trees ! A red-black tree is a binary search tree where each node has a color


slide-1
SLIDE 1

1

Red-Black Trees

2

Red-Black Trees

! Motivation: a binary search tree that is guaranteed to

be balanced (operations take time in the worst-case).

! A red-black tree is a binary search tree where each

node has a color attribute: either red or black and the following properties are satisfied:

– Every node is either red or black. – Every leaf (empty child) is black. – Both children of a red node are black. – Every simple path from a node to a descendant leaf contains the same number of black nodes.

( )

n O lg

3

The height of Red-Black Trees

! Lemma: A red-black tree with n internal nodes has

height at most

! Definition: Black-height, bh(x), is the number of black

nodes on any path from x to a leaf (not counting x itself).

! Proof: The subtree rooted at any node x contains at

least internal nodes (by induction on height of x). The black-height of the root is at least h/2, and thus,

) 1 lg( 2 + n

1 2

) (

x bh

) 1 lg( 2 1 2

2 /

+ ≤ ≤ − n h n

h

4

Rotations

y x A B C x y C B A

left right ! Note: rotations preserve the inorder key ordering in a

binary search tree.

slide-2
SLIDE 2

5

Red-Black Insert (RB-Insert)

! Use ordinary binary search tree insertion, and color

the new node red.

! If any of the red-black properties have been violated,

fix the resulting tree using recoloring and rotations.

! Which of the four properties can possibly be violated?

– Each node is still red or black; empty children are still black. – Each path still has the same number of black nodes. – But! What if the parent of the new node is also red?

6

RB-Insert: Case 1

! If x has both a red parent (B) and a red uncle (D),

re-color the parent and the uncle in black, and the grandparent (C) in red:

! Now the grandparent (C) may be in violation… ! If C is the root, we can simply color it black.

x x C B D A B D A C

7

RB-Insert: Case 2

! If x is the right child of a red parent and has a black

uncle, perform a left rotation (at A):

! This brings us into a configuration handled by Case 3

C D C D x x A A B B

8

RB-Insert: Case 3

! If x is the left child of a red parent and has a black

uncle, perform a right rotation (at C) and re-color:

! After Case 3, there is no longer a violation!

C D D B x x B A C A

slide-3
SLIDE 3

9

RB-Insert: Pseudocode

RB-Insert(RBTree T, RBNode x) BST-Insert(T, x); x.color = RED; while (x != T.root && x.parent.color == RED) { if (CaseOne(x)) { HandleCaseOne(x); x = x.parent.parent; } else if (CaseTwo(x)) { x = x.parent; LeftRotate(T, x); } HandleCaseThree(x); } T.root.color = BLACK;

10

Red-Black Remove (RB-Remove)

! Similar to ordinary binary search tree removal. ! Removing a red node does not violate any of the four

red-black properties.

! Removing a black node requires a fix-up, since the

number of black nodes along some paths has changed.

! The fix-up considers four different cases.

11

! Case 1 is transformed into one of the cases 2, 3, or 4

by exchanging the color of the nodes B and D and performing a left rotation:

RB-Remove: Case 1

B D E x C w A E A C x w D B

12

! Case 2 allows x to move one level up the tree by re-

coloring D to RED:

RB-Remove: Case 2

D x C w A E x C A E B B D

slide-4
SLIDE 4

13

RB-Remove: Case 3

D x w A E C E A w x

! Case 3 is transformed to case 4 by exchanging the

colors of nodes C and D and performing a right rotation : B C B D

14

RB-Remove: Case 4

D x A E A B w

! In case 4, the violation is resolved by changing some

colors and performing a left rotation (without violating the red-black properties): B C D C E