A New Algorithm for deleting red/Black Tree nodes

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/mxway/article/details/38080315

Five properties of the red and black trees on Wikipedia below

1. nodes are red or black

2. The root is black.

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

4. Each red node must have two black subnodes. (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 leaf contain the same number of black nodes.

Based on the five properties above, we can draw the following conclusion:

Conclusion 1: If X has only one non-empty subtree in the red/black tree, X must be black.


Proof: Assume that X is red. According to property 4, X has two black subnodes. There is only one non-empty subtree conflict with X.

Conclusion 2: In the red and black trees, if X is only one non-empty subtree. The root of the non-empty subtree must be red, and the non-empty subtree only has one root node.

Proof: if Y is the left child of X, the color of node y is black, and Y has a subtree. Because y is black, the right sub-Right of X is empty, therefore, the number of black knots in the path from X to each leaf node of the Left subtree is greater than the number of black knots in the path from X to the right leaf node, which violates the nature of 5, so node y is red. Because y is red, if y's subtree exists, according to property 4, we can conclude that Y's two subtree must be black. The number of black nodes in the path from X to Y to each leaf node is greater than that in the right leaf node path.

As mentioned above, when Y is the right child of X, it can also prove conclusion 2.


The following describes how to delete the red/black tree:

1. From the above two conclusions, we can see that if the node to be deleted has only one child, then the value of the child node is directly used to replace the value of the parent node. You can delete a subnode without entering the deletion and adjustment algorithm.

2. If the two children of the node to be deleted are not empty, we only need to find the successor node of the sequence in the current node. Replace the value of the current node with the value of the next node. When the successor node is used as the new current node, only one right or left or right child of the current node must be empty.

3. If the current node has a successor node after step 2, replace the current node value with its successor node value without entering the delete adjustment algorithm. If the current node does not have a successor node, go to the delete adjustment algorithm.

Algorithm flow steps:

1. Determine whether the left and right subtree of X is null.

2. if the left and right sides of X are not empty, find the subsequent node y of X in the central sequence, replace the value of X with Y, and convert y as the new x node to step 1. Otherwise, go to step 3.

3. If the left child of X is not empty, replace the value of X with the value of the left child, delete the left child of X, and the algorithm ends. Otherwise go to 4

If the right child of X is not empty, replace the value of X with the value of the right child.

4. If the right child of X is not empty, replace the value of X with the value of the right child, delete the right child of X, and the algorithm ends. Otherwise, go to Step 5.

5. Delete X and go to the Red/black tree to delete the Node Adjustment Program.

The following figure shows how to delete the red/black tree.

First, use the code in the previous article to generate a red/black tree. The insertion sequence is :,

Figure 1: red/black tree

1. delete node 22. Because node 22 has only one child 24, replace 22 with 24. According to the above conclusion, there is no need to delete the adjustment algorithm.

Figure 2: delete node 22

2. the color of the delete node 24 and 24 is black, and the brother 39 nodes are also black. The two children of 39 are empty and black, so the 39 nodes are red, parent node 28 is the new current node. Because 28 is red, the algorithm ends when 28 is black.

Figure 3: red/black tree after Node Deletion 24

3. delete node 39, Because 39 is a leaf node and the color is red. Delete directly without any adjustments.

Figure 4: red/black tree after delete node 39

4. delete node 28. Because node 28 is black, its brother node 47 is black, and its two children are also black. So node 47 is red. Parent node 41 changes to current node


Figure 5: delete node 28 first adjustment

Now 41 is the current node. Because the 41 sibling node 14 is black, the 14 children are also black. So the 14 nodes are red. Parent node 20, that is, the root node is the current node.

Figure 6: delete node 28 second adjustment

At this time, 20 is the current node. Since 20 is the end of the Root Node Adjustment Algorithm, 28 is deleted from the red/black tree.

Figure 7: red/black tree after node 28 is deleted

At this point, all the properties of the red/black tree are restored.

The following shows the C ++ code for deleting and adjusting nodes in the red/black tree. For details about the red/black tree deletion and adjustment algorithm, refer to July's blog. This code is in the previous articleDetailed steps for inserting the red/black tree at the bottom layer of STL map and code implementationAdded new features

Delete the C ++ program of the node.

Template <typename T> void rbtree <t>: deletevalue (const T & V1) {rbnode <t> * P = NULL; rbnode <t> * nextnode = NULL; search (V1, P); If (P = NULL) {cout <"the deleted value does not exist" <Endl; return ;} if (p-> left & P-> right) {rbnode <t> * tempnode = p-> right; while (tempnode) {// The successor node nextnode = tempnode; tempnode = tempnode-> left;} p-> val = nextnode-> val; P = nextnode ;} if (p-> left) {// replace rbnode directly with the value of the successor node <t> * temp = p-> left; P-> val = temp-> val; p-> left = NULL; Delete temp;} else if (p-> right) {// replace rbnode directly with the value of the successor node <t> * temp = p-> right; P-> val = temp-> val; P-> right = NULL; delete temp;} else {// No left or right subtree exists. You need to enter the delete adjustment algorithm deletereblance (p); If (P = root) {root = NULL ;} else if (P = p-> parent-> left) {// you need to modify the pointer field of the parent node p-> parent-> left = NULL ;} else if (P = p-> parent-> right) {// the pointer field of the parent node needs to modify p-> parent-> right = NULL;} Delete P ;}}

After the red/black tree is deleted, adjust the algorithm:

Template <typename T> void rbtree <t>: deletereblance (rbnode <t> * node) {rbnode <t> * parent = NULL; rbnode <t> * Other = NULL; while (node-> color = _ rb_black_node & node-> parent) {parent = node-> parent; If (node = parent-> left) {Other = parent-> right; if (Other-> color = _ rb_red_node) {// scenario 1 the sibling node is red parent-> color = _ rb_red_node; other-> color = _ rb_black_node; _ rbtree_rotate_left (parent); Other = parent-> right ;} if (Other-> left = NULL | Other-> left-> color = _ rb_black_node) & (Other-> right = NULL | Other-> left-> color = _ rb_black_node) {// in Case 2, the brother is black, the two siblings are also black other-> color = _ rb_red_node; node = parent; continue ;} if (Other-> right = NULL | Other-> right-> color = _ rb_black_node) {// In case 3, the right child of the sibling node is black, red other-> left-> color = _ rb_black_node on the left; // at this time, the left child must exist and the color is red, if this condition is not met, other-> color = _ rb_red_node; _ rbtree_rotate_right (other); Other = parent-> right ;} // In case 4, the right child of the sibling node is red other-> right-> color = _ rb_black_node; Other-> color = parent-> color; parent-> color = _ rb_black_node; _ rbtree_rotate_left (parent); break;} else {Other = parent-> left; If (Other-> color = _ rb_red_node) {// scenario 1: The sibling node is red parent-> color = _ rb_red_node; Other-> color = _ rb_black_node; _ rbtree_rotate_right (parent); Other = parent-> left ;} if (Other-> left = NULL | Other-> left-> color = _ rb_black_node) & (Other-> right = NULL | Other-> left-> color = _ rb_black_node) {// in Case 2, the brother is black, the two siblings are also black other-> color = _ rb_red_node; node = parent; continue ;} if (Other-> left = NULL | Other-> left-> color = _ rb_black_node) {// In case 3, the right child of the sibling node is black, red other-> right-> color = _ rb_black_node on the left; // at this time, the left child must exist and the color is red, if this condition is not met, other-> color = _ rb_red_node; _ rbtree_rotate_left (other); Other = parent-> left ;} // In case 4, the right child of the sibling node is red other-> left-> color = _ rb_black_node; Other-> color = parent-> color; parent-> color = _ rb_black_node; _ rbtree_rotate_right (parent); break ;}} node-> color = _ rb_black_node ;}



A New Algorithm for deleting red/Black Tree 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.