Red/black tree --- Delete

Source: Internet
Author: User

It's still the same, first of all:

Property 1. nodes are red or black

Nature 2. The root is black

Nature 3. All leaves are black (leaves are nil nodes)

Nature 4. If a node is red, its two sons are black

5. All simple paths from any node to its leaves contain the same number of black nodes.

We define:

We can see from the deletion of the binary search tree, the last deleted node must be a node with only one child node or one child node without any node (you can find the deletion of the binary search if you do not understand it ), we will actually Delete the node v. Its child node u (which may be a sentinel node or an internal node) will replace the location where v is deleted. U's brother node is S, if:

Let's take a look at what will happen when a node is deleted from the red/black tree:

1. If the color of the deleted node is red, the properties of the red/black tree will not change and no operation is required.

Because the deleted node is red, it will not affect the black height of any path, it will not damage the nature 5, it will not be two consecutive red nodes on the same path, it will not damage the Nature 4

2. If the deleted node is black

If the deleted node is black, the black height of the U path will be reduced by 1, and the property 5 will be damaged. At the same time, if the U and P (v) are both red, the property 4 will also be damaged.

The following describes how to deal with the second situation, that is, the deleted node is black,

The main property is that property 5 is damaged, that is, the number of black nodes on the path of the U node is reduced by 1, what we need to do is to try to add 1 to the number of black nodes in the left subtree (we assume that U is the left child of P (u ), how can we add 1 to the number of black nodes of the left child, while the number of black nodes of the right child remains unchanged, so that the right child does not hold 4 (there are two consecutive red nodes )?

Analysis:

What we need to do now is to make the deleted tree meet the characteristics of the red and black trees. We will discuss the following situations: (because the deleted node v is red, it will not be discussed here)

If the deleted node is black, it will damage the red/black tree, how can we balance the black height on the path of the deleted node with that before deletion?

1. if u is red or the root node, both of them can be satisfied. At this time, we can directly black the U without any operation.

If u is red, regardless of the color of P, after U is black, the black height on the path that passes through this node is added to 1, which is the same as the black height before deletion. This meets the requirements of the property 5, and nature 4

2. if u is black, we move P to the left, s will be moved to the top of P, we make P Black, S is the color of the original p, in this way, a black node will be added to the left child. Next we need to reduce the number of black nodes in the right child. We will analyze it in several situations:

2.1 If S is red, when P is red, s moving up will not damage the property of the red/black tree, but when P is black, the black height on the path of the L node is increased by 1 (because l's father is P after rotation, because l's father is red S, and then Black P, so the black height will increase by 1)

2.2 If S is black, when P is red, s will not damage the property of the Red-black tree, but when P is black, s will move up, the black node height passing through the R branch will be 1 less, because the Black node P is moved below

Therefore, the fixed method cannot be determined only by the s color. Therefore, we also need to look at the child node of S, because after moving, l will serve as the child node of the P node, P is the black node, so we want to reduce the adjustment of L, so we will discuss in the case that S is black, because S's left child l will not destroy the tree structure after moving to P child, so l color will not damage the properties of the red and black trees. Let's discuss the situation of the right child, no matter what the r color is, our goal is to keep the black height on the right unchanged from the original one, but s is black. Moving to the top may change to Red. If it does not change to red, the black height of the right child remains unchanged, if it turns red, the black height of the right subtree will be reduced by 1.

2.2.1 If the r of the right child is black, S turns to red after being moved up, and the black height of the right branch is reduced by 1, which cannot achieve the purpose of balancing with the original.

2.2.1 If the r of the right child is red, S turns to red after moving up, and the black height of the right branch is reduced by 1, we can change the r node of the right branch to black, in this way, the black height of the right branch remains unchanged.

The above is a description of the following details. The following four situations are listed in the introduction to algorithms. I only know the steps and I don't know why, here, we will record that the first three cases in the introduction are both in the fourth case, because only the fourth case is involved, only when the red/black tree is set successfully


The main maintenance in the deleted species is property 5, because it will destroy the structure of the entire tree, which is also the most troublesome one, it will reduce the black height of a branch. The overall solution is to add a black node to its path and keep the black height of its right branch unchanged, in this way, the original nature is restored, but the conditions are harsh.

There are also dual-black nodes and red-black nodes in the introduction to algorithms, which are both a strategy and do not exist. Do not misunderstand them when you look at them.

I will not talk about the four situations here. I have mentioned many things better than I have. Here I just want to record them from another aspect, only personal understanding...

Thank you for your reference!

Http://dongxicheng.org/structure/red-black-tree/

Http://wangdei.iteye.com/blog/236157

Http://blog.csdn.net/v_JULY_v/article/details/6105630

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.