Red-black tree structure and algorithm implementation

Red-black tree structure

Number of red and black (Red-black tree) is a self-balancing binary search tree, as shown below:
Here Insert Picture Description
conditions need to be met first red and black number is a binary search tree, to increase its own rules on this basis.

Binary search tree rules

  1. If the left subtree of any node is not null, then the value of the left sub-tree, all the nodes are less than the value of its root
  2. If the right subtree of any node is not empty, then the right sub-tree nodes are all values ​​greater than the value of its root
  3. Any node of the left and right subtrees are also binary search tree
  4. No equivalent key node

Regular red-black tree

  1. Node is red or black.
  2. The root node is black.
  3. Two children each red node are black. (Red you can not have two consecutive nodes on all paths from the root to each leaf)
  4. From any node to all leaves of each path contains the same number of black nodes.

AVL trees and red-black tree is more versatile because they look fast but inserted AVL slow, red-black tree to find the insertion are very good.

Red-black tree algorithm

Rotation algorithm

Note: A, B, C have child nodes are possible.

Left-handed
Here Insert Picture Description
right-handed
Here Insert Picture Description

Insertion algorithm

The overall steps are as follows:

  1. The first step: the red-black tree as a binary search tree, the node is inserted.
  2. Step two: colored insert node is "red."
  3. Third step: rotation by the operation of a series or colored, so that once again become a red-black tree .

Insert node red reason: the inserted node colored red, will not violate "feature (5)"! Contrary to a few characteristics, it means that we need to deal with the less.

Steps adjustments:

  1. If the insert is the root node, this node directly painted black.
  2. If the parent node is inserted node is black, nothing needs to be done.
  3. If the parent node is inserted node is red, if the uncle node is red, the parent node becomes black and uncle, grandfather node turns red, as the current node to node grandfather, continue to adjust.
  4. If the parent node is inserted node is red, the node is black uncle. Divided into four cases.

details as following:

  1. The current node is the father left the child, the father left the child's grandfather (Left-Left), deal with ideas: a grandparent will be right-handed; b color exchange of parent and grandparent...
  2. The current node is the right child of the father, the father left the child's grandfather (Right-Left), deal with ideas: a parent node L, and the parent node as the current node; b and then use the Left Left situation.
  3. The current node is the right child of the father, the father is the grandfather of the right child (Right-Right), deal with ideas: a grandparent will be left; b color exchange of parent and grandparent.
  4. The current node is the father left the child, the father is the grandfather of the right child (Left-Right), deal with ideas: a right-handed parent node, and the parent node as the current node; b then use the Right Right situations.

Insertion algorithm exemplary adjustment process

Adjust the red-black tree three cases

  1. The root node is inserted: this node directly painted black.
  2. Is inserted into the parent node is black: nothing needs to be done. After the node is inserted, it is still red-black tree.
  3. Is inserted into the parent node is red: need to be adjusted.

The parent node is red in both cases

  1. The parent of the current node is red, and the uncle of the current node is red.
  2. The parent of the current node is red but the uncle of the current node is black.

For the first type: uncle node is red, the following steps:

  1. The "parent", "Uncle node" is set to black.
  2. The "grandparent" to "red."
  3. The "grandparent" to "current node" (newly inserted node), then continue on the "current node" operation.

Below, the newly inserted node 35, the need for grandparent node 60 continues operation.
Here Insert Picture Description
The parent node is red, the node is black uncle

Divided into four cases:

  1. The current node is the father left the child, the father left the child's grandfather (Left-Left), deal with ideas: a grandparent will be right-handed; b color exchange of parent and grandparent...
  2. The current node is the right child of the father, the father left the child's grandfather (Right-Left), deal with ideas: a parent node L, and the parent node as the current node; b and then use the Left Left situation.
  3. The current node is the right child of the father, the father is the grandfather of the right child (Right-Right), deal with ideas: a grandparent will be left; b color exchange of parent and grandparent.
  4. The current node is the father left the child, the father is the grandfather of the right child (Left-Right), deal with ideas: a right-handed parent node, and the parent node as the current node; b then use the Right Right situations.

For the first two kinds: uncle node is black, and the current node is the right child of its parent node, follow these steps:

  1. The "parent" as a "new current node."
  2. The "new current node" as the fulcrum left. (Fulcrum A node corresponding to the upper left image)
  3. Continue to check and adjust.

Below, we need to node 60 is operated by the previous step
Here Insert Picture Description

For the first three kinds: uncle node is black, and the current node is the left child of its parent node, follow these steps:

  1. The "parent" to "black."
  2. The "grandparent" to "red."
  3. In "grandparent" for right-handed as a fulcrum. (Fulcrum A node corresponding to the upper right-hand picture)
  4. Continue to check and adjust.

Here Insert Picture Description

Deletion algorithm

