Red and black Tree of data structure (II.)--insert operation

Source: Internet
Author: User

Insert or delete operations, it is possible to change the balance of the red and black trees, the use of color change and rotation of the two magic can be used to deal with all cases, the unbalanced red and black trees into a balanced red-black tree.

In the color change or rotation, often involves three generations of grandchildren: x represents the base node of the operation, p represents the parent of X, and G represents the parent node of the parent of X.

Let's start by outlining the insertion process:

1. Find the insertion point along the tree, and if the two child nodes of a black node are found to be red, a color transform is performed (the parent node becomes red and two red child nodes become black).

2, the 1th step, will not change the black height of the subtree, but there may be a color conflict (red-red color conflict), do one or two rotations can be resolved. The Red child node is x, the Red parent node is p, and the number of rotations is determined by whether X is the inner descendant of G or the descendant of the outer side.

3. After locating the insertion point, set X to the newly inserted node. If P is black, you do not need to make any changes and the insertion is complete.

4, if P is red, then there is a red-red color conflict, need to do two color changes, if X is the outer descendants of G, and then perform a rotation, if X is the inner descendants of G, then two times rotation. Eventually, the tree can be turned into a balanced red-black tree.

Now it doesn't matter why we do this, we analyze it slowly.

The 1th and 2nd steps seem to have nothing to do with inserting a new node, but in order to clear the way for the insertion of the new node, it will be reflected when the new node is inserted later.

Let's look at the detailed procedure for step 1th:


, find the P point and find that its two child nodes are all red, then make a color transform (if p is the root, the black remains the same). This transformation does not change the total number of black nodes on the path from the root node through the P-to-leaf node or the empty node, i.e. it does not change its black height. See p, X1, X2 as the three vertices of the triangle, before the color transformation, through this triangle will add a black node, after the color transformation, p becomes red, X1, X2 becomes black, whether through X1 or through X2, or will add a black node.

If the parent node of P is black, there will be no problem, but if the parent of P is also red, a red-red color conflict will occur, which needs to be corrected by rotation. There are two situations in which a color conflict occurs that requires a different treatment.

Note that at this point we select the red-red color conflict child nodes in the parent-son node as the Datum node, that is, X. If X is the same as p on the side of P on the side of G, the X is the outer descendant of G, whereas the inner descendant is the opposite.

The condition 1:x for the lateral descendant nodes.


, which indicates that after the color transformation, 12 and 25 nodes have a color conflict, 12 is 50 of the descendants of the outer side.

In this case, you need to take three steps:

1, change the color of G;

2, change the color of P

3, the center of G to the direction of the X upward rotation (in this case, right-handed).


The miracle happened, the tree suddenly balanced, and was in accordance with the red and black rules.

It is important to note that, in this case, because 25 is the left child of 50, the right-hand operation is performed, and the addition of the right child node requires a left-handed operation. Both left and right are rotated in the direction of ascending x .

The condition 2:x as the medial descendant node.

It is more complicated to fix this situation, and if we take the same approach as the inner descendants, X will not move up but move horizontally, making the tree more unbalanced. Therefore, a different approach is needed to solve the problem.

Let's use a rotation to make X the outer descendant , and then use one rotation to balance the tree.

This situation requires a four-step operation:

1, change the color of G;

2, change the color of x;

3, with P as the center to the direction of the x upward rotation;

4, the center of G to the direction of the X upward rotation.

At this point, the previous work has been completed, the following is the insertion of new nodes. At the insertion point, we use the new node as the benchmark, or X.

As we have said earlier, we always default the new node to red. Then, when the insertion point is found, there are two cases, one is that X's parent node is P is black, directly inserted (because inserting a red new node will not affect the tree's black height, there will be no color conflict), the other case is that X's parent node p is also red, after inserting a red-red color conflict, It needs to be corrected by color transformation and rotation.

When a color conflict occurs, the treatment is similar to the method mentioned above, depending on whether X is the inner descendant or the outer descendant.

Descendants of the outer side:

Inner descendants:

Let's discuss if there are any other situations.

If X has a sibling node S, which is another child of P, it makes any required rotations more complicated. If P is black, no rotation is required, regardless of whether X has a sibling node, and if P is red, it is notpossible to have a separate black child node before insertion, because it makes the black height of the s and the child nodes different. In summary, after inserting a new node, there will be no sibling nodes in X and rotation correction is required.

If P has a sibling node, which is the tert-node of x, it will make any required rotations more complicated. If P is black, do not make any rotation after X is inserted, if p is red, you must be red, otherwise, G to P's black height is different from G to u black height. However, the parent node of two red child nodes has been disposed of before inserting, so this situation does not exist. In summary, after inserting a new node, there is no case that P has sibling nodes and requires rotation correction.

Until now, it is clear why the color of the parent of the two red child nodes is transformed in the search for the insertion point, on the one hand, in order to make the tree more balanced and, on the other hand, greatly simplifying the rotation operation after insertion.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Red and black Tree of data structure (II.)--insert operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.