0% found this document useful (0 votes)
30 views

Lecture10 RBT

The document discusses red-black trees and their insertion algorithm. It begins by describing the properties of red-black trees, including that they guarantee logarithmic height. It then discusses the challenges of inserting nodes while maintaining the properties. There are three cases for fixing violations: recoloring nodes, rotating the tree structure, or a combination of both. The insertion algorithm uses these techniques of recoloring and rotation to resolve violations in O(log n) time.

Uploaded by

Somya Arya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lecture10 RBT

The document discusses red-black trees and their insertion algorithm. It begins by describing the properties of red-black trees, including that they guarantee logarithmic height. It then discusses the challenges of inserting nodes while maintaining the properties. There are three cases for fixing violations: recoloring nodes, rotating the tree structure, or a combination of both. The insertion algorithm uses these techniques of recoloring and rotation to resolve violations in O(log n) time.

Uploaded by

Somya Arya
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit 2

Red Black Trees

B Trees
Binomial Heaps Amortized Analysis (Not mentioned in

syllabus, but needs to be studied) Fibonacci Heaps Disjoint Sets Data Structures (Not mentioned in syllabus, but needs to be studied)

Red-Black Trees
Lecture 10

Red-Black Trees
Red-black trees:
Binary search trees augmented with node color
Operations designed to guarantee that the height

h = O(lg n)
First: describe the properties of red-black trees
Then: prove that these guarantee h = O(lg n) Finally: describe operations on red-black trees

Red-Black Properties
The red-black properties:

1. Every node is either red or black 2. Every leaf (NULL pointer) is black
Note: this means every real node has 2 children

3. If a node is red, both children are black


Note: cant have 2 consecutive reds on a path

4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Example: RED-BLACK-TREE
26 17
NIL NIL

41 30 47

NIL

38
NIL NIL

NIL

50
NIL NIL

For convenience we use a sentinel NIL[T] to represent

all the NIL nodes at the leafs


Color[NIL[T]] = BLACK

NIL[T] has the same fields as an ordinary node The other fields may be set to arbitrary values

Black-Height of a Node
26
h=1 bh = 1 NIL h=4 bh = 2

17
NIL h=2 30 bh = 1

41

h=3 bh = 2

NIL

h=1 bh = 1 38 NIL

47

h=2 bh = 1

50
NIL

h=1 bh = 1 NIL

NIL

NIL

Height of a node: the number of edges in a longest path to a leaf

Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x

Height of Red-Black Trees


black-height: # black nodes on path to leaf

A height-h node has black-height h/2


Theorem: A red-black tree with n internal

nodes has height h 2 lg(n + 1)

RB Trees: Proving Height Bound


Prove: n-node RB tree has height h 2 lg(n+1)

Claim: A subtree rooted at a node x contains

at least 2bh(x) - 1 internal nodes


Proof by induction on height h Base step: x has height 0 (i.e., NULL leaf node) What is bh(x)?

RB Trees: Proving Height Bound


Prove: n-node RB tree has height h 2 lg(n+1)

Claim: A subtree rooted at a node x contains

at least 2bh(x) - 1 internal nodes


Proof by induction on height h Base step: x has height 0 (i.e., NULL leaf node) What is bh(x)? A: 0 Sosubtree contains 2bh(x) - 1 = 20 - 1 = 0 internal nodes (TRUE)

RB Trees: Proving Height Bound


Inductive proof that subtree at node x contains

at least 2bh(x) - 1 internal nodes


Inductive step: x has positive height and 2 children Each child has black-height of bh(x) or bh(x)-1 The height of a child = (height of x) - 1 So the subtrees rooted at each child contain at least 2bh(x) - 1 - 1 internal nodes Thus subtree at x contains (2bh(x) - 1 - 1) + (2bh(x) - 1 - 1) + 1 = 22bh(x)-1 - 1 = 2bh(x) - 1 nodes

RB Trees: Proving Height Bound


Thus at the root of the red-black tree:

n 2bh(root) - 1 n 2h/2 - 1 lg(n+1) h/2 h 2 lg(n + 1)


Thus h = O(lg n)

RB Trees: Worst-Case Time


So weve proved that a red-black tree has

O(lg n) height These operations take O(lg n) time:


Minimum(), Maximum()

Successor(), Predecessor()
Search()

Insert() and Delete(): Will also take O(lg n) time But will need special care since they modify tree

Red-Black Trees: An Example


Color this tree
5 7 9 12

