44
Red-Black Trees Presentation by Aasim Ali (04- 9174)

Red-Black Trees Advanced)

Embed Size (px)

Citation preview

Page 1: Red-Black Trees Advanced)

Red-Black Trees

Presentation by Aasim Ali (04-9174)

Page 2: Red-Black Trees Advanced)

Red-Black Trees

• Definition: A Red-Black Tree (shortened as RB Tree or RBT) is a Binary Search Tree (BST) where:1. Every node is either RED or BLACK.2. The root is BLACK3. Every leaf (sentinel node) is BLACK.4. If a node is RED, then both of its children are BLACK.5. For each node, all paths from the node to descendant leaves contain

the same number of BLACK nodes.

• Definition: The BLACK-height of a node, x, in a Red-Black Tree is the number of BLACK nodes on any path from x to any leaf (sentinel node), not counting x.

• Definition: The BLACK-height of a Red-Black Tree is the BLACK-height of Root node.

Page 3: Red-Black Trees Advanced)

Black-Height of: r = 2 x = 2 y = 1 z = 1

A few RBT visual examples

A valid Red-Black Tree

x

r

z

y

Black-Height of tree = 2 (since r is the root)

Page 4: Red-Black Trees Advanced)

Black-Height of:x = 2 y = 1 z = 1

A few RBT visual examples

A valid Red-Black Tree of Black-Height 3

x

y

z

Contd.

Page 5: Red-Black Trees Advanced)

A few RBT visual examples

Black-Height of:x = 3 y = 2 z = 1

A valid Red-Black Tree of Black-Height 3

Contd.

x

y

z

Page 6: Red-Black Trees Advanced)

It is also a valid Red-Black Tree, because:

No RED Child has RED parent

Black-Height from any node to all its successor leaf nodes is same

Hence all leaf nodes are not

required to be at same

(ordinary) height

A few RBT visual examplesContd.

Page 7: Red-Black Trees Advanced)

Theorem 1:Any Red-Black Tree with root x hasat least n = 2bh(x) – 1 internal nodes

where bh(x) is the black height of node x.

Proof:By induction:

when h(x) = 0: 2bh(x) – 1 = 20 – 1 = 1 – 1 = 0when h(x) > 0: (2bh(x)-1 – 1) + (2bh(x)-1 – 1) + 1 = 2bh(x) – 1

Theorems Related to RB Trees

Page 8: Red-Black Trees Advanced)

Theorem 2:

In a red-black tree, at least half the nodes on any path from the root to a leaf must be black.

Proof:

If there is a red node on the path then there must be a corresponding black node.

Theorems Related to RB Trees Contd.

Page 9: Red-Black Trees Advanced)

Theorem 3: In a red-black tree, no path from any node, x, to a leaf is more than twice as long as any other path from x to any other leaf.

Proof: • Every path from a node to any leaf contains the same number of black

nodes [definition]• At least ½ the nodes on any such path are black [Theorem 2]• Therefore, there can be no more than twice as many nodes on any path

from x to a leaf as on any other path. • Hence, the length of every path is no more than twice in length of any

other path from x to any other leaf

Theorems Related to RB TreesContd.

Page 10: Red-Black Trees Advanced)

Theorem 4:A red-black tree with n internal nodes has height h <= 2 lg(n + 1).

Proof:Let h be the height of the red-black tree with rooted at x.

bh(x) >= h/2 [Theorem 2]n >= 2bh(x) – 1 [Theorem 1]

Thereforen >= 2 h/2 – 1n + 1 >= 2h/2

lg(n + 1) >= h/22 lg(n + 1) >= h

Theorems Related to RB Trees Contd.

Page 11: Red-Black Trees Advanced)

Operations on Red-Black Tree

Search

Insertion

Deletion

Page 12: Red-Black Trees Advanced)

Search

• Search works exactly as in ordinary Binary Search Tree (BST)

• Since h <= 2 lg (n + 1) hence search takes O (lg n) even in worst case(where h is height of the tree, and n is total number of nodes)

Page 13: Red-Black Trees Advanced)

Mechanism for Insertion and Deletion

• Basic mechanism for Insertion & Deletion: same as in BST

• Ensuring the validity of Red Black (RB) Properties may require certain adjustments for fixing of any violations

• Fixing adjustments include:– Change of colors– Rotations: Left rotation and Right rotation

• Two approaches are used for fixing:– Top-Down:

Adjustments during the pre-insert/pre-delete downward traversal (while moving from the root towards insertion/deletion point)

– Bottom-Up: Post-insert/post-delete adjustments during the upward traversal

(while moving from the point of insertion/deletion towards root)

Only Bottom-Up cases are discussed further in this presentation

Page 14: Red-Black Trees Advanced)

Left Rotation: Rotate R around P

Possible Rotations in Red Black Fixing

