Red and black Trees

Source: Internet
Author: User

The red-black tree is a self-balancing binary search tree with an extra bit (bit) for each node that represents the color of the closing point (red or black). These color-representing bits are used to ensure that the red-black tree maintains a basic balance when inserting and deleting.

The red-black tree does not guarantee full balance, but is sufficient to allow the search to be completed in O (log n) time, with N being the number of elements in the tree. Insert and delete operations, along with the re-construction and re-coloring of the tree structure, can also be done in O (log n) time.

In addition to satisfying the properties of the binary search tree, the red-black tree also needs to meet the following characteristics:
1. A knot is either red or black
2. The root node is black
3. All leaf nodes do not contain data, which means nil, and are all black.
4, if a node is red, then its sub-nodes are black
5, each path from a given node to its descendants leaf node (nil) contains the same number of black nodes, the number of black nodes from the root node to the leaf node (nil) is called the Black Tree height (black-height)



These properties compel a feature of the red-black tree: The longest path from the root node to the leaf node is no greater than twice times the shortest path. This will ensure that the red and black trees are generally balanced. Because like insert, delete, and search in the worst case run time also need to be proportional to the height of the tree, the preceding features theoretically guarantee the upper bound of the tree height, which makes the red and black tree in the worst case is also efficient, and this point is different from the general two-fork search tree.

How exactly is this feature guaranteed? Is the result of the interaction of the preceding 4th and 5th properties. Assuming B is the number of black nodes that satisfy the 5th attribute, and the number of nodes included in the shortest path from the root node to the leaf node, the longer path can only be constructed by inserting a red node, whereas the 4th attribute does not allow inserting more than one contiguous red node, because the nodes of the red node must be black. Therefore, if the black nil leaf node is ignored, the longest path is composed of 2xB nodes, where red nodes and black nodes alternate, and if the black Nil leaf node is calculated, the longest path consists of 2xb-1 nodes.

Relationship to 4-step B-Tree (B-tree of order 4)



The red and black tree is similar to the structure of a 4-order B-Tree, in a 4-order B-tree, each node can contain 1 to 3 values, and 2 to 4 child pointers. In such a B-tree, each node contains a value corresponding to the value of the black node in the red-black tree, the value before and after the value is optional (because there can be 3 total values), common in a node, the value of the front and back in the red black tree corresponding to the value of the Red node.

One way to understand this correspondence is to move the red nodes up in the red and black trees so that they align horizontally with their black parent nodes, and construct a cluster (cluster) in the horizontal direction, which corresponds to a 4-order B-tree. The minimum fill factor (that is, only one value) in each cluster is one-third of the maximum fill capacity (with three values).


This digest from: Https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

Red and black Trees

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.