Red-black tree (RB tree) principle Detailed graphics

Studied data structure are aware of the concept of a binary tree, but there are many more common binary tree types, such as complete binary tree, full binary tree, binary search tree, balanced binary tree, binary, etc. perfect; we are saying today is that red-black tree is a non-strict balanced binary tree, balanced binary tree is increased on the basis of binary search tree on automatically to maintain the balance of nature, insert, search, delete the relatively high efficiency. Red-black tree is the cornerstone TreeMap storage structures.

A. Binary search tree

Binary search tree called a binary search tree, binary sort tree, we look at a typical binary search tree, what is this binary regular feature of it?

  1. Left subtree node is less than the node itself;
  2. Right subtree node is greater than the node itself;
  3. About the same as the sub-tree binary search tree;

The figure is a typical binary search tree

Binary search tree is the basis of a balanced binary tree, we look at how its search step

We want to find the value of the binary tree 58 nodes

Step 1: Find the root node, the value of the node 60

Step Two: To compare the value we're looking for the size of the node 58

If equal, then congratulations, have been found;

If less than, keep looking left subtree;

If so, then find the right subtree;

Obviously 58 <60, we find the node in the left subtree 56, at this time we have to locate the node 56

The third step: the second step in accordance with the rules keep looking

58> 56 that we need to continue to look for the right sub-tree, navigate to the right child node 58, congratulations, this time we have found.

We go through three steps have been found, in fact, is what we usually call binary search, binary search tree that seems to find very efficient, but equally it has a flaw, such as the following binary search tree.

To see such a binary search tree if awkward, lame typical big legs, but it is also a binary search tree, if we are looking for a value of 50 nodes, and a single list query is basically not much difference, the performance greatly reduced. This time we balanced binary tree on on stage, on the basis of a balanced binary tree is a binary search tree on added automatically to maintain the balance of nature.

Above big legs lame binary search tree after the self-balancing, it might become such a binary tree below.

After the self-balancing, then go to the node value of 50, the lookup performance will improve a lot. Red-black tree that is not strictly a balanced binary search tree.

II. Regular features red-black tree

What rules are there specific characteristics of red-black tree it?

  1. Nodes are divided into red or black;
  2. Root node must be black;
  3. Leaf nodes are black, and is null;
  4. Two child nodes connected to the nodes are black red (red-black tree nodes does not appear adjacent red);
  5. From any node to each leaf node of the path contains the same number of black nodes;
  6. The new node is added to the red-black tree node;

Rule looked like a lot, yes, because the red-black tree is a balanced binary tree, you need to automatically maintain the balance of nature, self-sustained rules balance the need to have the top six red-black tree is given

We take a look at a typical red-black tree in the end What is it like?

First interpretation of the rules a little, in addition to literally see the meaning, but also hidden what does it mean?

First, from the root node to the leaf node of the path is not greater than the longest shortest path twice

  How to calculate the path of the shortest path?

  From the rule 5, we know that the number of black nodes from the root node to each leaf node is the same, then the path of pure black nodes is the shortest path;

  What kind of path considered the longest path?

  The rules 4 and 3, if the red node, black node must have a connection, when the number of black and red nodes are the same nodes, the path is the longest, i.e. black node (or nodes red) * 2

Second. Why is newly added to the red-black tree node to node red

  Know from Rule 4, the current red-black tree in the number of black nodes from the root node to each leaf node is the same, at this time if new black node, it certainly break the rules, but not necessarily join the red node, unless it red is the parent node, thus adding a red node, the smaller the possibility of breaking the rules, let's also an example to illustrate.

In any case, the structure will be red-black tree destroy it? Then how to maintain the balance destroyed, mainly two ways to maintain the balance [ discoloration ] and [ rotation ], [ rotation ] divided [ L ] and [ right hand ], two methods may be combined with each other.

Here we insertions and deletions from the two scenarios to illustrate

III. Red-black tree node is inserted

When we in the insertion node is 66, into a red-black tree so

Obviously, this structure is still time to follow the rules of the six large, self-balancing mechanism to adjust without starting node equilibrium state;

If the value is again inserted inside it a node 51, this time into a red-black tree so

It is clear now that do not follow the rules of structure 4, and this time you need to start the self-balancing mechanism to adjust the balance node

3.1 color

We can change color by the way, the red-black tree structure to meet the rules

  • First address configuration does not follow this rule 4 (red connected node, the node 49-51), node 49 will need to black;
  • At this point we find that it violates the rule 5 (56-49-51-XX path black node than any other path), then we will be changed to red node node 45;
  • Haha, sister, but also a violation of Rule 4 (red nodes connected to the node 56-45-43), then we will node 56 and node 43 to a black node;
  • But we found at this time and in breach of Rule 5 (black node 60-56-XX path than the black node 60-68-XX), so we need to adjust node 68 is black;
  • carry out! !

After the final adjustment of tree:

But not so lucky at all times, can be reached directly through the color on purpose, most of the time also need to be addressed by rotation.

As based on the following tree, node 65 is added.

The following steps after the insertion node 65

This time, you will find that for node node 64 either red or black node, will be in violation of rule 5, in the path of a black node has failed to reach an agreement, this time by only [color] has been unable to reach the goal. We need to rotate the operation, of course, [the rotation] operations generally need to operate with [color].

Rotation including [ left ] and [ right-handed ],

L:

Counterclockwise rotation of the two nodes, so that a node is replaced by its right child node, while the node becomes the right child of the left child

L steps are as follows:

First breaks the relationship with the right child node G PL, while the reference points to the node C2 its right child node; and breaks the relationship with the left child node G and C2, while the application point to the left child node G of the PL

D-:

Clockwise rotation of the two nodes so that a node is replacing its left child node, while the node becomes the right child of the left child node

Dextrose steps are as follows:

First breaks the relationship with the left child node G PL, while the reference points to the node C2 of the left child node; and breaks the relationship PL and the right child node C2, while the application point PL of the right child node of G

The scene can not be rotated by discoloration divided into the following four categories:

3.2 Zuozuo node rotation

In this case, the parent node and the nodes are inserted left node, as shown below ( rotate original FIG. 1 below) such a case, we have to insert nodes 65

Rules are as follows: to grandparent [right-handed], with [color]

As a rule, the following steps:

Node rotating around 3.3

In this case, the parent node is the left node, the node is inserted into the right node, in the original rotation Figure 1, we want to insert node 67

Rules are as follows: Fathers node [L], then [dextrose] grandparent node, with [color]

As a rule, the following steps:

3.4 Right Left rotation nodes

In this case, the right node is the parent node, the node is the left node is inserted, as shown below ( rotate original FIG. 2 ) In this case, we have to insert nodes 68

Rules are as follows: Fathers [dextrose] node, grandparent node then [L], with [color]

As a rule, the following steps:

Right Right rotation node 3.5

In this case, the parent node and right node nodes are inserted, FIG. 2 in the rotation of the original, we have to insert nodes 70

Rules are as follows: to grandparent [L], with [color]

As a rule, the following steps:

3.6 red-black tree insert summary

  No need to adjust [Color] to achieve a balance [Color] + rotation available balance
