Data structures-red and black trees

Source: Internet
Author: User
Tags color gamut

Transferred from:http://dongxicheng.org/structure/red-black-tree/

1. Introduction

The red-black tree is a self-balancing binary search tree. It has a better statistical performance than a balanced binary tree (AVL tree), so red and black trees are used in many places. In C + + STL, many parts (currently including set, Multiset, map, Multimap) have applied the variants of red-black trees (there are some changes in the red-black trees in SGI STL, which provide better performance and support for set operations). It is complex, but its operation has a good worst-case run time and is efficient in practice: it can do search, insert, and delete operations in O (log n) time.

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

2. Nature of red and black trees

Red and black trees, as the name implies, through the red and black color gamut to ensure that the height of the tree is approximately balanced. Each of its nodes is a five-tuple: color (color), key (data), left (child), right (child), and P (parent node).

The definition of red and black trees is also its nature, with the following five articles:

Nature 1. node is red or black

Nature 2. The roots are black.

Nature 3. All 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 any node to its leaves contain the same number of black nodes.

These five properties force the key properties of the red-black tree: The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path. Why is it? Property 4 implies that no two contiguous red nodes can be found 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. Also according to the nature of 5 know: all the longest paths have the same number of black nodes, which indicates that no path can be more than any other path twice times longer.

3. Basic operation of red and black trees

Because the red-black tree is also a binary lookup tree, the find operation on the red-black tree is the same as the find operation on the normal binary lookup tree. However, insert operations and deletions on red and black trees can cause no longer conform to the nature of the red-black tree. Restoring the nature of the red-black tree requires a small amount (O (log n)) for color changes (which is actually very fast) and no more than three tree rotations (two times for the insert operation). While insertions and deletions are complex, the operation time can still be maintained at 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) Assign the color of the new node to red

(3) The tree is re-adjusted from the bottom to the red and black trees

where step (1) is found in the same way as the normal binary search tree, step (2) assigns the color of the newly inserted node red, because: if set to Black, it will lead to the path of the root to the leaf, there is an additional black node, this is difficult to adjust. However, when the red node is set, it can cause two consecutive red nodes to conflict, so it is much easier to adjust by color flips and tree rotation. Some details of step (3) are discussed below:

The node to be inserted is n, the 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 to be a red-black tree.

[2] If P is red (known that its parent node G must be black), after inserting z, violates the nature of 4, need to adjust. Adjust the following 3 kinds of conditions:

(a) n's Uncle U is red

As shown, we redraw p and u as black and redraw the node G as red (to keep the property 5). Now that the new node n has a black parent node p, because any path through parent node p or Uncle node u must pass through grandfather node G, the number of black nodes on those paths has not changed. 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 recursively adjust the color on the grandfather node G.

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

As shown, we have a left rotation of the new node and its parent node to the P role; Next, the previous parent node p is processed according to the case (c) to resolve the still defunct property 4.

(c) Uncle U of N is black, and N is the left child

As shown, a right rotation of the grandfather node G; In a rotation-generated tree, the previous parent node p is now the parent node of the new node n and the previous grandparent node g, then the color of the previous parent node p and the grandfather node G is exchanged, and the result of the tree satisfies the nature of 4, while the nature 5[4] also remains satisfied.

3.2 Delete operations

The delete operation can be summarized in the following steps:

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

(2) Replace the node with the deletion node successor or node (only the data can be replaced, do not have to adjust the pointer, the successor is the middle sequence traversal in the node next to the node, namely: Right child's left child node)

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

where step (1) is found in the same way as the normal binary search tree, step (2) Replaces the deletion node with the successor node, because this ensures that the successor node is still a red-black tree, and that the subsequent node may be a leaf node or a node with only the right subtree, This allows the deletion to be achieved simply by replacing the successor node with a node. If the node that needs to be deleted has two sons, then the problem can be converted to delete 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 cases:

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

Since n is black, p may be black or red, s may be black or red.

(1) S is red

The p must be red at this point. We rotate the parent of N to the left and then convert the red brother to N's grandfather. We then swapped the colors of N's father and grandfather. 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 proceed with the case (2), (3) or (4).

(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 all paths through s, which are those paths that previously did not pass N, with a black node missing. Because deleting N's initial father makes all the paths through n less a black node, this makes things all balanced up. However, all paths through P now have a black node that is less than the path that does not pass the p. Next, you adjust the tree with p as the n recursion.

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

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

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

In this case we do a left spin on the father of N, so that s becomes the father of N and the parent of S's right son. We then swapped N's father and s for the color, and made s the right son black. The subtree is still 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 path of N.

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 on data structures and algorithms, see: Data Structures and Algorithms summary

———————————————————————————————-

original articles, reproduced please specify: reproduced from Dong's blog

This article link address: http://dongxicheng.org/structure/red-black-tree/

Dong, author Introduction: http://dongxicheng.org/about/

Collection of articles for this blog:http://dongxicheng.org/recommend/

Data structures-red and black trees

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.