Red and Black Tree summary

Source: Internet
Author: User

1. The nature of the red-black tree.

1) The root node is black.

2) All nodes are either red or black.

3) The child of the red node must be black.

4) all with the leaf node end, and the leaf node is Sentinel, are black, there is no actual key value.

5) All Black heights for each node are equal, and the black height is the number of black nodes that start from this node to each sentinel.

2. Insertion of red and black trees.

1) Insert. There is no difference from ordinary binary search trees.

2) Insert repair. (For convenience, the inserted node is usually first set to the red node)

Case 1: If the Father P of the inserted node z is black, then it does not affect any of the properties of the red-black tree.

Case 2: If the Father P of the insertion node z is red, affecting the property 3, then P's father G must be black, so the color of the other child of P's father (that is, the Uncle node of P) has the following conditions.

Case 2.1:u is red, just turn u,p into black, their father G turns red, and then assign G to Z for the next round of repairs.

Case 2.2:u is black, then see whether Z and P are both on the same side (that is, the left child, or all right children), if it is the same side, can rotate around G, and change the color of the p,g can be restored.

Case 2.3:u is black, and then see whether Z and P are both on the same side (that is, the left child, or all right children), if it is the opposite side, can rotate around p, and the rotation of Z to the child (that is, before the rotation of the father), and then into the situation 2.2, to repair.

3. Removal of red and black trees.

1) Delete. Normal delete can, note the record delete point of the child moving whereabouts X, and its deletion point Z of the original color, if it is red, does not affect the red black tree of any nature, otherwise to be deleted repair.

2) Remove the fix. (If the root node turns red during repair, make sure to turn it black eventually)

Case 1: If x is red, you only need to turn black to restore the properties.

Case 2: If x is black, X's brother W is red, then X's father P is black, turns him black, and the brother that rotates around p can convert to X is W is black i.e. case 3.

Case 3: If X is black, the X's brother W is black, if the side of W's child is black, the opposite child is also black, turns w into red, and the father of X and W as the new X for the next round of repair.

Case 4: If x is black, X's brother W is black, if W's side child is black, the side child is red, and rotation around W can be converted into case 5;

Case 5: If X is black, the brother of X is black, and if its same side child of W is red, the father rotates around X and W and the corresponding discoloration can be repaired.

4. Code

Red and Black tree summary

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.