Red and black Trees

Source: Internet
Author: User

Red and black Trees

< This article is reproduced from:http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html>

The definition of a red-black tree (RBT): It is either an empty tree or a two-fork search tree with a bit of nature:

1. The node is not red or black.

2. The root node is black.

3. All null junctions are called leaf nodes, and the color is considered black .

4. All red node's child nodes are black.

5. All paths from any node to its leaf node contain the same number of black nodes.

3rd, it is obvious that the leaf node here is not usually what we call the leaf node, marked with nil for the leaf node, why not according to the regular card, because the general leaf node also line, but will make the algorithm more complex; 4th, that the tree is never allowed to exist two consecutive red nodes; The path to the left leaf node of the Red 8 to 1 contains 2 black nodes, and the path to the leaf node under 6 also contains 2 black nodes. All properties 1-5 together constrain the tree's equilibrium performance-that is, the longest path on the tree is unlikely to be greater than the shortest path of twice times. Why? Because the 1th node in the tree is not red or black, because the 4th tree does not allow the existence of two consecutive red nodes, then the longest path from a node to its leaf node must be red and black staggered, then the shortest path must be a pure black node and 5th contains the same number of black nodes on all paths from either node to its leaf node, so that the number of black nodes on the longest path is equal to the number of black nodes on the shortest path! and the 2nd root node is black, the 3rd leaf node is black, then know: the longest path <=2* shortest path. The better the balance of a binary tree, the higher its efficiency! Obviously the balance performance of the red black tree is slightly worse than the AVL, but after a lot of tests proved that the efficiency of the red and black trees is still very good, can reach O (LOGN), I do not know, I can not do a lot of experiments, just listen to people say, O (∩_∩) o haha ~ But at least you know his time complexity must be less than 2O (LOGN)!

Insert operation

Due to the nature of the constraint: The insertion point cannot be a black node, the red node should be inserted. because you insert a black node will destroy the nature of 5, so each insertion point is a red node, but if his father is also red, it is not destroyed by the nature of the 4? Yes, so do some "spin" and some node discoloration! Also for the narrative convenience we will mark the node to be inserted as n (red), the parent node is p, the grandfather node is G, and the Tertiary node is U. The following lists all of the conditions that are encountered when inserting:

Case 1: The tree is empty tree, directly into the root node location, violating the nature of 1, the node color from red to black.

Scenario 2: The parent node of INSERT node n p is black, does not violate any nature and does not require any modification.

< note: Scenario 1 is simple, case 2, p is black, everything is safe, but P is not the same as red, but also the real place to learn! >

Below the------are the various cases where p is red-----

Case 3: N is red, p is red, (the ancestor node must exist, and is black, because red can not appear continuously) U is also red, here whether P is the left child of G, or right child, whether N is P's left child, or right child.

Action: Change p, u to black, g to red, not end.

Analysis: N, p are red, violation of the nature of 4; If p is changed to black, according to the nature of 4, it is obvious that there is a black node missing, violating the nature of 5; So we change the g,u to the opposite color, so that the number of black nodes through the path of G has not changed, that is, 4, 5, but If the parent node of G is red, there is a violation of 4, so, after the above operation is not finished, you need to use G as the starting point, that is, g as an inserted red node to continue to retrieve (the only case that needs to be recycled)----inwhich case, according to that situation ~ Either end in the middle, or you know the root node (at which point the root node turns red, a node is retrieved, and the wood is there, then turn it into black).

Case 4: N is red, p is red, U is black, P is G's left child, N is P's left child (or P is G's right child, N is P's right child; anyway).

Operation: P, G discoloration, p, G transform that is left Zodan (or right-right paddle), end.

Analysis: To know that after the P, G transformation (rotation), the position of the P is the position of the current year, so the red P turns black, and black g into red is to not violate the nature of 5, and maintain the number of black nodes that reach the leaf node is unchanged! It can also be understood as: that is equivalent (just equivalent, not the real thing, just for better understanding;) to move the red N-head to the head of the opposite black U, so that it conforms to the nature of 4 also does not violate the nature of 5, so it ends.

