Title: Analysis of Algorithms CS 477/677
1Analysis of AlgorithmsCS 477/677
- Red-Black Trees
- Instructor George Bebis
- (Chapter 14)
2Red-Black Trees
- Balanced binary search trees guarantee an
O(lgn) running time - Red-black-tree
- Binary search tree with an additional attribute
for its nodes color which can be red or black - Constrains the way nodes can be colored on any
path from the root to a leaf -
- Ensures that no path is more than twice as long
as any other path ? the tree is balanced
3Example RED-BLACK-TREE
26
17
41
NIL
NIL
30
47
38
50
NIL
NIL
NIL
NIL
NIL
NIL
- For convenience we use a sentinel NILT to
represent all the NIL nodes at the leafs - NILT has the same fields as an ordinary node
- ColorNILT BLACK
- The other fields may be set to arbitrary values
4Red-Black-Trees Properties
- (Satisfy the binary search tree property)
- Every node is either red or black
- The root is black
- Every leaf (NIL) is black
- If a node is red, then both its children are
black - No two consecutive red nodes on a simple path
- from the root to a leaf
- For each node, all paths from that node to
descendant leaves contain the same number of
black nodes
5Black-Height of a Node
- Height of a node the number of edges in the
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
a leaf, - not counting x
6Most important property of Red-Black-Trees
- A red-black tree with n internal nodes
- has height at most 2lg(n 1)
- Need to prove two claims first
7Claim 1
- Any node x with height h(x) has bh(x) h(x)/2
- Proof
- By property 4, at most h/2 red nodes on the path
from the node to a leaf - Hence at least h/2 are black
8Claim 2
- The subtree rooted at any node x contains at
least 2bh(x) - 1 internal nodes
9Claim 2 (contd)
- Proof By induction on hx
- Basis hx 0 ?
- x is a leaf (NILT) ?
- bh(x) 0 ?
- of internal nodes 20 - 1 0
- Inductive Hypothesis assume it is true for
hxh-1
x
NIL
10Claim 2 (contd)
- Inductive step
- Prove it for hxh
- Let bh(x) b, then any child y of x has
- bh (y)
- bh (y)
b (if the child is red), or
b - 1 (if the child is black)
NIL
NIL
NIL
NIL
NIL
NIL
11Claim 2 (contd)
- Using inductive hypothesis, the number of
internal nodes for each child of x is at least - 2bh(x) - 1 - 1
- The subtree rooted at x contains at least
- (2bh(x) - 1 1) (2bh(x) - 1 1) 1
- 2 (2bh(x) - 1 - 1) 1
- 2bh(x) - 1 internal nodes
h
h-1
bh(l)bh(x)-1 bh(r)bh(x)-1
12Height of Red-Black-Trees (contd)
- Lemma A red-black tree with n internal nodes has
height at most 2lg(n 1). - Proof
- n
- Add 1 to both sides and then take logs
- n 1 2b 2h/2
- lg(n 1) h/2 ?
- h 2 lg(n 1)
root
height(root) h
bh(root) b
r
l
2b - 1
2h/2 - 1
number n of internal nodes
since b ? h/2
13Operations on Red-Black-Trees
- The non-modifying binary-search-tree operations
MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR, and
SEARCH run in O(h) time - They take O(lgn) time on red-black trees
- What about TREE-INSERT and TREE-DELETE?
- They will still run on O(lgn)
- We have to guarantee that the modified tree will
still be a red-black tree
14INSERT
- INSERT what color to make the new node?
- Red? Lets insert 35!
- Property 4 is violated if a node is red, then
both its children are black - Black? Lets insert 14!
- Property 5 is violated all paths from a node to
its leaves contain the same number of black nodes
15DELETE
- DELETE what color was the
- node that was removed? Black?
- Every node is either red or black
- The root is black
- Every leaf (NIL) is black
- If a node is red, then both its children are
black - For each node, all paths from the node to
descendant leaves contain the same number of
black nodes
OK!
OK!
16Rotations
- Operations for re-structuring the tree after
insert and delete operations on red-black trees - Rotations take a red-black-tree and a node within
the tree and - Together with some node re-coloring they help
restore the red-black-tree property - Change some of the pointer structure
- Do not change the binary-search tree property
- Two types of rotations
- Left right rotations
17Left Rotations
- Assumptions for a left rotation on a node x
- The right child of x (y) is not NIL
- Idea
- Pivots around the link from x to y
- Makes y the new root of the subtree
- x becomes ys left child
- ys left child becomes xs right child
18Example LEFT-ROTATE
19LEFT-ROTATE(T, x)
- y ? rightx ?Set y
- rightx ? lefty ? ys left subtree becomes
xs right subtree - if lefty ? NIL
- then plefty ? x ? Set the parent relation
from lefty to x - py ? px ? The parent of x becomes the
parent of y - if px NIL
- then rootT ? y
- else if x leftpx
- then leftpx ? y
- else rightpx ? y
- lefty ? x ? Put x on ys left
- px ? y ? y becomes xs parent
20Right Rotations
- Assumptions for a right rotation on a node x
- The left child of y (x) is not NIL
- Idea
- Pivots around the link from y to x
- Makes x the new root of the subtree
- y becomes xs right child
- xs right child becomes ys left child
21Insertion
- Goal
- Insert a new node z into a red-black-tree
- Idea
- Insert node z into the tree as for an ordinary
binary search tree - Color the node red
- Restore the red-black-tree properties
- Use an auxiliary procedure RB-INSERT-FIXUP
22RB Properties Affected by Insert
OK!
- Every node is either red or black
- The root is black
- Every leaf (NIL) is black
- If a node is red, then both its children are
black - For each node, all paths
- from the node to descendant
- leaves contain the same number
- of black nodes
If z is the root ? not OK
OK!
23RB-INSERT-FIXUP Case 1
- zs uncle (y) is red
- Idea (z is a right child)
- ppz (zs grandparent) must be black z and
pz are both red - Color pz black
- Color y black
- Color ppz red
- z ppz
- Push the red violation up the tree
24RB-INSERT-FIXUP Case 1
- zs uncle (y) is red
- Idea (z is a left child)
- ppz (zs grandparent) must be black z and
pz are both red - color pz ? black
- color y ? black
- color ppz ? red
- z ppz
- Push the red violation up the tree
25RB-INSERT-FIXUP Case 3
- Case 3
- zs uncle (y) is black
- z is a left child
- Idea
- color pz ? black
- color ppz ? red
- RIGHT-ROTATE(T, ppz)
- No longer have 2 reds in a row
- pz is now black
26RB-INSERT-FIXUP Case 2
- Case 2
- zs uncle (y) is black
- z is a right child
- Idea
- z ? pz
- LEFT-ROTATE(T, z)
- ? now z is a left child, and both z and pz are
red ? case 3
27RB-INSERT-FIXUP(T, z)
- while colorpz RED
- do if pz leftppz
- then y ? rightppz
- if colory RED
- then Case1
- else if z rightpz
- then Case2
- Case3
- else (same as then clause with
right and left
exchanged) - colorrootT ? BLACK
28Example
Insert 4
11
y
z
y
z and pz are both red zs uncle y is red
z and pz are both red zs uncle y is black z is
a right child
z
z and pz are red zs uncle y is black z is a
left child
29RB-INSERT(T, z)
- y ? NIL
- x ? rootT
- while x ? NIL
- do y ? x
- if keyz lt keyx
- then x ? leftx
- else x ? rightx
- pz ? y
30RB-INSERT(T, z)
- if y NIL
- then rootT ? z
- else if keyz lt keyy
- then lefty ? z
- else righty ? z
- leftz ? NIL
- rightz ? NIL
- colorz ? RED
- RB-INSERT-FIXUP(T, z)
31Analysis of RB-INSERT
- Inserting the new element into the tree O(lgn)
- RB-INSERT-FIXUP
- The while loop repeats only if CASE 1 is executed
- The number of times the while loop can be
executed is O(lgn) - Total running time of RB-INSERT O(lgn)
32Red-Black Trees - Summary
- Operations on red-black-trees
- SEARCH O(h)
- PREDECESSOR O(h)
- SUCCESOR O(h)
- MINIMUM O(h)
- MAXIMUM O(h)
- INSERT O(h)
- DELETE O(h)
- Red-black-trees guarantee that the height of the
tree will be O(lgn)
33Problems
- What is the ratio between the longest path and
the shortest path in a red-black tree? - - The shortest path is at least bh(root)
- - The longest path is equal to h(root)
- - We know that h(root)2bh(root)
- - Therefore, the ratio is 2
-
34Problems
- What red-black tree property is violated in the
tree below? How would you restore the red-black
tree property in this case? - Property violated if a node is red, both its
children are black - Fixup color 7 black, 11 red, then right-rotate
around 11
35Problems
- Let a, b, c be arbitrary nodes in subtrees ?, ?,
? in the tree below. How do the depths of a, b, c
change when a left rotation is performed on node
x? - a increases by 1
- b stays the same
- c decreases by 1
36Problems
- When we insert a node into a red-black tree, we
initially set the color of the new node to red.
Why didnt we choose to set the color to black? - (Exercise 13.4-7, page 294) Would inserting a new
node to a red-black tree and then immediately
deleting it, change the tree?