1. Red and black Tree nature
A red-black tree is a self-balancing binary lookup tree (BST) in which each node adheres to the following rules:
Property 1. The node is red or black
Nature 2. The root node is black
Nature 3. All leaves are black (leaves are nil nodes)
Nature 4. If a node is red, its two sons are black. (That is, it is impossible to have contiguous red nodes.) Red nodes cannot have red parent nodes or red child nodes)
Nature 5. All simple paths from any node to its leaves contain the same number of black nodes.
2. Red and Black Tree conclusion
Conclusion 1:
These five properties of the red-black tree emphasize the key properties of the red-black tree: The longest possible path from the root to the leaf is twice times longer than the shortest possible path. Why is it? Property 4 implies that no two contiguous red nodes can be found on any simple path, so that the shortest possible path is all black nodes, and the longest possible path has alternating red and black nodes. Also according to the nature of 5 know: all the longest paths have the same number of black nodes, which indicates that no path can be more than any other path twice times longer.
According to this nature, the tree is generally balanced. Because some operations of the tree, such as insertions, deletions, and lookups, are proportional to the height of the tree in the worst case time, the height limit of the tree makes the red and black trees efficient in the worst case, and differs from the normal two-fork lookup tree (BST).
Conclusion 2:
height of the red black tree with n nodes <=
This conclusion can be based on the following facts to prove that:
1) for a generic two-fork tree, suppose K is the minimum number of nodes from the root node to the leaf path, then n >= 2k–1 (for example, if K is 3, then n is at least 7). This expression can also be written as K-<=
2) from the nature of the red black tree described above 5, can be obtained, a red black tree containing n nodes, there is a path from the root node to the leaves, where there is a maximum of a black node.
3) from the nature of the red black Tree 4, can be obtained, a red black tree containing n nodes, at least [N/2] a black node.
from these points above, you can get this conclusion: N nodes of the red-black tree height <=
Conclusion 3:in a red-black tree, if X has only one non-empty tree, the x must be black. Proof:Assuming that x is red, according to the Nature 4 can be introduced X has two black child nodes. With X there is only one non-empty tree contradiction.
Conclusion 4:in a red-black tree, if X has only one non-empty tree. The root of the non-empty tree must be red, and the non-empty tree has only one root node. Proof:Suppose Y is the left child of X, node Y has a black color and Y has a subtree. Since y is black, the right child of x is empty, so the black node number on the path from X to its left subtree leaves nodes is greater than X to its right sub-leaf to the leaf node on the path of black nodes, violating the nature of 5, so node y is red. Because Y is red, if the subtree of Y is present, the two subtrees trees of y can be obtained according to the nature of the 4 must be black. The number of black nodes from X to the path through Y to each leaf node is greater than the number of black nodes on the right child leaf node path. As mentioned above, when Y is the right child of X can also prove conclusion 4.3. Why do I need a red-black tree?Two forks the most operations of the find tree (such as Search, Max, min, insert, delete, etc.) require an O (h) time, where H is the height of the tree. In extreme cases, these operations may require an O (n) time. If we can ensure that the height of the tree is maintained at O (logn) after each insert and delete operation, we can guarantee that all of these operations will have an O (logn) limit. The height of the red-black tree is always O (logn), where n is the number of nodes.
4. Comparison with the balanced binary tree (AVL tree)The AVL tree is more balanced than the red-black tree, but the AVL needs to be rotated more often when it is inserted or deleted. Therefore, if the application requires frequent insertions and deletions, it is better to use a red-black tree. If the number of insertions and deletions is less, and the number of lookups is greater, then the AVL tree is preferable to a red-black tree.
5. How does the red and black tree ensure balance?use a simple example to understand the balance. The chain structure of 3 nodes cannot be seen in a red-black tree. We can try all the color combinations and make them violate the nature of the red and black trees.
A). 3-node chain structure cannot be a red-black tree
The following are not red-black trees:
/ \/\/ c7>20 Nil NIL nil /\/\/\ \ Nil Nilviolation of Nature 5 violation of Nature 5 violation of Nature 4
b). here are possible red and black tree combinations 30 / \ / ten / \ / \ / \ / nil nil nil Nil nil Nil Nil Nil
From the above example, we can see how the red and black trees are guaranteed to be balanced.
6. Questionscan the nodes in the red and black tree all be black nodes?
The answer is: Yes! Because it does not violate any nature of the red-black tree. Red nodes only allow black children, but black nodes can have either red children or black children.
Refer to the StackOverflow above for a discussion of this.
Http://stackoverflow.com/questions/6406658/is-a-tree-with-all-black-nodes-a-red-black-tree
Subsequent articles in this series describe insertions and deletions.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Red and Black Trees (1)-Introduction