Data Structures2

CS6045: Advanced Algorithms
Data Structures
Red-Black Trees
• Red-black trees:
– A variation of binary search trees
– Operations designed to guarantee that the height h = O(lg n)
• Red-black trees 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
Note: this means every red node has 2 children and can’t 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
Red-Black Trees
• Height of a node: number of edges in a longest path to a leaf
• black-height of a node x: bh(x) is # black nodes on path from x
to leaf, without counting x
Red-Black Tree?
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 Tree?
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
Implications of the Properties
• If a red node has any children, it must have two
children and they must be _____
• A: Black
• If a black node has only one child that child must
be a ____________
• A: Red leaf
Height of Red-Black Trees
• What is the minimum black-height bh(x) of a node
x with height h?
• A: a height-h node has 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
Height of Red-Black Tree
• Claim: A subtree rooted at a node x contains at least
2bh(x) - 1 internal nodes
• Proof: by induction
– 1. x is a leaf, bh(x) = 0, # internal
node = 20-1 = 0
– 2. height of x be h and bh(x) = b,
– Any child of x had height h -1 and black height bh(x) either b
(if the child is red) or b -1 (if the child is black)
– By induction, each child of x has >= 2bh(x)-1 – 1 internal nodes
– 3. the subtree rooted at x contains >= 2(2bh(x)-1 – 1 ) + 1 =
2bh(x) – 1 internal nodes
Height of Rad-Black Tree
• Theorem: A red-black tree with n internal nodes
has height h  2 lg(n + 1)
• Proof: by previous claim, the subtree rooted at a
node x contains >= 2bh(x) - 1 internal nodes
• A height h node has black height >= h/2
• Thus
n >= 2h/2 – 1
n + 1 >= 2h/2
lg(n+1) >= h/2
h <= 2lg(n+1)
• A red-black tree has O(lg n) height
RB Trees: Worst-Case Time
• Corollary: 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
7
5
9
12
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
8
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
12
Red-Black Trees:
The Problem With Insertion
• Insert 8
– Where does it go?
– What color
should it be?
7
5
9
8
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
12
Red-Black Trees:
The Problem With Insertion
• Insert 8
– Where does it go?
– What color
should it be?
7
5
9
8
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
12
Red-Black Trees:
The Problem With Insertion
• Insert 11
7
– Where does it go?
5
9
8
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
12
Red-Black Trees:
The Problem With Insertion
• Insert 11
– Where does it go?
– What color?
7
5
9
8
12
11
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?
• Can’t be red! (#3)
7
5
9
8
12
11
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?
• Can’t be red! (#3)
• Can’t be black! (#4)
7
5
9
8
12
11
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
9
8
12
11
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
7
– Where does it go?
5
9
8
12
11
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
9
8
12
11
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?
5
• A: no color! Tree
is too imbalanced
• Must change tree structure
to allow recoloring
– Goal: restructure tree in
O(lg n) time
7
9
8
12
11
10
RB Trees: Rotation
• Basic tree-restructuring structure is called rotation:
y
x
x
rightRotate(y)
C
A
y
leftRotate(x)
A
B
B
C
• Left rotation and right rotation are inverses of each
other
RB Trees: Rotation
y
x
A
x
rightRotate(y)
C
A
B
y
B
C
• Only pointers are changed
–
–
–
–
x keeps its left child
y keeps its right child
x’s right child becomes y’s left child
x’s and y’s parents change
• Does rotation preserve inorder key ordering?
Rotation Example
RB Trees: Rotation
• Running Time
– O(1)
//constant number of points are modified
Red-Black Trees: Insertion
• Insertion: the basic idea
– Insert x into tree, color x red
– Only r-b property 3 might be violated (if x.p is red)
• If so, move violation node up tree until a place is
found where it can be fixed (till x.p is black)
– Total time will be O(lg n)
RB Insert: Case 1
• Case 1: “uncle” is red
• In figures below, all ’s are
equal-black-height subtrees
if (y.color == RED)
z.p.color = BLACK;
y.color = BLACK;
z.p.p.color = RED;
z = z.p.p;
C
case 1
A

A
D y
B z

C new z



if z is a right child

D
B




Change colors of some nodes, preserving #4: all downward paths have equal bh
The while loop now continues with x’s grandparent as the new x
RB Insert: Case 1
• Case 1: “uncle” is red
• In figures below, all ’s are
equal-black-height subtrees
if (y.color == RED)
z.p.color = BLACK;
y.color = BLACK;
z.p.p.color = RED;
z = z.p.p;
C
case 1
A
B z


C new z
A
D y


if z is a left child

B z

D



Same action whether x is a left or a right child

RB Insert: Case 2
if (z == z.p.right)
z = z.p;
LEFT-ROTATE(T,z);
// continue with case 3 code
C
A

y
• Case 2:
– “Uncle” is black
– Node z is a right child
• Transform to case 3 via a
left-rotation
C
case 2
B
B z


A z


y

Transform case 2 into case 3 with a left rotation
This preserves property 4: all downward paths contain same number of black nodes
RB Insert: Case 3
• Case 3:
z.p.color = BLACK;
z.p.p.color = RED;
RIGHT-ROTATE(T,z.p.p);
– “Uncle” is black
– Node z is a left child
• Change colors; rotate right
C
B
A z


y
B
case 3
z A

C




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 z’s parent is a left child
• If z’s parent is a right child, cases 4-6 are
symmetric (swap left for right)
• Running time: O(lg n)
Red-Black Trees: Deletion
• Rules for BST Deletion
– If vertex to be deleted is a leaf, just delete it
– If vertex to be deleted has just one child, replace it with
that child
– If vertex to be deleted has two children, replace it with
it’s successor, then goes to the first 1 and 2 cases
Review BST Delete
• 3 cases:
– x has no children: Remove x
– x has one child: Splice out x
7
7
5
8
3
2
6
4
5
9
9
3
6
2
z
6
4
z
6
– x has two children:
• Swap x with successor
• Perform case 1 or 2 to delete it
7
5
2
8
6
1 3
5
9
6
4
7
3
8
6
1 2
5
9
6
4
7
3
1 4
z
8
6
9
6
exchange
successor(z)
Red-Black Tree Deletion
• If the deleted node is red?
– Not a problem,
– No r-b properties violated
• If the deleted node is black?
– If the node is not the root, deleting it will change the
black height along some path
Red-Black Deletion
• The goal of r-b deletion
– Delete a red leaf
• General idea
– As we traverse the tree looking for the leaf to delete, we
change every node we encounter to red
– If this causes a violation of the r-b properties, we fix it
Red-Black: Deletion
• Base case: check the root
• Make the root red
• Move x to the appropriate child of the root
• Proceed to the main case
Red-Black: Deletion
• Main case: x has at least on red
child
– Down the tree to the next level
– If the new x is red, continue down
• Case 1: x has two black children,
x’s sibling is red
• Case 2: x has two black children,
sibling has 2 black children
• Case 3: x has two black children,
sibling’s left child is Red
• Case 4: x has two black children,
sibling’s right child is Red
Red-Black: Deletion
• Terminate case:
– Find the node to be deleted – a red leaf or a node with
one child that is a leaf (case 1 or 2 of BST deletion)
– Case 1: delete the appropriate node (a red leaf)
– Case 2: delete the appropriate node and color its child
as black
– Color the root black
RB Delete: Case 1
• Case 1: “sibling” t is red
B
x A

D
case 1
B
D t

C
 
E
x A
  
E

C

Make t black and x.p red; then left rotate on x.p

RB Delete: Case 2
• Case 2: “sibling” t is black, and both of t’s children are black
B
x A

B
case 2
A
D t


E
C
 
D


Make x, t red and
If x.p was red, make it black
Down the tree to the next level
C
 
E

RB Delete: Case 3
• Case 3: “sibling” t is black, t’s left child is red, and t’s right child
is black
B
x A

x A
D t

B
case 3
C
 

E

C


D

E
 
Make t red and t’s left child black, then right rotate on t
RB Delete: Case 4
• Case 4: “sibling” t is black, and t’s right child is red
B
x A

D
case 4
B
D t

C
 
E
A
  
E

C

Make x, and t red, and make t’s right child and x.p black
then left rotate on x.p

RB Delete: Case 5 - 8
• Cases 1-4 hold if x is a left child
• If x is a right child, cases 5-8 are symmetric (swap
left for right)
• Running time: O(lg n)
Example 1
• Delete 10
x
Base Case
Example 1
• Delete 10
x
Main Case
Example 1
• Delete 10
x
Main Case
Example 1
• Delete 10
x
Example 1
• Delete 10
Example 1
• Delete 10
Example 2
• Delete 10
x
Base Case
Example 2
• Delete 10
x
Main Case
Example 2
• Delete 10
x
Case 2
Example 2
• Delete 10
x
Case 2
Example 2
• Delete 10
Example 2
• Delete 10
Example 3
• Delete 11
Base Case
x
Example 3
• Delete 11
x
Main Case
Example 3
• Delete 11
x
Case 1
Example 3
• Delete 11
x
Example 3
• Delete 11
x
Case 2
Example 3
• Delete 11
x
Case 2
Example 3
• Delete 11
Example 3
• Delete 11