You are on page 1of 81

Red-Black Trees

by Thomas A. Anastasio

1

A Red-Black Tree with NULLs shown Black-Height of the tree = 4

2

Red-Black Trees
• Definition: A red-black tree is a binary search tree where:
– – – – Every node is either red or black. Each NULL pointer is considered to be a black node If a node is red, then both of its children are black. Every path from a node to a leaf contains the same number of black nodes.

• Definition: The black-height of a node, n, in a red-black tree is the number of black nodes on any path to a leaf, not counting n.
3

A valid Red-Black Tree Black-Height = 2

4

5 .

6 .

7 . Proof: by induction on height of x. has at least n = 2bh(x) – 1 internal nodes.Theorem 1 – Any red-black tree with root x. where bh(x) is the black height of node x.

Proof – If there is a red node on the path.Theorem 2 – In a red-black tree. 8 . there must be a corresponding black node. at least half the nodes on any path from the root to a leaf must be black.

N. there can no more than twice as many nodes on any path from N to a leaf as on any other path. no path from any node. every path from a node to any leaf contains the same number of black nodes. By Theorem 2. Proof: By definition. Therefore the length of every path is no more than twice as long as any other path 9 . to a leaf is more than twice as long as any other path from N to any other leaf.Theorem 3 – In a red-black tree. a least ½ the nodes on any such path are black. Therefore.

n >= 2bh(x) . bh(x) >= h/2 From Theorem 1.Theorem 4 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1).1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h 10 . By Theorem 2. Proof: Let h be the height of the red-black tree with root x.

Bottom –Up Insertion • Insert node as usual in BST • Color the Node RED • What Red-Black property may be violated? – – – – Every node is Red or Black Leaf nodes are Black NULLS If node is Red. both children must be Black Every path from node to descendant leaf must contain the same number of Blacks 11 .

color grandparent red. point X to grandparent. rotate right on grandparent 12 . color grandparent red.Bottom Up Insertion • Insert node. color X black.color it black 1: Both parent and uncle are red -. X is pointer to it • Cases 0: X is the root -. rotate right on grandparent 3 (zig-zig): Parent is red. rotate left on parent. X and its parent are opposite type children -. but uncle is black.color grandparent red. Color it RED.color parent and uncle black. but uncle is black. X and its parent are both left or both right children -. check new situation 2 (zig-zag): Parent is red.color parent black.

G P X U G P X U Case 1 – U is Red Just Recolor and move up 13 .

G P S X U X P G Case 2 – Zig-Zag Double Rotate X around P. X around G Recolor G and X S U 14 .

G P X S U P X G Case 3 – Zig-Zig Single Rotate P around G Recolor P and G S 15 U .

Insert 4 into this R-B Tree 1 11 2 7 5 8 14 15 Black node Red node 16 .

7 into an initially empty Red-Black Tree 17 . 1. 9.Insertion Practice Insert the values 2. 6. 3. 4. 5.

Asymptotic Cost of Insertion • O(lg n) to descend to insertion point • O(1) to do insertion • O(lg n) to ascend and readjust == worst case only for case 1 • Total: O(log n) 18 .

Red-Black Trees Bottom-Up Deletion .

replace it with that child 3. If vertex to be deleted has two children. just delete it. If vertex to be deleted has just one child. 2. If vertex to be deleted is a leaf.Recall “ordinary” BST Delete 1. replace the value of by it’s in-order predecessor’s value then delete the inorder predecessor (a recursive step) .

2. What can go wrong?? . think of deletion as replacing with the NULL pointer. U. If deleted node. V. Do ordinary BST deletion. think of deletion as replacing U with V.Bottom-Up Deletion 1. If U had one child. is a leaf. Eventually a “case 1” or “case 2“ will be done (leaf or just one child). V.

deleting it will change the black-height along some path .Which RB Property may be violated after deletion? 1. If U is black? If U is not the root. If U is red? Not a problem – no RB properties violated 2.

or propagated up to the root and out of the tree. There are symmetric cases for V as a right child .Fixing the problem • Think of V as having an “extra” unit of blackness. This extra blackness must be absorbed into the tree (by a red node). • There are four cases – our examples and “rules” assume that V is a left child.

Terminology • The node just deleted was U • The node that replaces it is V. which has an extra unit of blackness • The parent of V is P • The sibling of V is S Black Node Red Node Red or Black and don’t care .

is Red – Rotate S around P and recolor S & P • NOT a terminal case – One of the other cases will now apply • All other cases apply when S is Black . S.Bottom-Up Deletion Case 1 • V’s sibling.

Case 1 Diagram P V+ S V+ S Rotate S P Recolor P V+ .

it now has extra blackness and problem has been propagated up the tree . S. we’re done • If P is Black.Bottom-Up Deletion Case 2 • V’s sibling. is black and has two black children. – Recolor S to be Red – P absorbs V’s extra blackness • If P is Red.

Case 2 diagram P V+ Recolor and absorb P+ S S V Either extra black absorbed by P or P now has extra blackness .

