Red and black trees are self-balancing sorting trees, the advantage of self-balance is to reduce the traversal of the node, so the efficiency will be high. If a non-balanced two-fork tree is inserted in a sequential or reverse order, it is very easy to understand the rules of the red-black tree, which is likely to traverse N nodes, but it is difficult to maintain the rule. I. Rule 1. Each node is either red or Black 2. The root node must be Black 3. The red node must not be contiguous (parent node, child node cannot be red) 4. From any node, to all the routes at the bottom of the tree, the number of black nodes in the path should be the same when modifying red and black trees, remember to maintain this rule In general, the red node is inserted by default (unless it is the root node), and then rotated and color transformed after inserting, rotating, Transformation techniques There are several things about rotation and color transformation: 1. Inserting root does not violate rule 2. Parent Black The parent node of the Insert node is black and does not violate rule 3. Parent red Insert node's parent node is red and must violate rule 3 (because the red node is inserted by default), you need to fix the red-black tree at this point. This situation is subdivided into several cases. 4. Parent red, tertiary red parent node and tertiary node are all red (violation of rule 3)
In this case, the parent node and the tertiary node are changed to black, then the node has changed to red
But at this point, if the node is the root node, then the node must be black. So in the end, the method of repairing red and black trees is generally Forcing the root node to black this situation may result in 5.1 of cases 5.1 parent red, tertiary black, outer descendants parent node is red, tertiary node is black or null, and the new node is the outer descendants of the former node (violation of rule 3) descendants of the outer side: Point A is the left-left-....-left or right-right-.. -Right, then A is the outer descendant of B, otherwise a is the inner descendant of B symbol interpretation: N: Newly inserted node, P: Parent node, G: Zeng node, U: Tertiary node
Now you need to rotate the G-point, here is the right-hand
But at this time there is a clash of colors, adjust the color of P and g to swap, p to black, g to red
To summarize, 5.1 steps:step1:p-> black step2:g-> red step3: The G rotation 5.1.1 about the direction of rotation has left-right rotation, in particular, to see if the parent node is the left node of the node or the right node p is g, then the G-right rotation (because to move p to the position of G) p .... G.... Right-hand node ......... ..... G left 5.2 father red, tertiary black, inner descendants parent node is red, tertiary node is black or null, and the new node is the inner descendant of the former node (violation of rule 3) descendants of the outer side: Point A is the left-left-....-left or right-right-.. -Right, then A is the outer descendant of B, otherwise a is the inner descendant of B 5.2 and 5.1 have only one medial lateral difference. The inner descendants are rotated more than the outer side. The aim is to rotate the inner offspring into the outer offspring.
Here n is the inner descendant of G, now to turn N into a descendant of the outer side of G. Rotate P (This is left-handed) and let n rotate to the position of P
The next step is the same as the 5.1 treatment of the descendants of the outer side. The direction of rotation of the outer side of the 5.2.1 is based on the relative position of P point and N. When n is the right node of P, the p is left-handed, otherwise P-r is rotated to the position of P to summarize the situation: 1. The root node is inserted and does not violate rule 2. The parent node of the insertion point is black and does not violate rule 3. The parent of the insertion point, The tertiary node is red, turns the parent and uncle into black, and then turns the node into Red 4. The insertion point of the parent is red, the tertiary node is black or null, by rotating and changing the color, pay attention to the outer side of the inner descendants! Third, the analysis of TreeMap source JDK in the red and black tree implementation is treemap.
After each put, this method is called to maintain the rules of the red and black tree.
private void Fixafterinsertion (Entry<k,v> x) {x.color = RED; while (x! = NULL && x! = Root && X.parent.color = = red) {//parent node is red if (parentof (x) = = Leftof (par Entof (Parentof (x)))) {//parent node is the left node of the former node entry<k,v> y = Rightof (Parentof (Parentof (x))); if (colorof (y) = = red) {//Parent/Tertiary nodes are red, case 4//turns parent and tertiary nodes black, once node becomes red SetColor (Parentof (x), BLA CK); SetColor (y, BLACK); SetColor (Parentof (Parentof (x)), RED); x = Parentof (Parentof (x)); } else {//parent node is red, tertiary node is black or null if (x = = Rightof (parentof (x))) {//inner descendants x = Parento f (x); Rotateleft (x);//rotate into descendants of the outer side} setcolor (Parentof (x), BLACK); SetColor (Parentof (Parentof (x)), RED); Rotateright (Parentof (Parentof (x)));//Rotate once node} The else {//parent node is the right node of the former node entry<k,v> y = Leftof (Parentof (Parentof (x))); if (colorof (y) = = RED) {setcolor (Parentof (x), BLACK); SetColor (y, BLACK); SetColor (Parentof (Parentof (x)), RED); x = Parentof (Parentof (x)); } else {if (x = = Leftof (parentof (x))) {x = Parentof (x); Rotateright (x); } setcolor (Parentof (x), BLACK); SetColor (Parentof (Parentof (x)), RED); Rotateleft (Parentof (Parentof (x))); }}} Root.color = black;//force the root node to be turned black}
View Original: http://blog.zswlib.com/2016/11/01/jdk%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90%e7%ba%a2%e9%bb%91%e6%a0%91-%e6% 8f%92%e5%85%a5%e7%af%87/
JDK Source code Analysis red-Black tree-Insert article