In the realm of self‑balancing binary search trees, the AVL tree stands out as one of the earliest and most widely studied structures. Knowing how to calculate the balance factor in an AVL tree is essential for maintaining its height‑balance property, which guarantees O(log n) search, insertion, and deletion times.
What Is an AVL Tree?
An AVL tree is a type of binary search tree where the difference in height between the left and right subtrees of any node—known as the balance factor—is restricted to the values –1, 0, or +1. This strict constraint ensures that the tree remains approximately balanced after each insertion or deletion, preventing degeneration into a linked list. The name “AVL” comes from its inventors, Adelson‑Velsky and Landis, who introduced the concept in 1962.
Understanding Balance Factor
Definition
The balance factor of a node in an AVL tree is defined as the height of its right subtree minus the height of its left subtree. In formula notation:
[ \text{BalanceFactor}(N) = \text{height}(\text{right}(N)) - \text{height}(\text{