# Really understand the red and black trees, the real (large amount of data structure used in the Linux kernel, and often asked by two goods)

Source: Internet
Author: User

As a data structure, red and black trees are not simple, because all kinds of propaganda let it too mysterious, online collected a lot of articles about the red and black trees, nothing more than the same, introduce concepts, analysis performance, paste code, and then give a sentence of sin, it is the worst case how how to ...

We think that a binary tree is the worst case, that is, it degenerates into a list, so the lookup is a traversal. The question is, how does a balanced binary tree return the linked list! How does it keep the balance? Can you explain it in a nutshell? Of course! The general information about the red and black trees is directly given the same black nodes, red nodes are not contiguous as a strong enough but not too hard to ensure the balance of the tree, but in fact, it has a simpler way of understanding.

1. Find-in height not width for the lookup, if a binary tree height is n, then up to n steps can be done to find, this does not explain, explain this a little distracting. This means that the height of the tree should be as short as possible. The distance from the leaf node to the root node cannot vary significantly, given the average of the search.

2. The imbalance of the two fork tree a tree looks unbalanced in its search because the height of the subtree varies greatly.
Why is the binary tree so easy to become unbalanced, very simple, because it only two fork, about 50% probability, then insert n nodes are all left node or right node probability is 50% of the N-square, if it is 8 fork tree, then this probability is 12.5% of the N-square, which probability of large, their own calculation.

3. Multi-fork Tree-width change height in sections 1th and 2nd, we already know that the larger the width of the tree, the smaller the height, so that the faster the query, the Cisco router is not 256 or even 1024 fork tree? But is it really good? For sparse nodes, this can seriously consume memory.
If we consider the CPU's MMU system, we will know that the difference between a Level two page table and a three-page table is that the effect of dealing with sparse address space is different.

4. Weigh -2,3 trees We found that daosh, a lifetime of two, a binary tree is a perfect start, but we find it particularly easy to tilt, do not touch when tilted. We can not be on the 256-fork tree, even if that in the case of large nodes can not resist, so the blind width-to-height scheme is not extensible. We need to find a dynamic mechanism to keep a tree dynamically tuned to balance.
In order to more easily find this mechanism, so that it is more easily visible, temporarily increasing the width of the tree, if the increase to 3 fork tree can not find a plan, increase to 4 fork tree ... The N-fork tree We're talking about doesn't mean that a node must have n child nodes, but that it has a maximum of n child nodes.
So far, I have been my own metaphysical view, a few years ago, my idea ended, because that period of time particularly depressed, I want to find out some technical metaphysical thinking, but suddenly self-change, did not continue. Fortunately, I now find that there is a solution, and that the red and black trees are retreating from the 3 fork trees.
To my delight, my train of thought was not biased.
5.2-3 the balance transformation of a tree if it is a two fork tree, then you insert a node, you only have a maximum of 1 times to keep the height of the subtree unchanged, if it is a three-pronged tree, then there are 2 opportunities. Now, we've added a fork to the two-fork tree and turned it into a three-pronged tree.

In a binary tree, there are three branches when a node has two branches, and a three-pronged tree. A point can be divided into two parts of the area, in order to divide an interval into three parts, you need two points, so in the case of Three forks, the node stores two points instead of one, as shown in:

Now consider inserting a new node, how the 2-3 tree remains balanced. Very simple, we know that the insertion position must be a leaf, assuming that the current tree is balanced, now in two cases:
1). The parent node of the new leaf node that is inserted is a binary node The simplest case is that the fork node becomes a three-pronged node, as shown in:

2). The parent node of the new leaf node that is inserted is a three-pronged node this situation is more complex. The tree is always tall, and the way to keep it balanced is to grow taller at the same time, which is not possible, and inserting a node can only make the subtree of that node grow taller. However, if this information can be raised to the root, at the root height, it will achieve the "long-height"!
Or follow the above idea, we continue to increase the number of tree forks, we increase it to 4! Insert the new node as shown:

Unfortunately, we did not complete the task, but in the end we raised two questions, so long as the two problems were solved, all the problems were solved.

To solve these two problems, it is undoubtedly involved in node P's parent node and then up to the node, there are two possibilities:
possibility 1:p parent node PP is a two-fork node
This is too cool, we directly refer to P and its subtree all to the PP node, similar to the B insert scenario, as shown in:

Issue 2 resolves.

possibility 2:p parent node PP is a three-pronged node
This is a bit difficult to do, but there is a final blow! Anyway, the P node and its child nodes are all mentioned PP, maintain the bottom of the balance, so that you can solve the recursive, at this time, we once again encountered a three-fork node inside the insertion of sub-node problem, in order not to increase the height of the tree, the only way is to expand into a four-fork node-width change height. As shown in the following:

Finally, we find that, in the process of recursion, we have either encountered p. P is a binary node, at this point in accordance with the resolution of problem 2, the current node value directly refers to p ... P, its subtree lowers a height, offsets the increased height, balances hold, recursion ends, or recursively to the root node, where only a split operation is needed to perfect the end!

6. Evolution to the red and black trees Obviously, through the above description, we seem to have found a way to keep the tree balanced, and it is a fairly perfect balance! The core is the game between width and height. We can always use a width to offset a layer of height, the whole process is one or more times a plus minus, the final result is 0!
However, this is no longer a two-fork tree, some nodes become three forks, and save two values, the two values divide the interval into three parts, is for three forks! Therefore, in use is not as convenient as the binary tree, the comparison operation is complicated. In fact, the three-pronged node is processed into a binary node, and this tree becomes a red-black tree! How to deal with it? Very simple! As shown in the following:

See, the Red node is divided from 2-3 trees, in order to maintain a binary tree instead of 2-3 trees, the three fork node must be turned into a two-fork node, which is a width change the height of the fallback, that is, the height of the width, of course, the price is no longer perfect balance.

In accordance with the above transformation, you can try it out, could you change the two consecutive red nodes? No! Still tangled in the nature of the red-black tree? Looking at its evolution, you will find that many of the complex concepts of red and black trees and the performance of people without a clue are natural. Let's take a look at what the worst case scenario is.
or 2-3 tree analysis, if in a tree 2-3 tree, the leftmost path node is all three-pronged node, and the most right-hand path node is a two-fork node, then transform it into a binary red black tree, you will find the leftmost path is the red-black interval node, and the rightmost path is all black nodes, Their height difference is close to twice times. It is sad to have such a situation, but it is also very low probability.
All of the red and black tree operations, including rotation, can be mapped to 2-3 trees, and we have enough understanding of the game between 2-3 trees and the height and width. Please understand the red and black tree again, and then look at its nature and concept, together with left and right rotation, is there a new experience?

Really understand the red and black trees, the real (large amount of data structure used in the Linux kernel, and often asked by two goods)

Related Keywords:

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.