Red and black tree RBT (red black tree)

Source: Internet
Author: User

The red/black tree is a self-balancing binary search tree. It is a data structure used in computer science. A typical application is to implement correlated arrays. It was invented by Rochelle bell in 1972. It is called the "symmetric Binary Tree" and Its Modern name is
Leo J. guibas and Robert
Sedgewick was obtained in a paper written in 1978. It is complex, but its operation has a good run time in the worst case, and is efficient in practice:
It can be searched, inserted, and deleted in O (log n) time. Here N is the number of elements in the tree.

Usage and Benefits

Like the AVL Tree, the red/black tree provides the best possible and worst-case guarantee for the insertion time, deletion time, and search time. This is not just to make their time-sensitive applications such as real-time applications
Time Application) has value and makes them valuable as building blocks in other data structures that provide the worst-case guarantee; for example, many data structures used in computational ry can be based on the red/black tree.

The red and black trees are also particularly useful in functional programming. Here they are one of the most common persistent data structures used to construct correlated arrays and collections, after the mutation, they can be maintained as previous versions. In addition to the O (log n) time, the persistent version of The Red/black tree requires O (log n) space for each insertion or deletion.

The red and black trees are 2-4 trees.
Equivalent. In other words, for each 2-3-4 tree, at least one data element is a red-black tree in the same order. The insert and delete operations on the 2-3-4 trees are also equivalent to the color flip and rotation in the red and black trees. This makes tree 2-3-4 an important tool for understanding the logic behind the red-black tree. This is why many textbooks have introduced algorithms before the Red-black tree, although 2-3-4 trees are not frequently used in practice. Nature

The red/black tree is a binary search tree with a color attribute on each node. The color is red or black. In addition to the mandatory requirements for binary tree search, the following additional requirements are added for any valid red/black tree:

Property 1. nodes are red or black.

Property 2. The root is black.

Properties 3. All leaves are black (the leaves are nil nodes ).

Property 4. The two subnodes of each Red node are black. (There cannot be two consecutive red nodes in all paths from each leaf to the root)

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