Red-black properties: 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Red-Black Trees: The Problem With Insertion


Insert 8
Where does it go?

7 5 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 8
Where does it go?
What color

7 5 8 9 12

should it be?

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 8
Where does it go?
What color

7 5 8 9 12

should it be?

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 11
Where does it go?

7 5 8 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 11
Where does it go?
What color?

7 5 8 11 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 11
Where does it go?
What color? Cant be red! (#3)

7 5 8 11 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 11
Where does it go?
What color? Cant be red! (#3) Cant be black! (#4)

7 5 8 11 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 11
Where does it go?
What color? Solution: recolor the tree

7 5 8 11 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 10
Where does it go?

7 5 8 11 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

Red-Black Trees: The Problem With Insertion


Insert 10
Where does it go?
What color?

7 5 8 11 9 12

1. 2. 3. 4. 5.

Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black

10

Red-Black Trees: The Problem With Insertion


Insert 10
Where does it go?
What color? A: no color! Tree is too imbalanced Must change tree structure to allow recoloring Goal: restructure tree in

7 5 8 11 10 9 12

O(lg n) time

RB Trees: Rotation
Our basic operation for changing tree structure is

called rotation:
y x A C
leftRotate(x)

rightRotate(y)

x A y

B B C Does rotation preserve inorder key ordering? What would the code for rightRotate() actually do?

RB Trees: Rotation
y x A B C
rightRotate(y)

x A B y C

Answer: A lot of pointer manipulation x keeps its left child y keeps its right child xs right child becomes ys left child xs and ys parents change
What is the running time?

Rotation Example
Rotate left about 9:
7

5
8

9
12

11

Rotation Example
Rotate left about 9:
7

5
9

12

11

Red-Black Trees: Insertion


Insertion: the basic idea
Insert x into tree, color x red
Only r-b property 3 might be violated (if p[x] red) If so, move violation up tree until a place is found where it can be fixed Total time will be O(lg n)

rbInsert(T, x)
{treeInsert(T, x); color[x] = RED; while (color[p[x]] == RED) { if (p[x] == left[p[p[x]]])/*if xs parent is a left child*/ {y = right[p[p[x]]]; /*y is xs uncle */ if (color[y] == RED) { color[p[x]] = BLACK; Case 1: color[y] = BLACK; Uncle is color[p[p[x]]] = RED; x = p[p[x]];} RED else {if (x == right[p[x]]) /* x is a right child*/ Case 2: Uncle is {x = p[x]; BLACK & x is right leftRotate(x);} child color[p[x]] = BLACK; Case 3: Uncle is color[p[p[x]]] = RED; BLACK & x is left rightRotate(p[p[x]]);}} child else {(same as above, but with right & left exchanged)} } color[root[T]] = BLACK; }

RB Insert: Case 1
if (color[y] == RED) { color[p[x]] = BLACK; color[y] = BLACK; color[p[p[x]]] = RED; x = p[p[x]];}

Case 1: uncle is red

In figures below, all s are

equal-black-height subtrees

C A D y B x

case 1

C new x A D B

Change colors of some nodes, preserving #4: all downward paths have equal b.h. The while loop now continues with xs grandparent as the new x

RB Insert: Case 2
{if (x == right[p[x]]) {x = p[x]; leftRotate(x);}
// continue with case 3 code

Case 2: Uncle is black Node x is a right child Transform to case 3 via a

left-rotation C A

case 2

C B A x

B x

Transform case 2 into case 3 (x is left child) with a left rotation This preserves property 4: all downward paths contain same number of black nodes

RB Insert: Case 3
color[p[x]] = BLACK; color[p[p[x]]] = RED; rightRotate(p[p[x]]);

Case 3: Uncle is black Node x is a left child Change colors; rotate right

C B A x

case 3

B
x A

Perform some color changes and do a right rotation Again, preserves property 4: all downward paths contain same number of black nodes

RB Insert: Cases 4-6


Cases 1-3 hold if xs parent is a left child

If xs parent is a right child, cases 4-6 are

symmetric (swap left for right)

Analysis of Insertion
Insertion consists of two phases
First phase goes down the tree O(lg n)
Second phase goes up the tree fixing things While loop repeats only in case 1 moving the pointer up by two levels O(lg n) The most no of rotations performed are two O(1)

Total time = O(lg n)

Examples
Insert the following keys into an initially empty RB Tree 1. 41, 38, 31, 12, 19, 8 2. 8, 19, 12, 31, 38, 41

You might also like