Red/black tree,

Source: Internet
Author: User

Red/black tree,

Http://blog.csdn.net/sun_tttt/article/details/65445754

 

The red-black tree is a balanced binary tree, but it is not a perfect balanced binary tree.Although we hope that all searches can be found in ~ The lgN comparison ends, but it is too costly to maintain a perfect balance between the trees during dynamic insertion. Therefore, let's just relax and look at the limitations, we hope to find a data structure that can complete the search within the logarithm time.At this time, the red and black trees stood up.

 

Read the following to learn about the insert and delete operations for common Binary Trees.

The red-black tree is built on a common binary tree. It adds a color attribute to no node. At the same time, the entire Red-black binary tree needs to satisfy five properties at the same time.

 

Five properties to be met:

 

Property 1: nodes are red or black; 

The nodes in the tree are either red or black, and there are no other colors. How can they be called the Red and black trees.

Property 2: the root node is black; 

The root node is always black. It cannot be red.

Nature 3: each leaf node (NIL or empty node) is black; 

This may be a bit difficult to understand, as shown in the figure:

 

This image is a red/black tree. The NIL node is an empty node and black.

Property 4: The two subnodes of each Red node are black (that is, there are no two consecutive red nodes ); 

That is, the two consecutive nodes cannot be in red. The two consecutive nodes mean that the parent node and the child node cannot be in red consecutively.

Nature 5: All paths from any node to no leaf node contain the same number of black nodes;

You can see three of the same number of black nodes;

 

 

All the operations we perform when performing an insert or delete operation are to adjust the tree to conform to these five properties.

 

Next we will first introduce two basic operations, rotation.

The purpose of rotation is to assign one of the multiple nodes to one of the other, which is often used in insert and delete operations.

The following are left and right: left:

 

Right hand:

 

Insert

Let's clarify the name of each node

 

 

To meet these five properties of the red and black trees, If we insert a black node, it violates nature 5 and requires large-scale adjustment. If we insert a red node, therefore, only when the parent node to be inserted is red violates Nature 4 or when the inserted node is the root node, it violates nature 2. Therefore, the color of the node to be inserted is red.

 

Below are several possible insertion conditions:

1. When the inserted node is the root node, it can be blacked out directly;

2. When the parent node of the node to be inserted is black.

 

At this time, inserting a red node does not damage these five properties. Therefore, you do not need to adjust the insert directly.

 

3. If the parent node of the node to be inserted is red and the parent node is the left branch of the grandfather node.

There are two cases: the uncle node is black, and the uncle node is red.

When Uncle is black, there are two types of situations: one is that the node to be inserted is the left branch of the parent node, and the other is that the node to be inserted is the right branch of the father.

Let's take a look at the situation when the node to be inserted is the left branch of the parent node:

 

At this time, the Nature 4 is violated. We need to adjust the operation to make it conform to the Nature 4. We can change the color of the grandfather node and the parent node at the same time by right-handed the grandfather node, this becomes:

 

After such adjustment, it can be in line with Nature 4 and will not cause damage to other properties.

When the inserted node is the right of the parent node:

 

When the node to be inserted is the right branch of the parent node, We can first left-hand the parent node and change it to the following:

 

If we regard the original parent node as a new node to be inserted and the original node to be inserted as a new parent node, that is, when the node to be inserted is on the left of the parent node, yes, it is rotated according to the condition that the node to be inserted is on the left of the parent node, after the rotation is completed, it becomes as follows:

 

4. If the parent node of the node to be inserted is red and the parent node is the right of the grandfather node;

In this case, the situation 3 indicates an image. You can swap the Left and Right of case 3.

 

5. If the parent node of the node to be inserted is red and the uncle node is red, as follows:

 

 

 

At this time, you only need to black out the Father's Day node and Uncle node, and red the grandfather node.

The above is the whole process of insertion.

 

First, you need to know how to delete a common Binary Tree:
1. if you delete a leaf node, you can directly delete it;
2. If the deleted element has a subnode, you can directly move the subnode to the position of the deleted element;
3. if there are two subnodes, You can swap the smallest node on the right of the deleted element (the leftmost node on the right of the deleted element) with the deleted element, we call the leftmost node on the right of the deleted element a successor node (a successor element), and then perform operations based on case 1 or case 2.
 
