Red/black tree of Data Structure

Source: Internet
Author: User

Author:Dong| Reprinted, but the original source, author information, and copyright statement of the article must be indicated in hyperlink form
Web: http://dongxicheng.org/structure/red-black-tree/

1. Introduction

The red/black tree is a self-balancing Binary Search Tree. Its statistical performance is better than that of the balanced binary tree (AVL Tree). Therefore, the red and black trees are used in many places. In C ++ STL, many components (including set, Multiset, map, and multimap) apply the variant of the Red-black tree (the red-black tree in sgi stl has some changes, these modifications provide better performance and support for set operations ). It is complex, but its operation has a good run time in the worst case, and is efficient in practice: It can be searched in O (log n) Time, insert and delete operations.

This article describes the basic properties and operations of the red/black tree.

2. properties of the red/black tree

The red-black tree, as its name implies, uses the red-black color fields to ensure the approximate height balance of the tree. Each node has a quintuple: color, key, left, right, and P ).

The definition of the Red-black tree is also of its nature. There are five items:

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.

These five properties force the key nature of the red and black trees: the longest possible path from the root to the leaves is no more than twice the shortest possible path. Why? Nature 4 implies that there cannot be two adjacent red nodes in any simple path. In this way, the shortest paths are all black nodes, and the longest possible paths are alternate red and black nodes. At the same time, according to nature 5, all the longest paths have the same number of black nodes, which indicates that no path can be twice longer than any other paths.

3. Basic operations on the red/black tree

Because the red/black tree is also a binary search tree, the search operation on the red/black tree is the same as that on the common Binary Search Tree. However, the insert and delete operations on the red/black tree will no longer conform to the nature of the red/black tree. Restoring the red/black tree requires a small number of (O (log n) color changes (which is actually very fast) and no more than three tree rotations (two insert operations ). Although insertion and deletion are complex, the operation time can still be O (log n) times.

3.1 insert operation

The insert operation can be summarized as follows:

(1) locate the location to be inserted. the time complexity is: O (N)

(2) Assign the color of the new node to red.

(3) from the bottom up, re-adjust the tree to a red/black tree

The search method in step (1) is the same as that in the common Binary Search Tree. in step (2), the color of the newly inserted node is red because: if it is set to black, it will lead to a path from the root to the leaf and an extra black node, which is difficult to adjust. However, after a red node is set, two consecutive red nodes may conflict. you can adjust it by changing the color (color flips) and rotating the tree. This is much easier. The following describes the details of Step 3:

Set the node to be inserted to N, its parent node to P, and its parent G's sibling node to U (that is, P and u are two subnodes of the same node ).

[1] If P is black, the entire tree does not have to be adjusted to be a red-black tree.

[2] If P is red (it is known that its parent node G must be black), then after Z is inserted, it violates Nature 4 and needs to be adjusted. There are three situations for adjustment:

(A) n's uncle u is red

As shown in, we repaint P and U as black and repaint node G as red (used to preserve the property 5 ). Now the new node N has a black parent node P, because any path through the parent node P or Uncle node u must pass through the grandfather node G, the number of black nodes in these paths has not changed. However, the red grandfather node G's parent node may also be red, which violates Nature 4. To solve this problem, we recursively adjust the color on the grandfather node G.

(B) n's uncle u is black and N is the right child

As shown in, we perform a left-handed forwarding for P to change the role of the node and its parent node. Then, we process the previous parent node P according to the situation (c) to solve the problem of still invalid Nature 4.

(C) n's uncle u is black and N is the left child

As shown in, a right rotation is performed on the grandfather node G. In the tree generated by the rotation, the former parent node P is now the parent node of the new node N and the former grandfather node G, then, the color of the previous parent node P and grandfather node G is exchanged. The result tree satisfies the property 4, and the property 5 [4] is still satisfied.

3.2 delete operation

The delete operation can be summarized as follows:

(1) locate the location to be deleted. The time complexity is: O (n)

(2) Replace the node with the deleted node successor or node (only data replacement is required, and no pointer needs to be adjusted. The successor node is the node that is closely tied to the node in the middle-order traversal, namely: the leftmost child node of the right child)

(3) If the node to be deleted is replaced by a black one, you need to re-adjust the tree to a red-black tree.

The search method in step (1) is the same as that in the common Binary Search Tree. in step (2), the reason why the node is deleted is replaced by a successor node, this ensures that the successor node is still a red/black tree, and the successor node may be a leaf node or only a right subtree node, in this way, you only need to replace the successor node with a node to delete the node. If the node to be deleted has two sons, the problem can be converted to the problem of deleting another node with only one son. (Do not understand ??? See: http://zh.wikipedia.org/wiki/%E7%BA%A2%E9%BB%91%E6%A0%91) in step (3), if the delete node is a red node, his father and child are all black nodes, so that you can directly Delete the node, no adjustment is required. If the deleted node is a black node, there are four situations:

Set the node to be deleted to N, its parent node to P, and its sibling node to S.

Because N is black, P may be black or red, s may be black or red.

(1) S is red

P must be red at this time. We rotate n's parent node left and convert the red brother to n's grandfather. We then adjusted the colors of N's father and grandfather. Although all paths still have the same number of black nodes, now N has a black brother and a red father, so we can continue to press (2), (3) or (4.

(2) The Children of S and S are all black.

In this case, P may be black or red, and our simple re-painting S is red. The result is all the paths through S, which are those that didn't pass n before, with a black node missing. Because deleting the initial father of N removes a black node from all the paths of N, which balances everything. However, all paths passing through P are now less of a black node than those without passing through P. Next, we need to adjust P as N recursive adjustment tree.

(3) s is black, S's left child is red, and right child is black

In this case, we perform right rotation on S, so that S's left son becomes S's father and N's new brother. We then exchanged the colors of S and its new father. All paths still have the same number of black nodes, but now N has a black brother whose right son is red, so we entered the situation (4 ). N and his father are not affected by this change.

(4) S is black, and S's right child is red

In this case, we made a left rotation on N's father, so s became N's father and S's right son's father. We then exchanged the colors of N's father and S, and made S's right son black. The subtree still has the same color on its root, so attribute 3 is not violated. However, N now adds a black ancestor: either n's father turns black, or it is black, and S is added to a black grandfather. Therefore, a black node is added through the N path.

4. References

(1) Introduction to algorithms, Second Edition

(2) http://zh.wikipedia.org/wiki/%E7%BA%A2%E9%BB%91%E6%A0%91

--------------------------------

For more information about data structures and algorithms, see data structure and algorithm summary.

--------------------------------

Original article, reprinted Please note:Reposted from Dong's blog

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

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.