[Data structure and algorithm] Implementing red-black tree

1. Five rules of red-black trees

In addition to complying with the basic rules of binary search trees, red-black trees also add the following features:

  • Rule 1: Nodes are red or black;
  • Rule 2: The root node is black;
  • Rule 3: Each leaf node is a black empty node (NIL node);
  • Rule 4: The two child nodes of each red node are black (it is impossible to have two consecutive red nodes on all paths from each leaf to the root);
  • Rule 5: All paths from any node to each of its leaf nodes contain the same number of black nodes;

Insert image description here

Relative balance of red and black trees

The constraints of the previous five rules ensure the following key characteristics of red-black trees:

  • The longest pathfromroot to leaf node , will not exceed the shortest pathtwice;
  • The result is that this treeis basically balanced;
  • Although it does not achieve absolute balance, it can ensure that the tree is still efficient in the worst case;

Why can it be achievedthat the longest path is no more than twice the shortest path?

  • Property 4 determines that there cannot be two connected red nodes on the path;
  • Therefore, the longest path must be formed by alternating red nodes and black nodes;
  • Since the root node and leaf nodes are both black, the shortest path may all be black nodes, and the longest path must have more black nodes than red nodes;
  • Property 5 determines that there are the same number of black nodes on all paths;
  • This shows that no path is more than twice as long as any other path.

2. Three transformations of red-black trees

When a new node is inserted, it is possible that the tree is no longer balanced. The tree can be balanced in three ways:

  • change color
  • Left rotation
  • Right rotation;
2.1.Discoloration

In order to re-comply with the rules of the red-black tree, it is necessary to change the red node into black a>, or change the black node to red;

Insertednew nodes are usuallyred nodes :

  • When the inserted node isred, in most cases it is not violated Any rules for red-black trees;
  • And inserting a black node, will inevitably lead to one more black node on a path. a>, this is difficult to adjust;
  • Although red nodes may lead tored-red connections, this situation can be resolved throughcolor Transpose and rotate to adjust;
2.2.Left rotation

Taking node The position of the left child node is replaced by the parent node;

Insert image description here

Detailed explanation:

