简体   繁体   English

什么时候AVL树比哈希表更好?

[英]When is an AVL tree better than a hash table?

更具体地说,如果使用AVL树而不是哈希表,是否有任何操作可以更有效地执行?

I generally prefer AVL trees to hash tables. 我通常更喜欢AVL树而不是哈希表。 I know that the expected-time O(1) complexity of hash tables beats the guaranteed-time O(log n) complexity of AVL trees, but in practice constant factors make the two data structures generally competitive, and there are no niggling worries about some unexpected data that evokes bad behavior. 我知道哈希表的预期时间O(1)复杂度超过了AVL树的保证时间O(log n)复杂度,但是在实践中,恒定因素使这两个数据结构普遍具有竞争力,并且没有任何烦恼一些引起不良行为的意外数据。 Also, I often find that sometime during the maintenance life of a program, in a situation not foreseen when the initial choice of a hash table seemed right, that I need the data in sorted order, so I end up rewriting the program to use an AVL tree instead of a hash table; 另外,我经常发现,在程序维护期间的某个时候,在无法预料哈希表的初始选择似乎正确的情况下,我需要按排序的数据,因此我最终重写了程序以使用AVL树代替哈希表; do that enough times, and you learn that you may as well just start with AVL trees. 这样做足够的时间,您就会了解到,您最好还是从AVL树开始。

If your keys are strings, ternary search tries offer a reasonable alternative to AVL trees or hash tables. 如果您的键是字符串,则三元搜索尝试可以为AVL树或哈希表提供合理的替代方法。

An obvious difference, of course, is that with AVL trees (and other balanced trees), you can have persistency : you can insert/remove an element from the tree in O(log N) space-and-time and end up with not just the new tree, but also get to keep the old tree. 当然,一个明显的区别是,对于AVL树(和其他平衡树),您可以具有持久性 :您可以在O(log N)时空中从树中插入/删除元素,最后得到not不仅是新树,而且还有保留旧树的方法。

With a hash-table, you generally cannot do that in less than O(N) time-and-space. 对于哈希表,通常不能在少于O(N)的时间和空间上做到这一点。

Another important difference is the operations needed on the keys: AVL tress need a <= comparison between keys, whereas hash-tables need an = comparison as well as a hash function. 另一个重要的区别是键上所需的操作:AVL分支需要键之间的<=比较,而散列表需要=比较以及hash函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM