Red and black Trees

Source: Internet
Author: User

1. Concept

The red and black tree is a self-balancing binary search tree, a data structure used in computer science, and a typical use is to implement associative arrays. It was invented in 1972 by Rudolph Bell, which he called "symmetric binary B-tree", and its modern name was obtained in a paper written by Leo J. Guibas and Robert Sedgewick in 1978. It is complex, but its operation has a good worst-case run time and is efficient in practice: it can be searched, inserted and deleted in O (log) time, where the number of elements in the tree is.

Its statistical performance is better than that of balanced binary trees (some books are called avl-trees according to the author's name, Adelson-velskii and Landis), so red and black trees are used in many places. In C + + STL, many parts (currently including set, Multiset, map, Multimap) have applied the variants of red-black trees (there are some changes in the red-black trees in SGI STL, which provide better performance and support for set operations). Other balance trees are: avl,sbt, stretching trees, treap and so on.

2. Nature

A red-black tree is a two-prong search tree with color attributes for each node, color or red or black. In addition to the general requirements of the binary search tree, we have added the following additional requirements for any valid red-black tree:

Nature 1. The nodes are red or black.

Nature 2. The root node is black.

Properties 3 each leaf node is black.

Property 4 The two child nodes of each red node are black. (no two consecutive red nodes can be found on all paths from each leaf to the root)

Nature 5. All paths from any node to each of its leaves contain the same number of black nodes.

These constraints force the key properties of the red-black tree: The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path. The result is that the tree is broadly balanced. Because the worst-case time for operations such as inserting, deleting, and finding a value is proportional to the height of the tree, the theoretical upper limit on the height allows the red and black trees to be efficient in the worst case, unlike the normal two-fork lookup tree.

To understand why these features ensure this result, it is sufficient to note that the Nature 4 causes the path to not have two contiguous red nodes. The shortest possible paths are black nodes, and the longest possible paths have alternating red and black nodes. Because according to the nature of 5 all the longest paths have the same number of black nodes, this indicates that no path can be more than twice times longer than any other path.

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.