Red and black trees--in accordance with God

Source: Internet
Author: User

The red-black tree is Another variant of the AVL tree, and he can also maintain a sense of balance in the process of dynamic change, and the worst case scenario for a red-black tree is only the complexity of $o\left (\log n \right) $ , and below we will see that For insertion we have another implementation method that is easier than the AVL tree, not recursive. before we talk about technical details , we may have questions about how to develop new variants if you already have a low-complexity structure with AVL, and you can maintain a balance that doesn't allow the tree to break above the sky. What is the power of the red and black trees in the entire balance tree system in what position? First look at the basic information of today's protagonist.

Review previous structures, whether linear vectors, lists, stacks or queues, as well as semi-linear tree structures and non-linear graphs. Most of them present this characteristic: each time a dynamic operation, the logical structure changes, and then into a new state, the previous state is completely lost, this kind of structure is also called ephemeral data structure. The state of every fixed moment is fleeting. But this is not enough in a real-world scenario, and many times we may want to look at its historical version and modify it. The previous study is not enough to solve the problem, so to design a new structure, this is its motivation. Therefore, in addition to the value of the element, both static and dynamic, you need to specify a version number at the same time. If a data structure can support this type of requirement, it is called the persistent structures, or the so-called consistency structure, or a persistent structure (here is a read-out, Chen Lijie wrote a paper called "Research on the data structure of sustainable Interested in this area can be studied). At first glance, it's good to realize this idea: Copy a copy every time you do a dynamic operation, and each version is saved independently. But is this simplicity (like the old Man in the village) going to work? The following is a specific balanced binary tree that records 5 versions of its entire life cycle:

in this way, once a version number is specified, it can be transferred to the corresponding State, which is acceptable from a single access efficiency. If we record the number of different versions as h time to determine the state time to perform operation nh This is certainly not. Can you make some improvements? Control in $o\left (n\; +\; h\; \cdot \; \log n \right) $ : In addition to the space occupied by all elements, Be able to control the amount of space consumed by each version within the range of $\log n$

The answer is yes.

For this we need to take advantage of the correlation between adjacent versions of the same data structure. For each successive set of versions, the new version is slightly modified on the old version:

Two versions most of the elements are the same, and the difference is small. Using this feature we naturally think of a way: a lot of sharing, a small amount of updates (similar to the prefix and the interval plus, most of the same, do a few changes). As long as it is done properly, the complexity of the dynamic operation can be controlled in $O \left (h\; +\; n \right) $. For example, for a specific tree, the following is a feasible method of implementation.

in this picture each red line corresponds to a share, such as 1-2 this 10 and 1 in 8-8 ' Blue Line is said: Although 8 8 ' correspond to the same data object, but their parent node has changed in the first and last two versions. So in the new version we have to save a copy of it. sounds a little bit around, indeed, this high-level structure is already involved in computational geometry. In addition $\log n$ in fact there is room for improvement , if you can control the space difference between the front and back versions in $o\left (1 \right) \; $, the overall space complexity will be further optimized to $n\ ; +\; h$ instead of $h\; \cdot \; \log n$. This can be done as well.

The details are not mentioned first, but a necessary condition for this is very well understood: in the case of Bbst 's tree topology, the differences between adjacent versions cannot exceed constants. Unfortunately, the vast majority of Bbst, including the AVL tree previously learned, cannot guarantee this. Topological differences are caused by rotation operations during the self-tuning process, and each local rotation implies the introduction of differences on the structure. Conversely, if it is necessary to ensure that the differences in the topological structure of the previous and later versions do not exceed the constants, it must be ensured thatthe rotation operation between the 2 versions should not exceed a fixed value, and this number cannot be changed by the input amount.

Does the structure that satisfies this optimization condition have been learned in the knowledge? To find out, the two dynamic operations of the AVL tree are satisfied with the insertion. After each insertion, the whole tree is restored once rotated. But the deletion does not satisfy this, after removing a node from the AVL tree, it is possible to raise up to $\log n$ rotation from bottom to top, leading to drastic changes in the tree topology. So in order to make the above idea can be fulfilled, we need such a bbst, any of its dynamic operations on the tree topological structure of the impact, can be controlled within the range of constants. After inventing this variant, we named it "Red-black tree" according to the characteristics.

Periodic summary: Red black tree compared to the AVL tree is characterized by the change in the topology after each insert/delete is not more than $o\left (1 \right) \; $

Now let's analyze the structure of the rbtree.

The addition of the external node null, easy to consider, is equivalent to the initial situation of Left=right=null , when these external nodes are leaves. For this tree there are four rules defining uniform constraints:

    1. The roots are black, and the leaves (even if they are null) are black.
    2. In addition to root and leaf, each node or red or black
    3. If a node is red, the child node must be black
    4. The same number of black nodes must be included on the path from one node to the leaf

To pick a few instructions, the article 3, although concise, but in fact contains a lot of life experience , such as this requires the impossible to appear at the same time as the red father and son two generations, in exchange for an operable expression is: for the red node, Whether it is the child or the father must be black, it can be red and black or All Black (it only bound red), not all red. We will see later that this rule is extremely important for controlling the depth of the red and black trees. Article 4 , which may be considered to be designed to control the balance of the red and black trees. Because this is required, the number of black nodes must be equal in the only path from any external node to the root of the tree, and that must be balanced, not long and short on one side.

So what kind of tree does this pile of rules correspond to? And what are the principles behind these rules? Let's look at an example:

The root is black, the leaves seem to be not, but in fact we have increased the external node before, this time the external node unified Black can be. These are added to facilitate analysis and implementation, which we can ignore in the example. The third is also satisfied, each red node of the father and son are black. The 4th is also satisfied, look for two path to count a bit just fine. Now there are some intuitive feelings, but, back to look at the four rules still feel obscures (e. G Two red nodes can not be next to each other , I two paths on the number of black nodes can be good? )。 Can you just do it a little bit more?

The stone of his mountain, can attack jade, we need to use another structure to help understand. Let's take a look at the following example, in order to better understand this set of rules, we have to do an equivalent transformation: Ascension. Look first:

We put each red node up a layer and let him be taller with the black Father (note that some black nodes may accept two red nodes). It can be proved that it is absolutely impossible to have two red nodes adjacent to the situation, impossible. Even on one line, there must be a black node between them. Let's do it now-super transform form

That's it. Take another look at a larger example, so that we can grasp the nature of the red and black trees from the macroscopic.

This is a 100-node tree, the bottom nodes are red, and rugged (this is very bad for the control tree). What about after the transformation?

to play a different game: all the bottom nodes are the same height, right! Is this a coincidence?

Although the red and black tree rules obscures, convoluted, but from the point of view of Ascension transformation, will become enlightened, like a scroll. Actually from this point of view, the red and black tree is the 4 -order B - tree, (2 ,4) tree . Indeed, in order to understand this, it is only necessary to combine each black node with a red node that is highly consistent with it after Ascension, as a whole as an internal node of a B-tree. In this way, it concludes that there are only four combinations, each of which corresponds to A class of internal nodes of the 4- order B-Tree:

Each internal node has at least 2 branches, with a maximum of 4 branches, which is the characteristic of the 4-order B-tree.

And since he is also a B-tree, nature is also a balance tree, let us prove it, it is very necessary, because it will deepen the understanding of him. What we need to prove is that any red black tree made up of n internal nodes, whose height is not more than Logn in the asymptotic sense, is , of course, the same as all BST -- having n An internal node of the red-black tree, must also have n+1 external nodes. Simple point is to prove $\log _{2}\left (n\; +\; 1 \right) \; \leq \; h\; \leq \; 2\; \cdot \; \log _{2}\left (n\; +\; 1 \right) $ This inequality is established . The left side of the B-tree has been proved , and any BST is satisfied, so now only the right side of the good.

For example, in this red-black tree, if you ignore the human-induced external nodes, then each of these paths contains a black node is 6 . According to the rule, the total number of black nodes on any path should be 6, at which point we call the black height of the red black tree 6. Despite the addition of 3 red nodes, the height can be up to 9. The so-called tree height is the longest node in the whole tree corresponding to the length of the path, and in the red and black tree Any path, may appear adjacent black nodes, but not allowed to appear adjacent red nodes. This means that on each such path, the red node $\leq \; \frac{1}{2}$, while the black node $\geq \; \frac{1}{2}$. so the height of the red-black tree must not exceed 2 times its black height .

So how do you calculate the black height here ? It is the Ascension transformation . we know that after the Ascension transformation , the red- black tree corresponds to the 4-order B- tree

In such a B- tree all the external nodes are like the blue Circle , neatly distributed on the ground floor . each of these internal nodes is made up of one of the previous black fathers and his red children , so each path in the red-black tree

also corresponds to a path in the B -tree. How many black nodes there were on this path , and how long the corresponding path is in the B -tree-which means that the height of the B- tree should be exactly the black height of the previous red-black tree . That is: the height of theB -Tree is the black height of its corresponding red-black tree . In order to calculate the black height of a red black tree, it is only necessary to calculate the corresponding B- tree height. that's $h\. ; \leq \; \log _{2}\frac{n+1}{2}\; +\; 1\; \leq \; \log _{2}\left (n\; +\; 1 \right) $

Next Write implementation

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.