Red-Black Trees

1 / 69

# Red-Black Trees

## Red-Black Trees

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Red-Black Trees DefinitionsandBottom-Up Insertion

2. Red-Black Trees • Definition: A red-black tree is a binary search tree in which: • Every node is colored 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 NULL contains the same number of Black nodes. • By convention, the root is Black • Definition: The black-height of a node, X, in a red-black tree is the number of Black nodes on any path to a NULL, not counting X. UMBC CSMC 341 Red-Black-Trees-1

3. X A Red-Black Tree with NULLs shown Black-Height of the tree (the root) = 3Black-Height of node “X” = 2 UMBC CSMC 341 Red-Black-Trees-1

4. A Red-Black Tree with Black-Height = 3 UMBC CSMC 341 Red-Black-Trees-1

5. X Black Height of the tree? Black Height of X? UMBC CSMC 341 Red-Black-Trees-1

6. 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? • NULLs are Black? • If node is Red, both children must be Black? • Every path from node to descendant NULL must contain the same number of Blacks? UMBC CSMC 341 Red-Black-Trees-1

7. Bottom Up Insertion • Insert node; Color it Red; X is pointer to it • Cases 0: X is the root -- color it Black 1: Both parent and uncle are Red -- color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation. 2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children -- color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent 3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children -- color parent Black, color grandparent Red, rotate right(left) on grandparent UMBC CSMC 341 Red-Black-Trees-1

8. G X P U G X P U Case 1 – U is Red Just Recolor and move up UMBC CSMC 341 Red-Black-Trees-1

9. G P U X X S P G Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X S U UMBC CSMC 341 Red-Black-Trees-1

10. G P U S P X X G Case 3 – Zig-Zig Single Rotate P around G Recolor P and G U S UMBC CSMC 341 Red-Black-Trees-1

11. UMBC CSMC 341 Red-Black-Trees-1

12. 11 Insert 4 into this R-B Tree 14 2 15 1 7 5 8 Red node Black node UMBC CSMC 341 Red-Black-Trees-1

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

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

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

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

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

18. 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, U, must be black because it (a) was initially black, 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)

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

20. 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, 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)

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

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

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

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

25. Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree UMBC CSMC 341 Red-Black-Trees-1

26. Top-Down Insertion An alternative to this “bottom-up” insertion is “top-down” insertion. Top-down is iterative. It moves down the tree, “fixing” things as it goes. What is the objective of top-down’s “fixes”? UMBC CSMC 341 Red-Black-Trees-1

27. Red Black Trees Top-Down Deletion

28. Recall the 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 the value of by it’s in-order predecessor’s value then delete the in-order predecessor (a recursive step)

29. What can go wrong? • If the delete node is red?Not a problem – no RB 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

30. 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, we change every node we encounter to red. • If this causes a violation of the RB properties, we fix it

31. 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

32. Terminology • Matching Weiss text section 12.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. As usual, there are left-right symmetric cases.

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

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

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

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

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

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