Advantages of Binary Search Trees over Hash Tables

前端 未结 18 1077
醉酒成梦
醉酒成梦 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条回答
  •  萌比男神i
    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.

提交回复
热议问题