The presence of red-black tree rationality

EDITORIAL

   Why have mainly described binary search tree / trees also need to balance the red-black tree

1, a binary search tree shortcomings

  Binary search tree, I believe we have contact with, a binary search tree feature is the left child node of the tree is less than the value of the parent node, and the right sub-tree node is greater than the father node, as

  Based on this characteristic look of a binary tree, we find a node, you can take the idea is similar to binary search, quickly find a node. N nodes next binary search tree, under normal circumstances, to find the time complexity is O (logn).

  The reason why is that under normal circumstances, is a binary search tree because there may be an extreme case, for example,

 

   This case also satisfies the conditions of a binary search tree, however, in this case a binary search tree has been reduced to approximately one chain, to find the time complexity of such a binary search tree suddenly becomes O (n), can be thought of with knowledge, we must not allow this to happen, in order to solve this problem, so we come out of the balanced binary tree.

2, balanced binary tree

  In order to solve the balanced binary tree is a binary search tree degenerates into a list and was born, balanced tree has the following characteristics

  1, a binary search tree has all of the features.

  2, the height difference of each left subtree node and the right subtree most equal to 1.

  For example: Figure it is a balanced tree, while drawing two are not (node ​​on the right is the subject of this highly node)

 

  For Figure II, because the height of the left child node 9 to 2, while the right child height of 0. The difference between them more than one of.

  Balanced tree based on this characteristic can be guaranteed not to appear biased in the case of a large number of nodes of the side. On how to build a balanced tree, insert, delete, L, D and other operations not explained here, can see the concrete before I wrote an article: [comic] after the interviewer may ask you AVL tree, you put this article throw him.

  Thus, by balancing the tree, we have solved the binary search tree shortcomings. For balanced tree of n nodes, find the worst time complexity is also O (logn).

Why you need to have a balanced tree red-black tree?

  Although the balance of the tree to solve the binary search tree degenerates into an approximate list of shortcomings, able to find the time control in O (logn), but not the best, because a balanced tree requires that each node in the left subtree and right subtree height difference at most equal to 1, this requirement is too strict, resulting in each insert / delete nodes, almost will destroy the balance of the tree of the second rule, and then we all need to be adjusted by left and right hand, the once again meet the requirements to become a balanced tree.

  Obviously, if inserted in that, very frequently deleted scenes, the balance of the tree needs to be adjusted frequently, this performance will be greatly reduced balance of the tree, in order to solve this problem, so with the red-black tree, red-black tree with the following features:

1, with a binary search tree characteristics.

2, the root node is black;

3, each leaf node are black empty node (NIL), that is to say, the leaf node data does not exist.

4, any adjacent nodes can not be both red, that is to say, the red nodes are separated by a black node.

5, each node arriving at a leaf node which the node is reachable from all paths, contains the same number of black nodes.

For example, the image below (note that pictures in black, empty leaf nodes not shown) (images from geeks time)

  It is this characteristic red-black tree, so that it can in the worst case, it is possible to find a node complexity in O (logn) time. As for why we can guarantee the time complexity is O (logn), I will not go into detail here, the latter article may speak.

   However, with the balance of the tree is different, in the red-black tree insert, delete and other operations, not as balanced tree as frequently undermine the rules of the red-black tree, so do not frequent the adjustment, which is why we in most cases the reason for using red-black tree.

  However, if you want to say, just look at the efficiency of the case, balancing the tree faster than red-black tree.

  So, we can also say that red-black tree is a little strict balanced tree. It can also be said to be a compromise made program.

to sum up

  So, the final answer is, balanced tree to solve the situation degenerated into a binary search tree list, while red-black tree is a balanced tree in order to solve the case insert, delete and other operations that require frequent adjustment.

  However, there are a lot of other red-black tree of knowledge can test, for example, red-black tree What are scenarios? To the collection vessel HashMap, TreeMap the like, to use the internal structure of a red-black tree. There are n number of nodes to build a red-black tree, the time complexity is how much? Red-black tree and hash table should choose different scenes? What nature has red-black tree? Red-black tree time complexity of various operations is how much?

 

Guess you like

Origin www.cnblogs.com/chihirotan/p/11525400.html