A brief summary of red and black trees

Source: Internet
Author: User

A red-black tree is a two-prong search tree with color attributes for each node, with a color of red or black. In addition to the general requirements of the binary search tree, we have added the following additional requirements for any valid red-black tree:

Nature 1. The nodes are red or black.

Nature 2. The root is black.

Nature 3. All leaves are black (leaves are nil nodes).

Nature 4. Each red node must have two black child nodes. (no two contiguous red nodes can be found on all paths from each leaf to the root.) )

Nature 5. All simple paths from any node to each of its leaves contain the same number of black nodes.

Insert Rotation

To insert the new node is the n,n of the parent node is p, the grandfather node is G, the Uncle node is U, the insertion of n is red, when P is black, there is no problem, the reason for inserting rotation is red conflict, that is, N and P are red, the key to rotation is the color of U and N in the Zuozi also right subtree

Case (1), u= Red, order (p,u) = (black, black), g= red, end, at this time U in the left subtree or right subtree does not matter;

Case (2), u= Black, n in left subtree, right rotation can, p= black, g= red; end

Case (3), u= Black, n in right subtree, left rotation on p subtree, changed to case (2);

The insert operation rotates up to two times, in the second case;

In summary, insert rotation key in the color of the Uncle node, and insert the left and right positions of the node;

Delete rotation

If the node that needs to be removed has two sons, then the problem can be converted to delete another node with only one son, when the node to be deleted and the child node has a red, easy to solve; the difficulty is to delete and the son node is black, after deletion with the child node instead, this lesson subtree height minus one, After the substitution of the node is N, the parent node is p, the sibling node is s,s the left and right child nodes for SL and SR, at this time known as H (N) =h-1,h (S) =h, that is, the Zuozi height of p is less than the lower one, n must be black, n is the current node, Because the left and right black height imbalance of p requires rotation according to the following conditions:

Case (1) when (P, S, SL,SR) = (black, red, *,*), then, due to the intrinsic nature of the red-black tree, the latter two * must also be black. If the current tree is left-handed, the p subtree is still unbalanced, but the problem turns to the case (3) (4) (5), or n as the current node;

Case (2) when (p, S, SL,SR) = (black, black, black, black), make s= red, p subtree balanced, but height minus one, with P for current node upward recursion;

Case (3) when (P, S, SL,SR) = (red, black, black, black), make p= black, s= red; balance ends;

Note: The remaining situation (black, black, red, black), (black, black, black, red)/ (red, black, red, black), (red, black, black, red) boils down to (*, black, red, black) and (* , black, black, red);

Case (4) when (P, S, SL,SR) = (*, black, red, black), performs a right rotation on the S subtree, turns to the case (5), does not draw N and P;

Case (5) when (P, S, SL,SR) = (*, black, black, red), perform left rotation, complete

The delete operation rotates up to three times, and the rotation sequence is (1) (4) (5);

The removal of the red-black tree rotates to this end, and the removal of the rotation key is seen (P,S,SL,SR) in various combinations;

Reference: http://zh.wikipedia.org/wiki/red and black tree

A brief summary of 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.