简体   繁体   English

在AVL树的二进制搜索树

[英]Binary search tree over AVL tree

As far as I know the time complexity between AVL trees and Binary Search Trees are the same in average case, with AVLs beating BSTs in worst case scenarios. 据我所知, AVL树和二进制搜索树之间的时间复杂度在平均情况下是相同的,在最坏的情况下AVL击败BST。 This gives me a hint that AVLs are always superior than BSTs in every possible way to interact with them, perhaps adding a little complexity when it comes to balance implementations. 这给了我一个提示,即AVL总是优于BST以各种可能的方式与它们进行交互,在平衡实现方面可能会增加一些复杂性。

Is there any reason anyone should use BSTs instead of AVLs in the first place? 是否有任何理由首先应该使用BST而不是AVL?

First, getting the best possible performance is not the ultimate goal of programming. 首先,获得最佳性能并不是编程的最终目标。 So, even if option B was always faster and consumed less memory than A, it doesn't mean it's always the better option, if it's more complicated. 因此,即使选项B总是更快并且消耗的内存少于A,但这并不意味着它总是更好的选择,如果它更复杂的话。 More complicated code takes longer to write, is harder to understand and is more likely to contain bugs. 更复杂的代码需要更长的时间来编写,更难理解并且更可能包含错误。 So, if the simpler but less efficient option A is good enough for you, then it means it's the better choice. 因此,如果更简单但效率更低的选项A对您来说足够好,那么这意味着它是更好的选择。

Now, if you want to compare AVL tree with simple binary search tree (BST) without balancing, then AVL will consume more memory (each node has to remember its balance factor) and each operation can be slower (because you need to maintain the balance factor and sometimes perform rotations). 现在,如果你想在没有平衡的情况下将AVL树与简单的二进制搜索树(BST)进行比较,那么AVL将消耗更多的内存(每个节点必须记住它的平衡因子)并且每个操作都可以更慢(因为你需要保持平衡因素,有时进行轮换)。

As you said, BST without balancing has a very bad (linear) worst case . 如你所说,没有平衡的BST有一个非常糟糕(线性)的最坏情况 But if you know that this worst case won't happen to you, or if you're okay if the operation is slow in rare cases, BST without balancing might be better than AVL. 但是如果你知道这种最坏的情况不会发生在你身上,或者如果在极少数情况下你的运作速度很慢,那么没有平衡的BST可能比AVL更好。

Since there is the added overhead of checking and updating balance factors and rotating nodes, insertion and deletion in AVL trees can be pretty slow when compared to non-balanced BST's. 由于检查和更新平衡因子和旋转节点会增加额外的开销,因此与非平衡BST相比,AVL树中的插入和删除可能相当慢。

Because of the tight balancing, search will never take linear-like time, so you'll probably want to use AVL trees in situations where searching is a more frequent operation than updating the tree. 由于紧密的平衡,搜索永远不会采取类似线性的时间,因此您可能希望在搜索比更新树更频繁的操作的情况下使用AVL树。

My assumption is: when you mention BST, you mean a BST without balancing. 我的假设是:当你提到BST时,你指的是没有平衡的BST。

It could be argued that if you need a navigatable data structure and you know your data won't be worst case (sorted) and is somewhat small, a BST (without balance) would be adequate. 可以说,如果你需要一个可导航的数据结构,并且你知道你的数据不是最坏情况(排序)并且有点小,那么BST(没有平衡)就足够了。

But more than likely that's a rare case. 但这很可能是罕见的情况。

AVL tree is also a BST but it can rebalance itself. AVL树也是一个BST,但它可以重新平衡自己。 This behavior makes it faster in worst cases. 这种行为使得在最坏的情况下更快。 It keeps rebalancing itself so in worst case it will consume O(log n ) time when the plain BST will take O(n). 它保持自我重新平衡,因此在最坏的情况下,当普通BST将采用O(n)时,它将消耗O(log n)时间。 So, the answer to your question: It is always better to implement AVL tree than just plain BST. 那么,你的问题的答案:实现AVL树总是比普通的BST更好。

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

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