Cases Urealyticum 5 rules for red and black trees

Source: Internet
Author: User

Recently studied the red and black trees, do a simple summary, there is a wrong place please correct me, red and black tree has five rules, this article mainly on these five rules give a picture and text description:

Property 1. The node is red or black .

Nature 2. The root is black.

Nature 3. All the leaves are black.

Explanation: Take [4 5 6] Three nodes as an example, where [5] is placed on the root node, then [4,6] is the root node of the two child nodes, the two child nodes are also marked as black, because they are leaf nodes.


Figure 1

If you are inserting another node at this point [3], it should be intuitively inserted in [4] on the left child, but limited by the red and black tree of the third rule (as above), if inserted in [4] the left child, it must be marked black, but this will be the next fourth rule conflict.

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

Other words ,If [3] is inserted in the left child of [4], the left side of the path from [5] contains 2 black nodes, thus a node more than the right path, so there is a conflict with rule 4, the correct practice 2 is shown, that is, slightly adjust the structure of the tree:


Figure 2

If we were to insert two nodes at this point: [2 1], intuitively, [2] should be inserted in [3] the left child, [1] inserted in [2] the left child, because [1] is a leaf node, so [1] must be marked black, and then according to the rules 4,[2] must be marked as red, But this will conflict with the fifth rule that follows:

Property 5. No two contiguous red nodes can be found on all paths from each leaf to the root.

So the current red and black tree to do some fine-tuning, the correct interpolation method 3 is shown:


Figure 3

So now can also have an intuitive impression, proud of these five rules, red and black trees or relatively balanced (roughly balanced), so its search efficiency is high.

Cases Urealyticum 5 rules for 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.