C Binary Trees

Intermediate
6 min read
Updated: Nov 2026
1 example

Two Children at Most

In a binary tree each node has at most two children, named left and right. Useful shapes include a full tree (every node has 0 or 2 children), a complete tree (all levels filled except possibly the last, filled left to right), and a perfect tree (all leaves at the same depth). A perfect tree of height h has 2^(h+1) - 1 nodes.

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

Cheat Sheet

Keep these facts handy while you work with Binary Trees in C.

TopicDetail
FullEvery node has 0 or 2 children
CompleteFilled left to right
PerfectAll leaves at same depth
Max nodes2^(h+1) - 1 for height h

C Examples

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

Example 1 — Test whether a tree is full

A full tree and a tree with a single-child node.

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

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

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

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

static int is_full(const struct T *t) {
    if (!t) return 1;
    if (!t->left && !t->right) return 1;
    if (t->left && t->right) return is_full(t->left) && is_full(t->right);
    return 0;
}

int main(void) {
    struct T *a = mk(1, mk(2, mk(4, NULL, NULL), mk(5, NULL, NULL)), mk(3, NULL, NULL));
    struct T *b = mk(1, mk(2, NULL, NULL), NULL);

    printf("tree A full: %s\n", is_full(a) ? "yes" : "no");
    printf("tree B full: %s\n", is_full(b) ? "yes" : "no");

    free_tree(a);
    free_tree(b);
    return 0;
}

How It Works

  1. Every node of tree A has either zero or two children.
  2. Tree B's root has only a left child.
  3. The check recurses into both subtrees only when both exist.

Edge Cases & Pitfalls

Check these before relying on Binary Trees in real programs.

mix

Confusing full and complete

Full is about child counts; complete is about level filling.

null

Checking only one child

t->left->key crashes when left is NULL.

Key Takeaways

  • At most two children.
  • Full, complete, perfect are different.
  • Recurse on both sides.

One line: In a binary tree each node has at most two children, named left and right.

Frequently Asked Questions

A tree in which every node has at most two children, called left and right.
A full tree has nodes with either 0 or 2 children. A complete tree has all levels filled except possibly the last, which is filled from the left.
Continue with "C Binary Tree Representation" next. Represent a binary tree with linked nodes or with array index formulas. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Binary Tree Representation

Represent a binary tree with linked nodes or with array index formulas.

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