C Binary Tree Representation

Intermediate
6 min read
Updated: Nov 2026
1 example

Pointers or an Array

There are two common representations. Linked: each node is a struct with left and right pointers; it handles any shape. Array: store a (nearly) complete tree level by level; for 0-based index i, the children are at 2i + 1 and 2i + 2, and the parent is at (i - 1) / 2. Heaps use the array form.

Remember
left(i)   = 2 * i + 1
right(i)  = 2 * i + 2
parent(i) = (i - 1) / 2

Cheat Sheet

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

TopicDetail
LinkedFlexible, one malloc per node
ArrayCompact for complete trees
Left child2i + 1
Parent(i - 1) / 2

C Examples

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

Example 1 — Navigate an array tree

Seven values stored level by level.

C
#include <stdio.h>

int main(void) {
    int a[] = {10, 20, 30, 40, 50, 60, 70};
    int n = 7;

    int i = 1;
    int l = 2 * i + 1, r = 2 * i + 2;
    if (r < n) printf("node %d: left=%d right=%d\n", a[i], a[l], a[r]);

    int k = 5;
    printf("parent of %d is %d\n", a[k], a[(k - 1) / 2]);
    return 0;
}

How It Works

  1. Index 1 holds 20; its children sit at indexes 3 and 4 (40 and 50).
  2. Index 5 holds 60; its parent is (5 - 1) / 2 = 2, which holds 30.
  3. No pointers are needed.

Edge Cases & Pitfalls

Check these before relying on Binary Tree Representation in real programs.

gap

Sparse trees waste space

Missing nodes leave holes in the array.

base

1-based formulas

With 1-based arrays use 2i and 2i + 1. Do not mix.

Key Takeaways

  • Pointers: any shape.
  • Arrays: complete trees.
  • Index formulas replace links.

One line: There are two common representations.

Frequently Asked Questions

Level by level; the children of index i are at 2i+1 and 2i+2 (0-based), and the parent is at (i-1)/2.
When the tree is complete or nearly complete, as in a binary heap.
Continue with "C Preorder Traversal" next. Visit the root first, then the left and right subtrees. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Preorder Traversal

Visit the root first, then the left and right subtrees.

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