Swap the deleted element with the smallest element on the right, as shown in:

 
Then delete the deleted element:

The deleted elements referred to below are all deleted elements that have been exchanged.
After adding the color, the deleted and subsequent elements are only interchangeable, but not the color. Please note that.

 

 

The following describes the rules for deleting the red/black tree:
1. When the deleted element is red, it has no effect on the five properties and is deleted directly.
2. When the deleted element is black and the element is the root node, delete it directly.
3. When the deleted element is black and the right child node is red, black the right child node to the location of the deleted element,
By
 
Change

4. when the deleted element is black and the brother node is black, the two children of the brother node are also black, and the parent node is red, the color of the brother node and the parent node is changed; the NIL element indicates that each leaf node has two blank NIL elements whose color is black. When it is needed, it can be regarded as two black elements, you can ignore it when you don't need it.
 
By
 
To:
 
5. when the deleted element is black and the left branch of the parent node is black and the right branch of the brother is red, the color of the brother and the father must be exchanged, in addition, the right branches of the father and brother are painted black and left centered on the parent node.
By:

 
To:

 
6. when the deleted element is black and the left branch of the parent node is black and the left branch of the sibling node is red, you must first swap the color of the sibling node and the left child node of the sibling node, turn right, and then change to rule 5, and rotate according to rule 5.
By
 
First, the color of the left child node of the brother and the brother is swapped, and then right turns:

 
Then, rotate according to Rule 5:
 
7. When the deleted element is black and the right branch of the parent element, the image is used in case 5. 6.
8. the deleted element is black and the brother node is black. The child of the brother node is black and the father is black. In this case, the brother node needs to be red, let's look at the father as the deleted element (only as, but not actually deleted) to see which deletion rule of the father character and process the changes.
By:
 
To:
 
8. when the deleted element is black and the left branch of the parent element and the sibling node is red, you need to change the color of the sibling node and the parent node to the left of the parent node, in case 4, perform the following operations:
By:

 
Exchange the color of the sibling node and the parent node, and change the left-hand:
 
Perform the following operations according to case 4:

Now, the deletion process is complete. Nothing is mentioned. Remember to change the color of the root element to black when adding or deleting the element.
There is no language implementation here. I just talked about the steps to insert and delete the red and black trees. You can implement the red and black trees by yourself according to the steps.

Click here to build a red/black tree step by step according to the rules.

Finally:
1. the implementation of the Red-black tree is actually a tree of 2, 3, and 4, but the dual nodes or three nodes are marked in red, if you place a red node at the same height as its parent element and regard it as a parent element, you will find that it becomes a binary tree with a height of lgN, this 2.3.4 tree is very enlightening for the red and black trees.
2. in fact, the above steps can be deduced without rote memorization, because we re-balance a red-black tree that breaks the balance by inserting or deleting it. This is the same as rotating the position, change the red and black colors to conform to the five basic properties. For example, in case of delete operation scenario 4, we can remove the deleted element and find that there is one black element fewer on the left than on the right. What should we do at this time, we found that the child element of the sibling node is a red element, and this operation will not affect those five properties. So we can change the color and rotate to make the black numbers on both sides of the left and right the same.
3. The purpose of the rotation operation is to transfer an element to another place and conform to the properties of the left, right, and big binary tree. The purpose of color switching is to maintain the five properties of the red and black trees.
4. Always remember that all operations are to maintain those five properties.

Finally, there is actually a simpler red-black binary tree. This simple red-black binary tree is actually a 2.3-tree. It only allows the left node to be a red node, but the performance is definitely not as good as the red/black tree. This simple red-and-black binary tree is introduced in the fourth version of algorithm. After you have mastered it, you will see this simple red-and-black binary tree.
At the end of the last step, you must try to deduce the insertion and deletion rules by yourself. Otherwise, you will often forget that it is a bit hard to forget when you get up and look at it again.

 

The role of the red and black tree is generally used for the kernel epoll function, see know link: https://www.zhihu.com/question/30527705

Engineering is mainly used for file system and data search, and its algorithm time complexity can be found in: http://blog.csdn.net/gongyiling3468/article/details/47804223

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.