Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-Black trees Binary search trees with additional conditions. These conditions ensure that the trees are fairly well balanced. In this way we obtain.

Similar presentations


Presentation on theme: "Red-Black trees Binary search trees with additional conditions. These conditions ensure that the trees are fairly well balanced. In this way we obtain."— Presentation transcript:

1 Red-Black trees Binary search trees with additional conditions. These conditions ensure that the trees are fairly well balanced. In this way we obtain a good worst-case behavior. Some operations (such as search and listing) are not changed compared to ”just” binary search trees. Other operations become more complex to ensure that the tree remans to fulfill the additional properties. In the following we will go through the revised algorithms and verify that they work as expected. First of all the conditions 1.Each node is either red or black 2.The root is always black 3.All leafs are black (note: NIL – an extension) 4.If a node is red, then both children are black 5.For all vertices (v) the following holds: All paths from v to the leafs below contain the same number of black nodes. Question: How does this ensure that the tree is ”fairly well balanced”? In addition to the intuitive answer we can show that a red-black tree with n internal nodes has a height not exceeding 2 lg (n+1).

2 Red-Black trees

3 Rotations We need some tools in order to ensure that our red-black trees can maintain the red-black properties in case of insertion/deletion. Rotation:

4 Insertions Now it is getting a bit complicated – see next slide for an example to go through step-by-step:

5 Insertions

6 Challenge – correctness! First considerations: Which properties can be violated? 1.Each node is either red or black 2.The root is black 3.All leafs are black (note: NIL – an extension) 4.If a node is red, then both children are black 5.For all vertices (v) the following holds: All paths from v to the leafs below contain the same number of black nodes. We consider now the while-loop in fixup – invariant (show init, term., maint.): a.Node z is red b.If p[z] is root, then p[z] is black c.If red-black properties are violated, there is at most one violation, and this is of either properti (2) or (4). If it is (2), then it is because z is root and red. If it is (4), then it is because both z and p[z] are red.

7 a.Node z is red b.If p[z] is root, then p[z] is black c.If red-black properties are violated, there is at most one violation, and this is of either properti (2) or (4). If it is (2), then it is because z is root and red. If it is (4), then it is because both z and p[z] are red.

8 Termination P[z] is black So, condition 4 is now satisfied. Line 16 ensures that condition 2 is satisfied as well So: All conditions are satisfied

9 Maintenance 6 cases in the while-loop, but it is sufficient to study 3 of them (due to the symmetries). Depending on if z’s parent p[z] is left or right child of p[p[z]] – we assume p[z] is left child. We know that p[p[z]] exist; if p[z] is root, then it is black. But we only go into the loop if p[z] is red, implying that p[z] cannot be root. Thus, p[p[z]] does exist. Case 1: z’s uncle y is red. Case 2: z’s uncle y is black and z is a right child. Case 3: z’s uncle y is black and z is a left child.

10 Case 1 – z’s uncle y is red We go through this case when both p[z] and y er red. Since p[p[z]] is black we can colour both p[z] and y black. We can then move ’z’ two levels up in the tree. So, the while-loop in the next step is carried out based on p[p[z]]. In the following z’=p[p[z]]. a.z’ is red in the beginning of the next iteration. b.p[z’] does not change colour during the iteration. So, if p[z’] is the root, it was black both before and after the iteration – and then it is also black before the next iteration begins. c.1 and 3 is still satisfied, and 5 has not been changed. Note: If z’ is the root in the beginning of the next iteration, then 4 is now satisfied. That is, only 2 is violated if z’ is red. a.Node z is red b.If p[z] is root, then p[z] is black c.If red-black properties are violated, there is at most one violation, and this is of either properti (2) or (4). If it is (2), then it is because z is root and red. If it is (4), then it is because both z and p[z] are red. (since z is added in the bottom of the tree)

11 Case 2-3 z’s uncly y is black, the case depends on if z is right (case 2) or left (case 3) child. In case 2 we make a left-rotation, and then we are again back to case 3. As before, p[p[z]] exists. 5 is maintained, but 4 is resolved. a.Case 2 make z point to p[z], which is red. This is the only change of z (or z’s color) in both cases. b.Case 3 colours p[z] black, so if p[z] is the root in the beginning of the next iteration, then it is black. c.Properties 1,3,5 is maintained. There are not introduced any violation of 2, since the only node that is colored red becomes the child of a black node (rotation in case 3). That is, case 2+3 resolve the violation of 4 without introducing any new violations. a.Node z is red b.If p[z] is root, then p[z] is black c.If red-black properties are violated, there is at most one violation, and this is of either properti (2) or (4). If it is (2), then it is because z is root and red. If it is (4), then it is because both z and p[z] are red. (since z is added in the bottom of the tree)

12 Deletion – same game

13 Exercises related to today’s lecture Go through the procedure of inserting nodes with values 9, 8,7,6,5,4,3,2,1 in an ordinary binary search tree, which prior to this insertion only contains a single node with value 10. Repeat for a red-black binary search tree. Construct a binary search tree, which is well balanced as a starting point (and which can satisfy the red-black conditions), but that will become very unbalanced by removing nodes in an order suggested by you. Repeat then for the same red- black binary search tree, with deletion in the same order. From the book: 12-1, 13.1-2, 13.1-3, 13.1-4, 13.2-1, 13.2-3, 13.3-1, 13.3-3, 13.3-4, 13.4-3, 13.4-6, 13.4-7.


Download ppt "Red-Black trees Binary search trees with additional conditions. These conditions ensure that the trees are fairly well balanced. In this way we obtain."

Similar presentations


Ads by Google