Red and Black Tree study

Source: Internet
Author: User

Red-black tree is a more data structure used in engineering, its advantage is that the complexity of the search is O (LgN), and the red-black tree will be inserted in the data to maintain a balance, suppress the complexity of the explosion. Although it is not as efficient as Hashtable, it does not have to plan and allocate space beforehand. So how did it do it? Here's what it's all about: 1. Red and black trees are composed of red and black elements (coloring) 2. Red nodes have no red subnodes (red and Red are enemies) 3. All Black nodes from the root node to the leaf node are equal (left and right balanced). Available: 1 nodes are black. 2. The two sub-nodes of the red node are black Insert OperationHow is the data inserted? Here forget to mention one of its important properties, the middle of the data is always larger than the left, smaller than the right. This guarantees that the search will go down a path down the street. Here's an example: Insert Step 1. Smaller than the current key value, move to the left, larger than the current key value, to the right. Find the appropriate place to insert 2. The color of the decision-if you make it black, then it is more troublesome, because equal number of black nodes from the root node to the leaf node。   This can be more difficult to fix. --So make red 3. The red dots will cause two red dots to be contiguous, violating the Red node does not have a red child nodeThe conditions. So we're going to fix it after the insert.  This is the point where the red and black trees are more troublesome.    Note When repairing, empty child nodes should also be repaired together (considered black)! When repairing, the red node pulls up, can reduce the conflict! 2-3 TreesAbout the operation of the red and black tree, you can first see how the 2-3 tree operation, so it is easy to understand the operation of the red and black trees. Specifically, you can look at the algorithm of the book, the author (Robert Sedgewick and Kevin Wayne), the English original 424 pages.     Here is one of the pictures (re-painting), you can see that the design of a single key can be a multi-key node evolution. The original book 446 pages have such a sentence, it can be explained that the red and black trees from the 2-3 tree evolved. So the red and black trees are easy to understand after 2-3 of trees have been read.
The basic idea behind Red-black bsts are to encode 2-3 trees by starting  

  

 The form of red and black trees, that is, the simplicity of BST, and obtained the balance of 2-3 trees, red and black trees is a special form of 2-3 trees. 2-3 tree to Red black tree conversion: Here the red black spot is actually a red-black link, the red thick link in the figure indicates that the node below the red line is red. Red and Black tree operationThe left and right two graphs perform rotate operations (both in-and-out), but they are equivalent because the order of the data is constant (BCDEF). Just tilted the red link. Red and black tree insertThe first point is a black one. Insert a second point, which must be on the left or right side of it. On the left, just insert a red dot on the left. Insert a red dot on the right and do a left-handed operation. Here, the author basically tries to rotate the node left. This is a summary of the practice: Left rotate, right rotate, and color? IP in general, try to put the red link to the Ieft, when there are two consecutive red links to the left, do a turn, so that it has a red link on both sides. Finally turn this two link into black. removal of red and black treesDeleting will be a bit trickier, but if you think of it as a 2-3 tree, it's much better: for example, to delete a, simply merge the ABC into one group and delete it so that you don't have to consider the processing of the link.     There's a story in the book here. References: Algorithms-fourth editionhttp://www.cs.usfca.edu/~galles/visualization/redblack.html

Red and Black Tree study

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.