Why red and black trees are more efficient

Source: Internet
Author: User
Tags time 0

The red and black trees belong to the balanced binary tree. It is not strictly because it does not strictly control the difference between the left and right subtree height or the number of nodes is less than or equal to 1, but the red black tree height remains the average log (n), and the worst case height will not exceed 2log (n).

The Red-black tree is a two-fork search tree that satisfies the following properties:

1. Each node is either red or black.

2. The root node is black.

3. All leaf nodes are black (they are actually null pointers, denoted by nil). Leaf nodes do not contain any keyword information, and all query keywords are on non-endpoints.

4. The two sub-nodes of each red node must be black. In other words: no two consecutive red nodes can be found on all paths from each leaf to the root

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

Correlation theorem of red and black trees

1. The longest possible path from the root to the leaf is no more than twice times longer than the shortest possible path.

According to the properties of the above 5 we know that each path of the red black tree is a 3 black node. So the shortest path length is 2 (Paths without red nodes). Then according to the nature of 4 (two red nodes can not be connected) and the nature of the (leaf and root must be black node). Then we can conclude that a path with 3 black nodes can have up to 2 red nodes (the red and black intervals exist). That is to say, black depth is 2 (the root node is also black) the longest path of the red black tree is 4, the shortest path is 2. From this we can see that the red and black trees are roughly balanced. (certainly worse than balanced binary trees, AVL has a balance factor of up to 1)

2. Red black Tree height (h) not greater than twice times the black depth of the red black Tree (BD), i.e. H<=2BD

According to Theorem 1, it is not difficult to illustrate this point. BD is the shortest path length for red and black trees. The longest possible path length (maximum tree height) is the red-black path, which is equal to 2bd. So h<=2bd.

3. A tree of red and black trees with n internal nodes (excluding leaf nodes) high H<=2log (n+1)

Here we first prove that a red-black tree with n internal nodes satisfies n>=2^bd-1. This can be proved by mathematical induction, the application is summarized in the tree height h. When h=0, this is equivalent to a leaf node, black height bd is 0, while the internal node number n is 0, at this time 0>=2^0-1 is established. Assuming the tree height h<=t, n>=2^bd-1 established, we remember a tree high for the t+1 of red and black tree root node of the left subtree of the number of internal nodes is NL, the number of internal nodes of the right subtree is NR, remember the black height of the two sub-trees for BD ' (note the black height of the two subtrees is necessarily the same), Obviously the tree of the two trees high <=t, so there are nl>=2^bd '-1 and nr>=2^bd '-1, the two inequalities are added to nl+nr>=2^ (BD ' + 1)-2, the inequality is added to the left and right 1, get n>=2^ (BD ' + 1) -1, obviously BD ' +1>=BD, so the preceding inequality can become n>=2^bd-1, which proves that a red-black tree with n internal nodes satisfies n>=2^bd-1.

In accordance with the theorem 2,h<=2bd. namely n>=2^ (H/2)-1, then H<=2log (n+1)

From here we can see that the search length of the red and black tree is not more than 2log (n+1), so its lookup time complexity is also O (log n) level.



Operation of red and black trees

Because each red and black tree is also a special two-fork lookup tree, the find operation on the red and black tree is the same as the find operation on the normal binary lookup tree. However, inserting and deleting on a red-black tree will result in no longer conforming to the nature of the red-black tree. Restoring the properties of a red-black tree requires a small amount (O (log n)) of color changes (actually very fast) and no more than three tree rotations (two times for the insert operation). While insertions and deletions are complex, the operation time can still be maintained at O (log n) times.

The advantages of red and black trees

The red-black tree can search, insert, and delete operations with the time complexity of O (log2 (N)). In addition, any imbalance will be resolved within 3 rotations. This is something that AVL does not have.

And in practice, many languages implement the data structure of red and black trees. such as TreeMap, TreeSet (Java), STL (c + +), and so on.

Why red and black trees are more efficient

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.