Red-black Tree Delete operation

Source: Internet
Author: User

If the deleted node has two non-leaf nodes, it can be converted to delete a "replacement point" problem, the replacement point at most only one non-leaf child node. You can replace the node that was originally deleted by a precursor or a successor (with up to one non-leaf child node), so just focus on the problem of only one non-leaf child node, and once we've solved the problem, the workaround will work in two situations:
1, originally want to delete the node has a non-leaf child node
2, originally wanted to delete the node has two non-leaf children node (through the precursor and successor can be converted to a non-leaf child node)

OK, now use N to represent the alternative node (if the deleted node has no substitution node, then n is the node that was supposed to be deleted, and the attribute of the red and black tree can be used to make the node impossible to have grandson nodes), the substitution node is either a precursor or a successor, but at most one non-leaf child node ( The program is used in the precursor). When the node n is different color, the processing method is not the same, as follows.

node n is red:

If the node n is red, then its child node must be black, and two children node is a leaf node (because node n can only have a non-leaf child node, so only one side is a black non-leaf child node, the other side is a black leaf knot point, So the number of black nodes through the path of the node n is different (because the black non-leaf child node has at least two black leaf nodes), which violates the nature of the 5). At this point, N is deleted, then n is replaced by an empty leaf node (null), and the null node is black, so attribute 5 remains. Well, the node n is red, the situation is relatively simple, see the node n is black.

node n is black:

When the node n is black, it can be divided into two situations:
1th case: If the child node m has a non-leaf node, and the other is a black empty leaf node, then the non-leaf node m must be red. Because if the non-leaf node is also black, so that through the path of the node n the number of black nodes will be different (through the empty leaf node the number of black nodes on this side is 2, and through the black non-leaf node on this side is at least 3), thereby violating the nature of 5. So the non-leaf node m can only be red, the case is relatively simple, just use m instead of N, and the M to black, do not violate any attributes, at this time is still a qualified red black tree.

2nd case: If the two child node is empty leaf node (leaf node is black, and is null), then with the empty leaf node null substitution n, the path through the original alternative node n the number of black nodes will be reduced by 1 compared to the original, at this time, the classification is discussed, the substitution of the parent node n is expressed as p, The sibling node is expressed as S. The specific categories are as follows:

Case 1:

n is the root node, in which case we have done it (just by returning directly to this effect!) ), because in this case the whole tree has only one node (note the premise: n The two child nodes are empty leaf nodes, so the whole tree has only one node, the node to be deleted is the root node, directly after the return, in the last layer of the function with N's sub-node (NULL) instead of N, the equivalent of n deleted.

Note: In case 2,case 5,case 6, we assume that N is the left child node of the parent node p, and if it is the right child node, then the "left" and "right" should be swapped.

Case 2:

The brother node S of N is red (starting with Case 3 S is black). At this point, the parent node P (which is definitely black) and the sibling node s color are swapped, then the parent node p is left-handed. Note that at this point, if N is deleted and ended, then there is only one black node (NULL) on the left side of the P node, and two black nodes on the right side (S's left child and S's grandson node (black empty leaf node)), which violates attribute 5, so it is followed by case 4, cases 5, or case 6来 processing.

Case 3:

Node N's parent node p, the sibling node s and the children's node are all black. Simply set S to red at this point, so that all paths through s are less than one black node, which is the same as the black nodes through the path of N. However, all paths through P now have a black node less than the path that does not pass the p, so it is still a violation of nature 5. To fix this problem, we'll start with Case 1, rebalance on p, and always recursively to the root node, with one black node missing from all paths through the root node, and then back from Case 1.

Case 4:

Node N's parent node p is red, and the sibling node S and S's two child nodes (all empty leaf nodes) are black. At this point the color of P and S is swapped, which does not affect the number of black nodes through S, but adds a black node on the path through N, just to cancel the node n to be removed, so that attribute 5 is satisfied.


Case 5:

The left child of the knot N's brother node S is red, the right child is black (left red Right black), and node n is the left child of the parent node p. Swap the color of s with its left child, then right-turn s. Note that at this point the path through the parent node P has the same number of black nodes, but if you delete the n directly will violate attribute 5 (similar to case 2), so we continue to follow the cases 6来 processing.

Case 6:

Node N's sibling node S is black, S's right child is red (right red), and node n is the left child of parent node p. The parent node p and the sibling node s are colored, then the right child of S becomes black, then the left parent node p. (not to be continued)

Red-black Tree Delete operation

Related Article

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.