As shown in the picture above, after left rotation:

  • Node X replaces the original position of node a;
  • Node Y replaces the original position of node X;
  • Node X'sLeft subtree a is still node X'sLeft subtree (Here, the left subtree of
  • node Y'sright subtree c is still node Y'sright subtree;
  • Point Y directionZozishu b directionleft flat shiftachievement point

In addition, the binary search tree is still a binary search tree after left rotation:

Insert image description here

2.3.Right rotation

Taking node The position of the right child node is replaced by the parent node;

Insert image description here

Detailed explanation:

As shown in the picture above, after right rotation:

  • Node X replaces the original position of node a;
  • Node Y replaces the original position of node X;
  • node X'sright subtree a is still node X'sright subtree (Although the right subtree of
  • The left subtree of node YLeft subtree b is still the left subtree of node YLeft subtree;
  • The right subtree **c of node Y is translated to the right ** and becomes the node XLeft subtree;

In addition, the binary search tree is still a binary search tree after right rotation:

Insert image description here

3. Insertion operation of red-black tree

First of all, it is necessary to make it clear that the newly inserted node must bea red node while ensuring that the five rules of the red-black tree are met.

For the convenience of explanation, the following four nodes are specified: the newly inserted node is N (Node), and the parent node of N is (Grandpa), as shown in the figure below: G (Uncle), U’s The parent node is U (Parent), P’s sibling node isP

Insert image description here

3.1.Case 1

When the new node N inserted is at the root of the tree, it has no parent node.

In this case, you only need to change the red node into a black node to satisfy rule 2.

Insert image description here

3.2.Case 2

The parent node P of the new boundary point N is a black node, and no changes are required at this time.

At this time, both Rule 4 and Rule 5 are satisfied. Although the new node is red, the new node N has two black nodes NIL, so the number of black nodes on the path leading to it is still equal, so rule 5 is satisfied.

Insert image description here

3.3.Case 3

Node P is red, and node U is also red. At this time, node G must be black, that is,father red, uncle red, ancestor black.

In this case you need:

  • First, change the parent node P to black;
  • Then change the uncle node U to black;
  • Finally, the grandparent node G turns red;

becomesfather black uncle black ancestor red, as shown in the figure below:

Insert image description here

Problems that may arise:

  • The parent node of N's grandparent node G may also be red, which violates rule 4. In this case, the node color can be adjusted recursively;
  • When the recursive adjustment reaches the root node, it needs to be rotated, as shown in node A and node B in the figure below. The specific situation will be introduced later;

Insert image description here

3.4.Case 4

Node P is a red node, node U is a black node, and node N is the left child node of node P. At this time, node G It must be a black node, that is, father red, uncle black and ancestor black.

In this case you need:

  • Change color first: change the parent node P to black, and change the grandparent node G to red;
  • Post-rotation: perform right rotation with the grandparent node G as the root;

Insert image description here

3.5.Case 5

Node P is a red node, node U is a black node, and node N is the right child node of node P. At this time, node G It must be a black node, that is, father red, uncle black and ancestor black.

In this case you need:

  • First perform a left rotation with node P as the root. After the rotation, as shown in Figure b;
  • will then rednodesP and Red, the newly inserted N1Node Red viewed as a wholeBNodeBlackN is seen asrednode P1 As shown in Figure c. At this point the whole situation changes to situation 4.

Insert image description here

Then you can handle it according to situation 4:

  • Change color first: change the parent node P1 of the N1 node to black, and change the grandparent node G to red;
  • Post-rotation: Right rotation with the grandparent node G as the root, as shown in Figure e after rotation;
  • Finally, nodes N1 and P1 are transformed back to complete the insertion of node N, as shown in Figure f;

Insert image description here

3.6.Case

Insert nodes in the binary tree sequentially: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.

If you directly use an ordinary binary search tree, after all nodes are inserted, it will look like this:

Insert image description here

is a seriousimbalanced tree, which is equivalent to a linked list and cannot reflect the high efficiency of a binary search tree. Inserting nodes according to the five rules of red-black trees can ensure that the search binary tree is a balanced tree to the greatest extent. The following is a detailed explanation of the process:For convenience of explanation, some leaf nodes (NIL) of the red-black tree are omitted

Insert 10

SignContext 1:

  • insert node 10;
  • Change the color of node 10 to black;

Insert image description here

Insert 9

SignInformation 2

  • No changes are required;

Insert image description here

Insert 8

How to quickly determine whether it is case 3 or case 4:

Starting from the newly inserted node N, the four nodes passed by the arrow as shown in the figure are red, red, black and red 3 red nodes It is case 3. If there are red, red, black and black two red nodes, it is case 4;

Insert image description here

SignCircumstances 4

  • The parent node 9 turns black, and the grandparent node 10 turns red;
  • Rotate right with the grandparent node as the root;

Insert image description here

Insert 7

SignCircumstances 3

  • Parent node 8 and uncle node 10 turn black, and grandparent node 9 turns red;
  • A problem will arise at this time: it does not comply with rule 2, that is, the root node is not black. At this time, the binary search tree with 9 as the root node can be regarded as a whole as a newly inserted node N, and at this time it is consistent with the situation. 1, just change 9 back to black.

Insert image description here

Insert 6

SignCircumstances 4

  • Parent node 7 turns black, and grandparent node 8 turns red;
  • Perform right rotation with grandparent node 8 as the root;

Insert image description here

Insert 5

SignCircumstances 3

  • Parent node 6 and uncle node 8 turn black, and grandparent node 7 turns red;

Insert image description here

Insert 4

SignCircumstances 4

  • Parent node 5 turns black, and grandparent node 6 turns red;
  • Perform right rotation with grandparent node 6 as the root;

Insert image description here

Insert 3

First transformation: consistent withCase 3:

  • Parent node 4 and uncle node 6 turn black, and grandparent node 5 turns red;

After the transformation, it was found that 5 and 7 were two connected red nodes, so the entire subtree rooted at 5 was regarded as a newly inserted node N1, and the second transformation was performed.

Insert image description here

Second change: SignInformation 4:

  • Parent node 7 turns black, and grandparent node 9 turns red;
  • Perform right rotation with grandparent node 9 as the root;

Insert image description here

Finally, N1 is restored to obtain the transformed red-black tree:

Insert image description here

Insert 2

SignCircumstances 4

  • Parent node 3 turns black, and grandparent node 4 turns red;
  • Perform right rotation with grandparent node 4 as the root;

Insert image description here

Insert 1

First transformation: consistent withCase 3:

  • Parent node 2 and uncle node 4 turn black, and grandparent node 3 turns red;

After the transformation, it is found that 3 and 5 are two connected red nodes, so the entire subtree with 3 as the root is regarded as a newly inserted node N1, and the second transformation is performed.

Insert image description here

Second change: SignInformation 3:

  • Parent node 5 and uncle node 9 turn black, and grandparent node 7 turns red; that is, picture b -> picture c.

After the transformation, it is found that the red color of root node 7 does not comply with rule 2, so the red-black tree with 7 as the root node is regarded as a newly inserted node N2, and the third transformation is performed.

Third change: SignInformation 1:

  • Just change root node 7 to black.

Insert image description here

Thus, the insertion of nodes 1~10 is completed. Although case 5 is not encountered, case 5 can be converted to case 4 through a left rotation operation. The principle is the same. As shown in the figure below, after completing the NIL of the leaf nodes of this red-black tree, it has been tested to meet the five rules of the red-black tree and basically belongs to a balanced tree, higher efficiency.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_46862327/article/details/134919031