Insertion and deletion of red and black trees

Source: Internet
Author: User

I. Introduction to red and black trees

The red-black tree is a balanced two-fork search tree, which is a commonly used data structure in computer science, and the most typical application is the implementation of data structures such as maps.

Red and black trees have the following restrictions:

1. The node must be red or black

2. The root node is black

3. All the leaf nodes are black.

4. The two child nodes of each red node are black, that is, there is no parent or child two nodes are all red

5. The number of black nodes on all simple paths from any node to each of its leaf nodes is the same.

To illustrate the limit of 3. In a red-black tree, a black child (called a sentry) is also added to the empty pointer at each node. So the limit of 3 must be established. In addition, the leaf node we are talking about is the original leaf, not the nil node.

Ii. insertion of red and black trees

The following article is good enough to write.

Http://www.cnblogs.com/xuqiang/archive/2011/05/16/2047001.html

Iii. removal of red and black trees (preparation knowledge)

Because the deletion is more complex, first write to prepare knowledge cushion one pad.

1 rotation

The first trick: Turn the bend into a straight

The second trick: Left and right: The main function is that the deeper subtree yields some depth to the shallow subtree

2 General find binary tree Delete node

There are many scenarios for deletion, but generally the following is rotated because the effect on the depth of each branch of the tree is small.

A. When deleted node n is a leaf node, delete directly

B. When deleted node n only one child, delete n , replace the location of the node with the child

C. When the deleted node n exists around the child, the real deletion point should be the sequence of n in the precursor, or the largest node of the left subtree, after which the value of n is replaced with the value of the true delete point. This boils down to the problem of a A/b for C.

3 by 2, All delete problems can be converted to delete leaf nodes or single nodes (only one child)

Iv. removal of red and black trees

1 when delete node n is a red leaf node, delete node n directly without affecting the red and black tree balance properties

2 when delete node n is a single node of red. Cannot appear if the child is red, violates the limit of 4, if the child is black, violates the limit of 5.

3 when delete node n is a black leaf node, due to the presence of sentry, can be converted to question 4

4 when the delete node n is a single node of black, both N has a black child node.

For example, the first explanation of the mark, delete means that the deleted node N, the child represents its child nodes, the father, brother are very good understanding. Black and red represent true colors, and cyan indicates an indeterminate color.

First, we are very bold to directly delete the node n, let the son take his place.

we indecisive to see, first of all, the red and black tree Delete problem is discussed in all cases, but there is a problem, that is 4 in such a delete will make " Parent " the black depth of the left subtree of the node is 1 less than the right sub-tree.

So the next thing we want to solve is not to delete the problem, but a red-black tree to adjust the selection problem. The requirement is that the left child of the parent node has a black node, and that the left subtree of the parent node is 1 smaller than the right subtree, requiring adjustments to it, which satisfies the red-black tree limit. Because this problem stems from black node n has black sub-node, we call it "double black problem".

4.1 Brother Red

X's brother W is red, then the son of W must be All Black, W Father P is also black.

Change the color of P and W while doing a left-hand rotation on p, which turns the condition 1 into a condition 2,3,4

4.2,4.3.4.4 collectively referred to as the Black brother problem

4.2 Black Brother Two nephews are divided into 4.2.1 and 4.2.2

4.2.1 Black brother and nephew Red father

P turns black, W turns red, solves the problem

4.2.2 Black elder brother black nephew black father

Because the x subtree is less than one black node relative to its sibling, the W can be red so that the X subtree matches the black node of the W subtree, maintaining balance.

If P had a brother, its black depth would be 1 smaller than that of a brother, so the 4.2.2 turned into a double-black problem with a 1-4 case.

4.3 Black brother left red nephew right black nephew

W is black, W left child red, right child black.

Swap W with the left child's color and turn the W right. Convert to Case 4

4.3 Black brother right red nephew

Since it is a sub-condition of the double black problem, it can be seen from the left graph that the "sub" subtree is the same as the black depth of the "1" and "2" subtrees.

So the adjusted subtree is the limit of meeting the red and black trees. (Both sides of the new "parent" subtree have equal black depth, so does the new brother's subtree)

Reference articles

http://blog.csdn.net/spch2008/article/details/9338923

http://blog.csdn.net/very_2/article/details/5722682

Insertion and deletion of red and black trees

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.