Balanced Trees and AVL Basics in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Keeping Trees Shallow

A plain BST can degrade into a chain. An AVL tree is a BST that keeps the balance factor (height of left minus height of right) of every node in {-1, 0, 1}. When an insert breaks this, the tree is repaired with rotations. A left rotation at x lifts its right child y above it, making x the left child of y.

Remember
struct T *rotate_left(struct T *x) {
    struct T *y = x->right;
    x->right = y->left;
    y->left  = x;
    return y;          /* new subtree root */
}

Cheat Sheet

Keep these facts handy while you work with Balanced Trees and AVL Basics in C in C.

TopicDetail
Balance factorheight(left) - height(right)
Allowed-1, 0, +1
Left rotationFixes right-right heavy
ResultHeight O(log n)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Heaps.

Example 1 — Fix a right-heavy chain with one rotation

Keys 10, 20, 30 inserted in order form a chain.

C
#include <stdio.h>

struct T { int key; struct T *left, *right; };

static int height(const struct T *t) {
    if (!t) return -1;
    int a = height(t->left), b = height(t->right);
    return 1 + (a > b ? a : b);
}
static int balance(const struct T *t) {
    return height(t->left) - height(t->right);
}
static struct T *rotate_left(struct T *x) {
    struct T *y = x->right;
    x->right = y->left;
    y->left = x;
    return y;
}

int main(void) {
    struct T n30 = {30, NULL, NULL};
    struct T n20 = {20, NULL, &n30};
    struct T n10 = {10, NULL, &n20};

    struct T *root = &n10;
    printf("before: root=%d height=%d balance=%d\n",
           root->key, height(root), balance(root));

    root = rotate_left(root);
    printf("after left rotation: root=%d height=%d balance=%d\n",
           root->key, height(root), balance(root));
    return 0;
}

How It Works

  1. The chain 10, 20, 30 has height 2 and balance -2 (too right-heavy).
  2. Rotating left makes 20 the root with 10 and 30 as children.
  3. Height drops to 1 and the tree stays a valid BST.

Edge Cases & Pitfalls

Check these before relying on Balanced Trees and AVL Basics in C in real programs.

cases

Four imbalance cases

AVL also needs right rotation and the double rotations (left-right, right-left).

recompute

Recomputing heights

This demo recomputes heights recursively; real AVL nodes store their height.

Key Takeaways

  • Balance factor within one.
  • Rotations restructure locally.
  • Guarantees O(log n).

One line: A plain BST can degrade into a chain.

Frequently Asked Questions

A self-balancing BST in which the heights of the two subtrees of every node differ by at most one.
To prevent the tree from degrading into a linked list, which would make operations O(n) instead of O(log n).
Continue with "C Heaps" next. Implement binary min and max heaps in C: insert, delete, heapify, and building a heap in linear time. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Heaps

Implement binary min and max heaps in C: insert, delete, heapify, and building a heap in linear time.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful