Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Red-Black Trees. 2 Black-Height of the tree = 4.

Similar presentations


Presentation on theme: "1 Red-Black Trees. 2 Black-Height of the tree = 4."— Presentation transcript:

1 1 Red-Black Trees

2 2 Black-Height of the tree = 4

3 3 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 nulls.

4 4 A valid Red-Black Tree Black-Height = 2

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

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

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

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

9 9 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

10 10 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, check new situation (recoloring may have created a problem) 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 on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children -- color parent black, color grandparent red, rotate right on grandparent

11 11 X P G U P G U Case 1 – U is Red Just Recolor see if there is a new problem at G X

12 12 X P G U S X P G S U Case 2 – Zig-Zag (X and P are opposite children) Double Rotate X around P; X around G Recolor G and X as can’t rotate a black from a common path without compensating Note, that if both kids of G were red, we would have recolored. If both kids were black, there would be no problem.

13 13 X P G U S P X G S U Case 3 – Zig-Zig (X and P are both left kids) Single Rotate P around G Recolor P and G as we’ve rotated a black from common path

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

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

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

17 17 Red-Black Trees Bottom-Up Deletion

18 18 Recall “ordinary” BST Delete 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)

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

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

21 21 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), or propagated up to the root and out of the tree. There are four cases – our examples and “rules” assume that V is a left child. There are symmetric cases for V as a right child

22 22 Terminology The node just deleted was D The node that replaces it is V, which has an extra unit of blackness (it was black and if deleted, will create an imbalance) The parent of V is P The sibling of V is S Black Node Red Node Red or Black and don’t care

23 23 Bottom-Up Deletion Case 1 V’s sibling, S, 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

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

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

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

27 27 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, and color S’s Right child Black This is the terminal case – we’re done

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

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

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

31 31 65 50 80 10 60 7090 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70

32 32 Red Black Trees Top-Down Insertion

33 33 Review of Bottom-Up Insertion In Bottom-Up 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

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

35 35 Goal of T-D Insertion Insertion is always done as a leaf (as in ordinary BST insertion) Recall from the Bottom-Up discussion 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

36 36 Goal (2) Therefore, the goal of top down 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, the uncle is Black. That way we may have to rotate and recolor, but not propagate back up the tree

37 37 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

38 38 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

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

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

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

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

43 43 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

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

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

46 46 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

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

48 48 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 children (or both right children) 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


Download ppt "1 Red-Black Trees. 2 Black-Height of the tree = 4."

Similar presentations


Ads by Google