Red/Black (1), red/Black

Source: Internet
Author: User

Red/Black (1), red/Black
1. Red/black tree

The red/black tree is a self-balancing Binary Search Tree (BST). Each node follows the following rules:

Property 1. nodes are 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, there cannot be adjacent red nodes. Red nodes cannot have red parent nodes or red child nodes)
5. All simple paths from any node to its leaves contain the same number of black nodes.


2. Red/black tree conclusion

Conclusion 1:The maximum possible paths from the root to the leaves are no more than twice the maximum possible paths.

Why? Property 4 implies that there cannot be two adjacent red nodes on any simple path. In this way, the shortest paths are all black nodes, and the longest possible paths have alternate red and black nodes. At the same time, according to nature 5, all the longest paths have the same number of black nodes, which indicates that no path can be twice longer than any other paths.

According to this nature, we can see that this tree is basically balanced. Some Tree operations, such as insert, delete, and search, are proportional to the height of the tree in the worst case, therefore, the height limit of this tree makes the red and black trees highly efficient in the worst case, unlike the ordinary Binary Search Tree (BST ).

Conclusion 2:Height of the red/black tree with n nodes <=

This conclusion can be proved based on the following facts:
1) for a common Binary Tree, if k is the minimum number of nodes from the root node to the leaf path, n> = 2 k-1 (for example, if k is 3, n is 7 at least ). This expression can also be written as k <=

2) from the property of the above-mentioned red-black tree 5, we can see that a red-black Tree Containing n nodes has a path from the root node to the leaf, A maximum of black nodes are available.

3) from the property 4 of the Red-black tree, we can obtain a red-black Tree Containing n nodes, with at least [n/2] Black nodes.

From the above points, we can draw this conclusion: the height of the red/black tree of n nodes <=
Conclusion 3:
In the red/black tree, if x has only one non-empty subtree, then x must be black. Proof: Assume that x is red. According to property 4, x has two black subnodes. There is only one non-empty subtree conflict with x.

Conclusion 4:
In the red/black tree, if x has only one non-empty subtree. The root of the non-empty subtree must be red, and the non-empty subtree only has one root node. Proof: if y is the left child of x, the color of node y is black, and y has a subtree. Because y is black, the right sub-Right of x is empty, therefore, the number of black knots in the path from x to each leaf node of the Left subtree is greater than the number of black knots in the path from x to the right leaf node, which violates the nature of 5, so node y is red. Because y is red, if y's subtree exists, according to property 4, we can conclude that y's two subtree must be black. The number of black nodes in the path from x to y to each leaf node is greater than that in the right leaf node path. As mentioned above, when y is the right child of x, it can also prove conclusion 4.
3. Why do most operations (such as search, maximum value, minimum value, insertion, and deletion) of the Binary Tree need O (h) times, where h is the height of the tree. In extreme cases, these operations may take O (n) time. If we can ensure that the tree height is maintained at O (Logn) after each insert or delete operation, we can ensure that the upper limit of all these operations is O (Logn) times. the height of the red/black tree is always O (Logn), where n is the number of nodes.
4. Compared with the AVL tree, the AVL tree is more balanced than the red and black trees. However, AVL needs to rotate more times during insertion or deletion. Therefore, it is better to use the red/black tree if the application requires frequent insert and delete operations. If the number of inserts and deletes is relatively small, and the number of searches is more, the AVL Tree is better than the red/black tree.
5. How to ensure the balance of the red and black trees the following uses a simple example to understand the balance. The chain structure of the three nodes cannot appear in the red/black tree. We can try all the color combinations and make them violate the nature of the red and black trees.

A) The chain structure of the three nodes cannot be a red/black tree.

The following are not red/black trees:

30                30                     30/\/\/\20NIL20 NIL20NIL /\/\/\10NIL10NIL10NIL violation nature 5 violation nature 5 violation Nature 4

B) below are possible Red/black tree combinations20 20/\/\10 30 10 30// \/\ NIL

In the above example, we can know how the Red and black trees ensure balance.

6. Are all nodes in the red/black tree black?
The answer is yes! Because it does not violate any nature of the red and black trees. Only black children are allowed for red nodes, but black nodes can have either red nodes or black children.
You can refer to the discussion on this in stackoverflow.
Http://stackoverflow.com/questions/6406658/is-a-tree-with-all-black-nodes-a-red-black-tree

Subsequent articles in this series will introduce insert and delete operations.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.