First, the basic nature of red and black trees
1, red black tree is essentially a binary search tree BST, to meet the left children are small root, right children are greater than (equal to) the nature of the root, but it from the root node to the furthest leaf node length will not exceed the nearest leaf node twice times, so is approximately balanced.
"Proof" because of the nature 5 if a black height of 3 red black tree, the shortest path is black-black-black length of 2 longest path for black-red-black-red-black length of 4 so can prove
2. Each node of the red and black tree is either black or red
3, the root must be black
4, if a node is red, then its two children must be black
5, the path from any node down to the leaf node must contain the same number of black nodes, this number becomes black height.
To construct a red-and-black tree, that is, two aspects. One is to construct a binary search tree, one for node dyeing.
The most critical of these is the nature of the 5. The path from any node to the leaf node must contain the same number of black nodes. The basic of the adjustment work we do is to operate on the basis of that nature.
Second, the first knowledge of red and black trees
First look at how to build a red and black tree with {9,7,15,6,11,19}
Declaration: We initialize the insertion node to red,
"Cause" because the previous red black tree satisfies the same number of black nodes from the root node to the leaf node if inserting a black node would violate the nature of the 5 so insert the red node if there is a violation of nature and then make other adjustments
First it's the first node 9, and we're just taking it as a root,
We found it against Nature 3 so change it to black
Then insert the 7
All properties are not changed after insertion
And then the 15.
And then the 6.
At this point we found a violation of the red node two sons must be black this property
How do I adjust it?
The first thing to think about is to change the 6 node directly to Black
But after black, the nature of 5 violates
We consider that no 6 of the time the original red black tree to meet the nature of 5 and fairy Alai said that the same layer of 7 and 15 are red
What if we turned them all black? It's only a layer of black and high.
So we changed 7 and 15 to black.
Found to satisfy all properties
inserting 11 and 19
Insertion 10
At this point, we found that there was another problem.
We draw a scoop.
Red and black Trees