This action might not be possible to undo. Are you sure you want to continue?

BooksAudiobooksComicsSheet Music### Categories

### Categories

### Categories

Editors' Picks Books

Hand-picked favorites from

our editors

our editors

Editors' Picks Audiobooks

Hand-picked favorites from

our editors

our editors

Editors' Picks Comics

Hand-picked favorites from

our editors

our editors

Editors' Picks Sheet Music

Hand-picked favorites from

our editors

our editors

Top Books

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Audiobooks

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Comics

What's trending, bestsellers,

award-winners & more

award-winners & more

Top Sheet Music

What's trending, bestsellers,

award-winners & more

award-winners & more

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

Presentation by Aasim Ali (04-9174)

**Red-Black Trees
**

• Definition: A Red-Black Tree (shortened as RB Tree or RBT) is a Binary Search Tree (BST) where:

1. 2. 3. 4. 5. Every node is either RED or BLACK. The root is BLACK Every leaf (sentinel node) is BLACK. If a node is RED, then both of its children are BLACK. 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.

**A few RBT visual examples
**

r x y z A valid Red-Black Tree Black-Height of: r=2 x=2 y=1 z=1 Black-Height of tree = 2 (since r is the root)

A few RBT visual examples

Contd.

x y z A valid Red-Black Tree of Black-Height 3 Black-Height of: x=2

y=1

z=1

A few RBT visual examples

Contd.

x y z A valid Red-Black Tree of Black-Height 3 Black-Height of: x=3

y=2

z=1

**A few RBT visual examples
**

en s ar de f no ame ea all l be at s e enc d to t H ire eigh requ nary) h i (ord ot

Contd.

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

Theorems Related to RB Trees

Theorem 1: Any Red-Black Tree with root x has at 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 = 0 when h(x) > 0: (2bh(x)-1 – 1) + (2bh(x)-1 – 1) + 1 = 2bh(x) – 1

Theorems Related to RB Trees

Contd.

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.

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 Trees

Contd.

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 n >= 2bh(x) – 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2 lg(n + 1) >= h [Theorem 2] [Theorem 1]

**Operations on Red-Black Tree
**

Search Insertion Deletion

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)

**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

**Possible Rotations in Red Black Fixing
**

Types of Rotations: (a) Left Rotation, (b) Right Rotation Rotation is O(1) operation: simply requires update of fixed number of pointers

**Left Rotation: Rotate R around P
**

G P G

**Right Rotation: Rotate L around P
**

G P RL P

L

L

R L

L

R

L

L

R

R

L

R

R

L

L

L

R

R

L

R

R

L

L

R

R

L

R

R

Original Structure <BACK>

**Red-Black Trees
**

Bottom-Up Insertion

Bottom–Up Insertion

• • • Insert node as in BST Color the Node RED Two Red-Black properties may be violated?

1. 2. 3. 4. 5. Every node is Red or Black Root is Black Sentinel nodes are Black If node is Red, both children must be Black Every path from node to descendant leaf must contain the same number of Blacks

**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) children 3: (zig-zig) –X and its parent are both left or both right children

**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

Red Node

Black Node

Sentinel Node

Bottom-Up Insertion Case 0

X

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 Insertion Case 1
**

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 G P X U

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

**Bottom-Up Insertion Case 2
**

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 X

G P is left U X is right

X S

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

**Bottom-Up Insertion Case 3
**

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

G U S

P

X

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

**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)

Examples of Insertion

Let’s Insert: 96, 97, 99 45 18 10 25 50 5 15 20 30

Insert 96 as RED: No Needs fixing Insert 97 as RED: fixing needed case 2 (zig-zag)

80 60 75 55 70 65 72 77 82 84 86 92 88 95 97 98 96 98 96 97

Examples of Insertion

Let’s Insert: 96, 97, 99 45 18 10 25 50 5 15 20 30 55 70 65 72 60 75 77 84 82 86 92 96 80 88 95

Contd.

97

Case 1

98

Case 1

99

Insert 99 as RED: Needs fixing

Examples of Insertion

Let’s Insert: 3, 56, 57 60 18 18

Case 3 (zig-zig)

Contd.

80 88 75 45 55 65 77 84 82 86 92 96 95 97 98 99

45

10 25 50

5 15 20 30 3

Case 1 Insert 3 as RED: Needs fixing

Examples of Insertion

Let’s Insert: 3, 56, 57 60 18 10 5 3 20 30 15 25 50

Case 1

Contd.

80

Case 0

88 75 65 77 84 82 86 92 96

Case 1

95 97 98 99

45 55

Case 1

56 57

Insert 5657 as RED: Needs fixing Insert as RED: No fixing needed

**Red-Black Trees
**

Bottom-Up Deletion (starting with BST delete)

**Ordinary BST Delete
**

1. 3. 5. Leaf: just delete it. Has just one child: replace it with that child 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)

**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

**RB Properties Verification
**

Let’s analyze the effect of simple BST Delete on RBT Properties 3. If U is RED? Not a problem – no RB properties violated 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 needed Sibling of a non-root BLACK node is guaranteed to exist

5.

**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.

**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

**Bottom-Up Deletion Case 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 U V S

Case 1 Diagram P V+ S

Rotate

P V+ S

S P V+

Recolor

**Bottom-Up Deletion Case 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 U V S

Case 2 diagram

Recolor and absorb

P V+

P+ S

S

V

Either extra black absorbed by P or P now has extra blackness

Bottom-Up Deletion

• 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

}

U V

Case 3

P S

Case 3 Diagrams P V+ S P

Rotate

P V+ S S

Recolor

V+

Bottom-Up Deletion

• 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

}

Case 4

• This is the terminal case It does not cause recursive call

P U V S

Case 4 diagrams P V+ S

Rotate

P V+ S

S P V

Recolor

**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)

Examples of Deletion

Let’s Delete: 20, 25, 30 45 18 30 10 25 50 5 15 20 30

Delete 25 30 (BLACK): Fixing needed (BLACK): Needs trivial fixing Delete 20 (RED): No fixing Case 4 Extra blackness U=30 will absorbed by V=30 Extra blackness of of U=25 is be absorbed by P=18

80 60 75 55 70 65 72 77 82 84 86 92 88 95 97

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.a

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd