Red and black trees-Introduction to Algorithms (15)

Source: Internet
Author: User

1. What is a red-black tree

(1) Introduction

In the previous article, we introduced a two-fork search tree with a basic dynamic set operating time complexity of O (h). Unfortunately, these collection operations are faster when the binary search tree height is low, which means that when the height of the tree is high (or even if the tree becomes a 1 chain), the collection operations are not faster than executing on the linked list.

So we need to build a "balanced" two-fork search tree.

The red and Black trees (red-black tree) is one of them. It ensures that in the worst case, the time complexity of the basic set operation is O (LGN).

(2) Nature

Unlike the normal binary search tree, the red-black tree adds a storage bit to each node to represent the color of the node (only one of black or red), so a node now contains 5 attributes: Color,key,left,right and P. By constraining the colors of each node, you can guarantee that any simple path from the root to the leaf will not be twice times longer than the other path (which guarantees "balance").

This constraint ( nature ) is:

① root node and leaf node are black;

The sub-nodes of the ② red knot must be black;

③ the simple path of any node to all of its descendants ' leaf nodes contains the same number of black nodes, and the number of black nodes (not including the departure nodes) is black height (black-height, denoted by BH (x), the black height of the red black tree is the black height of the root node).

is a red and black tree:

Perhaps you would be surprised that the red and black trees above did not meet the leaf knot must be black this nature ah. In fact, it is satisfied, because the painting is actually the internal node of the tree, we are in the real processing, will be in the leaf node of the left and right children point to a value of nil, the color of the black node (external nodes ), that is, the real leaf node is the black value of nil node, This satisfies the nature of the red-black tree. As shown in the following:

However, if the above method is used to deal with, virtually adding so many "useless" nodes, it is bound to waste a lot of storage space. In fact, we can combine these nil nodes into one, just like we did:

But for the sake of convenience, our subsequent discussion ignores the value of nil.

(3) Why red and black trees are a good search tree

Because

The height of a red black tree with n nodes is at most 2LG (n+1).

We can first prove by mathematical induction: the subtree with either node X is at least 2BH (x)-1 internal nodes. Then according to the constraint ③ can draw the above conclusion (concrete proof slightly).

As a result, dynamic collection operations search, Minimum, Maximum, successor, and predecessor can be done in O (LGN) time in the red-black tree.

Since the red-black tree is actually a "balanced" two-fork search tree, we only need to study its insert and delete operations, and the other operations are consistent with the two-fork search tree.

2. Rotate

Before we investigate the insert operation, let's first describe the rotation operation.

When we insert and delete operations on a red-black tree, we violate the above constraints because we modify the structure of the tree. At this point, you need to modify the color and pointer structure of some nodes in the tree to maintain the nature of the red and black trees.

The modification of the pointer structure is done by rotation . There are two kinds of rotation operations: left-and right-handed.

The pseudo-code for the left-hand operation is given below:

It can be seen that the left-hand (right-handed) operation can be done in O (1) time.

is a practical example of a left-handed operation:

3. Insert

(1) algorithm

We can insert an element into the tree by using a binary search tree-like method. It can be completed within O (LGN) time. The insertion algorithm is described as follows:

It can be seen that by default the newly inserted node is inserted in red (after the reason given). The most distinct of the insertion algorithms with the two-fork search tree is that in the final step we call the Rb-insert-fixup method to maintain the nature of the red-black tree. Here's a description of it:

is an example:

(2) Analysis

The above process may be a bit complicated, let's take a closer look at two ways to get started.

First, we should make it clear that the Rb-insert-fixup method is to maintain the nature of the red and black trees, so we need to figure out which properties the insertion of a node (red) will break.

Secondly, we need to analyze exactly what the above three cases are doing and what the impact is .

① which properties will be destroyed

It is clear that only the nature of the ①-- root node must be black (when and only if the tree is empty when inserted) and the nature ②-- the red knot will be black for the child to be destroyed.

② three kinds of cases

First, the primary premise of the loop is that the parent node of z is red. Then, we analyzed three cases based on the parent node of Z is the left child basis (the opposite is similar, do not do analysis).

It is also easy to see that the Z-node is always red before each iteration, and the Y-node is the "uncle" of the Z-node (y = Z.p.p.right, which is called the Tertiary node with y as Z).

Case 1: Z's tertiary node Y is red (it also shows that the "Grandpa" node of z is black). What to do : the "grandfather" of Z knot is red, and the "Grandpa" knot points are black; Z rises 2 levels, pointing to its "Grandpa" knot. What's the impact : The above operation has no effect on the black height of any simple path, and the operation does not make the situation "improved", but only makes Z rise by 2 levels.

Case 2: Z's tertiary node y is black and z is the right child. What to do : Z points to its parent node, and a left-hand operation on the z-node. What 's the impact: the above operation has no effect on the black height of any simple path, except that case 2 is changed to Case 3.

Case 3: Z's tertiary node y is black and z is the left child. What to do : The parent node of z is set to Black, the "Grandpa" node of z is red, and the "Grandpa" node of Z is right-handed.

What is the effect : After two changes to the color, it causes the black height plus 1 of all paths from the root node to the left, and the black height of all paths to the right, while the right-hand operation causes the black height to return to equilibrium.

(3) Proof

Let us then analyze (prove) the above process using the cyclic invariant (the basic algorithm (1) of the Loop invariant):

This immutable formula is:

1) The junction z is Red;

2) A maximum of only 1 red-black properties are broken, either the nature of the ①--root knot is broken for black, or the child of the nature ②--Red knot must be black to be broken.

Initialize :

Since we set the newly inserted node to red by default, it is obviously true that the node Z is red initially.

Before the iteration, if there is only one node, that is, only the root node, the nature of the ① is broken; otherwise, z and Z.P are red, the nature ② is broken;

In summary, the invariant is established at the initial time.

Keep:

From the above analysis of three cases we can see: node Z is always red, the three operations did not change the black height on any path, that is, the nature of ③ is always satisfied. It is clear that after each case 1, the nature of the ① or nature of the ② is broken. Complete Case 2 as well. When case 3 is completed, the loop terminates (because in case 3 we put Z.P in black), this time satisfies the red-black nature.

Invariant is always true.

Terminate:

The condition for iteration termination is: Z.P is black. By means of a retention analysis, we see that termination can only occur after you have completed cases 1 and 3. And after the execution of case 1 Z is red, Z.P is black, so the nature of ② cannot be broken, then it is only possible that the nature of ① is broken; After case 3 has been fulfilled, all the red-black properties have been satisfied, that is, a legitimate red-black tree.

From the above analysis, we can draw: at the end of the cycle, either the binary tree is already a legitimate red-black tree, or only the nature of the ①--root node for black is broken.

So at the end of the cycle, we only need to do one time to set the root node to black, then fixed the legitimacy of the red black tree.

(4) Description

Since the height of a red black tree with n nodes is O (LG N), the first 16 rows of the Rb-insert need O (LG N) time, and in Rb-insert-fixup only when Case 1 occurs, the while loop executes; 1, the pointer z will rise 2 layers. Thus the while loop performs at most LG N times, so the rb-insert-fixup time complexity is O (LG N), so the time complexity of the entire insert operation is O (LG N).

4. Delete

(1) algorithm

As with the binary search tree, we first give the transplant method, which replaces the subtree with the root node of U as the subtree of the root node for V:

The following is a description of the algorithm for the delete operation:

As you can see, the above delete operation is not much different from the normal binary search tree. The biggest difference is that the above operation in 22 line plus a maintenance of the red and black nature of the process, rb-delete_finxup, the process of the operation is as follows:

(2) Analysis

Rb-delete also divided into three different cases, of which the third case was divided into two small species, respectively, corresponding to the situation as shown:

We also want to analyze what is going on in various situations and what the impact (in terms of the nature of the red and black) is.

Can be found in fact is the ordinary binary search tree when the classification of the deletion, in fact, the above code completely contains the ordinary binary search tree delete operation code, but on the basis of the maintenance of the red and black character code. So what we do with these duplicated code is no longer analyzed.

We will record the color of the deleted node z before each deletion, because it determines whether we need to fix the red-black character (if z is red, its parent and child will be black, the z will not violate the nature of ③, and the child with Z would not be affected by the nature ②), Y points to the right child of z,x pointing to Z; for the latter two cases, we record the color of the least node y of the key in the right subtree of the deleted node, and similarly, if it is red, no matter what color Z is, unify the color of Y to Z, and replace Z with the way it is shown. Have no effect on the nature of the red and black. In combination with the above analysis, we found that the "output" (the processed result) of four cases is consistent: if the recorded color is red, it indicates that the red and black properties have not changed, and if it is black, the red and black nature must be broken. Specifically, if node y is black, for example, there will be 3 effects:

1) If y turns out to be a root node, and a red child of Y becomes a new root node, it will violate the nature of ①. As in the left side of ①.

2) If the parent node of x and X is red, it will violate the nature ②. such as the left side of the ③, X is the case of red.

3) All cases (except that Y is the root node) will result in a 1 less black knot point on the simple path that contains the Y node, and will violate the nature ③. The way to fix this is to "paint a layer of black" on the X-node that now occupies the Y-node position, and of course, the "coloring" operation is not reflected in the code, that is, we do not modify the color property of x, we just "remember" in the mind, when appropriate, the black layer of x will be applied to a red The X-nodes "coated with two layers" may be double-black or red-black, each of which "contributes" 2 or 1 black knot points.

Let us then analyze the Rb-delete-fixup correction process:

The correction process is divided into 4 cases, we first give each case corresponds to:

Then analyze each case (based on the X is the left child):

Case 1: X's right brother W is red, indicating that the parent node of x must be black. The action is: Swap W and its parent node color, that is, to change W to Black, its parent node transposition red, and then to the parent node left, W re-point to the right brother of X (the node is originally W's left child, so must be black). This is case 1 over to case 2.

Case 2: W's children are All Black (W is also black). You do this by changing W to red and X to its parent node.

Case 3: The left child of W is red and the right child is black (W is also black). The operation is: Swap W and its left child's color, that is, the W transposition Red, the left child to change to black, and then to w right-hand, W re-point to the right brother X.

Case 4: W's right Child is black (w is black). W swap color with the parent node of X, and the right child of W is black, the parent node of X is left-handed, and X points directly to the root node, and the loop ends.

To finish the while loop, one more step is to place the root node in black. This will ensure that the nature of the ① is satisfied.

It can be proved that all the red and black properties have been fulfilled by the above operation. The proof procedure is similar to that of the insert operation, omitted here.

It is not difficult for us to analyze the time complexity of Rb-delete is O (LG N).

5. Summary

Like the above, we can construct a red-black tree. It ensures that the time complexity of the basic set operation is O (LG N).

First record here, and then give the common binary search tree and red black tree Java implementation code.

Red and black trees-Introduction to Algorithms (15)

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.