Advantages of Binary Search Trees over Hash Tables

前端 未结 18 1030
醉酒成梦
醉酒成梦 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:24

    It also depends on the use, Hash allows to locate exact match. If you want to query for a range then BST is the choice. Suppose you have a lots of data e1, e2, e3 ..... en.

    With hash table you can locate any element in constant time.

    If you want to find range values greater than e41 and less than e8, BST can quickly find that.

    The key thing is the hash function used to avoid a collision. Of course, we cannot totally avoid a collision, in which case we resort to chaining or other methods. This makes retrieval no longer constant time in worst cases.

    Once full, hash table has to increase its bucket size and copy over all the elements again. This is an additional cost not present over BST.

提交回复
热议问题