`
v5qqcom
  • 浏览: 1287861 次
文章分类
社区版块
存档分类
最新评论

hash vs RBTree

 
阅读更多

数据结构:最小堆/哈希表 /二叉树 /平衡二叉树/红黑树的意义(什么情况下使用)


接触堆数据结构是在排序里面讲的,空间复杂度O(1),时间复杂度O(NlogN),但是在实践中还是不如快速排序(好像快速排序可以更好的利用硬件特 性)。堆的意义就在于:最快的找到最大/最小值,在堆结构中插入一个值重新构造堆结构,取走最大/最下值后重新构造堆结构 其时间复杂度为O(logN),而其他方法最少为O(N).堆实践中用途不在于排序,其主要用在调度算法中,比如优先级调度,每次取优先级最高的,时间驱 动,取时间最小/等待最长的 等等 ,分为最大堆/最小堆。
  哈希表主要可以在O(1)时间内对查找对象定位,但是事实上,如果输入集合不确定 的情况下,可能出现大量的冲突,虽然有很多好的哈希函数,但是随着随机输入,大量冲突还是不可避免,可能出现最差情况。所以,哈希表如果用在输入集合确定 (即以后只会做查询操作)的情况下,选择合适的函数函数和解决冲突的方法(perfect hash)可以在O(1)时间内完成查找(有证明,看不懂)。
  二叉树支持动态的插入和查找,保证操作在O(height)时间,这就是完成了哈希表不便完成的工作,动态性。但是二叉树有可能出现worst-case,如果输入序列已经排序,则时间复杂度为O(N)
  平衡二叉树/红黑树就是为了将查找的时间复杂度保证在O(logN)范围内。
  所以如果输入结合确定,所需要的就是查询,则可以考虑使用哈希表,如果输入集合不确定,则考虑使用平衡二叉树/红黑树,保证达到最大效率

什么时候用map,什么时候用hash table?

* Hash tables have faster average lookup and insertion time (O(1)), while balanced binary trees have faster worst-case lookup and insertion time (O(log n) instead of O(n)). These make trees more useful in real-time and interactive systems, and in high-security systems where untrusted users may deliberately supply information that triggers worst-case performance in a hash table. Hash tables are more useful for very large arrays, where O(1) performance is important.
* Hash tables have more compact storage for small value types, especially when the values are bits.
* There are simple persistent versions of balanced binary trees, which are especially prominent in functional languages.
* Building a hash table requires a good hash function for the key type, which can be difficult to write, while balanced binary trees require a total ordering on the keys.
* Balanced binary trees preserve ordering -- allowing one to efficiently iterate over the keys in order or to efficiently locate an association whose key is nearest to a given value. Hash tables do not preserve ordering and therefore cannot perform these operations as efficiently.
* Balanced binary trees can be easily adapted to efficiently assign a single value to a large ordered range of keys, or to count the number of keys in an ordered range.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics