1. Gyanastra
  2. Data Structures
  3. AVL Trees
  4. Balanced Binary Search Trees and the AVL Tree Definition
Free lesson, no account needed 4 min read 13 practice questions after it
Asked in past papers16 times
  • GATE CSE202020122009200820052002199619901988
  • ISRO202020172013
  • UGC NET2023202220122005

Data Structures · AVL Trees

Balanced Binary Search Trees and the AVL Tree Definition

Overview

An AVL tree is a balanced binary search tree (BBST) named after the three Russian scientists who discovered it.
Balancing keeps the height of the tree logarithmic, which keeps search time logarithmic; this is the core motivation for the AVL scheme and the other balanced structures in its family.

Key concepts

  • Balanced binary search tree (BBST): a binary search tree in which, for nn nodes, the height of the tree is always of the order of log⁡n\log n — that is what "balanced" means.
  • AVL tree: a balanced binary search tree; it is one example of a balanced BST.
  • Balance factor: the quantity defined for every node on which the whole AVL idea rests; it is computed from the heights of the node's left and right subtrees.
  • Other balanced BST / fast-search structures in the same family: red-black trees, 2-3 trees, Fibonacci heaps, and many others. All of them exist to help search data very fast, i.e. in logarithmic time (log⁡n\log n time).
    Among these, AVL is the most popular method.
Figure. The balance factor of a node comes from the heights of its left and right subtrees. In an AVL tree it must be minus 1, 0 or plus 1, otherwise rotations restore balance and search stays O of log n.

How it works

  1. Every balanced binary search tree is also a binary search tree (it satisfies all BST properties).
  2. The distinguishing property is the height: in a balanced BST, height =O(log⁡n)= O(\log n) always; in an ordinary BST that is not balanced, the height can be as large as O(n)O(n).
  3. The height directly controls the search time: if height =O(n)= O(n), search time =O(n)= O(n); if height =O(log⁡n)= O(\log n), search time comes down to O(log⁡n)O(\log n).
  4. So the whole purpose of balancing is to keep the height logarithmic, which keeps searching fast.
  5. Several schemes achieve this: AVL trees, red-black trees, 2-3 trees, Fibonacci heaps, etc.
  6. AVL trees achieve balance by defining, for every node, a balance factor, computed from the heights of the node's left and right subtrees.

Formulas

  • h=O(log⁡n)h = O(\log n) — height of a balanced BST with nn nodes

  • h=O(n)h = O(n) — height of an unbalanced (ordinary) BST with nn nodes (worst case)

  • search time=O(height of the tree)\text{search time} = O(\text{height of the tree}) — balanced: O(log⁡n)O(\log n); unbalanced: O(n)O(n)

  • Here nn = number of nodes in the tree, hh = height of the tree.

Comparison

PropertyOrdinary Binary Search TreeBalanced Binary Search Tree (e.g., AVL)
Is it a BST?YesYes (every BBST is also a BST)
Height (nn nodes)Can be O(n)O(n) if not balancedAlways O(log⁡n)O(\log n)
Search timeO(n)O(n) in the worst (unbalanced) caseO(log⁡n)O(\log n)

Revision summary

  • AVL tree = balanced binary search tree, named after three Russian scientists.
  • Balanced BST guarantee: for nn nodes, height =O(log⁡n)= O(\log n).
  • Every balanced BST is a BST, but not every BST is balanced.
  • Unbalanced BST worst-case height is O(n)O(n), giving O(n)O(n) search; balanced height O(log⁡n)O(\log n) gives O(log⁡n)O(\log n) search.
  • The height of the tree determines the search time — this is the core motivation for balancing.
  • Other balanced structures to remember: red-black tree, 2-3 tree, B-tree.
  • AVL is the most popular balanced BST method.
  • The whole AVL idea rests on the balance factor of a node — memorise this term.

Previous-year exam insights

  • Balance factor of a node = height(left subtree) − height(right subtree);
    in an AVL tree it must be −1, 0 or +1 for every node, otherwise rotations (LL, RR, LR, RL) restore balance.
    Asked inUGC NET 2022GATE 1996
  • Maximum height of an AVL tree with n nodes is about 1.44 log⁡2n\log_{2} n (more precisely h < 1.4405 log⁡2(n+2)\log_{2}(n+2) − 0.3277); minimum height is ceil(log⁡2(n+1)\log_{2}(n+1)) (floor(log⁡2n\log_{2} n) for the number-of-edges convention).
    Asked inISRO 2020GATE 2009ISRO 2017
  • Minimum number of nodes in an AVL tree of height h is N(h) = N(h−1) + N(h−2) + 1 with N(0)=1, N(1)=2 (Fibonacci-type recurrence), giving the maximum-height bound.
    Asked inGATE 2009ISRO 2017
  • Insertion, deletion and search in an AVL tree with n nodes each take Θ(log⁡n)\Theta(\log n) worst case; building an AVL tree by n successive insertions takes Θ(nlog⁡n)\Theta(n \log n) total.
    Asked inGATE 2020GATE 2012GATE 2008ISRO 2017
  • AVL trees are more strictly balanced (height difference ≤ 1) than red-black trees, so they need more rotations on insertion/deletion but give faster searches; a red-black tree with n nodes has height at most 2log⁡2(n+1)2 \log_{2}(n+1).
    Asked inUGC NET 2023
  • Search cost in a BST equals Θ(height); a balanced BST with n nodes has height Θ(log⁡n)\Theta(\log n), so searching is Θ(log⁡n)\Theta(\log n) worst case, whereas an unbalanced BST can degenerate to Θ(n)\Theta(n).
    Asked inGATE 2012GATE 2008
Pages 1–2 of 2