Algorithm basis: What is a red-black tree?

Before learning red-black trees, you need to understand Binary Search Tree.

1. Binary search tree

1. Binary search tree (BST) features

(1) left subtree value roommate node are less than or equal to the value of the root node.
(2) the right subtree of the value on node roommate are greater than or equal to the value of the root node.
(3) left and right The subtrees are also binary sort trees.

The figure below is a typical binary search tree:
Insert picture description here
What are the benefits of such a data structure? Let's take the example of finding the number whose node is 10:

1. View root node 9:

Insert picture description here

2. Since 10> 9, check the right child 13:

Insert picture description here

3. Since 10 <13, check the left child 11:

Insert picture description here

4. Since 10 <11, look at the left child 10 and find that 10 is the node to find:

Insert picture description here

2. Advantages of Binary Tree

This method is exactly the idea of ​​binary search. The maximum number of searches required is equivalent to the height of the binary search tree. A similar method is used when inserting nodes, and the size of the new node is found to be suitable for insertion. position.

3. Defects of Binary Tree

The defect of binary tree is mainly reflected in the insertion of new nodes.

Suppose the initial binary search tree has only three nodes, the root node value is 9, the left child value is 8, and the right child value is 12:
Insert picture description here

Next, we insert the following five nodes in sequence: 7, 6, 5, 4, 3. According to the characteristics of the binary search tree, what will the result be like?
Insert picture description here

Although the shape of the above figure also conforms to the characteristics of the binary tree, the performance of the search is greatly reduced and almost becomes linear.

How to solve the unbalanced binary search tree several times to insert a new node caused it? Thus a red-black tree .

Two. Red-Black Tree

The red-black tree (Read Black Tree)is a self-balancing binary search tree. In addition to meeting the basic characteristics of a binary search tree, it also has the following additional features ( must be satisfied at the same time )

1. The node is red or black.
2. The root node is black.
3. Each leaf node is a black empty node (NIL node).
4 The two child nodes of each red node are black. (There can be no two consecutive red nodes on all paths from each leaf to the root)
5. All paths from any node to each leaf contain the same number of black nodes.

The tree in the figure below is a typical red-black tree:
Insert picture description hereit is precisely because of these restrictions that the self-balance of the red-black tree is guaranteed. The longest path from the root to the leaf of the red-black tree will not exceed the shortest path. 2 times.

When inserting or deleting nodes, the rules of the red-black tree may be broken. At this time, some adjustments need to be made to continue to maintain our rules.

Under what circumstances will the rules of the red-black tree be broken, and under what circumstances will the rules be broken? We cite two simple chestnuts:

1. Insert a new node with a value of 14 into the original red-black tree:

Insert picture description hereSince the parent node 15 is a black node, this situation will not break the rules of the red-black tree, and no adjustment is required.

2. Insert a new node with a value of 21 into the original red-black tree:

Insert picture description hereSince the parent node 22 is a red node, this situation breaks the rule 4 of the red-black tree (two child nodes of each red node are black), and adjustments must be made to make it conform to the red-black tree rules again.

There are two ways to adjust: color change and rotation. Rotation is divided into two forms: left rotation and right rotation.

Discoloration:

In order to meet the red-black tree rules again, try to change the red node to black, or change the black node to red.

The figure below shows a part of the red-black tree. Note that node 25 is not the root node. Because node 21 and node 22 appear red continuously, which does not conform to rule 4, the node 22 is changed from red to black:
Insert picture description herebut this is not the end, because the extra black node breaks rule 5, so a chain reaction occurs. Need to continue to change node 25 from black to red: it
Insert picture description here
is still not over at this time, because node 25 and node 27 form two continuous red nodes, and node 27 needs to be changed from red to black:

Rotate left:

Rotate the two nodes of the red-black tree counterclockwise so that the parent node is replaced by its own right child, and you become your own left child. It's weird to say, let's look at the picture below: In the
Insert picture description herepicture, Y, who is the right child, replaces X, and X becomes his own left child. This is left rotation.

Rotate right:

Rotate the two nodes of the red-black tree clockwise so that the parent node is replaced by its own left child, and you become your own right child. Look at the picture below: In the
Insert picture description herepicture, Y, the left child, replaces X, and X becomes his right child. This is a right rotation.

Insert picture description hereSo, when to use color change and when to use rotation?

The insertion and deletion of the red-black tree contains many situations, and each situation has a different processing method.

Let's give a typical example, everyone will experience:

Let's take the case of inserting node 21 as an example:
Insert picture description herefirst, what we need to do is to change the color of node 25 and the nodes below it:
Insert picture description hereat this time node 17 and node 25 are two consecutive red nodes, then change node 17 Into a black node? I'm afraid it's inappropriate. This not only breaks Rule 4, but according to Rule 2 (the root node is black), it is impossible to turn node 13 into a red node.

Discoloration can no longer solve the problem. We regard node 13 as X and node 17 as Y, and rotate left as in the schematic diagram just now:

Insert picture description hereInsert picture description hereInsert picture description hereSince the root node must be a black node, it needs to change color. The color change result is as follows: Is
Insert picture description herethis the end? not at all. Because the number of black nodes in the two paths (17 -> 8 -> 6 -> NIL) is 4, the number of black nodes in the other paths is 3, which does not meet the rule 5.

At this time, we need to regard node 13 as X and node 8 as Y, and rotate to the right as in the diagram just now:
Insert picture description hereInsert picture description hereInsert picture description hereFinally, change the color according to the rules: in
Insert picture description herethis way, our red-black tree becomes compliant with the rules again. The adjustment process of this example is more complicated and has gone through the following steps:

Color change -> Rotate left -> Color change -> Rotate right -> Color change

Three. Application scenarios of red-black trees

There are many applications of red-black trees. Among them, the collection classes TreeMap and TreeSet of JDK are implemented by red-black trees. In Java 8, even HashMap also uses red-black trees.

A few notes:

1. Regarding the adjustment of the red-black tree self-balance, there are many situations involved when inserting and deleting nodes. Due to space reasons, it is impossible to list them one by one. Friends who are interested can refer to Wikipedia, which is very clear.

2. The above example of the red-black tree adjustment process is a more complicated situation, and the small partners who don't understand too clearly don't have to be in the corner. The key is to understand the main idea of ​​red-black tree self-balance adjustment.

Guess you like

Origin blog.csdn.net/gaolh89/article/details/104400606