The principle and realization of inserting the red and black tree

Source: Internet
Author: User

    • the five properties of red and black trees (nature four and nature five are of particular importance)

1. The node must be red or black

2. The root node is black

3. All the leaf nodes are black.

4. The two child nodes of each red node are black, that is, no parent or child two nodes are all red.

5. The number of black nodes on all simple paths from any node to each of its leaf nodes is the same.

    Note the following two points:

      1. new node is red child
      2. two fork tree left branch to discuss, right branch opposite can
      • No. 0 Case:

    If the Father node is black, then insert it directly! ()

      • First case:

    If, Father node is black, its uncle is red, then, directly to turn grandfather into red, then, father and uncle turn black! ()

      • second case:

    If, the Father node is red, its uncle is black, then, the new node is the right child, then left first, turned into a third situation! ()

      • The third case:

    If, the Father node is red, its uncle is black, then the node is left child, then proceed with (Grandfather's node) turn right! ()

    The principle and realization of inserting the red and black tree

    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.