Red Black Trees
Red-Black Tree - Introduction
- Motivation: Keep search tree (relatively) balanced
- Deepest leaf is no more than twice as deep as shallowest leaf
- Basic ideas:
- Simulate 2-3-4 Tree in a binary tree
- Each node is colored red or black
- Red nodes are part of their (black) parent in the simulate 2-3-4 tree
- Invented by Bayer (1972)
- AKA: Symmetric binary B-Trees or half-balanced trees
Properties of Red-Black Trees -
- Red-Black Trees have the following properties:
- Every node has a color
- The root is black
- Every leaf is a special node called NIL (with no key)
- NIL is black
- If a node is red, then it's children are black [ie no 2 red trees in a row]
- Every path from root to leaf has the same number of black nodes
- Question: I understand the properties, but what good are they?
- Answer: The properties allow binary trees to simulate 2-3-4 trees!
- 2-3-4 Trees are guaranteed to remain balanced!
- 2-3-4 Trees are hard to implement directly: red-black simulations
are easier
- Let's look at 2-3-4 Trees and then
finish red-black trees
Connection Between 2-3-4 and Red-Black Trees
- Red nodes can be considered to be an additional value in the parent
- B / B, B means what?
- B / R, B means what?
- B / B, R means what?
- B / R, R means what?
Example RB Tree
- Nodes in each tree level:
- 26
- 17, 41'
- nil, nil, 30, 47
- -,-,-,-, nil, 38', nil, 50'
- nil below 30' and 50'
- Nodes whose values have a prime mark (ie ') are red
- To what 2-3-4 tree does this correspond?
Example RB Tree
- Let's review the RB properties:
- Every node has a color
- The root is black
- Every leaf is a special node called NIL (with no key)
- NIL is black
- If a node is red, then it's children are black [ie no 2 red trees in a row]
- Every path from root to leaf has the same number of black nodes
Left and Right Rotate
- Consider a tree in which x and y are values and α, β, and γ are subtrees
- Contents of each level before right and after left rotate:
- Contents of each level before left and after right rotate:
- Changes root and shifts β from x to y
RB Insert
- Steps:
- First, insert node z the normal way: O(lg n)
- Next, color the node z red
- Finally, fix up the tree to restore RB/2-3-4 properties
- What problems can occur:
- Property 2: z is the root (must be red)
- Property 5: z's parent is red (2 red nodes in a row)
- RB-Insert-Fixup (see example p 317):
- If z is a root, simply color it black
- If z's parent is red, then z's grandparent must be black (why)
- Let y be z's grandparent's other child
- Fix depends on whether y is red or black:
RB Insert
Fixup(T, z)
while z.p.color = Red loop
If z is on left side of z's grandparent Then
y := right side of z's grandparent
If y.color = red then -- Case 1
z.p.color := Black
y.color := Black
z.p.p.color := Red
z := z.p.p
else -- Case 2 or Case 3
if z is on right of z's parent -- Case 2
z := z.p
LeftRotate(T, Z)
end if;
z.p.color := Black -- Case 3
z.p.p.color := Red
RightRotate(T, Z)
end if
else z is on right side of z's grandparent
-- same as left, but with left and right reversed
end loop
What is the performance of fixup/insert: O(lg n)