Right Rotation: Rotate L around P

L

P

LL LR RL

R

RR

G

L

LL LR

P

R

RL RR

G

Types of Rotations: (a) Left Rotation, (b) Right Rotation

Rotation is O(1) operation: simply requires update of fixed number of pointers

RL

P

LL LR RL RR

G

Original Structure

<BACK>

Page 15: Red-Black Trees Advanced)

Red-Black Trees

Bottom-Up Insertion

Page 16: Red-Black Trees Advanced)

Bottom–Up Insertion

• Insert node as in BST

• Color the Node RED

• Two Red-Black properties may be violated?1. Every node is Red or Black

2. Root is Black

3. Sentinel nodes are Black

4. If node is Red, both children must be Black

5. Every path from node to descendant leaf must contain the same number of Blacks

Page 17: Red-Black Trees Advanced)

Cases of Bottom-Up Insertion

• Insert node; Color it RED; X is pointer to it

• Cases

0: X is the root (Possibility: inserting 1st node, propagated effect of fixing)

1: Both parent and uncle are RED(Only this case may need recursive fixing)

Two further cases when Parent is RED, and uncle is BLACK:2: (zig-zag) –X and its parent are opposite type (left/right) children3: (zig-zig) –X and its parent are both left or both right children

Page 18: Red-Black Trees Advanced)

Terminology for Insertion

• The node causing violation is pointed to by X (being the new node in the start of Insertion Fixing)

• The parent of X is P• The uncle of X is U• The grandparent of X is G• The Sibling of X is S

Black NodeRed Node Sentinel Node

Page 19: Red-Black Trees Advanced)

X

Case 0 – X is root (Trivial case)

Just change the color of X to Black (This is the only case that increases Black-Height of the tree)

Bottom-Up InsertionCase 0

X

Page 20: Red-Black Trees Advanced)

P

G

U

Case 1 – Both P and U are RED

Just Recolor and move up:

Color parent and uncle BLACK

Color grandparent RED

Point X to grandparent

Check for violation in new situation (it may invoke any of four cases)

X

Bottom-Up InsertionCase 1

XX and P can be either child;it does not affect the strategy

Page 21: Red-Black Trees Advanced)

Case 2 – Zig-Zag: P is RED; U is BLACK; P & X are opposite children

Double Rotate & Recolor:

Left rotate X around P

Right rotate X around G

Swap colors of G and XS X

P

G

U

Bottom-Up InsertionCase 2

X

This simulation shows Case 2 when X is right child and P is left childSymmetric handling is needed when X is left child and P is right child

is left

is right

Page 22: Red-Black Trees Advanced)

Case 3 – Zig-Zig : P is RED; U is BLACK; P & X are same direction children

Single Rotate and Recolor:

Right rotate P around G

Swap colors of P and G

X

P

G

U

S

Bottom-Up InsertionCase 3

This simulation shows Case 3 when X and P both are left childrenSymmetric handling is needed when X and P both are right children

Page 23: Red-Black Trees Advanced)

Asymptotic Cost of Insertion

• O(lg n) to descend to insertion point

• O(1) to do insertion

• O(lg n) to ascend and readjust == worst case only for insert-fix case 1

• Total: O(log n)

Page 24: Red-Black Trees Advanced)

989797

97

Insert 96 as RED: No fixing needed

84

80

60 88

45 75 95

18 55 70 77 82 86 92

10 25 50 65 72

5 15 20 30

Examples of Insertion

Let’s Insert:

96, 97, 99

96

Insert 97 as RED: Needs fixing

case 2 (zig-zag)

96 9898

Page 25: Red-Black Trees Advanced)

84

80

60 88

45 75 95

18 55 70 77 82 86 92

10 25 50 65 72

5 15 20 30

Examples of Insertion

Let’s Insert:

96, 97, 99

Insert 99 as RED: Needs fixing

97

96 98

99

96 98

97

Case 1

Case 1

84 95

88

Contd.

Page 26: Red-Black Trees Advanced)

1010

88

99

84

80

60

45 75 95

18 55 77 82 86 92

25 50

65

5 15 20 30

Examples of Insertion

Let’s Insert:3, 56, 57

Insert 3 as RED: Needs fixing

96 9896 98

97

84 95

3 Case 1

5 15

Case 3(zig-zig)

45

18

Contd.

Page 27: Red-Black Trees Advanced)

15556

88

99

84

80

60

75 95

77 82 86 9265

Examples of Insertion

Let’s Insert: 3, 56, 57

Insert 56 as RED: No fixing needed

96 9896 98

97

84 9518

55

50

4510

25

20 303

Insert 57 as RED: Needs fixing

57 Case 1

50 56

55 Case 1

10 45

18 Case 1

60 88

80 Case 080

Contd.

Page 28: Red-Black Trees Advanced)

Red-Black Trees

