BST Deletion in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Three Deletion Cases

Deleting from a BST has three cases. Leaf: remove it. One child: replace the node with its child. Two children: copy the inorder successor (smallest key in the right subtree) into the node, then delete the successor from the right subtree. The ordering is preserved in every case.

Remember
if (!t->left)  { r = t->right; free(t); return r; }
if (!t->right) { l = t->left;  free(t); return l; }
s = min_node(t->right);
t->key = s->key;
t->right = del(t->right, s->key);

Cheat Sheet

Keep these facts handy while you work with BST Deletion in C in C.

TopicDetail
LeafJust remove
One childSplice in the child
Two childrenReplace with inorder successor
TimeO(h)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Balanced Trees and AVL Basics in C.

Example 1 — Delete a leaf, a two-child node, and a one-child node

Start from keys 50, 30, 70, 20, 40, 60, 80.

C
#include <stdio.h>
#include <stdlib.h>

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

static struct T *mk(int k) {
    struct T *t = malloc(sizeof *t);
    if (!t) exit(1);
    t->key = k;
    t->left = t->right = NULL;
    return t;
}

static int insert(struct T **root, int k) {
    struct T **pp = root;
    while (*pp) {
        if (k == (*pp)->key) return 0;
        pp = k < (*pp)->key ? &(*pp)->left : &(*pp)->right;
    }
    *pp = mk(k);
    return 1;
}

static void inorder(const struct T *t) {
    if (!t) return;
    inorder(t->left);
    printf(" %d", t->key);
    inorder(t->right);
}

static void free_tree(struct T *t) {
    if (!t) return;
    free_tree(t->left);
    free_tree(t->right);
    free(t);
}

static struct T *min_node(struct T *t) {
    while (t->left) t = t->left;
    return t;
}

static struct T *del(struct T *t, int k) {
    if (!t) return NULL;
    if (k < t->key) t->left = del(t->left, k);
    else if (k > t->key) t->right = del(t->right, k);
    else {
        if (!t->left)  { struct T *r = t->right; free(t); return r; }
        if (!t->right) { struct T *l = t->left;  free(t); return l; }
        struct T *s = min_node(t->right);
        t->key = s->key;
        t->right = del(t->right, s->key);
    }
    return t;
}

int main(void) {
    int keys[] = {50, 30, 70, 20, 40, 60, 80};
    struct T *root = NULL;
    for (int i = 0; i < 7; i++) insert(&root, keys[i]);

    int victims[] = {20, 70, 30};
    for (int i = 0; i < 3; i++) {
        root = del(root, victims[i]);
        printf("delete %d:", victims[i]);
        inorder(root);
        printf("\n");
    }

    free_tree(root);
    return 0;
}

How It Works

  1. 20 is a leaf and is removed directly.
  2. 70 has two children; its successor 80 replaces it and the old 80 node is deleted.
  3. 30 now has only a right child (40), which takes its place.

Edge Cases & Pitfalls

Check these before relying on BST Deletion in C in real programs.

root

Not reassigning the root

Deletion can change the root, so assign root = del(root, k).

uaf

Using the freed node

Save the child pointer before free.

Key Takeaways

  • Three cases.
  • Two children: inorder successor.
  • Reassign the returned pointer.

One line: Deleting from a BST has three cases.

Frequently Asked Questions

Replace its key with the smallest key of the right subtree (its inorder successor), then delete that successor node.
Yes, the largest key in the left subtree works equally well.
Continue with "Balanced Trees and AVL Basics in C" next. Balance factors and rotations that keep a BST near O(log n). The C Trees and Binary Search Trees hub lists every topic in order.

Next: Balanced Trees and AVL Basics in C

Balance factors and rotations that keep a BST near O(log n).

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.

9 people found this page helpful