Advantages of Binary Search Trees over Hash Tables

前端 未结 18 889
醉酒成梦
醉酒成梦 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条回答
  • BSTs also provide the "findPredecessor" and "findSuccessor" operations (To find the next smallest and next largest elements) in O(logn) time, which might also be very handy operations. Hash Table can't provide in that time efficiency.

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

    Binary search trees are good choice to implement dictionary if the keys have some total order (keys are comparable) defined on them and you want to preserve the order information.

    As BST preserves the order information, it provides you with four additional dynamic set operations that cannot be performed (efficiently) using hash tables. These operations are:

    1. Maximum
    2. Minimum
    3. Successor
    4. Predecessor

    All these operations like every BST operation have time complexity of O(H). Additionally all the stored keys remain sorted in the BST thus enabling you to get the sorted sequence of keys just by traversing the tree in in-order.

    In summary if all you want is operations insert, delete and remove then hash table is unbeatable (most of the time) in performance. But if you want any or all the operations listed above you should use a BST, preferably a self-balancing BST.

    0 讨论(0)
  • 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.

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

    A hashmap is a set associative array. So, your array of input values gets pooled into buckets. In an open addressing scheme, you have a pointer to a bucket, and each time you add a new value into a bucket, you find out where in the bucket there are free spaces. There are a few ways to do this- you start at the beginning of the bucket and increment the pointer each time and test whether its occupied. This is called linear probing. Then, you can do a binary search like add, where you double the difference between the beginning of the bucket and where you double up or back down each time you are searching for a free space. This is called quadratic probing. OK. Now the problems in both these methods is that if the bucket overflows into the next buckets address, then you need to-

    1. Double each buckets size- malloc(N buckets)/change the hash function- Time required: depends on malloc implementation
    2. Transfer/Copy each of the earlier buckets data into the new buckets data. This is an O(N) operation where N represents the whole data

    OK. but if you use a linkedlist there shouldn't be such a problem right? Yes, In linked lists you don't have this problem. Considering each bucket to begin with a linked list, and if you have 100 elements in a bucket it requires you to traverse those 100 elements to reach the end of the linkedlist hence the List.add(Element E) will take time to-

    1. Hash the element to a bucket- Normal as in all implementations
    2. Take time to find the last element in said bucket- O(N) operation.

    The advantage of the linkedlist implementation is that you don't need the memory allocation operation and O(N) transfer/copy of all buckets as in the case of the open addressing implementation.

    So, the way to minimize the O(N) operation is to convert the implementation to that of a Binary Search Tree where find operations are O(log(N)) and you add the element in its position based on it's value. The added feature of a BST is that it comes sorted!

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

    A binary search tree can be implemented with a persistent interface, where a new tree is returned but the old tree continues to exist. Implemented carefully, the old and new trees shares most of their nodes. You cannot do this with a standard hash table.

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

    From Cracking the Coding Interview, 6th Edition

    We can implement the hash table with a balanced binary search tree (BST) . This gives us an O(log n) lookup time. The advantage of this is potentially using less space, since we no longer allocate a large array. We can also iterate through the keys in order, which can be useful sometimes.

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