Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-Black Trees Definitions and Bottom-Up Insertion.

Similar presentations


Presentation on theme: "Red-Black Trees Definitions and Bottom-Up Insertion."— Presentation transcript:

1 Red-Black Trees Definitions and Bottom-Up Insertion

2 8/3/2007UMBC CSMC 341 Red-Black-Trees-12 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.

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

4 8/3/2007UMBC CSMC 341 Red-Black-Trees-14 A Red-Black Tree with Black-Height = 3

5 8/3/2007UMBC CSMC 341 Red-Black-Trees-15 Black Height of the tree? Black Height of X? X

6 8/3/2007UMBC CSMC 341 Red-Black-Trees-16 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?

7 8/3/2007UMBC CSMC 341 Red-Black-Trees-17 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

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

9 8/3/2007UMBC CSMC 341 Red-Black-Trees-19 X P G U S X P G S U Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X

10 8/3/2007UMBC CSMC 341 Red-Black-Trees-110 X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G

11

12

13

14

15

16

17

18

19

20

21

22

23 8/3/2007UMBC CSMC 341 Red-Black-Trees-123

24 8/3/2007UMBC CSMC 341 Red-Black-Trees-124 11 14 15 2 1 7 5 8 Black node Red node Insert 4 into this R-B Tree

25 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

26 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

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

28 Top-Down Traversal X Y Z As we traverse down the tree and encounter this case, we recolor and possible do some rotations. There are 3 cases. 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.

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

30 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)

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

32 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)

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

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

35 An exercise – insert F D T W Z V L J P EK

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

37 8/3/2007UMBC CSMC 341 Red-Black-Trees-137 Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree

38 8/3/2007UMBC CSMC 341 Red-Black-Trees-138 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”?

39 Red Black Trees Top-Down Deletion

40 Recall the rules for BST deletion 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just one child, replace it with that child 3.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)

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

42 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

43 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

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

45 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)

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

47 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

48 P T X Case 2A X 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

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

50 Case 2A2 P T X L 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 L1L2 P T X L L1L2

51 Case 2A3 P T X X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree R1R2 P R X T R1 R LL

52 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, P is Black) Rotate T around P Recolor P and T Back to main case – step 2

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

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

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

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

57 Example 1 (cont’d) 15 17 16 20 231813 10 7 12 6 3 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 X

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

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

60 Example 2 (cont’d) 15 17 16 20 1310 12 6 3 4 2 X 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.

61 Example 2 (cont’d) 15 17 16 20 1310 12 6 3 4 2 X 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. P T

62 Example 2 (cont’d) 15 17 16 20 1310 12 6 3 4 2 P XT 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

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

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

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

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

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

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

69 Example 3 Solution 13 12 10 5 73 6 9 4 2 15


Download ppt "Red-Black Trees Definitions and Bottom-Up Insertion."

Similar presentations


Ads by Google