1. Red-Black Tree description : It is either an empty tree or a two-fork search tree with the following attributes:
1) node is not red or black;
2) The root node is black;
3) All NULL nodes are called the leaf node, and the color is considered black;
4) All the nodes of the red node are black;
5) All paths from either node to its leaf node contain the same number of black nodes.
Insert and delete operation times can be kept to O (log n) times, Figure 1 (this figure from Wikipedia) is a specific red-black tree:
Figure 1: Red and black trees
2. Red-black tree insert : Assuming that the insertion node is red, make specific adjustments based on the color of adjacent nodes:
1) for the empty tree, directly inserted, the color change to black;
2) The parent node of the insertion node is black, inserted directly, not adjusted;
3) Insert node parent and Uncle nodes are red, then set them both to black, set their grandfather node as red, set process 2. The entire tree is checked recursively.
Figure 2: The treatment process of the parent-uncle node is red
4) The parent node is red and the Uncle node is black or missing, the new node is the left child node of its parent node, and the parent node is the left child node of the grandparent node. For a right rotation of the grandfather node, in the rotation-generated tree, the previous parent node is now the parent node of the new node and the previous grandparent node, as shown in 3.
Figure 3: Father Red Uncle for Black or absent, father for grandfather Zuozi deal
5) The parent node is red and the Uncle node is black or missing, the new node is the right child node of its parent node, and the parent node is the left child node of the grandparent node. Perform a left rotation to swap the roles of the new node and its parent node, and then follow 4) to process it.
Figure 4: Father Red Uncle for Black or absent, father for grandfather Zuozi deal
3. Red-Black tree Delete:
1) Delete is the new root, direct operation.
2) S is red, on the father of N do left rotation, the red brother converted to N's grandfather, we then swapped N's father and grandfather's color.
Figure 5:s is red processing
3) The sons of N's father, S, and s are all black. In this case, we simply redraw s to red. The result is all paths through s, which are those paths that previously did not pass N, with a black node missing.
Figure 6:n's father, S and S's sons are all black processing
4) s and S's sons are all Black, but N's father is red. In this case, we simply exchanged the color of N's brother and father.
Figure 7:s and S's sons are All Black, N's father is red-handled
5) S is black, S's left son is red, S's right son is black, and N is its father's left son. In this case we do a right spin on s, so that S's left son becomes the father of S and the new brother of N. We went on to Exchange S and its new father's color.
Figure 8:s is black, S's left son is red, S's right son is black, and N is its father's left son to handle
6) s is black, S's right son is red, and N is its father's left son. In this case we do a left spin on the father of N, so that s becomes the father of N (P) and the right son of S.
Figure 9:s is black, S's right son is red, and N is its father's left son
Binary Tree Learning four: red and black trees (see Wikipedia)