[Introduction to algorithms] Lesson 10 balanced search tree and introduction to Algorithms

Source: Internet
Author: User
Tags color gamut

[Introduction to algorithms] Lesson 10 balanced search tree and introduction to Algorithms

If the tree structure cannot be balanced, the search performance will be greatly compromised. This section introduces several classic balancing trees, such as AVL, 2-3-4tree, and red/black tree, then I focused on the red and black trees. Next I will give a brief summary of the basic nature of the red and black trees.

First, in addition to the basic nature of BST, the red and black trees also have the following five basic properties:

1) each node has a color gamut. One node is either a black node or a red node.

2) The root node is a black node.

3) each leaf node is a black node (no key value)

4) The father of each Red node is a black node, that is, it is impossible to connect two red nodes.

5) the number of black nodes in the path from the root node to any leaf node is equal. This number is also known as the Black height.

The height of the red/black tree is O (lgn), which is proved as follows:

Merge all the red nodes of the Red-black tree with its parent node (the parent node must be black due to its nature, we can get a 2-3-4 tree (that is, the number of subnodes of each node is 2 ~ 4), which can be obtained by data structure knowledge. The number of leaf nodes in the original red/black tree is n + 1 with a key value. Assume that the height of the entire tree is h, then the number of leaf nodes should be h ^ 2 ~ H ^ 4, so there are h ^ 2 <= n + 1 <= h ^ 4, that is, the highest tree height is only log n + 1, that is, the tree's black height is log n + 1. According to nature 3, the highest tree height is only red and black, so its height is only 2log n + 1, that is, the tree height is O (lgn ).

The query operation of the red/black tree is the same as that of the ordinary BST, while the deletion and insertion operations are relatively complicated, because we need to ensure the five major properties of the red/black tree. Why do we need to ensure these five properties? Because these five properties are the guarantee of the red and black trees for the Balance Tree, the height of the red and black trees can be ensured to be Olgn, so that the basic operations of the red and black trees (delete, insert, query) can be completed within the Olgn time complexity.

Next, we will briefly introduce how the insert operation is completed. The delete operation is similar to the following:

The principle of the insert operation is to insert a red node and maintain the properties of the red and black trees by re-coloring and rotating up.

There are three cases

Case1 is linear and the grandfather node is black. The father node and the father and brother node are red, and the grandfather node is black, which is passed to two red child nodes each time, the tree height is 2lgn, so the operation is O (lgn)
Case2 zigzag Z-type father brother node is black rotation to case3 O (1) Rotation
Case3 zigzig linear Rotation

We can see from the above three cases that the insertion time complexity is the O (1) Rotation operation of the Olgn + that focuses on Dyeing and does not exceed 3 times.

It is worth mentioning that, in actual use, although it takes more time in theory than to rotate up and re-dyeing, when multiple users concurrently query and access the redtree, re-dyeing does not affect the query, because the user does not care about the color of each node, but the rotation needs to lock the subtree and its nodes, which may affect the operations of concurrent queries.

Finally, we will compare AVL with the red and black trees. In terms of the degree of balance, AVL is the absolute balance pursued. The depth of any leaf node will not exceed the depth of other leaf nodes + 1, the red-black tree only requires partial balancing, and its red-black Nature ensures its balance. Therefore, in terms of maintaining the balance, the red-black tree only needs to be rotated no more than three times, this is not what AVL trees can do, but in terms of query, AVL is an absolute balance, so the efficiency is slightly higher than that of the red and black trees. in actual application, this is not obvious. In terms of statistical performance, the red-black tree is better than AVL, And the set, multiset, map, and multimap in C ++ STL are all variants of the Red-black tree.

It is worth mentioning that the Balance Tree is a dynamic data structure, and its advantage is that it can maintain superior query efficiency under dynamic operations. If it is static data, the hash table is more efficient.


Related Article

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.