1. Gyanastra
  2. Data Structures
  3. AVL Trees
  4. Balance Factor of a Node
Free lesson, no account needed 6 min read 7 practice questions after it
Asked in past papers6 times
  • GATE CSE200919961988
  • ISRO2013
  • UGC NET20062005

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 −1-1, 00, or +1+1. These three values are the only ones allowed in an AVL tree.
  • Unbalanced node: a node whose BF is −2-2 or +2+2 (anything outside {−1,0,+1}\{-1, 0, +1\}). 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 11 (height starts from 11, not 00); a node one level above a leaf has height 22, the next level 33, and so on.
    Most problems start height from 00 (leaf height 00), which is also fine — always use the convention the problem states.
  • Empty subtree height: under the convention used here, an empty subtree has height 00.
  • A leaf node always has BF =0= 0; a node whose two subtrees have equal height also has BF =0= 0.
Figure. Node 3 has BF 2 because its left subtree has height 2 and its right subtree is empty. Node 2 has BF 1 and leaf 1 has BF 0, so the root is unbalanced.

How it works

  1. For a given node, compute the height of its left subtree.
  2. Compute the height of its right subtree.
  3. Subtract: BF=(height of left subtree)−(height of right subtree)\text{BF} = (\text{height of left subtree}) - (\text{height of right subtree}).
  4. Check the value:
    • BF =−1= -1, 00, or +1+1 → the node is balanced (allowed in an AVL tree).
    • BF =−2= -2 or +2+2 → the tree is unbalanced at that node.
  5. When a node becomes unbalanced (BF =±2= \pm 2), the tree must be rebalanced.

Formulas

  • BF(node)=H(left subtree of node)−H(right subtree of node)\text{BF}(\text{node}) = H(\text{left subtree of node}) - H(\text{right subtree of node})

  • BF\text{BF} = balance factor of the node

  • H(left subtree)H(\text{left subtree}) = height of the node's left subtree

  • H(right subtree)H(\text{right subtree}) = height of the node's right subtree

  • H=0H = 0 if the subtree is empty; under this problem's convention, H(leaf)=1H(\text{leaf}) = 1.

  • Balanced node condition: BF∈{−1,0,+1}\text{BF} \in \{-1, 0, +1\}. If BF∉{−1,0,+1}\text{BF} \notin \{-1, 0, +1\}, the node is unbalanced.

Worked examples

Example 1 — leaf node

  • A leaf has no left subtree and no right subtree, so BF=0\text{BF} = 0.
  • 00 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): BF=0\text{BF} = 0.
  • Node 2: height of left subtree =0= 0, height of right subtree =0= 0 → BF=0−0=0\text{BF} = 0 - 0 = 0 → valid, no problem.

Example 3 — left-left chain 3 → 2 → 1

        3
       /
      2
     /
    1
NodeHeight of left subtreeHeight of right subtreeBF = left − right
1 (leaf)0 (no left child)0 (no right child)0
21 (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 00; no right child → right height 00; BF(1)=0−0=0\text{BF}(1) = 0 - 0 = 0.
  • Node 2: left subtree is the leaf 1 → height 11; no right subtree → height 00; BF(2)=1−0=1\text{BF}(2) = 1 - 0 = 1.
  • Node 3: left subtree rooted at 2 has height 22 (leaf 1 has height 11, node 2 has height 22); no right subtree → height 00; BF(3)=2−0=2\text{BF}(3) = 2 - 0 = 2.
  • Result: BF values are 22, 11, 00 for nodes 3, 2, 1 respectively. Node 3 has BF =2= 2, outside {−1,0,+1}\{-1, 0, +1\}, so the tree is unbalanced at the root.

Comparison

Height conventionHeight of a leafEffect on BF values
Starts from 0 (most problems)0BF computed with empty subtree = −1-1 (under the 0-based convention an empty subtree is taken as height −1-1)
Starts from 1 (this problem)1Empty subtree height = 0; BF of a leaf = 0

Both conventions are acceptable; always use the one stated in the problem.

Revision summary

  • BF(node)=height(left subtree)−height(right subtree)\text{BF}(\text{node}) = \text{height}(\text{left subtree}) - \text{height}(\text{right subtree}).
  • Only three values are permitted in an AVL tree: −1-1, 00, +1+1.
  • BF =−2= -2 or +2+2 means the tree has become unbalanced and must be balanced.
  • A leaf node always has BF =0= 0; equal-height subtrees also give BF =0= 0.
  • In this problem, height of a leaf =1= 1 and height of an empty subtree =0= 0.
  • In the chain 3 → 2 → 1, the balance factors are 22 (root), 11, 00 (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 O(log⁡n)O(\log n).
  • 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
Pages 1–2 of 2