First, paste the property of the red and black trees:
Property 1. nodes are red or black
Nature 2. The root is black
Nature 3. All leaves are black (leaves are nil nodes)
Nature 4. If a node is red, its two sons are black
5. All simple paths from any node to its leaves contain the same number of black nodes.
The insert method is directly written below, such as the addition, deletion, and rotation of the Search Tree. If you do not understand it, check other information.
1. The inserted node is red by default. Why?
Red does not damage property 5. Property 5 is the most difficult to maintain. The damage of property 5 directly damages the structure of the entire tree. Therefore, the insertion point is red by default.
So the most vulnerable nature of red insertion is 2 and 4. The insertion process is also centered on maintaining this gender.
2. Insert demo (insertion point is X, Father p (x), uncle is Y)
1. If the parent node of the inserted vertex is black, the Nature 2 and 4 will not be damaged, so do not perform any operation.
1. If the parent node of the inserted vertex is red, the adjustment of the tree begins. The parent node is red, which can be divided into three situations:
Uncle 1.1 is red
1.2 uncle is black, X is p (x) Right child
1.3 uncle is black, X is p (x) Right child
Let's first talk about the situation in 1.1: as shown in Figure
In order to keep the property 4 unchanged, we need to set the P color to black, so that the left branch has a black node, so the grandfather should be red, so that the left branch remains unchanged, but now the right branch is missing my grandfather (my grandfather must be black), so I want to convert y to black. In this way, all the nodes below my grandfather are satisfied. We want to direct X to my grandfather, continue Loop
1.2 The father is red, the uncle is black, and X is the left child:
Rotate the parent to reach the third condition:
1.3 uncle is black, X is the father's left child
INSERT summary:
. What will happen after insertion? Let's assume that the insertion point is X, the father node is P, the uncle is Y, and X is inserted into the father's left child.
1. When the inserted father is black, the nature of the tree is not damaged, and no adjustment is made.
2. When the father is red after insertion, the damage properties 2, 4, and damage properties 2 can be simply changed to black, but it is hard to do if the damage properties 4, next, let's analyze
Property 4 means there cannot be two consecutive red nodes. If so, what should we do? We will black one of them, (X or P) in this way, Nature 4 can be maintained, but this will cause nature 5 to be
Damage, because black will add 1 to the Black height of a path. If it is shown, if it is black P, it will add 1 to the Black height on the P path, black X will add 1 to the Black height on the X path.
Grandpa W adds 1 to the Black height of the left child path. What we need to do is to reduce the black height of the left child by one. We found that grandpa must be black, if we turn my grandfather to the right, the black height on the left will be the same as the original one. Let's look at the right. (It means uncle y must be black; otherwise, it will meet 1.1 of the execution requirements ), the right side will not damage the property 4, but in order to make the property 5 not damaged, we will rotate the previous node w red, so that the entire tree meets all the properties
In a word, inserting a vertex may change the black height of several paths. We need to re-satisfy the properties of them by rotating and changing the color, this is also the reason why the first two of the three cases have switched to the third case, because they are most likely to become red and black trees after color and rotation.
In fact, insert is relatively simple. You can write it in the following three steps. The above mainly describes the operations mentioned in the introduction to algorithms, I only know the operation, but I don't know why I want to do this. Here we record that there may be a bit of chaos or a place to be corrected. Only one person understands this ....