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-at height not in width
For a lookup, if the height of a binary tree is n, then the search can be done in n steps, which does not have to be explained, which is 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 root 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 the 1th and 2nd sections, 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 life 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.
The balance transformation of 5.2-3 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:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/FE/wKioL1WOxwOAC-dLAAAr9C9bYYQ627.jpg "title=" Rb1.jpg "alt=" Wkiol1woxwoac-dlaaar9c9byyq627.jpg "/>
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 two-pronged node
This is the simplest case, where a fork node becomes a three-pronged node, as shown in the following:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/02/wKiom1WOxVOTymzUAACFywO_xR4614.jpg "title=" Rb2.jpg "alt=" Wkiom1woxvotymzuaacfywo_xr4614.jpg "/>
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:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/02/wKiom1WOxWPghGMyAAGt_5c2nNg174.jpg "title=" Rb3.jpg "alt=" Wkiom1woxwpghgmyaagt_5c2nng174.jpg "/>
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:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/FE/wKioL1WOxzPwE1kCAACrd-FZJpM560.jpg "title=" Rb4.jpg "alt=" Wkiol1woxzpwe1kcaacrd-fzjpm560.jpg "/>
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:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/02/wKiom1WOxYWy5xH0AAHa0oqNNPI015.jpg "title=" Rb5.jpg "alt=" Wkiom1woxywy5xh0aaha0oqnnpi015.jpg "/>
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-black tree
Obviously, by the above description, we seem to have found a solution that keeps the tree balanced, and it's a pretty 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:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/FE/wKioL1WOx1TxJkQKAABuVqDu2Ys804.jpg "title=" Rb6.jpg "alt=" Wkiol1wox1txjkqkaabuvqdu2ys804.jpg "/>
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)