Case 5: N is red, p is red, U is black, P is G's left child, N is P's right child (or P is G's right child, N is P's left child; it is opposite in both directions).

Operation: Need to make two transformations (rotation), the figure shows only one transformation-----first p, n transform, the color is unchanged, then it becomes the case 4, according to Case 4 operation, that is, the end.

Analysis: Because P, n are red, through the transformation, not violate the nature of 5; then it becomes 4, when G and G now the left child color, and transform, the end.

Delete operation

We know that deletes need to find "replacement point" to replace the deletion point, and then remove the replacement point in the original location. (Find the replacement point first, and replace the value of the replacement point with the values of the delete point)

classification of Alternative point n :

Alternate point n has at least one child node null

1. Then, if n is red, then two child nodes must be null ( because of the search for alternative points: The largest point of the left tree or the smallest point of the right tree---are leaf nodes ), then directly to the n is deleted, does not violate any nature, OK, ended;

2. if n is black and the other node m is not NULL, then the other node m must be red, and the sub-nodes of M are null ( in order to guarantee the nature of the red and Black tree ) then n is deleted, M occupies the position of N, and changed to black, does not violate any nature, OK, ended up;

3. if n is black and the other node is null, then n is deleted and the position is null, obviously the black node is removed and the property 5 is destroyed, then the N node is used as the starting point to retrieve the case and make the corresponding operation. It is also necessary to indicate that N is a black point (perhaps null, perhaps not, all the same), p is the parent node, and S is the sibling node (Sibling---Siblings) divided into the following 5 cases :

Case 1: S is red (then the parent node p must be black, the child node must be black), N is the left child of P (or n is the right child of P).

Operation: p, S color, and exchange----equivalent to the right-to-right rotation of the AVL is the P-centered S-left (or the left-hand rotation in AVL), not end (after n is removed, n is treated as null--black).

Parsing: We know that the left side of P is missing a black node, so that the operation is equivalent to the N-head plus a red node----does not violate any nature, but the path through n is still less a black node, you need to do a retrieval of n, and the corresponding operation can be balanced (no matter what you look down).

Case 2: The children of P, s and s are black.

Operation: s changed to red, not closed.
Parsing: The number of black nodes that went through the S-node path after s turns red is also reduced by 1, which is equal to the number of black nodes (NUM) from p to its leaf node to all paths. But this num is 1 less than before because the number of black nodes in the left and right subtree is reduced! Generally, p is a child of his parent node G, then the number of black nodes from G to its leaf node is not equal, so there is no end, it is necessary to start with P as a new starting point to search upward .

Case 3: P is red (s must be black), S children are black (that is, the children of s are all null).

Operation: P should be black, s changed to red, end.

Analysis: This is the simplest case, since the n side of a black node, then the S side to take out a black node to share, so that the S side is not less a black node, and n this side will be more a black node, so restore the balance, how beautiful things ha!

Case 4: P Any color, S is black, n is the left child of P, s right child SR is red, s left child arbitrarily (or n is P's right child, s left child is red, S's right child is random---s with red son ).


Operation: SR (SL) changed to black, p to black, S to p, p, s transform--this is relative to the right and right rotation in AVL (or the left-left rotation in AVL), ending.
Resolution: P, s rotation has a color change, equals to n this side adds a black node, p position (is position instead of p) color unchanged, s This side less a black node; SR has red to black, and s side adds a black node, which restores balance and ends.

Case 5: P Any color, S is black, n is P's left child, S's left child SL is red, S's right child SR is black---NULL (or n is P right child, s right child for Red, s left child for black---s with red Son reverse ).

Operation: SL (or SR) changed to black, s to Red, SL (SR), S transform; At this point the situation 4,SL (SR) becomes the black s,s becomes the Red SR (SL), the operation of the case 4, the two transformations, In fact, it is the right left two rotations of the AVL (or the two rotations of the AVL around).
Analysis: This situation if you operate according to the situation 4, because the SR is black, you can not compensate for the P, s transformation (rotation) to the s side caused by the loss! So I did not first to the S, SL after the transformation into a situation of 4, why not?

Red and black Trees

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.