Advantages of Binary Search Trees over Hash Tables

前端 未结 18 890
醉酒成梦
醉酒成梦 2020-11-29 15:02

What are the advantages of binary search trees over hash tables?

Hash tables can look up any element in Theta(1) time and it is just as easy to add an element....but

相关标签:
18条回答
  • 2020-11-29 15:43

    In addition to all the other good comments:

    Hash tables in general have better cache behavior requiring less memory reads compared to a binary tree. For a hash table you normally only incur a single read before you have access to a reference holding your data. The binary tree, if it is a balanced variant, requires something in the order of k * lg(n) memory reads for some constant k.

    On the other hand, if an enemy knows your hash-function the enemy can enforce your hash table to make collisions, greatly hampering its performance. The workaround is to choose the hash-function randomly from a family, but a BST does not have this disadvantage. Also, when the hash table pressure grows too much, you often tend to enlargen and reallocate the hash table which may be an expensive operation. The BST has simpler behavior here and does not tend to suddenly allocate a lot of data and do a rehashing operation.

    Trees tend to be the ultimate average data structure. They can act as lists, can easily be split for parallel operation, have fast removal, insertion and lookup on the order of O(lg n). They do nothing particularly well, but they don't have any excessively bad behavior either.

    Finally, BSTs are much easier to implement in (pure) functional languages compared to hash-tables and they do not require destructive updates to be implemented (the persistence argument by Pascal above).

    0 讨论(0)
  • 2020-11-29 15:44

    One advantage that no one else has pointed out is that binary search tree allows you to do range searches efficiently.

    In order to illustrate my idea, I want to make an extreme case. Say you want to get all the elements whose keys are between 0 to 5000. And actually there is only one such element and 10000 other elements whose keys are not in the range. BST can do range searches quite efficiently since it does not search a subtree which is impossible to have the answer.

    While, how can you do range searches in a hash table? You either need to iterate every bucket space, which is O(n), or you have to look for whether each of 1,2,3,4... up to 5000 exists. (what about the keys between 0 and 5000 are an infinite set? for example keys can be decimals)

    0 讨论(0)
  • 2020-11-29 15:45

    Remember that Binary Search Trees (reference-based) are memory-efficient. They do not reserve more memory than they need to.

    For instance, if a hash function has a range R(h) = 0...100, then you need to allocate an array of 100 (pointers-to) elements, even if you are just hashing 20 elements. If you were to use a binary search tree to store the same information, you would only allocate as much space as you needed, as well as some metadata about links.

    0 讨论(0)
  • 2020-11-29 15:45

    One "advantage" of a binary tree is that it may be traversed to list off all elements in order. This is not impossible with a Hash table but is not a normal operation one design into a hashed structure.

    0 讨论(0)
  • 2020-11-29 15:47

    main advantage of hash table is that it does almost all ops in ~=O(1). And its very easy to understand and implement. It does solve many "interview problems" efficiently. So if u want to crack a coding interview, make best friends with hash table ;-)

    0 讨论(0)
  • 2020-11-29 15:49

    Hash Tables are not good for indexing. When you are searching for a range, BSTs are better. That's the reason why most database indexes use B+ trees instead of Hash Tables

    0 讨论(0)
提交回复
热议问题