The red/black tree is a binary search tree. to delete the red/black tree, follow the rules of the Binary Search Tree. To delete node Z (5), delete the minimum value of Y in the right subtree of Z and then assign the value of Y to the Z node. :
Then we will consider the red and black trees.
1. The deleted node y is red. We will not handle it. It is still a red-black tree.
2. If the y node is black and the x node is red, you only need to change the color of X to black. The number of black nodes will not change.
Only y nodes are black, and x nodes are black.
Case 1: X is black, PX (the original parent node of Y) is black, W is red.
Operation: first, dye Px in red, W in black, and then perform left-hand operation on W. The new x points to the original x node (the white node does not care about the color now), and the W points to the C node. Continue to call the delete operation.
Why does X point to x nodes? We use count (X) to represent the number of black nodes from x nodes to the leaf node path. Set count (PX ),
Then count (x) = count (PX)-2; because the Black y node is deleted in the middle. Count (A) = count (B) = count (PX)-1; then count (X) is still 1 smaller than count (A), continue to call.
Case 2: X is black, W is black, and A and B are also black. Simply dye W in red. The new x points to the PX node.
Before the change: Count (PX) = M, count (x) = m-2. Count (w) = m-1.
After the conversion: Count (w) = m-2, count (x) = m-2, then count (PX) = m-1.
A black parent node equivalent to PX is deleted.
Case 3: X is black, W is black, A is red, and B is black. (We do not care about the pixel color ).
First, a performs the right rotation operation, and then changes a to black, w to red, X to X, and w to a node. The proof is the same as above.
Case 4: X is black, W is black, B is red, and A is black or red.
Operation: Perform the left rotation operation on W, and then dye W to the original PX color, B to the black color, and PX to the black color.
This can end.
It can be proved in the same way.
Time Complexity of the delete operation: O (lgn ).
So far, the insert/delete operation of the red/black tree has been completed.