AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs

AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs

AVL trees are self-balancing binary search trees that prevent the tree from becoming unbalanced. We compute balance factors as the absolute value of left subtree height minus right subtree height. If any node has a balance factor of 2 or worse, we rebalance using rotations on trinode subtrees. This keeps search, insert, and other operations efficient at logarithmic time.