[Data structure] red black tree

Source: Internet
Author: User

1. Overview

Red black tree is a self-balancing binary lookup tree, similar to the red black tree and the AVL tree, which maintains the balance of the binary lookup tree with specific actions when inserting and deleting operations, resulting in higher lookup performance.

Although it is complex, its worst-case run time is also very good, and is efficient in practice: it can be found, inserted and deleted in O (log n) time, where n is the number of elements in the tree.

2. Nature

A red-black tree is a two-prong search tree with color attributes for each node, with a color of red or black. In addition to the general requirements of the binary search tree, we have added the following additional requirements for any valid red-black tree:

1, the node is red or black.

2, the root is black.

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

4. Each red node must have two black child nodes. (no two contiguous red nodes can be found on all paths from each leaf to the root.) )

5. All simple paths from any node to each of its leaves (paths that do not contain duplicate points) contain the same number of black nodes.

Below is a detailed illustration of a specific red-black tree:

 

3. Operation

Because each red and black tree is also a special two-fork lookup tree, the read-only operation on the red and black tree is the same as the read-only operation on the normal binary lookup tree. However, inserting and deleting on a red-black tree will result in no longer matching the nature of the red-black tree. Restoring the nature of the red-black tree requires a small amount (O (log n)) for color changes (which is actually very fast) and no more than three tree rotations (two times for the insert operation). While insertions and deletions are complex, the operation time can still be maintained at O (log n) times.

3.1 Insert operation

We first increment the node with a two-fork look-up tree and mark it as red. (If set to black, it will lead to the path of the root to the leaf, there is an additional black node, this is difficult to adjust.) However, when the red node is set, it may cause two consecutive red nodes to conflict, so you can adjust by color flips and tree rotation. What to do next depends on the color of the other neighboring nodes. As in the human family tree, we will use the term Uncle node to refer to the sibling node of the parent node of a node.

Attention:

Properties 1 and Properties 3 are always maintained.
Property 4 is threatened when the red node is added, the black node is redrawn red, or rotated.
Property 5 is threatened when the black node is added, the red node is redrawn black, or rotated.

 

In the following node to be inserted, the parent node labeled N,n is labeled P,n, and the grandfather node labeled G,n is marked U. Any color shown in the figure is either assumed by the circumstances in which it is located, or is assumed to be implied (imply).

Scenario 1:

  The new node n is located on the root of the tree and has no parent node. In this case, we redraw it to black to satisfy the nature 2. Because it increases the number of black nodes on each path by one, Nature 5 matches.

Scenario 2:

  The parent node of the new node P is black , so the property 4 is not invalidated (the new node is red). In this case, the tree is still valid. Property 5 is also not threatened, although the new node n has two black leaf sub-nodes, but since the new node n is red, the path through each of its child nodes has the same number of black nodes as the path through which the black leaves are replaced, so this property is still satisfied.

Scenario 3:

  if both parent p and Uncle node u are red , (when the new Insert node n is the left child node of P or the right child node belongs to Case 3, where the right figure shows only n as P left dial hand) then we can redraw them to black and redraw the grandfather node G as red (to keep the property 5). Now our new node n has a black parent node p. Because any path through parent node p or Uncle node u must pass through grandfather node G, the number of black nodes on these paths has not changed. However, the red Grandfather node G may be the root node, which violates the nature of 2, it is also possible that the parent node of the grandfather node G is red, which violates the nature of 4.

To solve this problem, we recursively proceed to the entire process of scenario 1 on the grandfather Node G. (check g as a newly added node for various situations)

 

Scenario 4:

  The parent node p is red and the Uncle node U is black or missing , and the new node n is the right child node of its parent node p and the parent node p is the left child node of its parent node. In this case, we perform a left rotation to change the role of the new node and its parent, and then we deal with the previous parent node p in case 5 to resolve the still defunct property 4. Note that this change will cause some paths to pass through the new node n (1th leaf node) or not through node P (compared to the 3rd leaf node), but because the two nodes are red, the property 5 is still valid.

Scenario 5:

  The parent node p is red and the Uncle node U is black or missing, the new node n is the left child node of its parent node, and the parent node p is the left child node of its parent node G. In this case, we do a right rotation for the grandfather Node G, and in the rotation-generated tree, the previous parent node p is now the parent node of the new node n and the previous grandparent 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 in violation of property 4, so G must be black). We switched the previous parent node p and grandfather node G color, the result of the tree satisfies the nature 4. Nature 5 also remains satisfied because all paths through any one of these three nodes were previously passed through the grandfather Node G, and now they all pass through the previous parent node p. In each case, this is the only black node in the three nodes.

 

3.2 Delete operations

For a binary lookup tree, when deleting a node with two non-leaf sons, we find the largest element in its left subtree, or the smallest element in its right subtree, and transfer its value to the node to be deleted.

We first replace the node to be deleted with its son. For convenience, call this son N (in a new position), calling its brother (another son of his father) to be S. In the following, we still use p to address N's father, SL called S's left son, Sr called S's right son.

Scenario 1:

  N is the new root . In this case, we are done. We have removed a black node from all paths, and the new root is black, so the nature is maintained.

Scenario 2:

  S is red . In this case we made a left spin on the father of N, converted the red brother to N's grandfather, and we then swapped the colors of N's father and grandfather. After both of these operations, although the number of black nodes on all paths has not changed, now N has a black brother and a red father (whose new brother is black because it is a son of red s), so we can proceed to 6来 processing in case 4, situation 5 or situation.

  

Scenario 3:

  The sons of N's father, S, and s are all black. in this case, we simply redraw s to red. The result is all paths through s, which are those paths that previously did not pass N, with a black node missing. Because deleting N's initial father makes all the paths through n less a black node, this makes things all balanced up. However, all paths through P now have a black node less than the path that does not pass the p, so it is still a violation of nature 5. To fix this problem, we need to do a rebalancing on p starting with scenario 1.

 

Scenario 4:

  the sons of S and S are All Black, but N's father is red . In this case, we simply exchanged the color of N's brother and father. This does not affect the number of black nodes that do not pass n paths, but it adds one to the number of black nodes on the path through n, fill the black nodes that are deleted on those paths.

 

Scenario 5:

S is black, S's left son is red, S's right son is black, and N is its father's left son. In this case we do a right spin on S, so that S's left son becomes the father of S and the new brother of N. We went on to Exchange S and its new father's color. All paths still have the same number of black nodes, but now N has a black brother, his right son is red, so we're in case 6. Neither N nor its father is affected by this transformation.

 

Scenario 6:

S is black, S's right son is red, and N is its father's left son. In this case we do a left spin on the father of N, so that s becomes the father of N (P) and the right son of S. We then swapped N's father and s for the color, and made s the right son black. Subtree is still the same color on its root, so the nature of 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 path of N.

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

    • It passed N's new brother. Then it must have passed the father of S and N, and they just exchanged colors. So the path keeps the same number of black nodes.

    • It passed N's new uncle, S's right son. Then it passed through S, S's father and S's right son, but now only through S, it is assumed to be its former father's color, and S's right son, it was changed from red to black. The compositing effect is that this path passes the same number of black nodes.

In any case, the number of black nodes on these paths has not changed. So we recovered the nature of the 4. The white nodes in can be red or black, but you must specify the same color before and after the transformation.

 

[Data structure] red 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.