BST Search in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Following One Path

Searching a BST compares the target with the current node: equal means found, smaller means go left, larger means go right. A NULL child means the key is absent. It follows one path, so it is O(h), and can be written iteratively without extra stack space.

Remember
struct T *search(struct T *t, int k) {
    while (t && t->key != k)
        t = (k < t->key) ? t->left : t->right;
    return t;
}

Cheat Sheet

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

TopicDetail
HitEqual key
MissReached NULL
TimeO(h)
StyleIteration is simple and safe

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: BST Deletion in C.

Example 1 — Search with comparison counts

Find 60 and fail to find 45.

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 int search(const struct T *t, int k, int *steps) {
    *steps = 0;
    while (t) {
        (*steps)++;
        if (k == t->key) return 1;
        t = k < t->key ? t->left : t->right;
    }
    return 0;
}

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 steps;
    if (search(root, 60, &steps)) printf("found 60 after %d comparisons\n", steps);
    if (!search(root, 45, &steps)) printf("45 not found after %d comparisons\n", steps);

    free_tree(root);
    return 0;
}

How It Works

  1. 60: 50, then 70, then 60 is found.
  2. 45: 50, then 30, then 40, then the right child of 40 is empty.
  3. Each step discards a whole subtree.

Edge Cases & Pitfalls

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

null

Dereferencing a miss

Check the result before using it.

unbal

Skewed trees

Search degrades to O(n).

Key Takeaways

  • One path.
  • Miss = NULL.
  • O(h).

One line: Searching a BST compares the target with the current node: equal means found, smaller means go left, larger means go right.

Frequently Asked Questions

Compare the key with the node and move left or right until it is found or a NULL child is reached.
Yes for balanced trees: O(log n) versus O(n).
Continue with "BST Deletion in C" next. Delete leaf, one-child, and two-child nodes using the inorder successor. The C Trees and Binary Search Trees hub lists every topic in order.

Next: BST Deletion in C

Delete leaf, one-child, and two-child nodes using the inorder successor.

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.

8 people found this page helpful