Are hashtables always faster than trees? Though Hashtables have O(1) search complexity but suppose if due to badly designed hash function lot of collisions happen and if we handle collisions using chained structure (say a balanced tree) then the worst case running time for search would be O(log n). So can I conclude for big or small data sets even in case of worst case scenarios hash tables will always be faster than trees? Also If I have ample memory and I dont want range searches can I always go for a hash table?
Are hashtables always faster than trees? Though Hashtables have O(1) search complexity but suppose
Share
No, not always. This depends on many things, such as the size of the collection, the hash function, and for some hash table implementations – also the number of delete ops.
hash-tables are
O(1)per op on average – but this is not always the case. They might beO(n)in worst cases.Some reasons I can think of at the moment to prefer trees:
O(n)that might occur. [This might be critical for real-time systems]O(1)is much higher then the tree’s – and using a tree might be faster for small collections.However – if the data is huge, latency is not an issue and collisions are unprobable – hash-tables are asymptotically better then using a tree.