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 nodes, the height of the tree is always of the order of — 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 ( time).
Among these, AVL is the most popular method.
Drawing the diagram…
How it works
- Every balanced binary search tree is also a binary search tree (it satisfies all BST properties).
- The distinguishing property is the height: in a balanced BST, height always; in an ordinary BST that is not balanced, the height can be as large as .
- The height directly controls the search time: if height , search time ; if height , search time comes down to .
- So the whole purpose of balancing is to keep the height logarithmic, which keeps searching fast.
- Several schemes achieve this: AVL trees, red-black trees, 2-3 trees, Fibonacci heaps, etc.
- 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
-
— height of a balanced BST with nodes
-
— height of an unbalanced (ordinary) BST with nodes (worst case)
-
— balanced: ; unbalanced:
-
Here = number of nodes in the tree, = height of the tree.
Comparison
| Property | Ordinary Binary Search Tree | Balanced Binary Search Tree (e.g., AVL) |
|---|---|---|
| Is it a BST? | Yes | Yes (every BBST is also a BST) |
| Height ( nodes) | Can be if not balanced | Always |
| Search time | in the worst (unbalanced) case |
Revision summary
- AVL tree = balanced binary search tree, named after three Russian scientists.
- Balanced BST guarantee: for nodes, height .
- Every balanced BST is a BST, but not every BST is balanced.
- Unbalanced BST worst-case height is , giving search; balanced height gives 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 (more precisely h < 1.4405 − 0.3277); minimum height is ceil() (floor() 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 worst case; building an AVL tree by n successive insertions takes 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 .
Asked inUGC NET 2023 - Search cost in a BST equals Θ(height); a balanced BST with n nodes has height , so searching is worst case, whereas an unbalanced BST can degenerate to .
Asked inGATE 2012GATE 2008