Binary search tree over AVL tree
Asked Answered
S

5

7

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. 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.

Is there any reason anyone should use BSTs instead of AVLs in the first place?

Storied answered 3/2, 2013 at 8:36 Comment(2)
AVL trees are out of fashion, nowadays they have been superseded by red-black trees.Cadman
@Cadman Not really true. RB trees haver faster insertion performance, but slower lookup performance. When dealing with huge amounts of nodes, AVL is faster even in insertion. Source: https://mcmap.net/q/240317/-difference-between-red-black-trees-and-avl-treesFortification
H
24

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. 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.

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).

As you said, BST without balancing has a very bad (linear) worst case. 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.

Huneycutt answered 3/2, 2013 at 14:20 Comment(1)
Exactly the reasons I was waiting for. Thank you and accepted.Storied
S
3

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.

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.

School answered 7/7, 2013 at 9:18 Comment(0)
O
0

My assumption is: when you mention BST, you mean a BST without balancing.

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.

But more than likely that's a rare case.

Obsolescent answered 3/2, 2013 at 14:1 Comment(0)
T
0

The AVL tree is a height-balanced tree where each node's right and left subtree height differences are either -1, 0, or 1. A factor known as the balance factor keeps the subtrees' height differences apart.

Tranquilize answered 26/12, 2023 at 18:26 Comment(0)
L
-1

AVL tree is also a BST but it can rebalance itself. 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). So, the answer to your question: It is always better to implement AVL tree than just plain BST.

Lamellar answered 4/3, 2013 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.