Delete a red/black tree

Source: Internet
Author: User
About the balance of the red and black trees-Conditions for deleting nodes [reprinted]

There have never been any good articles or implementations about algorithm deletion on the Internet. I will write them down here to facilitate future use.

The delete operation is always performed on a node with only one child or a leaf node, and will never be performed on a node with two children. The successor function is called only when there are two children in the node. At this time, the function must be executed along the right subtree of the node, A node with only one child will be found.

Will damage those balancing conditions

If a red node is deleted, no operation is required. No attribute of the red/black tree will be destroyed. When a black node is deleted, three problems may occur:

1. If the root is deleted and a red node has a new root, attribute 1 will be destroyed;

2. The black height of all generations of the node will be 1 less;

3. If the father and child of the node are both red, deleting the node may cause a red link;

How to restore balance

You can use the rb_delete_fixup function to restore the balance. The following is a complete algorithm:

Template <class type>
Void rb_tree <type >:: rb_delete_fixup (node <type> * x ){
Node <type> * w = NULL;

While (x! = Root) & (x-> cr = BLACK )){
If (x = x-> parent-> left ){
W = x-> parent-> right;
If (w-> cr = RED ){
// Case 1: w is RED. We change the color
// X's parent and w, then perform a left rotate on
// Parent of x, then we can enter 2, 3 or 4.
W-> cr = BLACK;
X-> parent-> cr = RED;
Left_rotate (x-> parent );
W = x-> parent-> right; // Adjust the w to the new sibling of x
}
If (w-> left-> cr = BLACK) & (w-> right-> cr = BLACK )){
// Case 2: w is BLACK and both left and right
// Children of w is black
W-> cr = RED;
X = x-> parent; // Shift x up a layer and re-enter the loop
}
Else if (w-> right-> cr = BLACK ){
// Case 3: w is BLACK and the left child of w is RED,
// Right child of w is BLACK
W-> cr = RED;
W-> left-> cr = BLACK;
Right_rotate (w );
W = w-> parent;
}
Else {
// Case 4: w is BLACK and the right child of w is RED.
// After some color change and a left rotation, the tree
// Is balanced again.
W-> cr = w-> parent-> cr;
X-> parent-> cr = BLACK;
W-> right-> cr = BLACK;
Left_rotate (x-> parent );
X = root;
}
}
Else {// x is right child of its parent
// Omit for simplicity
} // End if (x = x-> parent-> left)
}
X-> cr = BLACK;
}

The entire algorithm involves the following nodes: The deleted node is called z, and the successor of z is called y (when z has only one child, z and y are the same, z and y are not shown in the figure). the only child of y is x. The father of x is p [x]. The brother of x is w ,:

The core idea of the entire algorithm: All rotation will keep the root of the tree to the black height of all subnodes, such as α, β, gamma, Delta, ε, and ε. Let us assume that x has two layers of black, and finally, by changing and rotating the color, remove the extra black layer of x to rebalance the red and black trees.

For example, we assume thatXYes2Layer black, fromAToαAndβOfBlack heightEqual3, After the transformation is executedAToαAndβOfBlack heightStill equal3.

If the root is deleted, it must be a new red node that acts as the new root. Then, rb_delete_fixup turns the new root into black at the end, and the red and black trees restore the balance;

If there are two red connections, the x is directly dyed black, and the red and black trees directly restore the balance;

Only when x is not the root and x is black can the while LOOP be used to restore the black height balance of the red and black trees and solve the problem of red connection. In general, algorithms are classified based on the left/right children of p [x]. There are four types of algorithms. Since these two types of classification are symmetric, let's look at the situation where x is p [x] Left child. In this case, we use the w color to distinguish the 4 medium:

· Case 1: w is red;

Swap the colors of p [x] and w, and then left-handed to p [x. In this way, the D node becomes the new brother of x, because the original w is red, D must be black, and thus enter Case 2, 3 or 4.

· Case 2: w is black, and w's two children are black (the gray nodes in the figure do not matter in color, the same below );

In this case, we remove both x and w from the black layer, x into the single black, w into the red, so if p [x] is black, then the red and black trees will rebalance, if p [x] is red, the problem of red connection may occur. Move x up to a layer and reloop.

· Case 3: w is black, w's left child is red, and w's right child is black;

We swap the color of the left child of W and W, and then right-handed to W to enter case 4.

· Case 4: W is black, and W's right child is red (the left child is red or black );

The colors of P [X] and W are interchangeable. The right child of W is dyed black and then left-handed to P [X, because a black node A is added to the path from the root to α and β, an extra layer of black X is no longer needed, and the red and black trees will restore the balance.

As X is the right child of P [X], the situation is completely symmetrical. I will not discuss it here.

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.