"Reprint" Complete Simple red-black tree algorithm

Source: Internet
Author: User

Original:

Complete simple red-black tree algorithm

The recent set of rules, weekly sharing an algorithm, last week is the first week, sharing is the red and black trees, the following is the summary of their own study, the feeling of the Internet is not particularly clear, or is written in a particularly complex, not a little organized.

One, red black tree nature

1. Each node is either red or black.

2. The root node is black.

3. Each leaf node (leaf node refers to a nil pointer or null node at the end of the tree) is black

4. If a junction is red, then its two sons are black.

5. For any node, each path to the nil pointer to the leaf node tree end contains the same number of black nodes

Summary: The red and black tree is either a single black-red or has two sub-nodes in a balanced state

Second, the complexity of

O (LGN)

Third, node insertion

What steps do you need to take to insert a node into a red-black tree? First, the red and black tree as a binary search tree, the node is inserted, then the node is colored red, and finally, by rotation and re-coloring and other methods to correct the tree, so that it re-become a red black tree.

1. Color the inserted nodes in red, without violating the "properties (5)"! The less you violate one trait, the less we need to deal with. Next, it is necessary to make the tree satisfy other properties, and if it is satisfied, it will be a red and black tree.

2. For "Feature 4", it is possible to violate!

In short: The newly inserted node is red!

3.5 Cases of insertion:

(1) If the root node is inserted, because the original tree is an empty tree, this situation will only violate the nature of 2, so the node is directly painted black;

(2) If the parent node of the inserted node is black, because this does not violate the Nature 2 and the Nature 4, the red-black tree is not destroyed, so at this time do nothing.

(3) If the parent node of the current node is red and the other child node of the grandfather node (Uncle node) is red;

FIX: The parent node of the current node and uncle Black, the grandfather knot red, the current node point to the Grandfather node, the new current nodes restart the algorithm.

The following (4) (5) Take the left child as an example, the right child for symmetrical operation can

(4) The parent node of the current node is red, the Uncle node is black, the current node is the left child of its parent node;

FIX: The parent node turns black, the grandfather node turns red, and the grandfather node is pivot right.

(5) The parent node of the current node is red, the Uncle node is black, the current node is the right child of its parent node;

Workaround: The parent node of the current node is the new current node, with the new current node being the pivot left. Issue to (4)

Summary: The whole process is to solve the above problems, the key is the whole process to update the current node is which nodes

Iv. Deletion of nodes

The actions that need to be performed are: first, the red-black tree is treated as a binary lookup tree, and the node is removed from the two-fork lookup tree, and then the tree is corrected by a series of "rotation and recolor" to make it a red-black tree again.

The number of nodes to be deleted can be divided into three types according to their sons:

1. Delete leaf nodes (no sub-nodes)

(1) If the leaf node is red, delete it directly.

(2) If the leaf node is black, the sibling node has no sub-nodes.

FIX: brother Node B is painted red, parent p is painted black.

Note: After the operation of the node is all left child, right child for symmetrical operation can

(3) The leaf knot is black, and the sibling node has a child that is not nil. If the child is the right child.

FIX: The right child of B is painted black, B is painted as its parent node p original color, p is painted black, and p is left rotated once.

(4) The Leaf knot is black, and the sibling node has a child that is not nil, if the child is the left child.

FIX: The left child of B is painted black, B is painted in red, then a right rotation is made to B, and the problem is turned into the right child's case.

(5) The leaf node is black, and the sibling node has two children. If the sibling node (b) is red, the two children of B must be black.

FIX: paint B as black, B's left child to paint red, and then P (parent node) for a left rotation.

(6) The leaf node is black, and the sibling node (B) has two children. If B is black, the two children of B must be red.

FIX: The parent node p of B is painted black, B's right child is painted black, B is painted as its parent node p, and then a left rotation is made to p.

2. Delete node has an external node (there is a child node, this node C must be a red node, otherwise the number of black nodes on the path from D to each nil node will be different)

FIX: swap D (delete node) and C (sub-node) content (color remains the same), deleted node changed to C, the problem into the deleted node two children are nil. Re-see if the tree meets the red-black tree.

3. Delete node has two external nodes (with two sub-nodes)

Solution: Follow the binary search tree to delete nodes to find the successor S of D, swap the contents of D and S (the color remains the same), the deleted node becomes S, and if s has a node that is not nil, then continue to replace S with the successor of S (this process may be a first-level search, or the maximum value of Zuozi directly Depending on the size of the node that was to be deleted and the value of the actual delete node, the node to be deleted is smaller than the actual deleted node, first-level search, otherwise, until the two children of the deleted nodes are nil, and the problem translates to two children who were deleted node D as nil.

One of the left-hand is actually very simple, there is not clear to the Internet to check, here is no longer described in detail.

V. Application Scenarios

1. Famous Linux process scheduling completely Fair Scheduler, with red-black tree management Process Control block

2.epoll implementation in the kernel, managing event blocks with red and black trees

3.nginx, use red and black tree to manage timer, etc.

TreeMap implementation of 4.Java

5. Widely used in C + + STL. Both the map and the set are implemented with red and black trees.

Vi. Other

Insert and delete operation case illustrations from beginning to end:

http://blog.csdn.net/v_july_v/article/details/6284050

Implementation of Nginx Red and black tree (c implementation)

http://blog.csdn.net/liuxuejiang158blog/article/details/21417145

C's Red-black tree implementation

Http://www.cnblogs.com/skywang12345/p/3624177.html#a3

PHP's red-black tree implementation

http://www.zhangley.com/article/php_rbtree/

"Reprint" Complete Simple red-black tree algorithm

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.