and color S’s Right child Black • This is the terminal case – we’re done .Bottom-Up Deletion Case 3 • S is black • S’s RIGHT child is RED (Left child either color) – Rotate S around P – Swap colors of S and P.

Case 3 diagrams P V+ S V S Rotate S P P V Recolor .

Bottom-Up Deletion Case 4 • S is Black. S’s right child is Black and S’s left child is Red – Rotate S’s left child around S – Swap color of S and S’s left child – Now in case 3 .

Case 4 Diagrams P V+ S P P Rotate V+ S V+ S Recolor .

Delete 70 . Delete 80. in the order specified Delete 90.65 50 10 70 80 60 90 62 Perform the following deletions.

Red Black Trees Top-Down Insertion .

“ordinary” BST insertion was used. followed by correction of the tree on the way back up to the root • This is most easily done recursively – Insert winds up the recursion on the way down the tree to the insertion point – Fixing the tree occurs as the recursion unwinds .Review of Bottom-Up Insertion • In B-Up insertion.

T-Down insertion can be done iteratively which is generally faster . no further corrections are needed. the corrections are done while traversing down the tree to the insertion point.Top-Down Insertion Strategy • In T-Down insertion. • So. • When the actual insertion is done. so no need to traverse back up the tree.

Goal of T-D Insertion • Insertion is always done as a leaf (as in ordinary BST insertion) • Recall from the B-Up flow chart that if the uncle of a newly inserted node is black. we restore the RB tree properties by one or two local rotations and recoloring – we do not need to make changes further up the tree .

Goal (2) • Therefore. but not propagate back up the tree . the uncle is Black. • That way we may have to rotate and recolor. the goal of T-D insertion is to traverse from the root to the insertion point in such a way that RB properties are maintained. and at the insertion point.

there is no problem since the new node’s parent is black .Possible insertion configurations X (Red or Black) Y Z If a new node is inserted as a child of Y or Z.

done . no problem since the new node’s uncle (Z) is black – do a few rotations and recolor…. no problem since Z is black. If new node is child of Y.Possible insertion configurations X Y Z If new node is child of Z.

Possible insertion configurations X Y Z If new node is inserted as child of Y or Z. This is the only case we need to avoid. . it’s uncle will be red and we will have to go back up the tree.

Remember the goal – to create an insertion point at which the parent of the new node is Black.Top-Down Traversal X Y Z As we traverse down the tree and encounter this case. or the uncle of the new node is black. . There are 3 cases. we recolor and possible do some rotations.

Case 1 – X’s Parent is Black P P X Y Z Y X Z Just recolor and continue down the tree .

or (b) would have been made black when we encountered G (which would have had two red children -.X’s Parent and X’s uncle) . must be black because it (a) was initially black. U.Case 2 • X’s Parent is Red (so Grandparent is Black) and X and P are both left/right children – Rotate P around G – Color P black – Color G red • Note that X’s uncle.

P and G .Case 2 diagrams G P X Y Z S Y U P X Z S U G Rotate P around G. Z. Y. Recolor X.