Case 1: When the child node is inserted into the parent node is black Empty tree of the root node, the root node red to black The parent node is red left node, the node is black uncle, insert left child, then rotate [node] or so by
Case 2: - Parent and uncle red nodes The parent node is red left node, the node is black uncle, into the right child node, then the node [about] by rotation
Case 3: - - The parent node is red right node, the node is black uncle, insert left child, then rotate the right and left [node] by
Case 4: - - Red right node parent node, black node uncle, into the right child node, right node] [swing right through

IV. Red-black tree node deletion

Compared to the red-black tree node insertion, deletion node is more complex, we have to discuss whether the child node is null and red for thinking dimension.

At least one sub-section 4.1 is null

When the child node to be deleted node has at least one node is null, after you remove the node, the node will have its value instead of the current node can be, if all is null, then the current node is set to null, of course, if the violation the rules, the adjustment is needed, such as [], and [discoloration] rotation.

4.2 child nodes are non-null node

In this situation,

The first step: to find a successor or predecessor of the node

Precursor: left subtree maximum value of the node (which can be obtained at most a non-null child node, probably as null);

Successor: the minimum value of the right subtree node (which can be drawn up to only one non-null sub-nodes are likely null);

Predecessor and successor node is the value closest to the value of the node, like the node .prev = predecessor, the node .next = successor.

Step two: the value of the copy predecessor or successor to the node, then delete successor or predecessor

If you delete a node is left, then the value is copied to the predecessor node, and then delete the precursor; if you remove that right node, copy the value to the subsequent node, and then delete successor;

This is equivalent to a "tricky" approach, our aim is to delete a node value of the node is not present in the red-black tree, so focus on the purpose, we are not that concerned about whether we really want to delete Delete Nodes node, we also need to consider changes in the tree structure, as will frequently because the automatic balancing mechanism to adjust the structure of the tree itself.

We've already said, we want to remove is actually a predecessor or successor, so we have to explain the precursor to the main line, subsequent learning can refer precursors, including several cases

4.2.1 predecessor node is black and has a non-null child node

analysis:

Because you want to delete is left node 64, to find precursors of the node 63;

Then the value of the precursor 63 replacement value 64, and the two values of nodes (node to be deleted and precursors) are to be deleted node 63;

Delete precursor 63, when the middle part of the process to be on the map, but we found that it does not comply with Rule 4 red-black tree, hence the need for automatic balance adjustment;

Here directly through the [ color ] to complete.

4.2.2 precursor for the black nodes, and child nodes are null

analysis:

Because you want to delete is left node 64, to find precursors of the node 63;

Then the value of the precursor 63 replacement value 64, and the two values of nodes (node to be deleted and precursors) are to be deleted node 63;

Delete precursor 63, when the middle part of the process to be on the map, but we found that it does not comply with Rule 5 red-black tree, hence the need for automatic balance adjustment;

Here directly through the [ color ] to complete.

4.2.3 precursor red node, while the child node is null

analysis:

Because you want to delete is left node 64, to find precursors of the node 63;

Then the value of the precursor 63 replacement value 64, and the two values of nodes (node to be deleted and precursors) are to be deleted node 63;

Delete precursor 63, the tree structure did not break the rules.

4.3 red-black tree delete summary

Red-black tree delete case of more, but also the following conditions exist:

Delete is the root node, then directly to the root node is set to null;

Left and right child nodes are to be deleted node is null, the delete node is set to null;

Left and right child nodes to be deleted node has a value, then there is a node to replace the node value;

Left and right child nodes of the node to be deleted is not null, then find the successor or predecessor, the value of the copy predecessor or successor to the node, then remove the predecessor or successor;

Way can cause an imbalance of the red-black tree nodes removed, then we need to pass [color] + [rotation] to adjust, to balance, but also gives the example above, suggest that you practice a lot, but not down Bibei .

V. summary

This paper describes the relevant principles of the red-black tree, red-black tree first base binary search tree, we briefly talk a bit binary search tree, and talked about the search process, and then for 6 large red-black tree regular feature, red-black tree insertion, deletion, use a lot of graphics to illustrate, technologies are out of practice, sometimes specious many places, when you begin to write, when in fact well understood. Use red-black tree is widely used, such as TreeMap and TreeSet are based on red-black tree implementation, and Jdk8 in HashMap when the list will be converted to a length greater than 8 red-black tree, red-black tree is more complicated, and I was still learning process, if there is something wrong place, please criticism and thank you hope to make progress together.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160024.htm