Data Structures · AVL Trees
Balance Factor of a Node
Overview
The balance factor (BF) of a node measures how much taller its left subtree is than its right subtree.
It is the quantity that decides whether a node in an AVL tree is balanced, so computing it correctly — under the height convention the problem states — is the first step in detecting an unbalanced tree.
Key concepts
- Balance factor (BF) of a node: the height of its left subtree minus the height of its right subtree.
- Balanced node: a node whose BF is , , or . These three values are the only ones allowed in an AVL tree.
- Unbalanced node: a node whose BF is or (anything outside ). The tree must then be rebalanced.
- Height convention: the height of a leaf depends on the convention chosen for the problem.
In this problem the height of a leaf is (height starts from , not ); a node one level above a leaf has height , the next level , and so on.
Most problems start height from (leaf height ), which is also fine — always use the convention the problem states. - Empty subtree height: under the convention used here, an empty subtree has height .
- A leaf node always has BF ; a node whose two subtrees have equal height also has BF .
Drawing the diagram…
How it works
- For a given node, compute the height of its left subtree.
- Compute the height of its right subtree.
- Subtract: .
- Check the value:
- BF , , or → the node is balanced (allowed in an AVL tree).
- BF or → the tree is unbalanced at that node.
- When a node becomes unbalanced (BF ), the tree must be rebalanced.
Formulas
-
-
= balance factor of the node
-
= height of the node's left subtree
-
= height of the node's right subtree
-
if the subtree is empty; under this problem's convention, .
-
Balanced node condition: . If , the node is unbalanced.
Worked examples
Example 1 — leaf node
- A leaf has no left subtree and no right subtree, so .
- is allowed → balanced.
Example 2 — insertion order 1, 2, 3 (partial check)
- Structure after inserting 1, 2, 3: 1 is the root; 2 is the right child of 1; 3 is the right child of 2.
- Node 3 (leaf): .
- Node 2: height of left subtree , height of right subtree → → valid, no problem.
Example 3 — left-left chain 3 → 2 → 1
3
/
2
/
1
| Node | Height of left subtree | Height of right subtree | BF = left − right |
|---|---|---|---|
| 1 (leaf) | 0 (no left child) | 0 (no right child) | 0 |
| 2 | 1 (node 1) | 0 (no right subtree) | 1 |
| 3 (root) | 2 (subtree rooted at 2) | 0 (no right subtree) | 2 |
- Node 1: no left child → left height ; no right child → right height ; .
- Node 2: left subtree is the leaf 1 → height ; no right subtree → height ; .
- Node 3: left subtree rooted at 2 has height (leaf 1 has height , node 2 has height ); no right subtree → height ; .
- Result: BF values are , , for nodes 3, 2, 1 respectively. Node 3 has BF , outside , so the tree is unbalanced at the root.
Comparison
| Height convention | Height of a leaf | Effect on BF values |
|---|---|---|
| Starts from 0 (most problems) | 0 | BF computed with empty subtree = (under the 0-based convention an empty subtree is taken as height ) |
| Starts from 1 (this problem) | 1 | Empty subtree height = 0; BF of a leaf = 0 |
Both conventions are acceptable; always use the one stated in the problem.
Revision summary
- .
- Only three values are permitted in an AVL tree: , , .
- BF or means the tree has become unbalanced and must be balanced.
- A leaf node always has BF ; equal-height subtrees also give BF .
- In this problem, height of a leaf and height of an empty subtree .
- In the chain 3 → 2 → 1, the balance factors are (root), , (leaf) — the root is unbalanced.
- Always check the height convention given in the problem before computing balance factors.
Previous-year exam insights
- Minimum number of nodes in an AVL tree of height h: N(h) = N(h−1) + N(h−2) + 1, with N(0) = 1 and N(1) = 2 (0-based height convention).
This recurrence gives the maximum possible height for a given number of nodes.
Asked inGATE 2009 - The four imbalance cases and their fixes: LL (left-left) → single right rotation; RR (right-right) → single left rotation; LR (left-right) → left rotation on left child then right rotation on node; RL (right-left) → right rotation on right child then left rotation on node.
Asked inISRO 2013 - After an insertion, only the nodes on the path from the inserted node up to the root can have their balance factor changed; rebalancing the lowest unbalanced node (via the appropriate single or double rotation) restores balance for the whole tree, so an insertion needs at most one rebalancing (one single rotation or one double rotation).
Asked inUGC NET 2005 - In an AVL tree, the balance factor of every node is −1, 0, or +1;
a node with BF = ±2 is unbalanced and triggers a rotation.
The height of an AVL tree with n nodes is . - When counting rotations for a sequence of insertions, perform insertions one by one, updating balance factors along the path; a single insertion may require one single rotation (LL or RR) or one double rotation (LR or RL), and rotations can change the structure so later insertions may need no further rotation.
Asked inISRO 2013 - The height of a leaf is 0 under the common convention (single node height 0), and an empty subtree has height −1; always use the convention stated in the problem, as the balance factor values are unaffected by a uniform shift of the height convention.
Asked inGATE 2009