哈希表v / s树 [英] Hash Table v/s Trees
问题描述
哈希表总是比树更快吗?
不,不总是。这取决于许多事情,例如集合的大小,散列函数和一些哈希表实现 - 还有删除操作的数量。
哈希表平均每个 c
某些原因我可以在 想到当时喜欢树木:
- 订购很重要。 [哈希表不维护订单,BST按定义排序]
- 延迟是一个问题 - 您不能忍受可能发生的
O(n)
。 [这可能对于实时系统至关重要] - Ther数据可能与您的哈希函数相关,并且许多元素散列到相同的位置[碰撞]不是不可取的。 [有时可以通过使用不同的哈希函数来解决]
- 对于相对较小的集合 - 多次哈希表的
O(1)$ c之间的隐藏常量$ c>比树的高得多 - 对于小集合使用树可能会更快。
但是 - 如果数据是巨大的延迟不是一个问题,冲突是不可取的 - 哈希表比使用树更顺利。
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?
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 be O(n)
in worst cases.
Some reasons I can think of at the moment to prefer trees:
- Ordering is important. [hash-tables are not maintaining order, BST is sorted by definition]
- Latency is an issue - and you cannot suffer the
O(n)
that might occur. [This might be critical for real-time systems] - Ther data might be "similar" related to your hash function, and many elements hashed to the same locations [collisions] is not unprobable. [this can be sometimes solved by using a different hash function]
- For relatively small collections - many times the hidden constant between hashtable's
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.
这篇关于哈希表v / s树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!