Red black tree definition
-
The root node is black
-
Nodes are either red or black
-
The path from any node to its descendants contains the same number of black nodes
-
Red nodes, whose children cannot be red
-
All leaf nodes are black. This leaf node refers to a leaf node that is NIL (or NULL) and is usually omitted.
Red black tree coloring, rotation rules
All newly inserted nodes are red except for the first node, which is the initial root, which is black.
-
1 If the parent node is black, insert the node directly
-
2 If the parent node of the new node is red
- 2.1 if
Insert the node
theUncle node
If it is red, proceeddyeing, the parent node and tertiary node becomeblack
, the grandfather node becomesred
Contradictions shift upward.
- 2.2 if
Insert the node
theUncle node
For the black-
2.2.1 If the bias of the inserted node and its parent node is the same as that of the parent node and its grandfather node. In other words, if the inserted node is the right (left) subtree of its parent node, and the parent node is the right (left) subtree of its inserted node’s grandfather, then the bias is consistent. At this point, the first step is to turn the parent node to black and the grandfather node to red. In the second step, the grandfather node is used as the benchmark to turn left.
-
2.2.2 If the bias of the inserted node and its parent node is inconsistent with that of the parent node and its grandfather node. At this point, the first step is node rotation, with the parent node as the benchmark for dextral rotation, so that the three nodes are biased to the same, and then the last biased to the same operation.
-
- 2.1 if
-
Supplement:
- The essence of rotation, in fact, is to align the nodes, all you have to do is think about when is it left, when is it right
- The rules for coloring and rotation serve the definition
- One problem is that staining can cause conflict to move up. What happens when you move up to the root node
Examples of red-black tree insertion
For example, insert int[] a = {10, 18, 16, 12, 13, 17, 15, 14, 9, 8,7}.
- Insert node 10,
The initial node, set as the root node
, the color ofblack
Color.
- Insert node 18 in color
red
Color, because the parent node isblack
Color, directly inserted.
- Insert node 16 in color
red
Color, because the parent node isred
Color, and uncle node isblack
Color (by definitionAll leaf nodes are black
), so it is true2.2.2
Conditions,First rotate, then dye, then rotate
.
- Insert node 12 in color
red
Color, because the parent node isred
Color, and uncle node is alsored
Color, in line with the2.1
Condition, parent node and tertiary node changeblack
Color, grandfather node changesred
Color, because the grandfather node is the root node, so again becomesblack
Color (definition: the root node is black).
- Insert node 13 in color
red
Color, because the parent node isred
Color, and uncle node isblack
With thatInserting a Node 16
The difference is that its bias is consistent with2.2.1
Conditions described are the same, so dye directly in rotation.
- Insert node 17 in color
red
Color, because the parent node isblack
Color, directly inserted.
- Insert node 15 in color
red
Color, because the parent node isred
Color, and uncle node is alsored
Color, in line with the2.1
Condition, parent node and tertiary node changeblack
Color, grandfather node changesred
Color, at this point, the red-black tree equilibrium condition is satisfied, no rotation is required.
- Insert node 14 in color
red
Color, because the parent node isred
Color, and uncle node isblack
Color, and the parent node is biasedDon't agree
. Conform to the2.2.2
Conditions, first rotate, before dyeing, before rotating.
- Insert node 9 in color
red
Color, because the parent node isblack
Color, directly inserted.
- Insert node 8 in color
red
Color, because the parent node isred
Color, and uncle node isblack
Color, and the parent node is biasedconsistent
. Conform to the2.2.1
Condition, first dye, in rotation
- Insert node 7 in color
red
Color, because the parent node isred
Color, and uncle node is alsored
Color, in line with the2.1
Condition, parent node and tertiary node changeblack
Color, grandfather node changesred
Color. Because node 9 and node 12 are in a continuous state, they do not meet the definition2.2.1
Conditions,12 nodes
changeblack
.Node 16
changered
By definitionThe path from any node to its descendants contains the same number of black nodes
, so the root node is turned black to not meet the definition, so according to2.2.1
, perform rotation.
supplement
- Red-black tree inserts sample site