C # and data structure--Tree theory--red black trees (upper)

Source: Internet
Author: User
Tags silverlight

Introduced

Today we introduce another kind of balanced binary tree: The red and black trees, which were invented by Rudolf Bayer in 1972, were called balanced binary B-trees (symmetric binary b-trees) and 1978 by Leonidas J. Guibas and Robert Sedgewick changed into a more modern name: The red-black tree.

The red and black trees are similar to the AVL trees mentioned earlier, and they are the balance of the binary lookup tree through specific operations while inserting and deleting operations, thus obtaining higher lookup performance. Since the red-black tree came out, the AVL tree has been put into the museum, it is said that the red-black tree has a better efficiency, higher statistical performance. But after I learned the principles of the red-black tree, I didn't believe it was true, and we'll talk about that later.

The difference between a red-black tree and an AVL tree is that it uses color to identify the height of the node, and it pursues a local balance rather than a very strict balance in the AVL tree. Before we explained the AVL tree, we have already experienced the complexity of AVL tree, but the complexity of AVL tree is nothing compared to red and black trees. The red-black tree is the real abnormal level data structure.

First comes a Silverlight-made red-black tree animation that helps you understand what a red-black tree is. It is important to note that the Silverlight 2.0 RTW must be installed to run the game properly and download the address:

Http://www.microsoft.com/silverlight/resources/install. aspx?v=2.0

Usage considerations:

The L node receives only integers, and if an illegal string is entered in the Add and delete operations, an integer is randomly added or deleted between the 0~99.

L can not enter a number in the edit box, click the Add and Remove buttons directly to add and remove operations.

L can drag the drag bar to control the animation speed.

The balance of the red and black trees

The red-black tree is the first binary lookup tree, each node is marked with color (red or black), the red-black tree satisfies the following 5 properties:

1, the color of each node can only be red or black.

2, the root node is black.

3. Each leaf node has two empty black nodes (called Black Sentinels), if a node n has only one left child, then N's right child is a black sentry; if node n has only one right child, then N's left child is a black sentry.

4, if a node is red, then its two sons are black. That is to say, you cannot have adjacent two red nodes on a single path.

5. For each node, the same number of black nodes are included in all paths from the node to the nodes of its descendants.

The 3rd of these 5 properties of the red-black tree is more difficult to understand, but it is very necessary. We look at the left of this picture in Figure 1, if the Black Sentinel is not used, it completely satisfies the nature of the red-black tree, nodes 50 to two leaf nodes 8 and the leaf node 82 path of the black node number are 2. But if you join the Black Sentinel (as in Figure 1, the little black dot in the picture), the number of leaf nodes into 8 black sentinels, the root node 50 to the 8 leaf node path on the black height is not the same, so it is not a red-black tree.

To see the real red and black trees Please add several nodes to the above animation to see if the above properties are satisfied.

Related Article

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.