Knowledge / Computer Science and Engineering / Balanced trees: AVL, red-black and B-trees

Balanced trees: AVL, red-black and B-trees

Computer Science and Engineering (B.E./B.Tech) · Data structures · free study notes for exams, placements and interviews

In short

A balanced tree is a search tree that keeps its height at O(log n) by restructuring itself after every insertion and deletion, so search, insert and delete never degrade to the O(n) worst case of a skewed binary search tree. AVL trees balance by height difference, red-black trees by colour rules that allow looser balance but cheaper updates, and B-trees / B+ trees balance by keeping many keys per node so that a tree over millions of records is only three or four disk blocks deep. This topic is examined in university Data Structures papers (AICTE/CBCS "Trees" unit) and in GATE — AVL rotations and height bounds under Data Structures, B and B+ tree indexing explicitly under DBMS file organisation (exact syllabus wording changes between years, so check the current one). In industry these are the data structures inside std::map, Java's TreeMap, the Linux kernel's rbtree, and every relational index in MySQL InnoDB, PostgreSQL and Oracle — which is why Oracle, Amazon, Microsoft IDC, Zoho, Flipkart and core-banking product teams ask them in technical rounds.

Explained

Why balance at all, and what "balanced" means

AVL trees (Adelson-Velsky and Landis, 1962)

// single left rotation (RR case), node z with right child y
y = z.right;  z.right = y.left;  y.left = z;
z.h = 1 + max(h(z.left), h(z.right));
y.h = 1 + max(h(y.left), h(y.right));
return y;            // y is the new subtree root

Red-black trees

B-trees

Comparison and choosing one

FeatureAVLRed-blackB-tree / B+ tree
Keys per node11up to m−1 (hundreds)
Balance rule\BF\≤ 1colour rules 1-5all leaves at same level, nodes ≥ half full
Height bound≤ 1.44 log₂(n+2) − 1.33≤ 2 log₂(n+1)≤ log_⌈m/2⌉((n+1)/2)
Rotations / insert≤ 1 (single or double)≤ 20 (splits instead)
Rotations / deleteO(log n)≤ 30 (borrow/merge)
Extra storageheight or BF1 colour bitchild pointers, slack space
Best forlookup-heavy in-memoryupdate-heavy in-memorydisk/SSD indexes, range queries
Real usesymbol tables, in-memory DB indexesstd::map, TreeMap, Linux rbtreeMySQL, PostgreSQL, Oracle, file systems

Key points

Formulas and examples

BF(x) = h(left(x)) − h(right(x)), AVL requires BF(x) ∈ {−1, 0, +1} for all x. h(empty) = −1.
Minimum nodes in an AVL tree of height h: N(h) = N(h−1) + N(h−2) + 1, with N(0) = 1, N(1) = 2 ⇒ N(h) = F(h+3) − 1, F = Fibonacci (1, 1, 2, 3, 5, …).
AVL height bound: h ≤ 1.4404 log₂(n + 2) − 1.328 (h in edges, n = number of nodes).
Nodes in a binary tree of height h: n ≤ 2h+1 − 1; so h ≥ ⌈log₂(n+1)⌉ − 1 for any BST.
Red-black: bh(x) ≥ h(x)/2 and n ≥ 2bh − 1 ⇒ h ≤ 2 log₂(n + 1).
B-tree, min degree t (order m = 2t): keys per node t−1 ≤ k ≤ 2t−1; children t ≤ c ≤ 2t; height h ≤ log_t((n+1)/2).
B-tree of order m, height h (root at level 0): max keys = mh+1 − 1; min keys = 2·⌈m/2⌉h − 1.
Fanout from a disk page: (m−1)·(key size) + m·(pointer size) ≤ page size, solve for integer m.

Example 1 — AVL insertions with rotations. Insert 10, 20, 30, 40, 50, 25 into an empty AVL tree.

  1. 10, 20 → fine. Insert 30: node 10 has BF = −1 − 1 = −2, path right-right → RR, left rotate at 10 → root 20, children 10 and 30.
  2. Insert 40: BF(20) = 0 − 1 = −1 → still AVL, no rotation.
  3. Insert 50: node 30 has BF = −1 − 1 = −2, right-right → left rotate at 30 → subtree 40 (30, 50). Tree: 20 (10, 40 (30, 50)).
  4. Insert 25: it goes 20 → 40 → 30 → left. Now BF(30) = +1, BF(40) = 1−0 = +1, BF(20) = 0 − 2 = −2; the new key went into the left subtree of the right child → RL case: right-rotate at 40, then left-rotate at 20.
  5. Final tree: 30 (20 (10, 25), 40 (−, 50)) — height 2 edges; BF(30) = 0, BF(20) = 0, BF(40) = −1, leaves 0. One double rotation, i.e. two single rotations.

Example 2 — worst-case AVL height for n = 1000. Build N(h):

h01234567891011121314
N(h)124712203354881432323766099861596

Example 3 — red-black bounds. A red-black tree stores n = 15 keys.

Example 4 — B-tree fanout and disk accesses. A disk page is 4096 bytes, each key is 10 bytes and each child pointer 6 bytes. Ignore per-page overhead.

Example 5 — B-tree insertion with splits (order 3: max 2 keys, min 1 key). Insert 10, 20, 30, 40, 50.

  1. [10] → [10, 20].
  2. Insert 30 → [10, 20, 30] overflows (3 keys > 2). Median 20 moves up: root [20], children [10] and [30].
  3. Insert 40 → right leaf [30, 40]. Insert 50 → [30, 40, 50] overflows; median 40 moves up to the root: root [20, 40], leaves [10], [30], [50].
  4. Result: height 1, 3 leaves all at the same level, every node has ≥ 1 key. Inserting 60 then 70 would split again and push 60 up, overflowing the root [20, 40, 60] and creating a new root [40] — height becomes 2.

Where it is used

Interview questions

Common mistakes

Related topics

Trees and binary search trees · Heaps and priority queues · Hashing and hash tables · Segment trees and Fenwick trees · Tries and string data structures · Database indexing and file organisation

Open in the Knowledge reader →Find jobs →