Delete a red/black tree node

Source: Internet
Author: User

Next to the previous article.

Insert the red/black tree!


The deletion of the red/black tree is further considered in various situations.

First, consider the single support of the red/black tree, that is, only the parent node has one child node and the other is null. In this case, there is only one situation, that is, the parent node is black, the child node is red, because in other cases, the child is black, because the child is red and the child is red. When the child is black and the other child is black null, the number of black nodes on the left and right paths must be different. Therefore, a single branch can only be a black parent.

Here, the single branch is for any point. If there is only one child, it will be black, the child will be red, and the child will be a leaf node. The child is red. If there is a grandson whose grandson must be black, the number of sunspots in the path is not balanced.

Scenario 1:

If you delete a single red node, it does not affect other paths and does not affect the balance. Simply delete the node.

Scenario 2:

Delete black nodes

If the S node is black, replace yourself with the red child under a single branch, and then delete the black node. In this way, a black node is missing from the path, which affects the balance and needs to be adjusted.


Case 3:

When deleting a node, there are two non-empty children. At this time, we use the downward rotation idea to find the successor node s that is traversed in the middle order of the delete node P, that is, the number after the normal sorting is located after P, and the S node must have at most one subtree, that is, S has at most one right subtree, or no. If the S node has a right subtree, it must be a single branch. If the child is red and there is no grandson, this is case 2. Go to case 2. If the S node is a leaf node, it can be red or black, and red is Case 1.

In this case, the data of P and S is exchanged, but the color is not changed, the data is converted to delete s points, and the tree is still balanced before deletion.

Or you can analyze it as follows:

If the S node is red, it must not be a single branch. If it is a leaf node, It is Case 1. Delete it directly.

If the S node is black, use the child of S to null or the red child under a single branch to replace himself, and then delete the black node. In this way, a black node is missing in the path, which affects the balance, adjustments are required. (Note how to find the parent node of the S node after replacement if the child is null. There is no sentinel node. This place is in trouble .... Okay, add a parent node pointer to the parameter of the recursive adjustment function called)

Then, set a recursive adjustment function like insert. There are four types of adjustment:

First, the deleted judgment node is X, black, the parent node is P, and the sibling node is W. If you subtract one from the black node tree of the Child tree with X as the root, you need to perform the balancing operation.

Generally, there are four situations. However, there is another special case, that is, when X is the root node, it is determined to be the same as when it is inserted. If X is the root node and is now red, change it to black. For the following scenario 2, it may be converted to this situation. This is the second time when someone else's blog is pitfall. After writing, I will test whether there are other errors.

Case 1: W is red.

Then W's children are black and P is black. Change the color of W and P, that is, change W to black, change P to red, and then use P as the pivot to left or right, depending on the position of X in P, if X is left, left, and right. After the update, X obtains a new sibling node, and then continues to judge and adjust x.


Case 2: W is black and W is black. Because a black node is deleted from the subtree where X is located, the subtree where W is located must have a black node, that is, W must not be null.

Set W to red so that the number of black nodes on the sub-tree where W is located is reduced by one, and the number of black nodes on the sub-tree where p is located is also reduced by one. The P node must be used as the current node, that is, the new x node. If the new x is red, it is set to black, and the entire tree is balanced. If the new x is black, the new x node is determined.

Case 3: W is black.

If X is P, W is red and right is black.

Set W to red, black for the left child, and right-hand for the W node.

If X is P, W's right child is red, and left child is black,

Then W is red, the right child is black, and left-handed W nodes.

Obtain a new sibling node. 4. Here, the rotation of Condition 3 will not change the balance of any subtree.

Case 4: W is black.

If X is P, W's right child is red, and left child can be red, black, or null.

The color of W and P is switched. Because the next W is to become the new parent node, the color of the original p is obtained to prevent conflicts with the parent node of P.

Set the right child of W to black, so that the right subtree of W is black node tree + 1,

Take the P node as the pivot left, so that the color of W black will move to the left subtree, so that its node tree-1 + 1 = 0, the right subtree reduces a black node + 1-1 = 0; and the new P node is the original color, and the entire tree is balanced. End,


If X is P's right child, W's left child is red. Likewise,

Change the color of W and P to black, and use P as the node's right hand. End judgment.


The final adjustment is complete. There is also a very important place here. When the left and right sides are switched, the color of the parent node P corresponds to the left and right children C, resulting in the new parent node P color after rotation has not changed, then, you do not need to check it up.

An error occurred during the intermediate test. I tried to draw some pictures and found out the error. I only drew one picture. Others thought about it through others' blogs.



Deleting is not really complicated. Try to write the code:


Template <typename T> void rbtree <t>: getparentlinked (rbnode <t> * a, rbnode <t> * B) {if (a = root) {root = B; If (B) B-> parent = 0; return;} if (a-> parent-> left =) {A-> parent-> left = B; If (B) B-> parent = A-> parent;} else {A-> parent-> right = B; if (B) B-> parent = A-> parent ;}} template <typename T> bool rbtree <t >:: Delete (const T & X) {rbnode <t> * P = root, * s, * Fa; If (! Root) return false; while (p) {If (p-> value = x) break; else if (x <p-> value) P = p-> left; else P = p-> right;} If (p) {If (p-> left & P-> right) {// when left and right children exist, rotate S = p-> right; while (S-> left) {S = s-> left;} int temp = p-> value; p-> value = s-> value; s-> value = temp; P = s;} If (p-> Red) {// indicates the red leaf node, directly Delete getparentlinked (p, p-> left); Delete P;} When else {// is black, replace the red child with him. // since it has been rotated down, there is only one special possibility, that is, to delete the black root node in a single branch. Fa = p-> parent; // there may be no parent node. If (p-> left) {S = p-> left; getparentlinked (p, p-> left);} else {S = p-> right; getparentlinked (p, p-> right);} Delete P; adjustafterdelete (S, FA);} return true;} else return false;} template <typename T> void rbtree <t> :: adjustafterdelete (rbnode <t> * X, rbnode <t> * P) {If (p) {// if there is a parent pointer, there are four situations. Rbnode <t> * w; bool inlefttree = true; // mark the left and right conditions. The left and right conditions have different rotation directions and different judgment methods. If (p-> left = x) W = p-> right; else {W = p-> left; inlefttree = false;} If (W-> Red) {w-> Red = false; P-> Red = true; If (inlefttree) lrotation (p); // rotate with P as the pivot point, do not consider whether the current node is empty. Elserrotation (p); adjustafterdelete (x, P);} else {If (! Getcolorwithnull (W-> left )&&! Getcolorwithnull (W-> right) {w-> Red = true; X = P; If (X-> Red) x-> Red = false; // an error occurs once, it depends on the new x node. Elseadjustafterdelete (x, x-> parent);} else if (inlefttree & getcolorwithnull (W-> right) // situation 4 | (! Inlefttree & getcolorwithnull (W-> left) {bool tempred = p-> red; P-> Red = W-> red; W-> Red = tempred; if (inlefttree) {w-> right-> Red = false; lrotation (p);} else {w-> left-> Red = false; rrotation (p );}} else {// case 3 W-> Red = true; If (inlefttree) {w-> left-> Red = false; rrotation (w); adjustafterdelete (X, p) ;}else {w-> right-> Red = false; lrotation (w); adjustafterdelete (X, p );}}}} else {// if the parent node does not exist, the node is the root node and ends in black. If (x) x-> Red = false; return ;}}



My red/black tree code!


Delete a red/black tree node

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.