C Tree Terminology

Intermediate
6 min read
Updated: Nov 2026
1 example

The Vocabulary of Trees

The root is the top node. A node's children hang below it; its parent is above. Nodes without children are leaves. The depth of a node is the number of edges from the root; the height of a tree is the number of edges on the longest root-to-leaf path. A subtree is any node together with its descendants.

Remember
        1        root, depth 0
       / \
      2   3      children of 1
     / \
    4   5        leaves, depth 2

Cheat Sheet

Keep these facts handy while you work with Tree Terminology in C.

TopicDetail
RootNode with no parent
LeafNode with no children
DepthEdges from the root
HeightEdges on the longest path down

C Examples

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

Example 1 — Measure leaves, height, and depth

The tree has root 1, children 2 and 3, and leaves 4 and 5 under 2.

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 leaves(const struct T *t) {
    if (!t) return 0;
    if (!t->left && !t->right) return 1;
    return leaves(t->left) + leaves(t->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 depth(const struct T *t, int key, int d) {
    if (!t) return -1;
    if (t->key == key) return d;
    int x = depth(t->left, key, d + 1);
    return x >= 0 ? x : depth(t->right, key, d + 1);
}

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

    printf("leaves: %d\n", leaves(root));
    printf("height (edges): %d\n", height(root));
    printf("depth of node 5: %d\n", depth(root, 5, 0));

    free_tree(root);
    return 0;
}

How It Works

  1. Leaves are 4, 5, and 3.
  2. An empty tree has height -1 so a single node has height 0.
  3. Node 5 is two edges below the root (1 to 2 to 5).

Edge Cases & Pitfalls

Check these before relying on Tree Terminology in real programs.

def

Different height definitions

Some texts count nodes, others edges. State which you use.

depth

Depth versus height

Depth is measured from the root down; height from a node to its deepest leaf.

Key Takeaways

  • Root and leaves are the ends.
  • Depth goes down from the root.
  • Height of an empty tree: -1 (edges).

One line: The root is the top node.

Frequently Asked Questions

Depth is the number of edges from the root to the node; height is the number of edges on the longest path from the node down to a leaf.
A node with no children.
Continue with "C Binary Trees" next. Define a binary tree node and check structural properties such as fullness. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Binary Trees

Define a binary tree node and check structural properties such as fullness.

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