Red and black Trees

Source: Internet
Author: User

Directory

    • Definition and nature
    • Rotating
    • Insert
    • Delete
Definition and nature

Red-black tree, is a balanced "binary search tree".

The red and black tree icons are as follows:

Properties:

    1. The knot is either red or black.
    2. The root node is black.
    3. The empty junction is black.
    4. The two sons of the Red knot are all black.
    5. Each path of any node to an empty node contains the same number of black nodes.

PS: Empty nodes are only used as auxiliary and do not contain data.

Rotating

When inserting and deleting, the tree is rotated and re-colored in order to continue to preserve the nature of the red and black tree.

Here are the left and right icons of the tree:

Insert
    • If the root node is inserted, apply the node directly to black.
    • If the parent node of the inserted node is black, the node is painted red.
    • If the parent node of the inserted node is red, it is divided into the following two cases:

"Red Father Red Uncle"

Re-coloring: Black father black Uncle Red grandfather, if the grandfather knot point of the parent node is still red, then it is a double red problem.

"Red father, Uncle Black."

At first glance, the natural red Father Black Uncle does not exist.

But the red Father Red Uncle problem may be turned into a double red situation, then may appear in black uncle.

Two cases:

Delete

"Situation one: Delete nodes as leaf nodes"

    • If the node is red, it can be deleted directly, without affecting the nature of the red and black tree, the algorithm ends.
    • If the node is black, then the red black tree is not balanced after deletion. (discussed later)

"Scenario Two: Delete node has only one sub-node."

The node is black (it cannot be red), its sub-node is red, and the sub-node replaces the node's position color becomes black.

"Scenario Three: Delete a node with two sub-nodes."

Refer to "Binary search tree", equivalent to the leftmost node and the deleted node Exchange values, and then delete the leftmost node, the leftmost node is the leaf node, so back to the situation one.

The following focus is on case one, where the leaf node is black.

Case1: Red brother (Black father two black nephew is inevitable)

    1. Do a left spin and recolor ()
    2. The father dyed black, 1 red, delete the node (such as Case4).

Case2: Black brother left red nephew right

    1. Once right-handed ().
    2. case3: Black brother right red nephew .

CASE3: Black brother right red nephew

    1. Do a left spin
    2. Delete node, red-black nature recovery.

Case4: Black brother, two black nephew, red father

The father dyed black, the elder brother dyed red, deletes the knot point.

CASE5: Black elder brother black nephew black father

To remove the knot after the balance, the elder brother dyed red.

But at this point, the Black node on the path through the parent node is not passed ... 1 fewer, so you need to start with case1 and rebalance the parent node.

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.