Because each red/black tree is also a special Binary Search Tree, the read-only operation on the red/black tree is the same as that on the common Binary Search Tree. However, the insert and delete operations on the red/black tree will no longer conform to the nature of the red/black tree. Restoring the properties of the red/black tree requires a small number of (O (log n) color changes (which is actually very fast) and no more than three tree rotations (two insert operations ). Although insertion and deletion are complex, the operation time can be kept
O (log n) times.

Insert

We first add a node using the binary tree search method and mark it as red. (If it is set to black, it will lead to a path from the root to the leaf, and an extra black node, which is difficult to adjust. However, when a red node is set
If there are two consecutive red nodes in conflict, you can adjust them by color switching (color flips) and tree rotation .) The following operations depend on the color of other neighboring nodes. Like in the family tree of humans, we will use the term "Uncle node" to refer to the brother node of the parent node of a node. Note:

  • Nature 1[1] and nature 3[2] Always keep.
  • Nature 4[3] It is threatened only when Red nodes are added, when black nodes are re-painted, or when rotation is performed.
  • Nature 5[4] It is threatened only when black nodes are added, red nodes are repainted, or rotated.
In the following, the node to be inserted is marked as N, the parent node of N is marked as P, the grandfather node of N is marked as g, and the uncle node of N is marked as U. Any color shown in the figure is either a hypothesis made by the circumstances in which it is located or implied by these assumptions
(Imply. Scenario 1: The new node N is located on the root of the tree and has no parent node. In this case, we re-paint it as black to satisfy the nature of 2 [5]. Because it adds one to the number of black nodes in each path, the attribute is 5.[4] Yes. Case 2: the parent node P of the new node is black, so the property is 4[3] The node is not invalid (the new node is red ). In this case, the tree is still valid. Nature 5[4] It is not under threat. Although the new node N has two black leaf subnodes, because the new node N is red, the path of each sub-node of the sub-node has the same number of black nodes as the path of the Black leaves replaced by the sub-node, so it still satisfies this nature.

Note: In the following circumstances, we assume that the parent node of the new node is red, so it has a grandfather node, because if the parent node is a root node, the parent node should be black. Therefore, a new node always has an uncle node, although it may be a leaf node in case 4 and 5.

Case 3: if both the parent node P and the uncle node u are red (when node N is newly inserted as the Left or Right child node of P belongs to situation 3, here, the right figure only shows N as the case of P on the left), then we can re-paint the two of them as black and re-paint the grandfather node G as red (to maintain the nature of 5[4]). Now our new node N has a black parent node p. Because any path through the parent node P or Uncle node u must pass through the grandfather node g, the number of black nodes on these paths has not changed. However, the red grandfather node G's parent node may also be red, which violates Nature 4[3]. To solve this problem, we recursively perform the whole process of scenario 1 on the grandfather node G. (Use G as a newly added node for various checks)

Note: In the remaining circumstances, we assume that the parent node P is the left child node of its parent G. If it is a right subnode, the left and right of case 4 and case 5 should be reversed.

Case 5: the parent node P is red, the uncle node u is black or missing, the new node n is the left child node of the parent node, and the parent node P is the left child node of the parent node G. In this case, we perform a right rotation for the grandfather node g;
In the tree generated by rotation, the previous parent node P is now the parent node of the new node N and the previous grandfather node G. We know that the previous grandfather node G is black, otherwise the parent node P cannot be red.
(If P and G are both red, it violates the property 4, so g must be black ). We switched the color of the previous parent node P and grandfather node g, and the result tree satisfied the property 4[3]. Nature 5[4] still satisfied, because all the paths through any of the three nodes previously passed through the grandfather node g, and now they all passed through the previous parent node p. In each case, these are the only black nodes in the three nodes. Note that insertion is actually an in-situ algorithm, because all the above calls use tail recursion. Delete

If the node to be deleted has two sons, the problem can be converted to the problem of deleting another node with only one son (for the sake of convenience, the son mentioned here, son of a non-leaf node ). For a binary search tree, When deleting a node with two non-leaf sons, we find the maximum element in either its left subtree or its right subtree, and transfer its value to the node to be deleted (as shown here ). We then delete the node from which we copied the value. It must have fewer than two non-leaf sons. Because only copying a value does not violate any attribute, This simplifies the problem as how to delete a node with at most one son. It does not care whether the node is the node to be deleted or the node from which the value is copied.

In the rest of this article, we only need to discuss how to delete nodes with only one son (if both of them are empty, that is, they are all leaves, we can regard one of them as its child ). If we delete a red node (the child of the node will be a leaf node), its father and son must be black. Therefore, we can simply replace it with its black son without damaging attributes 3 and 4. By deleting a node, only one red node is missing from all the paths, so that attribute 5 can be ensured. Another simple case is that the deleted node is black and its son is red. If we only remove this black node and replace it with its red son, it will destroy attribute 5. However, if we re-paint its son as black, then all the paths through it will pass through its black son, so that you can
To keep attribute 5.

It should be further discussed that when the node to be deleted and its son are both black, this is a complicated situation. We should replace the node to be deleted with its son. For convenience, he called his son N (in his new position) and his brother (another son of his father) s.

If n and its initial father are black, then the father who deleted it will lead to a black node missing from the path that passed n. Because this violates attribute 4, the tree needs to be rebalance. Consider the following situations:

Case 1: n is the new root. In this case, we're done. We remove a black node from all paths, and the new root is black, so the attributes are kept.

Note: In case 2, 5, and 6, we assume that N is the father's left son. If it is the right son, the left and right should be reversed in these cases.

Case 2: S is red. In this case, we made a left rotation on N's father and converted the red brother into N's grandfather. We proceed to the debugging
N father and grandfather colors. Although all the paths still have the same number of black nodes, now N has a black brother and a red father, so we can continue to handle the problem in case 4, 5, or 6. (Its new brother is black because it is a son of red S .)

Case 3: N's fathers, S and S's sons are black. In this case, we simply re-paint s in red. The result is all the paths through S, which are those that have never passed n before, with a black node missing. Because deleting the initial father of N removes a black node from all the paths of N, which balances everything. However, all paths passing through P are now less than those without passing through P, so they still violate attribute 4. To fix this problem, we need
1. Perform rebalancing on p. Case 4: the sons of S and S are both black, but the fathers of N are red. In this case, we simply exchange the colors of N's brother and father. This does not affect the number of black nodes that do not pass N. However, it adds one to the number of black nodes through N, and adds the black nodes deleted from these paths. Case 5: S is black, S's left son is red, S's right son is black, and N is his father's left son. In this case, we perform right rotation on S, so that S's left son becomes S's father and N's new brother. We then exchanged the colors of S and its new father. All the paths still have the same number of black nodes, but now N has a black brother whose right son is red, so we entered situation 6. N and his father are not affected by this change.

Case 6: S is black, S's right son is red, and N is his father's left son. In this case, we made a left rotation on N's father, so s became N's father and S's right son's father. We then exchanged the colors of N's father and S, and made S's right son black. The subtree still has the same color on its root, so attribute 3 is not violated. However, N now adds a black ancestor: either n's father turns black, or it is black, and S is added to a black grandfather. Therefore, a black node is added through the N path.

At this time, if a path does not pass N, there are two possibilities:

  • It passes through n's new brother. So it must have passed S and N's father before and now, and they only exchanged colors. Therefore, the path keeps the same number of black nodes.
  • It passes through n's new uncle, S's right son. So it used to pass S, S's father, and S's right son, but now only through S, it is assumed that it is the color of its former father, and S's right son, it is changed from red to black. The merging result is that this path passes the same number of black nodes.

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.