Hash binary tree

 

 

We talked about the hash table that section, inserted into the hash table, delete, search operations can be done constant time complexity class O (1), very efficient. The binary search tree at a more balanced situation, insert, delete, search the complexity of the operation time is O (logn), relative to the hash table, as if there is no advantage, why should we use a binary search tree it?

I think there are several reasons:

First, the data in the hash table is stored disorder, if ordered to be output data, you need to be sorted. For a binary search tree, we need only inorder traversal, can be within the time complexity of O (n), the output data sequence ordered.

Second, the hash table expansion consuming a lot, but when faced with a hash collision, unstable performance, despite a binary search tree unstable performance, but in the project, the most commonly used performance balanced binary search tree is very stable, stable in time complexity O (logn).

Third, in general terms, despite the time complexity of search and other operations of the hash table is a constant level, but because there is a hash conflict, this is not necessarily constant smaller than logn, so the actual search speed may not necessarily better than O (logn) fast. Plus time-consuming hash function, not necessarily high search tree than balanced binary efficiency.

Fourth, the structure of the hash table look more complex than the binary tree, a lot of things to consider. For example, the hash function design, conflict resolution, capacity expansion, volume reduction and so on. Balanced binary search tree only need to consider the balance of this problem, and the solution to this problem is relatively mature, fixed.

Finally, in order to avoid too much of a hash collision, the load factor of the hash table can not be too much, especially in conflict resolution method based on open addressing hash table, or you'll waste some storage space.

Taking these points, a balanced binary search tree in some respects superior to the hash table, so that there is no conflict between the two. We in the actual development process, requires a combination of specific needs to choose which one to use.

 

Guess you like

Origin www.cnblogs.com/yuanjiangw/p/11967736.html