X’s Parent and X’s uncle) . or (b) would have been made black when we encountered G (which would have had two red children -.Case 3 • X’s Parent is Red (so Grandparent is Black) and X and P are opposite children – Rotate P around G – Color P black – Color G red • Again note that X’s uncle. U. must be black because it (a) was initially black.

Rotate X around P.Case 3 Diagrams (1 of 2) G P S Y X Z S U P Y X Z G U Step 1 – recolor X. Y and Z. .

Case 3 Diagrams (2 of 2) G X P Z U P Z X G S Y S Y U Step 2 – Rotate X around G. Recolor X and G .

An exercise – insert F D T L W J E K P V Z .

Z X Z Y G P Rotate P around G Recolor P.Y.G Y Z P G X Z G P Recolor X.Z X P Y Z Y Z Case 2 P is Red X & P both left/right P X Y Case 3 P is Red X and P are opposite children X Y G Recolor X.Z Rotate X around P Y Z G X P Z Rotate X around G Recolor X. G P X G Y Z .Y.Y.Top-Down Insert Summary Case 1 P is Black Just Recolor X P Recolor X.

Red Black Trees Top-Down Deletion .

If vertex to be deleted has just one child. replace the value of by it’s in-order predecessor’s value then delete the inorder predecessor (a recursive step) . If vertex to be deleted is a leaf. 2. replace it with that child 3. If vertex to be deleted has two children.Recall the rules for BST deletion 1. just delete it.

If the deleted node is black? If the node is not the root.What can go wrong? 1. If the delete node is red? Not a problem – no RB properties violated 2. deleting it will change the black-height along some path .

we fix it . we change every node we encounter to red. – If this causes a violation of the RB properties.The goal of T-D Deletion • To delete a red leaf • How do we ensure that’s what happens? – As we traverse the tree looking for the leaf to delete.

Bottom-Up vs. Top-Down • Bottom-Up is recursive – BST deletion going down the tree (winding up the recursion) – Fixing the RB properties coming back up the tree (unwinding the recursion) • Top-Down is iterative – Restructure the tree on the way down so we don’t have to go back up .

2 – – – – – X is the node being examined T is X’s sibling P is X’s (and T’s) parent R is T’s right child L is T’s left child • This discussion assumes X is the left child of P. there are left-right symmetric cases. .Terminology • Matching Weiss text section 12. As usual.

we know – P is also Red (we just came from there) – T is black (since P is Red.Basic Strategy • As we traverse the tree. • When we change X to Red. it’s children are Black) . to Red. we change every node we visit. X.

Make the root Red b.Step 1 – Examine the root 1. Move X to the appropriate child of the root c. Otherwise designate the root as X and proceed to step 2B. Proceed to step 2 2. If both of the root’s children are Black a. .

T is Black We are going to color X Red. X has 2 Black children 2B. P is Red. we continually encounter this situation until we reach the node to be deleted X is Black. X has at least one Red child .Step 2 – the main case As we traverse down the tree. then recolor other nodes and possibly do rotation(s) based on the color of X’s and T’s children 2A.

we can do either 2A2 or 2A3 X T . T has 2 Black Children 2A2. T’s left child is Red 2A3.Case 2A X has two Black Children P 2A1. T’s right child is Red ** if both of T’s children are Red.

P and T and move down the tree .Case 2A1 X and T have 2 Black Children P P X T X T Just recolor X.

Case 2A2 X has 2 Black Children and T’s Left Child is Red Rotate L around T. then L around P Recolor X and P then continue down the tree L P X T P X L2 L1 L2 T L L1 .

T and R then continue down the tree P X T R X L R1 R2 T P R1 R R2 L .Case 2A3 X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X. P.

P is Black) Rotate T around P Recolor P and T Back to main case – step 2 .Case 2B X has at least one Red child Continue down the tree to the next level If the new X is Red. continue down again If the new X is Black (T is Red.

the main case .Case 2B Diagram P X T Move down the tree. P P X T T X If move to Black child (2B2) If move to the Red child (2B1) Rotate T around P. Recolor P and T Move down again Back to step 2.

Delete the appropriate node as a Red leaf Step 4 Color the Root Black . find the node to be deleted – a leaf or a node with one non-null child that is a leaf.Step 3 Eventually.

moving X to 6 .Example 1 Delete 10 from this RB Tree 15 6 17 3 10 7 12 13 16 18 20 23 Step 1 – Root has 2 Black children. Color Root Red Descend the tree.

Descend down the tree.Example 1 (cont’d) 15 X 3 6 17 12 10 7 13 16 18 20 23 One of X’s children is Red (case 2B). . arriving at 10. Since the new X (12) is also Red (2B1). arriving at 12. continue down the tree.

Example 1 (cont’d) 15 6 17 3 10 7 12 16 13 18 20 23 X Step 3 -Since 10 is the node to be deleted. replace it’s value with the value of it’s only child (7) and delete 7’s red node .

Example 1 (cont’d) 15 6 3 7 12 16 17 20 13 18 23 The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black. .

Set X = root and proceed to step 2 .Example 2 Delete 10 from this RB Tree 15 6 17 3 2 4 10 12 13 16 20 Step 1 – the root does not have 2 Black children. Color the root red.

Example 2 (cont’d)
X
6 15 17

3
2 4 10

12
13

16

20

X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.

Example 2 (cont’d)
15 6

P

17

3
2

T
4

X
10

12
13

16

20

X has 2 Black children. X’s sibling (3) also has 2 black children. Case 2A1– recolor X, P, and T and continue down the tree, arriving at 10.

Example 2 (cont’d)
15 6 3 2 4 17

P X
10

12

16 13

20

T

X is now the leaf to be deleted, but it’s Black, so back to step 2. X has 2 Black children and T has 2 Black children – case 2A1 Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black

Example 2 Solution
15 6 3 2 4 12 13 16 17 20

Color Root red.Example 3 Delete 11 from this RB Tree 15 10 5 3 2 4 7 6 9 11 12 13 Valid and unaffected Right subtree Step 1 – root has 2 Black children. Set X to appropriate child of root (10) .

Example 3 (cont’d) 15 10 X 12 5 3 2 4 7 6 9 11 13 X has one Red child (case 2B) Traverse down the tree. arriving at 12. .

rotate T around P. recolor T and P Back to step 2 .Example 3 (cont’d) 15 10 P 5 3 2 4 T 7 6 9 X 11 12 13 Since we arrived at a black node (case 2B2) assuring T is red and P is black).

P and T and continue traversal .Example 3 (cont’d) 15 5 P 10 3 2 4 7 6 T 9 11 X 12 13 Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X.

Recolor the root black .Example 3 (cont’d) 15 5 10 3 2 4 7 6 9 11 P 12 X 13 T Having traversed down the tree.delete 11 as a red leaf. the leaf to be deleted. Step 4 -. Step 3 -. X and T both have two Black children. Recolor X. P and T. so back to step 2. we arrive at 11. but it’s Black.

Example 3 Solution 15 5 10 3 2 4 7 6 9 12 13 .