Red-black tree, B (+) tree, jump table, comparison AVL

In some e-learning materials.

This one: https: //www.zhihu.com/question/30527705

Copy the code

AVL tree: one of the first balanced binary tree. Application of less relative to other data structures. windows management process address space used in the AVL tree, 

red-black tree: balanced binary tree, widely used in the C ++ STL. map and set are achievable with red-black tree. We are familiar with the STL map container bottom is RBtree, of course, refers not unordered_map, which is the hash. 

B / B + tree with index data in the disk file organization and indexing database 

Trie tree trie, with statistics and sorting a large number of strings 

------ 

AVL is a highly balanced binary tree, so the usual result is to maintain this kind of well-balanced price paid is greater than the efficiency gains derived from them, and therefore of little practical application, 
more places with very strict pursuit of local rather than the overall balance of red-black tree. Of course, if the scene to remove the insert infrequent, but there are special requirements for finding, AVL or better than the red and black. 

Application of red-black tree on the lot, in addition to STL, as well as the above mentioned students 
epoll implemented in the kernel, with a red-black tree management event block 
nginx, a red-black tree with a timer and other management 
to achieve the Java TreeMap 
famous linux process scheduling Completely Fair scheduler, with a red-black tree management process control block 

B and B + is mainly used in the file system and database index the like, such as Mysql: B-tree index in MySql 

Trie tree of a typical application is a prefix match, such as the following this is very common scenario, when we entered, the search engine will give tips 
as well, such as IP routing, but also the prefix matching, to some extent, will be used Trie 

------

Skip list: Redis on the use of jump table, rather than red-black tree storage management elements which (it should be said that one element - direct Key, there's value should be a different data structure). 

First, the jump table is skiplist? Not ziplist. ziplist province is a very memory of the list (at the cost of slightly lower performance) in redis in, so the number of elements in the hash few (such as only a few dozen), 
then use this structure to store can be very small performance loss saving a lot of memory (redis is memory database ah, it could save or to save the) case. This problem clear. 

In the server side, and the case of the concurrent performance requirements, how to select suitable data structure (here red-black tree and jump tables). 
If you simply compare the performance, jump tables and red-black tree can say less, but with concurrent environment is not the same, 
if you want to update the data, jump tables need to be updated section is relatively small, it is relatively small lock things , so the cost of different thread contention lock is relatively less, 
but there is a red-black tree balanced process, involving a large number of nodes, the cost of lock contention also relatively high up. Performance will not as good as the former. 
In a concurrent environment skiplist have another advantage, in the red-black tree insertion and deletion of time may need to do several rebalance operation, this operation may involve other part of the whole tree, 
and skiplist operation is clearly more localized number, It pegged locks require fewer nodes, so in this case better performance.

Copy the code

In addition the use of jump table Redis authors describe the reason:

Copy the code

See the developers say, why did he choose skiplist at The Skip List 

There are A FEW Reasons: 
.. 1) They are not Very Memory Intensive It's up to you Basically 
Changing the Parameters the About at The Probability of A the Node to have have A GIVEN Number The of Levels memory Intensive the make the then less by Will 
Within last Btrees. 
Note: one disadvantage is the jump table memory consumption (due to the repeated tiered node), but the authors also said, you can adjust the parameters to reduce memory consumption, and those who reach almost balanced tree structure . 

2) A the sorted SET IS Often field target of MANY Z Range The or ZREVRANGE Operations, that IS, Traversing The Skip List AS A linked List. 
 With the this Operation The Cache locality of Skip Lists IS AT Least AS Good AS with OTHER kind of Balanced Trees. 
Note : redis investigation has a range of operation, using a jump table inside this doubly linked list, you can easily operate. There are also regional cache (cache locality) will not be worse than the balance of the tree.
3) They are simpler to implement, debug, and so forth. For instance thanks to the skip list simplicity I received a patch 
(already in Redis master) with augmented skip lists implementing ZRANK in O(log(N)). It required little changes to the code.
注:实现简单。zrank操作能够到O(log(N)).

About the Append Only durability & speed, I don't think it is a good idea to optimize Redis at cost of more code 
and more complexity for a use case that IMHO should be rare for the Redis target (fsync() at every command). 
Almost no one is using this feature even with ACID SQL databases, as the performance hint is big anyway.

About threads: our experience shows that Redis is mostly I/O bound. I'm using threads to serve things from Virtual Memory. 
The long term solution to exploit all the cores, assuming your link is so fast that you can saturate a single core, 
is running multiple instances of Redis (no locks, almost fully scalable linearly with number of cores), 
and using the "Redis Cluster" solution that I plan to develop in the future.

Copy the code

The above article has some abbreviation, are summarized as follows:

Copy the code

imho, imo (in my humble opinion , in my opinion): In my opinion, common in the forum. 

idk (I do not know): I do not know. 

