Data structure ~ red-black tree (characteristics, search, insert, delete)

Preface

Red-black tree is a data structure with excellent search efficiency and construction efficiency.
For example, when the hashmap in java8 is expanding, if the length of the table reaches 64 and the length of the linked list reaches 8, it will be converted to a red-black tree.

Every time new data is inserted, it will change color and rotate to keep the height within a reasonable height of log2(N)

concept

First of all, we must know that the red-black tree is a special kind of binary tree!

The shape is very similar to the AVL tree, but the AVL tree has a balance factor to force balance, while the red-black tree is balanced by some of its own characteristics, including color change and left-handed and right-handed.

rule

(1) Each node is either black or red. And every time a newly inserted node is red.

(2) The root node is black.

(3) Each leaf node (NUIL) is black. [Note: The leaf node here refers to the empty leaf node! ]

(4) If a node is red, its child nodes must be black, which means that two consecutive red nodes cannot appear, but two consecutive black nodes are possible

(5) All paths from any node to the descendants of that node contain the same number of black nodes.

Inquire

The red-black tree query is the same as the ordinary binary search tree query. When I want to check a certain number, I only need to compare the number from the root node. If it is greater than the root node, then continue to compare the root node. The right node, otherwise it is the left node of the root node, and so on.

insert

Before learning how to build a red-black tree, we must master a few basic knowledge!

1. When inserting data, the red-black tree will first traverse where the data should be inserted. The inserted position must be at the bottom , and it is impossible to suddenly insert a value in the middle.

2. The inserted data must be red (because the fifth rule of the red-black tree must be observed, if a branch adds a black node, the rule will be broken)

3. After inserting, in order to meet rule 4, it needs to use color change and left-handed and right-handed operations.

Changing colors means changing red to black and black to red. You only need to change the properties of an object. There is nothing to say.

There are two types of rotation

Left-handed
original appearance:

Insert picture description here
Transforming:
Insert picture description here
Transformation end:
Insert picture description here
Right rotation
Original appearance:
Insert picture description here
Transforming:
Insert picture description here
Transformation end:
Insert picture description here
Therefore, we find that left-rotation and right-rotation are a relative operation.
After we understand all the color changes and left-hand rotation and right-hand rotation, we will look at how the red-black tree actually changes color and rotates according to the actual situation.

Actual insertion analysis

There are roughly four situations:

Case 1: If it is the root node, directly insert it and it is done (insertion is still fixed in red, and then the root directory is set to black at the end of the code)

Case 2: The father of the inserted node is black, the same is true, the insertion is complete, no need to make any changes

Case 3: The father of the inserted node is red, and the color of the uncle node (another child of the grandfather of the inserted node) is also red

Case 4: The father of the inserted node is red, and the uncle node is black (this situation is the most troublesome because it needs to be judged again)

Dealing with different situations

Case 1: This tree does not have any nodes, and the inserted point is the root node. At the beginning, red is inserted, and then it is directly converted to black

Case 2: The parent node is black, insert it directly, without any color change or rotation

Situation 3: The parent node is red, and the uncle node is also red, directly turn the uncle and dad into black, and then change the grandpa to the same red as yourself, and continue to iterate (because it may appear that both grandpa and grandpa are red , Then we must continue to judge which situation)

For example, suppose the newly inserted is M, the father is F, the uncle is U, and the grandfather is G.
Insert picture description here
Case 4: The parent node is red, and the uncle node is black. This is more complicated and can be divided into several situations (in order to distinguish logic, it is divided into Circumstances and conditions).

Situation 1: If the new node is the right child of the father at this time

The method here is to convert status 1 to status 2, which requires rotation, which is to rotate left with the core of its parent node.

For example, 21 elements are newly inserted below.
Insert picture description here
Situation 2: If the new node is the left child of the father at this time

The function of the above situation 1 is to turn it into situation 2, so the solution core of situation 4 is still solved in situation 2.

The solution is to directly dye the parent node of the new node black and the grandfather node red, and then use the grandfather node as the core to rotate left.

As shown below
Insert picture description here

Delete operation (look for heirs)

Inserting a node is divided into two steps. The first step is to determine where the inserted data should be added by the size of the key; the second step is to balance the red-black tree

Deleting a node is also divided into two steps. The first step is to find the node to be deleted through the key, then find the heir of the node, and then delete the heir. The second step is to balance the red-black tree.

There will be 3 situations in total! (In the following, p is used to represent the node to be deleted)

Situation

Case 1: The p node does not have a child node

Case 2: The p node has one and only one child node

Case 3: Both child nodes of p node have

Dealing with different situations

Case 1: Delete the node directly

Case 2: Directly connect the only child node of the node to the parent node of the node, and delete the node

Case 3: Find an inherited node (because the way to find the inherited node results in the inherited node must be case 1 or case 2), write the value of the inherited node to the p node, and process the inherited node according to the case 1 or 2. Just find a scapegoat, hahaha, exchange data, and finally delete the scapegoat, just like deleting a binary search tree.

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/115047196