Red and black tree of data structure

Source: Internet
Author: User

It has been discussed in the binary tree that if you insert a tree node in random order, the vast majority of them will appear unbalanced. In the worst case, when the data is inserted in order, the binary tree will become a linked list, and the efficiency of insertion and deletion will be severely reduced

is the case of inserting a binary tree in ascending order of data:


A red-black tree is a way to solve a non-equilibrium tree, which is a two-fork search tree that adds some features.

In order to be able to search for a tree faster, it is necessary to ensure that the tree is always balanced (or at least mostly balanced), which means that the number of descendants on the left and the number of descendants on the right side of each node in the tree should be roughly equal

Red and black Rules

When inserting (or deleting) a node, you must follow certain rules, which are called red-black rules. If you follow these rules, the tree is balanced.

1, each node is not black or red

2, the root is always black

3, if the node is red, then its child nodes must be black, and vice versa is not necessarily set

4. Each path from the root to the leaf node or the empty node must contain the same number of black nodes

The empty sub- node in rule 4 refers to the position where a non-leaf node can be connected to a child node. In other words, it is the location of a possible left dial hand node with a right child node, or a node with a left dial hand node that may be connected to the right child node.

The number of black nodes from the root node to the leaf node path is called the black height

These constraints force the key properties of the red-black tree: The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path . The result is that the tree is broadly balanced. Because the worst-case time for operations such as inserting, deleting, and finding a value is proportional to the height of the tree, the theoretical upper limit on the height allows the red and black trees to be efficient in the worst case, unlike the normal two-fork lookup tree.

To understand why these features ensure this result, it is sufficient to note that rule 4 causes the path to not have two contiguous red nodes . The shortest possible paths are black nodes, and the longest possible paths have alternating red and black nodes. Because all the longest paths according to rule 4 have the same number of black nodes, this indicates that no path can be more than twice times longer than any other path.

Algorithm analysis

First, when the first node is inserted, the node is the root node, so it must be black.

When we add a new node, we'll start by default and the new node is red . Why is it? Because inserting a red node is less likely to violate the red-black rule than to insert a black node. Inserting a red node will certainly not change the black height of the tree; In addition, if the parent node of the insertion node is a black node, it does not violate the rule that the parent node is red, and if the node is a red node, then the tree must be transformed to accommodate the rule. Another point is that violating Rule 3 (both parent and child nodes are red) is easier to correct than violating rule 4 (black height)

Now let's do some experiments, insert 50,25,75 in turn, and get the following binary tree


This tree conforms to the red and black rules listed above, it is a balance tree

Color transform

Next, we'll add a new node with a value of 12. According to the rules of Binary search tree, 12 should be inserted in 25 of the left child node, but, because our default new node is red, and 25 is red, parent-child node can not be both red, then you need to 25 parent node and its parent node of the right child node color transformation, Why also transform the right child node of its parent node? Imagine, if we only convert 25 to black and 75 to remain red, then after inserting a new node, the root node of the left subtree of the black height is bound to be larger than the right subtree 1, which violates the red and black rules

In fact, in this case, it is generally necessary to change the color of the parent node of 25, because at this point the parent node of 25 is the root node, so the black remains unchanged

After inserting 12, generate the following red-black tree:


At this point the binary tree appears to be a little unbalanced, but still complies with the red and black rules

Next, we'll insert a new node with a value of 6.


The problem arises, 6 of the parent node is 12, red, and the new node is red, violating the red and black principle. If the 6 is converted to black, then the black height of 6 to heel is 3, and 75 to heel is 2, which also violates the red and black rules.

According to the above experience, if we change the color of 12 and 25?


Visible, still violating the red and black rules

That is, if a tree exceeds the two-layer imbalance (the subtree on one side is higher than two layers above the subtree on the other side), it is impossible to satisfy the red-black rule, because if the number of nodes on a path is more than the number of nodes on the other path, it either has more black nodes, or has two adjacent red nodes, Will violate the red and black rules.

We are in a dilemma and it seems that the color shift alone cannot get out of this cul-de-sac. This is the time to transform the red and black trees by another way.

Rotating

In order to balance a tree, you need to rearrange the nodes manually, if most of the nodes are on the left side of the root, you need to move some nodes to the right, to become left-handed; if most of the nodes are on the right side of the root, you will need to move some nodes to the right . The left and right rotation is relative to the root node of a subtrees tree.

Consider an example:


With the root as the center, after the right rotation, the result is as follows:


Right-handed, centered around 50, clockwise rotation of the surrounding nodes

If we were in the center of 25, we had a reverse operation--left, and the nodes around 25 were rotated counterclockwise, then back to the way they were before the rotation.

Note that when you rotate, you still follow the rules for searching the binary tree . For example, in the first picture of the tree in the right-hand, the node 25 in the center of 50 clockwise rotation, can only be rotated to the upper left of 50, because 25:50 is small, and 75:50 large, can not be rotated clockwise, so it still in the lower right side of 50 remain relative position unchanged.

A value smaller than the node, either left or right, can only be at the lower or upper left of the node, or the value larger than the node can be in the lower right or upper right of the node. this conforms to the rules of the search binary tree.

In fact, the rotation is far more complex than the simple example above, and then take a look at an example:


We have a two-layer, five-node tree right-handed, 75:50 large, still in the lower right of 50, and 25:50 small, from the lower left of 50 to the upper left of 50, 12 as the left child node of 25 has not changed, but the layer of the third layer has become the second layer. It can be found that the nodes on the right side of the 50 drop one level (including 50 itself) after the 50-centered right-hand rotation, while the nodes on the left of 50 rise by one level.

However, one of the strange things that happened was that 37 from the intuitive point of view, released the lateral movement, from the left sub-tree moved to the right sub-tree. Let us analyze the reasons for this situation: Left Dial hand tree Group raised a level, 37 as 25 of the right child node, should have risen, with 50 rise a level, but, at this time 25 of the right child node has been 50 preconceived occupy, so that, 37 good along the right sub-tree to find home again, Finally, it can only be wronged in the left bottom of 50 to take root.

In the above example, 37 is called the medial offspring of the 50-node right-hand operation, and 12 is the descendant of the outer side .

In fact, the whole subtrees tree will also occur collective movement, such as:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Red and black tree of data structure

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.