Description: Red black tree is a self-balancing binary search tree, a data structure used in computer science, and a typical use is to implement associative arrays.
It was invented in 1972 by Rudolf Bayer, then known as the balanced binary B-tree (symmetric binary b-trees). Later, in 1978, Leo J. Guibas and Robert Sedgewick changed to today's "red-black Tree".
Red and black trees are similar to AVL trees in that they maintain the balance of binary lookup trees with specific actions when inserting and deleting operations, resulting in higher lookup performance.
Although it is complex, its worst-case run time is also very good, and is efficient in practice: it can be found, inserted and deleted in O (log n) time, where n is the number of elements in the tree. Use:
Its statistical performance is better than the balance of binary trees, red and black trees in many places have applications. TreeSet and TREEMAP data structures in Java. In C + + STL, many parts (including set, Multiset, map, Multimap) have applied the variants of red-black trees (there are some changes in the red-black trees in SGI STL, which provide better performance and support for set operations). Other balance trees are: avl,sbt, stretching trees, treap and so on.
Properties:
Nature 1. The nodes are red or black.
Nature 2. The root node is black.
Properties 3 each leaf node (nil node, empty node) is black.
Property 4 The two child nodes of each red node are black. (no two consecutive red nodes can be found on all paths from each leaf to the root)
Nature 5. All paths from any node to each of its leaves contain the same number of black nodes.
Summary: These constraints force the key properties of the red-black tree:
The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path. The result is that the tree is broadly balanced. Because the worst-case time for operations such as inserting, deleting, and finding a value is proportional to the height of the tree, the theoretical upper limit on the height allows the red and black trees to be efficient in the worst case, unlike the normal two-fork lookup tree.
To know why these features ensure this result
, note that the nature of 4 causes the path to not have two contiguous red nodes sufficient. The shortest possible paths are black nodes, and the longest possible paths have alternating red and black nodes. Because according to the nature of 5 all the longest paths have the same number of black nodes, this indicates that no path can be more than twice times longer than any other path.
A two-pronged tree:
Illustration of Binary tree operation: 1. Left-handed
2. Right-handed:
Insert operation:
When inserting a red-black tree, we always insert a red node, as it is possible to avoid tree adjustments during the insertion process. So, when we insert a node, what properties of the original tree might change the columns? Because we are inserting in a binary tree way, the search nature of the element does not change.
If the inserted node is the root node, property 2 will be destroyed, and if the parent node of the insertion node is red, it will break the property 4. So, all in all, inserting a red node only destroys the property 2 or property 4. Our recovery strategy is simple,
1. Move the nodes that violate the nature of the red and black tree upward, and if you can move to the root node, it is easy to restore the properties of the red-black tree by directly modifying the root node. The nature of the red-black tree should be restored directly by modifying the root node.
2. Exhaustive all the possibilities, and then attributed to the same class of methods to deal with the same category, can not be directly processed into the following cases
Insert Repair operation: 1) Condition 1: The root node is inserted.
The original tree is an empty tree, this situation will only violate the nature of 2.
Countermeasures: Apply this node directly to black.
2) Case 2: The parent node of the inserted node is black.
This will not violate the nature of 2 and Nature 4, the red-black tree has not been destroyed.
Strategy: Do nothing.
3) Case 3: The parent node of the current node is red and another child node of the grandparent node (Uncle node) is red.
The parent node of the parent node must exist, otherwise it is not a red-black tree before it is inserted. At the same time, divided into the parent node is the grandfather node left dial hand or right son, for symmetry, we just have to untie One direction. Here, we only consider the case where the parent node is the grandfather left dial hand. At the same time, it can be divided into the current node is the parent node of the left dial hand or the right child, but the same processing method is the same. We classify this as the same class.
Countermeasures: The parent node of the current node and uncle Black, the grandfather node is red, the current node points to the grandfather node, the new current node to restart the algorithm.
For Scenario 3, before the change [current node is 4 node]:
After the change:
4) Case 4: The parent node of the current node is red, the Uncle node is black, the current node is the right child of its parent node
Action: The parent node of the current node is the new current node, with the new current node being the pivot left.
As shown, before the change [current node is 7 nodes]:
After the change:
5) Case 5: The parent node of the current node is red, the Uncle node is black, the current node is the left child of its parent node
Solution: The parent node turns black, the grandfather node turns red, and the grandfather node is pivot right
As shown [current node is 2 nodes]
After the change:
Summarize:
After the above situation 3, condition 4, Situation 5 and so on 3 kind of inserting the repair situation operation, the reader will discover, the later situation 4, the situation 5 is to be in the case 3 inserts the node 4, carries on the series inserts the repair situation operation, however, points to the current node n the pointer has been changing. So, you can take it for granted that the whole situation, 3, 4, 5 is a complete procedure for inserting and repairing the situation.
Delete operation: Normal search Tree Delete operation
1. There is no son, that is, the leaf node. Directly set the parent node's corresponding son pointer to null, delete son node is ok.
2. Have a son. Then the parent node's corresponding son pointer points to the son's only child, delete son node is ok.
3. There are two sons. This is the most troublesome situation, because after you delete the node, it is also guaranteed to satisfy the structure of the search binary tree. In fact, it is also easier, we can choose the largest element of the left son or the smallest element of the right son to the location of the node to be deleted, you can ensure that the structure is unchanged. Of course, you have to remember to adjust the subtree, after all, there are node deletions. It is customary to choose the largest element of the left son, in fact, the smallest element of the right son is the same, no difference, but people are accustomed to from left to right. Here we also select the largest element of the left son and place it at the point where it is to be abridged. The largest element of the left son is actually very easy to find, as long as the left son constantly to search the right subtree can be, until found a no right sub-tree node. That's the biggest one.
Removal of red and black trees:
The above fixes look a bit complicated, and here's an analysis technique: We start with the node that we replaced it with from the truncated point, and think it has an extra black weight. What do you mean by the extra black here, we're not adding a red-black tree node to another color, except for red and black, here is just a hypothesis, we think we are pointing at it now, so there is an extra kind of black, it can be thought that its black is inherited from its parent node, it can now hold two colors, If it turns out to be red, then it is now red + black, if it turns out to be black, then it is now Black + black. With this extra black, the original red black tree Nature 5 can remain unchanged. Now as long as the flowers are restored to other properties, the practice is to move as far as possible to the root and exhaustive.
Note: The following cases 3, 4, 5, 6, and the Code of introduction to the above algorithm Rb-delete-fixup (T, X) recovery and maintain the CASE1,CASE2,CASE3,CASE4 corresponds.
Case 1: Current node is red + Black
Solution, dye the current node directly into black, end. At this time the red and black tree nature all restored.
Scenario 2: The current node is black + black and is the root node
Solution: Do nothing, end
Case 3: The current node is black + black and the sibling node is red (at this point the child nodes of the parent and sibling nodes are divided into black).
Solution: Dye The parent node red, dye the sibling node black, and then re-enter the algorithm (we only discuss the current node is the parent node when the left child situation). After this transformation, the original red black tree properties of 5 unchanged, and the problem into the sibling node is black (note: Before the change, the original is not a violation of the nature of 5, just to turn the problem into a sibling node black case).
Case 4: The current node is black and black and the sibling is black and the two child nodes of the sibling node are all black.
Solution: The current node and the sibling node to extract a heavy black appended to the parent node, the parent node as the new current node, re-enter the algorithm. (Property 5 unchanged after this transformation)
Case 5: The current node color is black + black, the sibling node is black, the brother's left dial hand is red, the right child is black.
Solution: The Brother node dyed red, Brother left dial hand node dyed black, and then in the Brother node for the fulcrum of the right rotation, and then re-enter the algorithm. This is to convert the current situation to case 6, while the nature of 5 is maintained.
Solution: The Brother node dyed the current node of the parent node color, the current node of the parent dye black, the sibling node right son dyed black, and then the parent node of the current node as the fulcrum of the left rotation, at this time the algorithm ends, red black tree all the properties of the correct adjustment.
My QR code is as follows, welcome to exchange discussion
You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:
Reference:
Http://baike.baidu.com/view/133754.htm
1190000000472153
Data structure-red and black tree detailed