Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3: AVL Trees & M-Way Search Trees

Lesson 9 of 14 in the free Data Structure notes on Siksha Sarovar, written by Rohit Jangra.

AVL Trees: The Need for Balance

The Problem: A BST fed sorted data (10, 20, 30, 40) degenerates into a linked list. Search becomes O(n) instead of O(log n).

BST vs AVL — Comparison

FeatureBSTAVL Tree
BalanceNo guaranteeAlways balanced (BF ∈ {-1, 0, +1})
SearchO(log n) avg, O(n) worstO(log n) always
InsertO(log n) avg, O(n) worstO(log n) + rotation overhead
DeleteO(log n) avg, O(n) worstO(log n) + rotation overhead
HeightCan be n (skewed)Always ≤ 1.44 log₂(n)
OverheadNoneMust maintain heights + rotations
Best ForRandom dataWhen worst-case guarantee needed

AVL Balance Factor

Balance Factor (BF) = Height(Left Subtree) - Height(Right Subtree)

BF ValueMeaningAction
0Perfectly balancedNo action needed
+1Left-heavy by 1Acceptable
-1Right-heavy by 1Acceptable
+2 or moreLeft-heavy violationRotation needed
-2 or lessRight-heavy violationRotation needed

The 4 Rotation Cases

CaseImbalance PatternFixSteps
LL (Left-Left)Inserted in left subtree of left childSingle Right RotationLift left child up
RR (Right-Right)Inserted in right subtree of right childSingle Left RotationLift right child up
LR (Left-Right)Inserted in right subtree of left childLeft Rotation then Right RotationDouble rotation
RL (Right-Left)Inserted in left subtree of right childRight Rotation then Left RotationDouble rotation

Worked Example: AVL Insertion with Rotation

Insert sequence: 30, 20, 10

Step 1: Insert 30 → [30] (BF = 0) Step 2: Insert 20 → 30 has BF = +1 (acceptable) Step 3: Insert 10 → 30 has BF = +2LL Case!

Before rotation:

    30 (BF=+2)            20 (BF=0)
   /                     /  \
  20 (BF=+1)    →      10    30
 /
10

After Right Rotation: Tree is balanced with height 1 instead of 2.

M-Way Search Trees

An M-Way Search Tree (order M) allows each node to have up to M children and M-1 keys.

M-Way Properties

PropertyBinary TreeM-Way Tree (Order M)
Max Children2M
Max Keys per Node1M - 1
Height (n items)O(log₂ n)O(log_M n)
Disk Reads (n=10⁶)~20~3-4 (M=100)
Best ForIn-memory dataDisk-based storage

Node Structure: [ P0, K1, P1, K2, P2 ... Km-1, Pm ]

  • Keys (K): Sorted values inside the node.
  • Pointers (P): Links to sub-trees.
  • All values in P0's subtree < K1 < all values in P1's subtree < K2 ...