Bottom-Up Deletion

(starting with BST delete)

Page 29: Red-Black Trees Advanced)

Ordinary BST Delete

1. Leaf: just delete it.

2. Has just one child: replace it with that child

3. Has two children then Delete-by-Copy:

copy the value of in-order predecessor in the node (selected for deletion) then delete the in-order predecessor (bringing it to case 1 or 2 above)

Page 30: Red-Black Trees Advanced)

Terminology for Deletion

• U was physically deleted Black node

• V takes U’s place, thus has an extra unit of blackness

• P is the parent of V

• S is the sibling of V

• + indicates extra blackness: P+ means P with extra blackness

• Sentinel nodes are not shown

Black Node

Red Node

Red or Black, and don’t care

Sentinel Node

Page 31: Red-Black Trees Advanced)

RB Properties Verification

Let’s analyze the effect of simple BST Delete on RBT Properties

1. If U is RED?Not a problem – no RB properties violated

2. If U is BLACK?(a) U is not root: black-height of some paths needs fixing (b) U is root: even then some verification is neededSibling of a non-root BLACK node is guaranteed to exist

Page 32: Red-Black Trees Advanced)

Cases of Bottom-Up Deletion

• Cases (caused by deletion of a BLACK node)1: Sibling of deleted node is RED (which implies: the Sibling has at least

one BLACK child, and the parent of deleted node is BLACK)

Three more cases when Sibling is BLACK

2: Both children (internal or external) of Sibling are BLACK

3: Deleted node is Left child and only the left child of Sibling is RED

4: Deleted node is Left child and at least right child of Sibling is RED

If Deleted node is Right child then symmetric strategy for 3 and 4 is used.

Page 33: Red-Black Trees Advanced)

Fixing the problem

• Think of V as having an “extra” unit of BLACKNESS:– It may be absorbed into the tree (by a RED node), or

– It may propagated up to the root and out of the tree

• There are four cases if V is a left child; and four symmetric cases if V is a right child

• V as left child is illustrated in the following slides

Page 34: Red-Black Trees Advanced)

Bottom-Up DeletionCase 1

• V’s sibling, S, is Red– Rotate S around P and recolor S & P

• NOT a terminal case – One of the other cases will now apply

• All other cases apply when S is Black

P

SU

V

Page 35: Red-Black Trees Advanced)

Case 1 Diagram

P

SV+

Rotate

P

V+

SRecolor

P

V+ S

Page 36: Red-Black Trees Advanced)

Bottom-Up DeletionCase 2

• V’s sibling, S, is black and has two black children– Recolor S to be Red

– P absorbs V’s extra blackness• If P is Red, we’re done

• If P is Black, it now has extra blackness and problem has been propagated up the tree

P

SU

V

Page 37: Red-Black Trees Advanced)

Case 2 diagram

P

SV+

P+

SV

Recolor and absorb

Either extra black absorbed by P or

P now has extra blackness

Page 38: Red-Black Trees Advanced)

Bottom-Up DeletionCase 3

• S is Black, S’s right child is Black and S’s left child is Red– Rotate S’s left child around S

– Swap color of S and S’s left child

– Now in case 4 P

SU

V

}

Page 39: Red-Black Trees Advanced)

Case 3 Diagrams

P

SV+

P

SV+Rotate

P

SV+

Recolor

Page 40: Red-Black Trees Advanced)

Bottom-Up DeletionCase 4

• S is black,S’s RIGHT child is RED(Left child either color)– Rotate S around P

– Swap colors of S and P, and color S’s Right child Black

• This is the terminal caseIt does not cause recursive call

P

SU

V

}

Page 41: Red-Black Trees Advanced)

S

P

V+Rotate

Case 4 diagrams

P

SV+

P

S

V

Recolor

Page 42: Red-Black Trees Advanced)

Asymptotic Cost of Deletion

• O(lg n) to descend to deletion point

• O(1) to do deletion

• O(lg n) to ascend and readjust == worst case only for delete-fixing case 1

• Total: O(log n)

Page 43: Red-Black Trees Advanced)

Delete 20 (RED): No fixing needed

84

80

60 88

45 75 95

18 55 70 77 82 86 92

10 25 50 65 72

5 15 20 30

Examples of Deletion

Let’s Delete:

20, 25, 30

97

Delete 25 (BLACK): Needs trivial fixing

Extra blackness of U=25 is absorbed by V=30

30

Delete 30 (BLACK): Fixing Case 4

Extra blackness of U=30 will be absorbed by P=18

Page 44: Red-Black Trees Advanced)

References

• Cormen, Leiserson, Rivest, Stein. Introduction to Algorithms, 2nd Ed. May 2001. May 2001.

• The following URL has a tutorial on Red-Black Trees [Julienne Walker and The EC Team]: Collection of Tutorials on Data Structures http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx