D & F Data Structure series-red/black tree

Source: Internet
Author: User

Red/black tree

Definition: A Binary Search Tree is a red-black tree if it meets the following requirements:

1) each node is either red or black

2) The root node is black.

3) each leaf node is black.

4) if a node is red, its two sons are black (that is, it is impossible to have two consecutive red nodes)

5) for each node, all paths from the node to its child node contain the same number of black nodes

Nature:

These constraints ensure the key feature of the red/black tree: the longest possible path from the root to the leaves is no more than twice the shortest possible path. The result is that the tree is roughly balanced. Because operations such as insert, delete, and find the worst case time of a value must be proportional to the height of the tree, this theoretical ceiling on height allows the red and black trees to be efficient in the worst case, unlike the ordinary Binary Search Tree.

You need to know why these properties ensure this result. Note that property 4 makes it sufficient for the path not to have two adjacent red nodes. The shortest paths may be black nodes, and the longest possible paths may be red or black nodes. Because all the longest paths in property 5 have the same number of black nodes, this indicates that no path can be twice longer than any other path.

Source and application:

* He was invented by Rudolf Bayer in 1972 and called it the "symmetric Binary Tree". His modern names were obtained in a paper written by Leo J. guibas and Robert Sedgewick in 1978. It is complex, but its operation has a good run time in the worst case, and is efficient in practice: It can be searched in O (log n) Time, insert and delete. N indicates the number of elements in the tree.

* The red and black trees are generally sorted internally, that is, they are all stored in the memory. The internal implementation of Microsoft STL map and set is the red and black trees. Tree B is usually stored in the memory, and most of the data is stored in the memory. Because the number of B-tree layers is small, you can ensure that each operation reads as few disks as possible.

* The RD Tree is used in many places in Linux. During vm_area_struct management, the Linux kernel uses a red/black tree to maintain memory blocks. Anticipatory, deadline, and cfq I/O scheduling both use the RB tree for request tracking, as well as the CD/DVD drive package management. The high-resolutiontimer uses the RB tree to organize scheduled requests.

Complexity:

The height of a red-black tree with N nodes is at most 2log (n + 1)

Operation:

Because each red/black tree is also a special Binary Search Tree, the read-only operation on the red/black tree is the same as that on the common Binary Search Tree. However, the insert and delete operations on the red/black tree will no longer conform to the nature of the red/black tree. To restore the property of a red/black tree, a small amount (O (LogN) And no more than three tree rotations (two insert operations ). Although insertion and deletion are complex, the operation time can still be O (LogN.

Wikipedia:Red/black tree

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.