rofl (rolling on the floor laughing) : laugh fell to the ground. 

roflmao (rolling on the floor laughing my ass of): combination of two former version, which is super funny meaning. 

sth (something): for something. 

nth (nothing): nothing. 

plz (please): Please. please suffix z is sound, it is according to the pronunciation abbreviated plz. 

thx (thanks): Thank you. Phonetically view, thanks ks may be replaced with the suffix letters X.

Copy the code

Red-black tree with B (+) comparative tree project implementation:

Copy the code

There are a few answers from the algorithm point of view, I try to distinguish between red-black tree with scenarios b + tree from an engineering point of view under analysis, a red-black tree node only keep one pair kv, so you can use something like an embedded list realization way, 
the data structure itself does not manage memory, more lightweight, more flexible and more save memory, for example, a node can exist simultaneously several tree or linked list, the kernel more common. 

And b + tree because each node to save more for kv, memory node structure will generally be managed by its own data structure is container in the true sense, relatively embedded method to achieve red-black tree, 
the benefits are easy to use, their own memory management easier to do lockfree, a plurality of storage node kv manner cpu cache hit ratio high, the user triplet or high concurrency index generally selected from b + tree. 

Besides b + b-tree and the tree, the intermediate nodes btree b + trees and more than the value stored, a case where the same degree, node larger, relatively cpu cache hit rate is not as good as b + tree. 
In addition to mention a, b + tree scan feature (the list to string together the leaf nodes) in the absence of lock case it is hard to do (I have yet to encounter a solution), so I currently see no lock leaves node b + They are not string together.

Copy the code

Features from the respective feature, analyzes various data structures scenarios:

Copy the code

Red-black tree, AVL tree simple terms are used to search for pictures. 

AVL tree: balanced binary tree, the balance factor is generally determined by difference and is achieved by rotating the right and left sub-tree height of not more than 1, and then compare it to red-black tree is strictly balanced binary tree, very strict equilibrium conditions (high tree the difference is only 1), 
as long as the insertion or deletion does not satisfy the above conditions will be balanced by rotating. Since the rotation is very time consuming. We can deduce case AVL tree is suitable for insertion and deletion relatively small number, but look for more. 

Red-black tree: balanced binary tree, through any one color for each constraint from the root node to the leaf simple paths, one path will ensure that no two times longer than other paths, thus approximately balanced. 
Therefore, stringent requirements with respect to the balanced AVL tree, its rotational balance kept low frequency When used in the case of search, insertion and deletion of the number of times we have to replace AVL with red-black tree. 
(Now part of the scene using a jump table to replace the red-black tree, you can search for "why use redis jump table (skiplist) instead of using the Black-Red?") B 

tree, B + tree: they are the same characteristics, a multi-channel Find tree, generally used database system, why, because they are less number of multi-layered branch chanting, 
know that disk IO is very time consuming, but like a lot of data storage so we have to effectively reduce the number of disk IO to avoid frequent disk to disk Find. 
B + Tree is a variant of the B-tree tree, there are n nodes in the subtree with n keywords, each of the data is not saved, the index only used, data is stored in the leaf node. For the file system and students. 

Trie tree: 
also known as trie, a tree structure, commonly used to manipulate strings. It is the same prefix different strings only save a copy. 
It would be relatively straightforward to save the cost of memory when the string is certainly space-saving, but it saves a lot of strings (the memory). 

There are similar
Prefix tree (prefix tree), suffix tree (suffix tree), radix tree ( patricia tree, compact prefix tree), crit-bit tree ( cost of memory to solve the problem), 
and in front of that double array trie. 
I understand the simple application of supplementary 
prefix tree: quick search string, the string sorting, the longest common prefix, suffix automatically display matching prefix. 
Suffix tree: Find String s1 s2 in, the number appearing in the string s1 s2, the string s1, s2 longest common portion, the longest string palindrome. 
radix tree: linux kernel, nginx.

Copy the code

Introduce red-black tree can see these two articles: the clearest explanation of the history of the red-black tree (on) + (lower)

http://mt.sohu.com/20161014/n470317653.shtml

http://mt.sohu.com/20161018/n470610910.shtml

When the search tree structure is changed, the conditions may be damaged red-black tree, the search tree must be adjusted so that the red-black tree again satisfy the condition. 
Adjustment may be divided into two categories: 
one is to adjust the color, i.e., change the color of a node; 
Another structural adjustment, change the set relationship retrieves tree structure. Restructuring involves two basic operations: L (Rotate Left), dextrose (RotateRight) 

Remember, no matter how many cases the specific adjustment operation only two: change the color of a certain node 2 for certain. node rotation.

Open the various analyzes a text string associated algorithm, and the various data structures used, the prefix tree includes a suffix trees and other trees.

Guess you like

Origin blog.csdn.net/hellozhxy/article/details/92845357