Title: Red
1Red Black Trees
- 14332351
- Programming Methodology II
2Why Balance is Important
- Searches into an unbalanced search tree could be
O(n) at worst case
1
Very unbalanced search tree
2
3
4
5
3Review Binary Search Trees
- Binary Search Trees (BSTs) are an important data
structure for dynamic sets - In addition to data, elements have
- key an identifying field inducing a total
ordering - left pointer to a left child (may be NULL)
- right pointer to a right child (may be NULL)
- p. pointer to a parent node (NULL for root)
4Review Binary Search Trees
- BST property keyleft(x) ? keyx ?
keyright(x) - Example
5Review BST Insert
- Adds an element x to the tree so that the binary
search tree property continues to hold - The basic algorithm
- Like the search procedure above
- Insert x in place of NULL
- Use a trailing pointer to keep track of where
you came from (like inserting into singly linked
list) - Like search, takes time O(h), h tree height
6Review More BST Operations
- Minimum
- Find leftmost node in tree
- Successor
- x has a right subtree successor is minimum node
in right subtree - x has no right subtree successor is first
ancestor of x whose left child is also ancestor
of x - Intuition As long as you move to the left up the
tree, youre visiting smaller nodes.
7Review More BST Operations
- Delete
- x has no children
- Remove x
- x has one child
- Splice out x
- x has two children
- Swap x with successor
8Red-Black Trees
- Red-black tree is approximately balanced binary
search tree with following 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
9Black-Height
- black-height black nodes on path to leaf
- What is the minimum black-height of a node with
height h? - a height-h node has black-height ? h/2
- Suppose number of black nodes are 10, then the
minimum height can be 10 and maximum height of
the tree can be at most 19. Hence the maximum can
be at most 1 less than twice of its minimum
height.
10RB Trees Worst-Case Time
- These operations take O(log n) time
- Minimum(), Maximum()
- Successor(), Predecessor()
- Search()
- Insert() and Delete()
- Will also take O(log n) time
- But will need special care since they modify tree
11Red-Black Trees An Example
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
12Red-Black Trees The Problem With Insertion
- Insert 8
- Where does it go?
7
5
9
12
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
13Red-Black Trees The Problem With Insertion
- Insert 8
- Where does it go?
- What color should it be?
7
5
9
12
8
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
14Red-Black Trees The Problem With Insertion
- Insert 8
- Where does it go?
- What color should it be?
7
5
9
12
8
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
15Red-Black TreesThe Problem With Insertion
- Insert 11
- Where does it go?
7
5
9
12
8
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
16Red-Black TreesThe Problem With Insertion
- Insert 11
- Where does it go?
- What color?
7
5
9
12
8
11
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
17Red-Black TreesThe Problem With Insertion
7
- Insert 11
- Where does it go?
- What color?
- Cant be red! (3)
5
9
12
8
11
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
18Red-Black TreesThe Problem With Insertion
- Insert 11
- Where does it go?
- What color?
- Cant be red! (3)
- Cant be black! (4)
7
5
9
12
8
11
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
19Red-Black TreesThe Problem With Insertion
- Insert 11
- Where does it go?
- What color?
- Solution recolor the tree
7
5
9
12
8
11
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
20Red-Black TreesThe Problem With Insertion
- Insert 10
- Where does it go?
7
5
9
12
8
11
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
21Red-Black TreesThe Problem With Insertion
- Insert 10
- Where does it go?
- What color?
7
5
9
12
8
11
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
10
22Red-Black TreesThe Problem With Insertion
- Insert 10
- Where does it go?
- What color?
- A no color! Tree is too imbalanced
- Must change tree structureto allow recoloring
- Goal restructure tree in O(log n) time
7
5
9
12
8
11
10
23Rotation of Red-Black Trees
- Rotation of red-black trees.
- A structural change to the red-black trees.
- Insertion and deletion modify the tree, the
result may violate the properties of red-black
trees. To restore this properties rotations are
done. - We can have either of left rotation or right
rotation. -
24RB Trees Rotation
rightRotate(y)
C
leftRotate(x)
C
25RB Trees Rotation
- 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
y
x
rightRotate(y)
x
C
A
y
A
B
B
C
26Rotation Example
27Red-Black Trees Insertion
- Insertion the basic idea
- Insert x into tree, color x red
- Only r-b property 3 might be violated (if px
red) - If so, move violation up tree until a place is
found where it can be fixed - Total time will be O(lg n)
28- rbInsert(x)
- treeInsert(x)
- x-color RED
- // Move violation of 3 up tree, maintaining 4
as invariant - while (x!root x-p-color RED)
- if (x-p x-p-p-left)
- y x-p-p-right
- if (y-color RED)
- x-p-color BLACK
- y-color BLACK
- x-p-p-color RED
- x x-p-p
- else // y-color BLACK
- if (x x-p-right)
- x x-p
- leftRotate(x)
- x-p-color BLACK
- x-p-p-color RED
- rightRotate(x-p-p)
Note p Parent Node
29RB Insertion Cases
- xs parent is a left child
- Case 1 uncle is red
- Case 2
- Uncle is black
- Node x is a right child
- Case 3
- Uncle is black
- Node x is a left child
- xs parent is a right child
30RB Insert Case 1
- Case 1 uncle is red
- In figures below, all ?s are equal-black-height
subtrees
- if (y-color RED)
- x-p-color BLACK
- y-color BLACK
- x-p-p-color RED
- x x-p-p
New x
C
C
case 1
A
D
A
D
y
B
B
x
?
?
?
?
?
?
?
?
?
?
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
31RB Insert Case 1
- Case 1 uncle is red
- In figures below, all ?s are equal-black-height
subtrees
- if (y-color RED)
- x-p-color BLACK
- y-color BLACK
- x-p-p-color RED
- x x-p-p
New x
C
C
case 1
A
D
A
D
y
?
?
?
?
?
?
Same action whether x is a left or a right child
32RB Insert Case 2
- Case 2
- Uncle is black
- Node x is a right child
- Transform to case 3 via a left-rotation
- if (x x-p-right)
- x x-p
- leftRotate(x)
- // continue with case 3 code
C
C
case 2
A
B
y
y
?
?
?
?
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
33RB Insert Case 3
- Case 3
- Uncle is black
- Node x is a left child
- Change colors rotate right
- x-p-color BLACK
- x-p-p-color RED
- rightRotate(x-p-p)
B
C
case 3
A
B
x
y
?
C
?
?
?
?
?
Perform some color changes and do a right
rotation Again, preserves property 4 all
downward paths contain same number of black nodes
34RB 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)
35Red-Black Delete principle (1)
- Use ordinary binary search tree deletion.
- If any of the red-black properties have been
violated, fix the resulting tree using
re-coloring and rotations. - The five properties of Red Black trees
- Every node is either red or black
- The root is black.
- Every null leaf is black
- Both children of a red node are black
- All paths from a node to its descendant leafs
contain the same number of black nodes
36Example 1 deletion and fixup (1)
37Example 1 deletion and fixup (2)
No violation
38Red-Black Delete principle (2)
- If we delete a Red node tree still is a Red-Black
tree - Assume we delete a black node
- Let x be the child of deleted node
- If x is red, color it black and stop
- If x is black mark it double black
- Violations
- If the parent y of the spliced node x is red,
then properties 2, 4, 5 may be violated. - If x is red, re-coloring x black restores all of
them! - So we are left with cases where both x and y are
black. We need to restore property 5.
y
x
39RB Delete cases
- Four cases to fix this situation for node x
- Case 1 xs sibling w is red
- Case 2 xs sibling w is black, as well as both
children of w - Case 3 xs sibling w is black, ws left is red
and right is black - Case 4 xs sibling w is black, and ws right
child is red.
40RB-Delete-Fixup (pseudocode)
- RB-Delete-Fixup(T, x)
- while x ? rootT and colorx black
- do if x leftparentx
- then w ? xs brother
- if colorw red then do
Case 1 - // after this x stays, w changes
to xs new brother, and we are in Case 2 - if colorw black and its
two children are black - then do Case 2. // after
this x moves to parentx - else if colorw black
and colorrightw black
- then do Case 3
- // after this x stays, w changes
to xs new brother, and we are in Case 4 - if colorw black and
colorrightw red - then do Case 4 //
after this x rootT. - else same as everything above but
for x rightparentx - colorx ? black
41Case 1 xs sibling w is red
- Case 1 is transformed into one of the Cases 2, 3,
or 4 by switching the color of the nodes B and D
and performing a left rotation
B
w siblingx
A
D
C
E
No change in black height!
42Case 2 xs sibling w is black and both its
children are black
- Case 2 allows x to move one level up the tree by
re-coloring D to red
w siblingx
D
A
C
E
childrenw
Decreases black height of nodes under D by one!
43Case 3 xs sibling w is black and ws children
are red and black
- Case 3 is transformed to Case 4 by exchanging the
colors of nodes C and D and performing a right
rotation
B
w siblingx
D
A
C
E
childrenw
No change in black height!
44Case 4 xs sibling w is black and its right
children is red
- In this case, the violation is resolved by
changing some colors and performing a left
rotation without violating the red-black
properties
B
w siblingx
D
A
C
E
childrenw
Increases black height of nodes under A by one!
45RB-Delete
- To delete a node x from an RB-Tree, do
- Delete the node x from the binary tree
disregarding the colors. - Fix the resulting tree if necessary by applying
on x Cases 1, 2, 3, and 4 as required and
following their consequences - The complexity of the operation is O(log n)
46RB-Delete Complexity
- If Case 2 is entered from Case 1, then we do not
enter the loop again since xs parent is red
after Case 2. - If Case 3 or Case 4 are entered, then the loop is
not entered again. - The only way to enter the loop many times is to
enter through Case 2 and remain in Case 2. Hence,
we enter the loop at most O(h) times. - This yields a complexity of O(lg n).
47AVL Trees
- Balanced Trees After insert and delete
operations we fix the tree to keep it (almost)
balanced. - AVL Tree A binary search tree with the following
additional balance property For any node in the
tree, the height of the left and right subtrees
can differ by 1 at most. - Note that we require this balance property for
every node, not just the root.
48Example
12
12
16
8
8
16
14
10
4
14
4
10
2
6
2
6
1
An AVL tree
Not an AVL tree (look at node 8, 12)
49AVL Tree properties
- An insertion into an AVL tree requires at most
one rotation to rebalance a tree. - A deletion may require log(N) rotations to
rebalance the tree
50How to maintain balance
- General rule after an insert or delete
operation, we fix all nodes that got unbalanced. - Since the height of any subtree has changed by at
most 1, if a node is not balanced this means that
one son has a height larger by exactly two than
the other son. - Next we show the four possible cases that cause a
height difference of 2. In all figures, marked
nodes are unbalanced nodes.
51(only) Four imbalance cases
k2
k1
Case 1 The left subtree is higher than the right
subtree, and this is caused by the left subtree
of the left child.
Case 4The symmetric case to case 1
k1
k2
C
A
B
B
A
C
Case 2 The left subtree is higher than the right
subtree, and this is caused by the right subtree
of the left child.
k2
k1
Case 3The symmetric case to case 2
k1
k2
R
R
P
P
Q
Q
52Single Rotation - Fixing case 1
k2
k1
right rotation
k1
k2
C
B
A
B
C
A
- The rotation takes O(1) time. Notice that the new
tree is a legal search tree. - For insert - it must be the case that subtree A
had been increased, so after the rotation, the
tree has height as before the insert. - For delete, it must be the case that C had been
decreased, so after the rotation, the tree has
height shorter by 1.
53Example (caused by insertion)
12
12
k2
k1
8
16
16
4
k2
k1
C
A
14
10
4
8
14
2
B
C
B
A
6
2
10
6
1
1
- Notice that the tree height has not changed
after the rotation (since it was an insert
operation).
54Single Rotation for Case 4
k1
k2
left rotation
k2
k1
A
B
C
B
A
C
55Example (caused by deletion)
Deleting X and performing a single rotation
A
B
A
B
X
C
A
B
C
C
- For the rotation, k1 is node A, and k2 is node
B. We make k2 the root, and k1 its left son. - Notice that the tree height has changed after
the rotation (since it was a delete operation).
56Fixing case 2 - first try...
k2
k1
right rotation
k1
k2
C
A
A
C
B
B
Single rotation doesnt help - the tree is still
not balanced! What can we do? Use rotations on
k1s sub tree to reduce case 2 to case 1!
57Example (caused by insertion)
Note that above is a good friend of case 1
58Double Rotation to fix case 2
k3
k2
k3
left rotation on k1
right rotation on k3
k2
k1
k3
k1
k1
k2
D
D
B
C
C
A
A
D
B
C
B
A
- After insertion - original height (of the root)
stays the same.
59Double Rotation to fix case 3
k1
k2
right rotation on k3 left rotation on k1
k3
k3
k1
k2
A
B
C
D
A
D
B
C
60Additional Reference
- Java applet
- http//www.ece.uc.edu/franco/C321/html/RedBlack/r
edblack.html - http//www.cs.mcgill.ca/cs251/OldCourses/1997/top
ic18/RBTreeApplet.html - Introduction to Algorithms by Cormen, Thomas H.,
Charles E. Leiserson, and Ronald L. Rivest