https://www.jianshu.com/p/84416644c080
delete the following general steps:

  1. The first step: the red-black tree as a binary search tree, the node will be deleted.
  2. If the node is a leaf node is deleted, and the red, it is deleted directly.
  3. If the node is a leaf node is removed, and is black, this case needs to be adjusted, to delete a node, the node replace it with nil, nil node for balancing operations (detailed below).
  4. If the node is removed only a leaf node, then it must be a black node and its child nodes are red (red-black tree characteristics of the decision). This time to the parent with the child node delete nodes, and the color black child node, the number of black guarantee.
  5. Minimum node has two child nodes, the delete node found right subtree (successor node), and then replace the deleted node. The original position of the successor node as the current node because the node successor must be a leaf node or only a subtree, go to 2,3,4 handle the situation.

For the first three cases, need to be balanced operation, we will

  1. The current node (nil) is the root, do not operate.
  2. The current node (nil) sibling is black, if the child node of all black sibling, parent node is red: switching node father and siblings color, balance ends.
  3. The current node (nil) sibling is black, if the child node of all black sibling, parent node is black: the sibling painted red, father node as the current node to continue.
  4. The current node (nil) sibling is black, if the child nodes are not all black sibling, siblings on the left, the left subtree is red: right-handed to the father node, switching node father and siblings color, the sibling the left subtree painted black, ending balance.
  5. The current node (nil) sibling is black, if the child nodes are not all black sibling, siblings right, its right subtree is red: right-handed to the father node, switching node father and siblings color, the sibling right subtree painted black, ending balance.
  6. The current node (nil) sibling is black, if the child node siblings are not all black, siblings on the left, the left subtree is black: the sibling left, switching sibling and sibling nodes in the right subtree color conversion 4th in the case.
  7. The current node (nil) sibling is black, if the child nodes are not all black sibling, siblings right, its right child is black: the sibling right-handed, left subtree exchange color sibling and sibling nodes, in the case of conversion to 5.
  8. The current node (nil) sibling is red, if the left sibling node to the parent node right-handed, switching node father and siblings color, converted to black sibling situation.
  9. The current node (nil) sibling is red, if siblings on the right, left to the parent node, switching node father and siblings color, convert case of sibling nodes is black.

Here Insert Picture Description

Here Insert Picture Description
The first step to delete the following circumstances:

  1. Is deleted node has no son, that is a leaf node, the direct delete nodes.
  2. Only one son node is deleted, delete the node, and replace it with a unique location child node of the node.
  3. If the node has a child about to be deleted, first find the smallest node right subtree delete node (successor node), and then replace the deleted node.

DETAILED FIG follows:
Here Insert Picture Description

Step adjustment tree in the following case

  1. Node is red, no need to adjust, as long as the color replacement node to node to remove color to re-balance.
  2. Node is a black colorant necessary to rotate

Original link:
https://blog.csdn.net/yy_0733/article/details/95164590
https://www.jianshu.com/p/7b38abfc3298?utm_source=oschina-app
https://www.cnblogs.com/nananana /p/10434549.html

Operation example red-black tree

1st For the first element is inserted into the empty red-black tree, red insertion node, the second breach of the provisions, make color operation, the node changes to black, as shown below.
Here Insert Picture Description

2nd further inserted two child nodes, operating without any discoloration, as shown below.
Here Insert Picture Description

3rd into the left child node D to node B, inserted because the parent node B and C uncle new node (the grandfather of another child node node) are also red, in violation of Article III, the easiest way is to the parent node B and node C uncle to black, as shown below.
Here Insert Picture Description

4th inserted to the left child node D E, insert the first insertion node E is red, its parent node D is red, and the newly inserted E is left child of its parent node D, the solution is, it first parent node D becomes black, grandparent node B turns red, and then rotated to the right to grandparent node B as a fulcrum, as shown in FIG.
Here Insert Picture Description
5th to the Node B is inserted into the right child node F., His father and uncle node E, the node B are red, the parent node B and node E is changed to black uncle and grandparent node D turns red, as shown.
Here Insert Picture Description

Sixth plurality inserted behind to facilitate observation of several node, as shown below.
Here Insert Picture Description

7th inserted into the left child node G to K, uncle node and its parent node are red, and thus the parent node becomes black uncle and grandparent node turns red, as follows from FIG. 1 becomes 2. Thus B and D would conflict is red, then B is regarded as the newly inserted node, the parent node B is red, black uncle node, and B is the right child of its parent node, as a fulcrum to D rotated to the left, as in FIG. 3 becomes 4 (blue arrow indicates the position does not mean the transfer is connected to the node). Next, B and D are still red conflict, D will be looking at the newly inserted node, its parent node B is red, black uncle node C, and is the left child of its parent node B, so will its parent node B turns black, grandparent node a turns red, and the a node as a fulcrum of rotation to the right, into the following 5 6 7 becomes again, so that the entire red-black tree is balanced.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Operation Reference: https: //blog.csdn.net/duduhonghong/article/details/83860904
portion reference: https: //www.cnblogs.com/2sheep2simple/p/10780560.html

Published 67 original articles · won praise 32 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_43751710/article/details/104637909