Data structure of red-black tree (three)--delete operation

Source: Internet
Author: User
Tags dashed line

Deleting a node can also change the balance of the tree, and the imbalance caused by the deletion is more complex than the balance correction caused by the insertion.

Simplification is a common method in algorithm analysis. Below we will want to delete the node into three main categories: to delete the node as a leaf node, to delete a node only one child node and to delete has two child nodes.

There are two possible colors for deleting nodes, and they need to be treated separately.

To simplify the discussion, we fix the case where we want to delete the node on the left, and if you want to delete the node on the right side, make a mirror correction to the action.


1. To delete a node is a leaf node1.1 to delete a node as red, the parent node must be black and there will be no sibling nodes.

Only two cases are shown, with a yellow border for the node you want to delete :


The node can be deleted directly, and the black height of the subtree is unchanged, which will not affect the nature of the red and black trees.

1.2 to remove a node as black, the parent node can be red and black, and there must be a sibling node.

Why is it that the black node must have a sibling node ? If a black node does not have a sibling node, regardless of whether the parent node is red or black, there is less black height from the node to the parent node than the empty node to the parent node, so this situation does not exist.

Here are some possible scenarios that are analyzed individually:

1.2.1 parent node is red, then the brother node must be black

There are two kinds of cases related to whether the sibling node has left dial hand nodes.

(1) If the sibling node has no left child node, the remediation policy is as follows:


, the dashed line indicates that the child nodes to which they are connected can be absent and have no effect on the remediation process.

(2) If the sibling node has a left dial hand node, the remediation policy is as follows:


1.2.2 parent node is black, the sibling node can be red or black 1.2.2.1 The sibling node is red, it must have two black child nodes.

There are three kinds of cases related to whether there are child nodes in the left nephew node.

(1) If the left nephew node has a right child node, the remediation policy is as follows:


(2) If the left nephew node has no right child node, only the left Dial hand node, the remediation policy is as follows:


At this point, the left nephew node has a right child node, and then the same as in the case of a correction.

(3) If the left nephew node is a leaf node, the remediation strategy is as follows:


1.2.2.2 Brothers node is black

There are three kinds of cases related to whether the sibling node has child nodes.

(1) If the sibling node has a right child node, the remediation policy is as follows:


(2) If the sibling node has no right child node, only the left Dial hand node, the remediation policy is as follows:

(3) If the sibling node has no left child node and no right child node, as shown in:


At this point, it is not possible to rely on the subtree itself , because the subtree has a black height of 2, and if the node with the yellow border is deleted, no matter how the color or rotation is changed, the subtree will not be able to restore the black height.

There are two ways to solve this problem:

(1) With the help of the grandparent node and the grandparents of the grandfather node to remove the node, as long as the grandfather node and the grandfather node of the other subtree contains a red node, can be changed by color and rotation. However, if the nodes in the other subtree of the grandparent node and the grandparent node are all Black:


At this point, depending on the subtree itself is not solved, but also need to rely on the upper nodes. Layer passes until the root node. If the root node still does not resolve, you need to adopt another way of thinking

(2) lower the black height.


But for this subtree, it is true that it complies with the red and black rules, but the black height of the subtree decreases, affecting the black height of the whole red and black tree.


As you can see, after the transformation, the black height of the Zuozi is 1, while the black height of the right subtree is 2, which violates the red and black rules. At this time, it is necessary to reduce the black height of the right subtree, and pass the layer upward, until the root node, and eventually the whole tree black height is lowered.

However, some right subtrees are not able to lower the black height, for example:


In the right sub-tree can not reduce the black height, but in this case, the right subtree must have a red node, the first way to solve the problem.

In short, the combination of two ideas, is always able to solve the "black triangle" this stubborn problem.

2. to delete a node with only one child node,The node must be black and the child nodes must be red

The Red node has no child nodes, or two black nodes, so the node cannot be red.

If the node has only one black node, the Black child node to the black height of the node is inconsistent with the black height of the sub-node to that point, so the child node can only be red.

In summary, only the two cases shown will appear.


Just black the Red child node and move it up to the deleted node position.

3. to delete a node, both the left Dial hand node and the right child node.

Red and black trees also belong to the binary search tree, so first find the node to delete the successor node . The subsequent node search process is as follows: Starting from the right child node of the point, if there is a left dial hand node, jump to the left child node, layer down, until a child node has no left child node. is actually finding the smallest value in a collection that is larger than the key value of the node to delete. This part of the content in the binary search tree has been introduced, no longer repeat.

In the first step, we want to delete the node and the data in the subsequent node, so that the deletion of the node is transferred to the successor node.

, the color of any node is not changed after the data exchange, and now the red node with the yellow border is removed. As already demonstrated, the successor node is definitely a node with no left child node , either a leaf node or a node with only one right child node, and the deletion of these two cases has been analyzed before and given a workaround.

At this point, all possible deletions of the red and black tree have been discussed.

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

Data structure of red-black tree (three)--delete 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.