Red and black tree detailed principles of the history of the strongest essence

Source: Internet
Author: User


Without authorization, shall not be reproduced privately, or be held liable

Contact author [email protected] get authorization

Reprint please indicate the author and source



Many of the online red and black trees are not analyzed clearly insert delete all kinds of situations is how to come, most of them the analysis of the picture is very complex, in fact, these conditions are extremely simple, I am here to deduce how the situation is how to do not explain how to adjust the situation, because the structure is easy to adjust, and many online. The essence of the red and black tree is to understand how all kinds of situations are coming, and figuring out this is really mastering the red and black trees.
Five properties: 1, the node is either red, or Black 2, the root node is black 3, NIL (leaf node) is black, this is no egg 4, red node two child nodes are Black 5, from a node to each leaf node on the path of the number of black nodes equal


Red black tree must meet these 5 conditions, the first 3 properties of basic nonsense, adding and deleting nodes, properties 4 and properties 5 may be destroyed, red black tree adjustment, mainly meet these two conditions can be
1 insertingIn order to satisfy the 4 and 52 conditions, the key point is: the inserted node must be a red node, because the number of black nodes from a node to each leaf node is the same as the original, will not change, so that the nature of 5 is satisfied, only to consider the nature of the 4 can be satisfied. Convention: The new insertion node is n, the parent node is p, Uncle node is U, grandfather node is G, the sibling node is s, the left dial hand of the sibling node is SL, and the right child of the sibling node is SRbecause the original red and black tree before inserting the node must be balanced, only need to discuss, Grandpa generation, fathers, sons and so on 3 generation situation, that is, the agreed node situation

We are now only poor to the case of violation of the nature of the 4, this is the need to adjust the situation of red and black trees
P is red to violate 4, insert N before the original tree is balanced (meet 5 properties), G can not be red, otherwise violate 4 so need to adjust the case must have: N=r, G=b, p=r these 3 fixed conditions
The only thing left is you and the descendants of the unknown.
Analysis 1: Before inserting N, G's left dial hand only P is red, then G's right child can not have black node, so you are either red, or does not exist. So when u is red, does it have a child node? The answer is definitely not, that is, you have no child, assuming you want to have a son, must be a sunspot, the front has been said, g of the right subtree can not have black nodes. Conclusion: U either red or not present
Analysis 2:p in the end before inserting n this node, will there be children? The answer is definitely not. P is red, p if the child must be a sunspot, then p before inserting N, one side is empty, the number of sunspots is 0, the other side can not have sunspots, so p again inserted n before also no child


Please state one: u=r, at this time assume that P has another node (there is a newly inserted n), the other node must be black, the problem comes, before inserting N, after P's left and right sub-number of black nodes are not the same, it is inferred that p cannot have another node, then you can not have child nodes, there must be black, The number of black nodes on the left and right sub-tree of G will not wait, conclusion: In a moment, there must be only one child node N,u No child node adjustment: P and N dye black, g dye red, n points to G recursion, until n is the root node, dyed black

Case two: u=b (accurately expressed as U not), N is the left child of P, similar to two, U is nil node conclusion: Situation two o'clock G no right son, p no right son, N is P's left sub-adjustment: P,g color Exchange, g for Axis right-handed, end
Situation three: u=b (accurately expressed as U did not), N is the right child of P, n before inserting through G's right subtree of the black node number 2, then g to P to p right son of the black node number only one, this situation only U is nil node only said the conclusion: the situation three o'clock G no right son, p no left son, N is the right sub-adjustment of P: the P-node is the axis left-handed, and becomes the second


2 Delete

After deleting the node x, you need to select a node in the subtree to replace the position of X, we choose the largest node in the X left subtree to replace, and this node must be at the bottom of the tree (assuming the root node is the top), the replacement of the node dye x color, which is equivalent to, just delete the replacement node, The delete operation moves to the bottom of the tree, and the delete operation is done at the bottom, which is much easier to discuss.
The replacement node is the largest node in the X left subtree, which has a special nature and no right child node
Now the problem becomes: Delete the node at the bottom of a tree without the right child X

If x is red, displace node n is red without adjustment, End if x is red, displace node n is black, equivalent to delete black node at bottom of tree, this case merges to below X for black case if x is black, displace node n is red, n node black, End If X is black, replace node n is black, discussion
Only delete black nodes, need to discuss

In case of need adjustment, the condition is: X is black, X has no right son
Convention Delete node is x,x the parent node is P,x's sibling node is S, S's left dial hand is SL, S's right child is SR
According to the left sub-condition of X to discuss the situation one: X has left dial hand, the left son must be red, because the right child is empty, according to the nature of the red black Tree 5, the left son can not have a black node, so left dial hand can not have sub-node delete x, left child replacement, left child n Red, n dyed black, end
Case two: x no left son, only according to the case of S to discuss 1 s=r,s must have son, and the two sons are black, namely sl=b, Sr=b. Because the largest node in the X-left subtree, x must be the right node of P, the right subtree of P has a black node number of 1, the left subtree each path Black node tree is 1, S is red, only its two sons for Black. SL and SR will not have child nodes, otherwise the entire P's left and right sub-tree is unbalanced
Adjustment: P Right-turn to 2-1 case
2 s=b, S can no longer have black child nodes
2-1 s no node adjustment: s dye red, upward recursion
2-2 SL Red, no SR adjustment: P Right-turn, SL dyed Black
2-3 SL and SR are red adjusted: P Right-turn, SL dyed Black
Situation 2-2,2-3 can be combined into one case
2-4 SL No, SR for Red Adjustment: s left, turns into case 2-2 attached: no nodes with nodes for black unification into a meaning











Red and black tree detailed principles of the history of the strongest essence

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.