This paper introduces another balanced binary tree: red black tree, which was invented by Rudolf Bayer in 1972. It was called balanced binary B-trees ), leonidas J. change guibas and Robert Sedgewick to a more modern name: The red and black trees.
Similar to the AVL Tree mentioned earlier, the red/black tree maintains a balance between the binary search tree through specific operations during the insert and delete operations to achieve high search performance. Since the emergence of the red/black tree, the AVL tree has been put in the museum. It is said that the red/black tree has better efficiency and higher statistical performance. However, after I understand the implementation principles of the red and black trees, I don't believe this is true. We will discuss this in the future.
The difference between a red-black tree and an AVL Tree is that it uses color to identify the height of a node. It pursues a local balance rather than a very strict balance in the AVL Tree. We have learned about the complexity of the AVL Tree before, but the complexity of the AVL Tree is nothing more than that of the red and black trees. The red/black tree is a real abnormal data structure.
First, we will use a red/Black Tree Animation made by Silverlight to help you understand what a red/black tree is. Note that the Silverlight 2.0 RTW must be installed to run the game properly ,:
Http://www.microsoft.com/silverlight/resources/install.aspx? V = 2.0
Note:
L nodes only receive integers. If an invalid string is entered in the add or delete operations, a 0 ~ value is randomly added or deleted ~ An integer between 99.
L enter a number in the edit box and click add or delete to add or delete a number.
L you can drag a bar to control the animation speed.
Balance of red and black trees
The red/black tree is a binary search tree. Each node is marked with a color (red or black). The red/black tree meets the following five properties:
1,The color of each node can only be red or black.
2,The root node is black.
3,Each leaf node has two empty black nodes (known as the Black Sentinel ).Only one left child, then nThe right child of is a black sentinel; If node nOnly one right child, then nThe left child is a black Sentinel.
4,If a node is red, both of its sons are black. That is to say, there cannot be two adjacent red nodes in a path.
5,For each node, all paths from the node to its child leaf node contain the same number of black nodes.
Of the five properties of the red and black trees, the 3rd points are hard to understand, but they are very necessary. We can see the picture on the left in Figure 1. If the black Sentinel is not used, it is completely red-black, nodes 50 to two leaf nodes 8 and leaf node 82 have two black knots. However, if the number of leaf nodes is changed to 8 after the black Sentinel is added (the small black dot in the right figure, the black height of the root node 50 to the eight leaf node paths is different, so it is not a red-black tree.
To see the true red/black tree, add several nodes in the above animation to see if the above features are satisfied.
Rotation of the red/black tree
Like aVL, red/black trees are divided into ll, RR, LR, and RL rotation types. The difference is that after rotation, the color of the node is changed, rather than the balance factor. For a rotation animation demonstration, see Flash Animation in aVL:
Http://www.cnblogs.com/abatei/archive/2008/11/17/1335031.html
Insert nodes on the red/black tree
Before discussing the insert operation of the red/black tree, you must understand that the initial color of any new node to be inserted is red. This is easy to understand, because inserting black spots will increase the number of black nodes on a path, resulting in an imbalance in the black height of the entire tree. However, if the parent node of the new node is red (as shown in Figure 2), it will return to the red-black tree: two adjacent red nodes cannot appear on a path. In this case, a series of operations are required to maintain a balance between the red and black trees.
To clearly indicate the insert operation, the following words "new" are used in the node to indicate a newly inserted node. The "parent" word is used to represent the parent node of the newly inserted node; use "uncle" to represent the sibling node of the "parent" node, and use "ancestor" to represent the parent node of the "parent" node. Insert operations are divided into the following situations:
1,Black parent
As shown in figure 3, if the parent node of the new vertex is a black node, inserting a red dot will not affect the balance of the red and black trees. At this time, the insert operation is complete. One of the advantages of the Red-black tree over the AVL Tree is that the black tree is more common, so that the probability that the red-black tree needs to rotate is less than that of the AVL Tree.
2. Red parent
If the parent node of the new vertex is red, a series of operations are required to ensure that the entire tree is red and black. As shown in 3, because the parent node is red, it can be determined that the grandfather node must be black. In this case, you need to determine the operation based on the color of the uncle node. The blue node indicates that the color is unknown. Since it may be necessary to perform multiple rotation operations on the path from the root node to the new point, the starting point of each imbalance judgment (we can regard it as the new point) is different. So here we use a blue arrow to point to this starting point, which is called a judgment point.
2.1Hongshu
When the uncle node is red, as shown in figure 4, you do not need to perform the rotation operation, as long as the father node and Uncle node are changed to black, the grandfather node is changed to red. However, because the parent node of the grandfather node may be red, it violates the red-black tree nature. In this case, the grandfather node must be used as the new judgment point to continue the upward balancing operation.
It should be noted that no matter whether the "parent" is on the left or right of the "uncle", whether the "new" is the "parent" Left or Right child, their operations are the same.
2.2Uncle Hei
When the uncle node is black, it needs to be rotated to show that all rotating operations are possible.
Scenario 1:
Case 2:
Case 3:
Case 4:
It can be observed that after the rotation is complete, all the new rotating roots are black. At this time, you do not need to backtrack up to perform the balancing operation. The insertion operation is complete. Note that the "uncle", "1", "2", and "3" nodes in the above four figures may be the blackguard nodes.
In fact, the insert operation of the red and black trees is not very difficult, or even easier than the insert operation of the AVL Tree. However, the delete operation is much more complex than the AVL tree. The following describes the delete operation of the red/black tree.
Delete nodes on the red/black tree
The red/black tree is a binary search tree. Its deletion is similar to that of the Binary Search Tree. First, you need to find the real deletion point. When the deleted node n exists for the child, the real deletion point should be n in the forward order, for more information, see Delete the binary search tree. 9. When node 20 is deleted, the actual deleted node is 18, and the data on node 20 is 18.
Therefore, it can be inferred that the actual deletion point must be a node with only one child or no child. The following two conclusions can be drawn based on the nature of the red and black trees:
1,In the delete operation, only one red child or no child node is deleted..
2,If a deleted vertex is a red node, it must be a leaf node..
Understanding these two points is very important. As shown in 10, except for case (a), node n of any other condition cannot meet the property of the red/black tree.
In the following discussion, we use the blue arrow to represent the true deletion point, which is also the first judgment point during the rotation operation. The true Deletion Point is marked with the old one, the location of the old vertex is replaced by its child node (only one child is allowed at most). We use "new" to represent the child node of the old vertex. Delete operations can be divided into the following situations:
1The old vertex is a red node.
If the old vertex is a red node, it must be a leaf node and can be deleted directly. 11.
2, Red and black
When the old vertex is a black knot and the new vertex is a red knot, replace the New vertex with the old vertex and then dye the new vertex into black (12 ). Note that the old vertex is red and the new vertex is black.
3Double Black
When both the old and new vertices are black (the new vertices are empty knots, this is also the case), the situation is complicated and you need to decide what kind of operation to perform based on the color of the old vertices and brothers. We use "brother" to represent the brother node of the old vertex. This can be divided into two situations: red and black brothers:
3.1Red brother
Because the sibling node is red, the parent node must be black. After the old vertex is deleted, the new vertex replaces its location. Two possible scenarios are demonstrated:
The red brother needs to rotate the RR or LL type, and then dye the parent node in red and the brother node in black. Then, the new vertex is used as the judgment vertex to perform the balancing operation again. We can observe that after the rotation operation is completed, the judgment point is not traced back, but is reduced by a layer, which turns into a black brother.
3.2Brother Hei
The situation of the black brother is the most complicated. You need to decide what to do based on the color of the black brother's child node (represented by "yellow" Here) and the father node.
3.2.1Brother Two, red father
As shown in figure 14, this situation is relatively simple. You only need to change the parent node to black, the brother node to black, and the new node to black. The delete operation ends here.
3.2.2Black Brother, black brother, black father
As shown in Figure 15, the parent node is colored by the new node, the new node is black, and the old node is red. When the new node is red, the parent node is colored in red. At this time, you need to continue the upward balancing operation with the parent node as the judgment point.
3.2.3Brother-in-law
The following figure shows the following four scenarios:
Scenario 1:
Case 2:
Case 3:
Case 4:
As shown in the following example, the brothers who have read the above four figures may have a question. If the two nephew nodes in Case 1 and Case 2 are both red, should I perform ll rotation or LR rotation? The answer is to perform ll rotation. In case 3 and case 4, the RR rotation is prioritized.
From: http://hi.baidu.com/coolinc/blog/item/3aa07f3e162502eb54e723b1.html