Data structure (61): Take you to insert and delete the red-black tree (detailed graphic explanation)

table of Contents

1. The definition and nature of red-black trees

2. Insert

2.1, the red-black tree is an empty tree

2.2. The key inserted into the node already exists in the red-black tree.

2.3. The parent node of the inserted node is black

2.4. The parent node of the inserted node is red, and the uncle node exists and is red

2.5. The parent node is red, the uncle node does not exist or is black, the current node and the parent node are both left child nodes

2.6. The parent node is red and the left child node, the uncle node does not exist or is black, and the current node is the right child node

2.7. The parent node is red, the uncle node does not exist or is black, and the current node and parent node are both right child nodes

2.8. The parent node is red and is the right child node, the uncle node does not exist or is black, and the current node is the left child node

3. Delete 

3.1, the replacement node is red

3.2. The replacement node is black, the left child node, the sibling node is black, the right child node of the sibling node is red, and the left child node has any color

3.3. The replacement node is black, the left child node, the sibling node is black, the right child node of the sibling node is black, and the left child node is red

3.4. The replacement node is black, the left child node, the sibling node is black, and the left and right child nodes of the sibling node are black

3.5. The replacement node is black, the left child node, and the sibling node is red

3.6. The replacement node is black, the right child node, the sibling node is black, the left child node of the sibling node is red, and the right child node has any color

3.7. The replacement node is black, the right child node, the sibling node is black, the left child node of the sibling node is black, and the right child node is red

3.8. The replacement node is black, the right child node, the sibling node is black, and the left and right child nodes of the sibling node are black

3.9. The replacement node is black, the right child node, and the sibling node is red


1. The definition and nature of red-black trees

The red-black tree is a binary search tree in which each node has a color attribute, and the color is red or black. In addition to the general requirements of binary search trees, the following additional requirements are added for any effective red-black tree:

  1. The nodes are red or black.
  2. The root is black.
  3. All leaves are black (the leaves are NIL nodes).
  4. Each red node must have two black child nodes. (There cannot be two consecutive red nodes on all paths from each leaf to the root.)
  5. All simple paths from any node to each of its leaves contain the same number of black nodes.

 

These constraints ensure the key characteristics of red-black trees: the longest possible path from root to leaf is no more than twice as long as the shortest possible path. The result is that the tree is roughly balanced. Because the worst-case time for operations such as inserting, deleting, and looking up a value is required to be proportional to the height of the tree, this theoretical upper limit on height allows red-black trees to be efficient in the worst case, and is different from ordinary Binary search tree.

To know why these properties ensure this result, it is enough to note that property 4 results in a path that cannot have two adjacent red nodes. The shortest possible path is all black nodes, and the longest possible path has alternating red and black nodes. Because all the longest paths have the same number of black nodes according to property 5, this indicates that no path can be more than twice as long as any other path.

In the representation of many tree data structures, a node may have only one child node, and the leaf nodes contain data. It is possible to use this paradigm to represent red-black trees, but this will change some properties and complicate the algorithm. For this reason, we use "nil leaf" or "null leaf" in this article. As shown in the figure above, it does not contain data and only serves as an indication that the tree ends here. These nodes are often omitted in the drawing, causing these trees to seem to contradict the above principles, but in fact they are not. The conclusion related to this is that all nodes have two child nodes, although one or two of them may be empty leaves.

Because each red-black tree is also a specialized binary search tree, the read-only operation on the red-black tree is the same as the read-only operation on the ordinary binary search tree. However, inserting and deleting operations on the red-black tree will result in no longer conforming to the nature of the red-black tree. Restoring the nature of the red-black tree requires a small amount of color changes (actually very fast) and no more than three tree rotations (two for insert operations). Although inserting and deleting are very complicated, the operation time can still be kept as few times.

2. Insert

We first increase the node with the binary search tree method and mark it as red. (If it is set to black, it will result in a path from the root to the leaf, and an extra black node. This is difficult to adjust. But after setting it to a red node, it may cause two consecutive red nodes to appear. Conflict, then you can adjust it through color transformation and tree rotation) The next operation depends on the colors of other neighboring nodes.

Like the human family tree, we will use the uncle node to refer to the sibling node of a node's parent node. note:

  • Property 1 and property 3 are always maintained.
  • Nature 4 is only threatened when adding red nodes, redrawing black nodes to red, or rotating.
  • Property 5 is only threatened when adding black nodes, redrawing red nodes as black, or rotating.

In the following example, the node to be inserted is marked as N, the parent node of N is marked as P, the grandparent node of N is marked as G, and the uncle node of N is marked as U. Any color shown in the figure is either an assumption made by its situation, or it is implied by the assumption.

Through the following functions, you can find the uncle and grandfather of a node.

node* grandparent(node* n)
{
    return n->parent->parent;
}

node* uncle(node *n)
{
    if(n->parent == grandparent(n)->left)   //如果父节点是左节点
        return grandparent(n)->right;
    else 
        return grandparent(n)->left;
}

2.1, the red-black tree is an empty tree

Method: draw the current node as black and insert it as the root node,

2.2. The key inserted into the node already exists in the red-black tree.

Method: Set U to the color of the current node, and then update the value of the current node to the value of the inserted node.

2.3. The parent node of the inserted node is black

 Method: Because the inserted node is red, just insert it directly.

2.4. The parent node of the inserted node is red, and the uncle node exists and is red

2.5. The parent node is red, the uncle node does not exist or is black, the current node and the parent node are both left child nodes

2.6. The parent node is red and the left child node, the uncle node does not exist or is black, and the current node is the right child node

2.7. The parent node is red, the uncle node does not exist or is black, and the current node and parent node are both right child nodes

2.8. The parent node is red and is the right child node, the uncle node does not exist or is black, and the current node is the left child node

3. Delete 

We refer to the node to be deleted as the target node. When we delete this node, we need to have a node to be added to the position of the deleted node (except for leaf nodes).

By default, we use the smallest node (substitution node) in the right subtree of the target node to cover the target node (of course, the largest node in the left subtree can also be used), and we only need to delete the replacement node after the coverage is completed. Now the problem is simplified to delete leaf nodes (because the smallest node of the subtree must be a leaf node).

Before deleting, let’s make a provision for the default name

The following specifically introduces the centralized situation of delete operations:

3.1, the replacement node is red

3.2. The replacement node is black, the left child node, the sibling node is black, the right child node of the sibling node is red, and the left child node has any color

3.3. The replacement node is black, the left child node, the sibling node is black, the right child node of the sibling node is black, and the left child node is red

3.4. The replacement node is black, the left child node, the sibling node is black, and the left and right child nodes of the sibling node are black

3.5. The replacement node is black, the left child node, and the sibling node is red

3.6. The  replacement node is black, the right child node, the sibling node is black, the left child node of the sibling node is red, and the right child node has any color

3.7. The replacement node is black, the right child node, the sibling node is black, the left child node of the sibling node is black, and the right child node is red

3.8. The  replacement node is black, the right child node, the sibling node is black, and the left and right child nodes of the sibling node are black

3.9. The replacement node is black, the right child node, and the sibling node is red

Delete the red-black tree After reading all these situations, have you found a common rule, that is, "borrow", "borrow from brothers"> "borrow from brothers and children"> "borrow from ancestors", "borrow" red nodes to Reach the balance after deletion. 

Guess you like

Origin blog.csdn.net/weixin_40179091/article/details/110912990