Data structure of the red and black tree detailed _c language

Source: Internet
Author: User
Tags color gamut

1. Introduction

The red-black tree is a self balanced binary lookup tree. Its statistical performance is better than the balanced binary tree (AVL tree), so the red-black tree is used in many places. In C + + STL, many parts (currently including set, Multiset, map, Multimap) apply the variant of the red-black tree (there are some changes in the red-black tree in the SGI STL, which provide better performance and support for set operations). It is complex, but its operation has a good worst-case runtime and is efficient in practice: it can do lookups, inserts, and deletes in O (log n) time.

This paper introduces the basic properties and basic operation of the red-black tree.

2. The nature of the red-black tree

Red-black trees, as the name suggests, through the red and black two color gamut to ensure that the height of the tree approximate balance. Each of its nodes is a five-tuple group: color (colors), key (data), left (left-hand child), right (children), and P (parent node).

The definition of the red-black tree is also its nature, which has the following five articles:

Nature 1. node is red or black

Nature 2. The root is black.

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

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

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

These five properties force the key nature of the red-black tree: The longest possible path from the root to the leaf is no more than twice times the shortest possible path. Why, then? Nature 4 implies that there can be no two contiguous red nodes on any simple path, so that the shortest possible path is all black nodes, and the longest possible path has alternating red and black nodes. At the same time, by Nature 5 knows: all the longest paths have the same number of black nodes, which indicates that no path can be more than twice times longer than any other path.

3. Basic operation of red and black tree

Because the red-black tree is also a binary lookup tree, the lookup operation on the red-black tree is the same as the lookup operation on the normal binary lookup tree. However, insert operations and deletions on the red-black tree can result in no longer conforming to the nature of the red-black tree. The nature of the red-black tree requires a small amount (O (log n)) color change (actually very fast) and no more than three tree rotations (two times for insert operations). Although inserts and deletes are complex, the operation time can still remain O (log n) times.

3.1 Insert operation

The insert operation can be summarized in the following steps:

(1) Find the location to insert, the time complexity is: O (N)

(2) The color of the new node is assigned to red

(3) Adjust the tree to a red-black tree from the bottom

Where the search method for step (1) is the same as the normal binary lookup tree, the first (2) step is to assign the color of the newly inserted node red because: if set to black, it will cause the root to the path of the leaf there is a road, more than one additional black node, this is difficult to adjust. But when set to a red node, which can cause two consecutive red nodes to conflict, it is much simpler to adjust by color flips and tree rotation. Some of the details of step (3) are discussed below:

Set the node to insert is n, its parent node is P, and its father G's sibling node is U (that is, p and U are two child nodes of the same node).

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

[2] If P is red (know that its parent node G must be black), then after inserting z, it violates the nature of 4 and needs to be adjusted. Adjustment time of the following 3 cases:

(a) n's Uncle U is red

As shown in the figure above, we redraw p and u to black and redraw node G to red (to preserve the property 5). Now the new node n has a black parent node p, because the number of black nodes on these paths does not change because any path through the parent node p or Uncle node is bound to pass through the grandfather node G. However, the parent node of the red Grandfather node G may also be red, which violates the nature of 4. To solve this problem, we adjust the color recursively on the grandfather node G.

(b) N's uncle U is black, and N is the right child

As shown in the figure above, we make a left rotation of p to replace the new node and its parent node role; Then, by case (c), the former parent node p is processed to resolve the still defunct property 4.

(c) N's uncle U is black, and N is the left child

As shown above, a right rotation of the grandfather node G; In the rotation-generated tree, the former parent node P is now the parent node of the new node N and the former grandparent node G, and then swaps the colors of the former parent node p and the Grandparent node G, and the resulting tree satisfies the property 4, while the nature 5[4] also remains satisfied.

3.2 Delete operation

The delete operation can be summarized in the following steps:

(1) Find the location to delete, the time complexity is: O (N)

(2) With the deletion node successor or node replacement of the node (only data replacement can be, do not need to adjust the pointer, the successor node is in the middle of the sequence traversal node next to the node, that is: the right child's most left child node)

(3) If the replacement node of the deletion node is black, you need to readjust the tree to a red-black tree

Where the search method for step (1) is the same as the normal binary lookup tree, the first (2) step replaces the deletion node with the successor node because it guarantees that the successor node is still a red-black tree, and the subsequent node may be a leaf node or a node with only the right subtree. The deletion can be achieved only by replacing the successor node with a node. If the node that needs to be deleted has two sons, then the problem can be translated into deleting another node with only one son. (Not read??? Refer to: http://zh.wikipedia.org/wiki/%E7%BA%A2%E9%BB%91%E6%A0%91) in step (3), if the deletion node is a red node, then his father and child are all black nodes, so that the node can be deleted directly, No adjustments are necessary. If the deletion node is a black node, there are four different scenarios:

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

Because n is black, p may be black, or it may be red, and s may be black or red.

(1) S is red.

P must be red at this point. We rotate the parent node of N to the left, and then convert the red brother to the grandfather of N. We went on to swap the colors of the father and grandfather of N. Although all the paths still have the same number of black nodes, now N has a black brother and a red father, so we can follow up on (2), (3) or (4) situations.

(2) The children of S and s are All black.

In this case, p may be black or red, we simply redraw s to red. The result is through all the paths of S, which are those paths that did not pass through N before, and a black node is missing. Because removing the initial father of N makes all paths through n less than a black node, this makes things all balanced. However, all paths through P now have a black node less than the path not through p. Next, adjust the tree with P as n recursive.

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

In this case we do the right spin on S, so that S's left son becomes the father of S and the new brother of N. We then exchanged the color of S and its new father. All paths still have the same number of black nodes, but now N has a right son is red black brother, so we entered the situation (4). N and its father are unaffected by this transformation.

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

In this case we do the left rotation on the father of N, so that s becomes the father of N and the right son of S. We then exchanged the colors of N's father and S, and made the right son of S black. Subtree is still the same color on its roots, so property 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 to the path through N.

4. Reference materials

(1) "Introduction to Algorithms", second edition

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

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.