The RB tree of the balanced binary tree

Source: Internet
Author: User

RB Tree (red-black tree) does not seek "complete balance"-it only requires partial balance requirements, reducing the need for rotation, thus improving performance. Due to its design, any imbalance will be resolved within three rotations . A typical use is to implement associative arrays (such as map and set in C + +)

Only to satisfy the nature of the tree, we call it a red-black tree:

1) Each node is either red or black.

2) The root node is black.

3) Each leaf node, that is, the empty node is black.

4) If a junction is red, then both of its sons are black.

5) For each node, the same number of black nodes are included on all paths from the node to its descendants node.

The steps of the RB tree to insert a node: We use the Japanese samurai class to make metaphors. The newly inserted node is red. insert operation, as if a new samurai, no background, no exploits. But he is capable and ambitious, just like Toyotomi Hideyoshi.

After inserting a new node according to the rules of the binary search tree, if the parent node of the new node is black, it is inserted directly (with a black type of the Lord, although eventually will not be reconciled to the people below, but there is no condition " down"). Otherwise, because the Red node's son node must be a black node, so to make adjustments, there are 4 kinds of situations (in this make some symbolic convention, the new node is X, its parent node is P, grandfather node is G, Uncle node is S, grandfather node is GG):

1) S is black, and X is inserted on the outer side. P,g do a single rotation and change the color of the p,g. (similar to the single rotation of the outer side of the AVL tree)

If the other minister of the great Lord is the drab assortment mediocrities, master and slave two rebel success! success, the original great Lord became the Home minister, humiliation, hardships, waiting for the time to rise again, and the Lord this one, the upper, family tradition fell.

2) S is black, and x is inserted inside. X,p do a single rotation and change the color of X and P, and then do a single spin on the X,g. (similar to inserting double rotations inside the AVL tree)

The greater ambition is to report the Lord's treason to the great Lord. The Lord's loyalty is limited, there are many handles. So the new Samurai report active, the Lord has become his own family minister. The foolish Lord will soon pay the price and become the next target of the new samurai.

3) S is red, and X is inserted on the outer side. P,g do a single rotation and change the color of x. At this point if GG is black, everything is done, otherwise, you have to continue to do it until there is no longer a continuous red parent-child node. (similar to the single rotation of the outer side of the AVL tree)

Other courtiers have the ability, but the Lord is more powerful and successful in the upper. Because of the strong Lord, the new samurai have been mediocre and degenerate.

4)S is red, and X is inserted inside. Change the color of the p,s,g directly. At this point if GG is black, everything is done, otherwise, you have to continue to do it until there is no longer a continuous red parent-child node.

Other family ministers have the ability, the master ability mediocre, uncontrollable, eventually lead to chaos, after a bloody, the big master public appeared a Laureate Wei, the home of the civil strife, the original two home minister of the strong home is all destroyed, home owners replaced by loyal and reliable people. And the Big Master's new home, ambition began to swell.

In order to avoid the above situation 3, 4 GG also red, we designed a "top down" red black tree. Assuming that the new node is a, then the path from the root node to a , as long as you see a node x two child nodes are red, the two sub-nodes are changed to black, while the X is changed to red. But if the parent of X is also red (note that at this point the Uncle node of X is not likely to be red), make a single rotation to change the color as described in case 1, or do some double rotation and then change the color as in the case 2.

The RB tree of the balanced binary tree

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.