Analysis of red/Black Tree insertion and Deletion

Source: Internet
Author: User

A red/black tree is a special binary search tree. It has the following five properties:

1. The root node is black.

2. Each node is either black or red.

3. the two sons of each Red node must be black.

4. All leaf nodes are black. (Note: The leaf node here is not a true leaf node, but a node that only has color attributes but does not store data, and does not have a son node)

5. Any node in a red/black tree has the same number of black nodes in the simple path from its location to any other leaf node.

These two properties determine that the longest path from the root node to the leaf node cannot be twice the shortest path. Therefore, the red and black trees are roughly balanced binary trees. But unlike the AVL Tree, because the red and black trees are not strictly balanced, the AVL Tree is strictly balanced.

 

Insert operation

First, it is agreed that the color of the new node to be inserted is red. Then insert the rule of the Binary Search Tree inserted by the node into the tree. This node is called N

1. The root node is empty. In this case, change the color of N to black.

2. The parent node of N is black. In this case, no modification is required.

3. The parent node of N is red (depending on the nature of 3, the grandfather node of N must be black ).

The primary node of A and N is red. In this case, change the color of N's parent node and Uncle node to black. If the grandfather node is a heel node, change it to black. Otherwise, change the color to red, the target node inserted with the grandfather node is enabled from Case 1.

Start recursive detection.

              

B. The uncle node of N is black, and the father nodes of N and n are on the same side (that is, when the father node is the grandfather's left son, n is also the left son of the parent node. When the parent node is the right son of the grandfather node. N is also the right son of the parent node ). Take the parent node as the grandparent Node

For example, if the left son node is changed to black, the grandfather node is changed to red, and the Right Hand is based on the grandfather node. (N is the right son of the parent node .)

              

C. The uncle node of N is black, and the parent node of N and n are not on the same side (that is, when the parent node is the grandfather's left son, n is the right son of the parent node. When the parent node is the right son of the grandfather node. N is also the left and right son of the parent node ). Take the parent node as the parent node

As an example. Take the parent node as the benchmark, perform left-hand operations, and then insert the parent node as the target node to B in case 3.

              

Delete operation

When a deleted node has two sons, it can be converted to a deleted node with only one son. For a binary search tree, When deleting a node with two non-leaf sons, we find the largest element in either its left subtree or

The smallest element in its right subtree and transfers its value to the node to be deleted. We then delete the node from which we copied the value. It must have fewer than two non-leaf sons. Because it only copies a value and does not violate any nature

The problem is simplified to 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.

All the situations can be converted to deleting a node with only one son. We agree that the node to be deleted is n (if n "has no" Son node, replace one of the other nodes with the son node of the leaf node)

1. When n is a red node. Delete N directly and replace it with its black son.

2. N indicates a black node and the parent node is red. Delete N directly, replace it with its son node, and change it to black.

3. N indicates a black node and the parent node is black. Delete n between us and replace it with its son node. The son node becomes n' and the color of N is changed to black.

A. n''s brother node and the two sons of the brother node are both black. Change the color of the sibling node and parent node.

B. n''s brother nodes are black, and the red son and the brother node are on one side (that is, when the brother node is the left son, the red son is also the left son. When the brother node is the right son, the red son is also the right son ). We use the sibling node as the right child

As an example. Change the color of the grandfather node and the brother node, change the color of the red right son to black, and then use the grandfather node as the baseline. (If the sibling node is the left son, the right hand is returned)

C. n''s sibling nodes are black and the red and brother nodes are not on one side (that is, when the brother node is the left son, the red son is also the right son. When the brother node is the right son, the red son is also the left son ). We use the sibling node as the right child

As an example. Swap the color of the brother node and its red son, and then use the brother node as the benchmark right-hand. In this case, n is the case of B above. (If the sibling node is the right son, the corresponding left hand)

The sibling nodes of D and n are red. Take the right son of a sibling node as an example. The colors of the parent node and the sibling node are exchanged and left-handed Based on the parent node (if the sibling node is left-handed, the right-handed is corresponding ), this n' has a black sibling node, followed

To N, you can enter A, B, and C.

E. n''s sibling node is black, and the parent node is also black. Change the color of the sibling node to red. Then, insert the node with the parent node as the target and judge from the beginning.

Analysis of red/Black Tree insertion and Deletion

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.