870 likes | 990 Views
Red-Black Trees. Manolis Koubarakis. Red-Black Trees. AVL trees and (2,4) trees have very nice properties, but: AVL trees might need many rotations after a removal (2,4) trees might require many split or fusion operations after an update
E N D
Red-Black Trees ManolisKoubarakis Data Structures and Programming Techniques
Red-Black Trees • AVL trees and (2,4) trees have very nice properties, but: • AVL trees might need many rotations after a removal • (2,4) trees might require many split or fusion operations after an update • Red-black trees are a data structure which requires only structural changes after an update in order to remain balanced. Data Structures and Programming Techniques
Definition • A red-black tree is a binary search tree with nodes colored red and black in a way that satisfies the following properties: • Root Property: The root is black. • External Property: Every external node is black. • Internal Property: The children of a red node are black. • Depth Property: All the external nodes have the same black depth, defined as the number of black ancestors minus one (recall that a node is an ancestor of itself). Data Structures and Programming Techniques
Example (2,4) Tree 12 15 5 10 3 4 13 14 17 6 7 8 11 Data Structures and Programming Techniques
Corresponding Red-Black Tree 12 15 5 17 13 3 10 14 7 4 11 6 8 In our figures, we use light blue color instead of black. Data Structures and Programming Techniques
(2,4) Trees vs. Red-Black Trees • Given a red-black tree, we can construct a corresponding (2,4) tree by merging every red node into its parent and storing the entry from at its parent. • Given a (2,4) tree, we can transform it into a red-black tree by performing the following transformations for each internal node : • If is a 2-node, then keep the (black) children of as is. • If is a 3-node, then create a new red node , give ’s first two (black) children to , and make and ’s third child be the two children of (the symmetric operation is also possible; see next slide). • If is a 4-node, then create two new red nodes and , give ’s first two (black) children to give ’s last two (black) children to , and make and be the two children of . Data Structures and Programming Techniques
(2,4) Trees vs. Red-Black Trees (cont’d) 15 15 14 13 14 13 13 14 7 6 7 8 6 8 Data Structures and Programming Techniques
Proposition • The height of a red-black tree storing entries is . • Proof? Data Structures and Programming Techniques
Proof • Let be a red-black tree storing entries, and let be the height of . We will prove the following: • Let be the common black depth of all the external nodes of . Let be the (2,4) tree associated with , and let be the height of . • Because of the correspondence between red-black trees and (2,4) trees, we know that • Hence, by the proposition for the height of (2,4) trees. By the internal node property of red-black trees, we have Therefore, . Data Structures and Programming Techniques
Proof (cont’d) • The other inequality, follows from the properties of proper binary trees and the fact that has internal nodes. Data Structures and Programming Techniques
Updates • Performing update operations in a red-black tree is similar to the operations of binary search trees, but we must additionally take care not to destroy the color properties. • For an update operation in a red-black tree , it is important to keep in mind the correspondence with a (2,4) tree and the relevant update algorithms for (2,4) trees. Data Structures and Programming Techniques
Insertion • Let us consider the insertion of a new entry with key into a red-black tree . • We search for in until we reach an external node of , and we replace this node with an internal node , storing and having two external-node children. • If is the root of , we color black, else we color red. We also color the children of black. • This operation corresponds to inserting into a node of the (2,4) tree with external-node children. • This operation preserves the root, external, and depth properties of , but it might violate the internal property. Data Structures and Programming Techniques
Insertion (cont’d) • Indeed, if is not the root of and the parent of is red, then we have a parent and a child that are both red. • In this case, by the root property, cannot be the root of • By the internal property (which was previously satisfied), the parent of must be black. • Since and its parent are red, but ’s grandparent is black, we call this violation of the internal property a double red at node . Data Structures and Programming Techniques
Insertion (cont’d) • To remedy a double red, we consider two cases. • Case 1: the sibling of is black. In this case, the double red denotes the fact that we have created in our red-black tree a malformed replacement for a corresponding 4-node of the (2,4) tree , which has as its children the four black children of and . • Our malformed replacement has one red node () that is the parent of another red node () while we want it to have two red nodes as siblings instead. • To fix this problem, we perform a trinode restructuring of as follows. Data Structures and Programming Techniques
Trinode Restructuring • Take node , its parent , and grandparent , and temporarily relabel them as and , in left-to-right order, so that and will be visited in this order by an inorder tree traversal. • Replace the grandparent with the node labeled , and make nodes and the children of keeping inorder relationships unchanged. • After restructuring, we color black and we color and red. Thus, the restructuring eliminates the double red problem. Data Structures and Programming Techniques
Trinode Restructuring Graphically 30 20 20 10 30 10 Data Structures and Programming Techniques
Trinode Restructuring Graphically (cont’d) 30 20 10 10 30 20 Data Structures and Programming Techniques
Trinode Restructuring Graphically (cont’d) 10 20 20 10 30 30 Data Structures and Programming Techniques
Trinode Restructuring Graphically (cont’d) 10 20 30 10 30 20 Data Structures and Programming Techniques
Trinode Restructuring vs. Rotations • The trinode restructuring operations are essentially the four kinds of rotations we discussed for AVL trees. Data Structures and Programming Techniques
Insertion (cont’d) • Case 2: the sibling of is red. In this case, the double red denotes an overflow in the corresponding (2,4) tree . To fix the problem, we perform the equivalent of a split operation. Namely, we do a recoloring: we color and black and their parent red (unless is the root, in which case it is colored black). • It is possible that, after such a recoloring, the double red problem reappears at (if has a red parent). Then, we repeat the consideration of the two cases. • Thus, a recoloring either eliminates the double red problem at node or propagates it to the grandparent of • We continue going up performing recoloring until we finally resolve the double red problem (either with a final recoloring or a trinode restructuring). • Thus, the number of recolorings caused by insertion is no more than half the height of tree , that is, no more than by the previous proposition. Data Structures and Programming Techniques
Recoloring 30 20 40 10 20 30 40 10 Data Structures and Programming Techniques
Recoloring (cont’d) … 30 … 30 … … 20 40 40 10 20 10 Data Structures and Programming Techniques
Example • Let us now see some examples of insertions in an initially empty red-black tree. Data Structures and Programming Techniques
Insert 4 4 Data Structures and Programming Techniques
Insert 7 4 7 Data Structures and Programming Techniques
Insert 12 – Double Red 4 7 12 Data Structures and Programming Techniques
After Restructuring 7 12 4 Data Structures and Programming Techniques
Insert 15 – Double Red 7 12 4 15 Data Structures and Programming Techniques
After Recoloring 7 12 4 15 Data Structures and Programming Techniques
Insert 3 7 12 4 15 3 Data Structures and Programming Techniques
Insert 5 7 12 4 15 5 3 Data Structures and Programming Techniques
Insert 14 – Double Red 7 12 4 15 5 3 14 Data Structures and Programming Techniques
After Restructuring 7 14 4 15 12 5 3 Data Structures and Programming Techniques
Insertion of 18 – Double Red 7 14 4 15 12 5 3 18 Data Structures and Programming Techniques
After Recoloring 7 14 4 12 5 15 3 18 Data Structures and Programming Techniques
Insertion of 16 – Double Red 7 14 4 12 5 15 3 18 16 Data Structures and Programming Techniques
After Restructuring 7 14 4 5 16 3 12 18 15 Data Structures and Programming Techniques
Insertion of 17 – Double Red 7 14 4 5 16 3 12 18 15 17 Data Structures and Programming Techniques
After Recoloring – Double Red 7 14 4 5 16 3 12 18 15 17 Data Structures and Programming Techniques
After Restructuring 14 16 7 4 12 18 15 5 3 17 Data Structures and Programming Techniques
Proposition • The insertion of a key-value entry in a red-black tree storing entries can be done in time and requires recolorings and one trinode restructuring. Data Structures and Programming Techniques
Removal • Let us now remove an entry with key from a red-black tree • We proceed like in a binary tree search searching for a node storing such an entry. • If does not have an external-node child, we find the internal node following in the inorder traversal of This node has an external-node child. We move the entry at to , and perform the removal at . • Thus, we may consider only the removal of an entry with key stored at a node with an external-node child . Data Structures and Programming Techniques
Removal (cont’d) • To remove the entry with key from a node of with an external-node child we proceed as follows. • Let be the sibling of and the parent of . We remove nodes and , and make a child of . • If was red (hence is black) or is red (hence was black), we color black and we are done. Data Structures and Programming Techniques
Graphically Data Structures and Programming Techniques
Removal (cont’d) • If, instead, is black and is black, then, to preserve the depth property, we give a fictitious double black color. • We now have a color violation, called the double black problem. • A double black in denotes an underflow in the corresponding (2,4) tree . • To remedy the double-black problem at , we proceed as follows. Data Structures and Programming Techniques
Removal (cont’d) • Case 1: the sibling of is black and has a red child . • Resolving this case corresponds to a transfer operation in the (2,4) tree . • We perform a trinode restructuring: we take the node , its parent , and grandparent , we label them temporarily left to right as and , and we replace with the node labeled , making it parent of the other two nodes. • We color and black, give the former color of , and color black. • This trinode restructuring eliminates the double black problem. Data Structures and Programming Techniques
Restructuring a Red-Black Tree to Remedy the Double Black Problem … 30 … 30 … … 10 20 40 20 40 10 Data Structures and Programming Techniques
Restructuring (cont’d) … 30 … 30 … … 10 20 40 10 40 20 Data Structures and Programming Techniques
After the Restructuring … 20 … 20 … … 30 10 30 10 40 40 Data Structures and Programming Techniques