C Non-linear Data Structures

Intermediate
6 min read
Updated: Nov 2026
1 example

Branching Relationships

In a non-linear data structure one element may connect to several others. A tree has a root and branches; each node has a parent (except the root) and any number of children. A graph allows arbitrary connections, including cycles. Heaps and hash tables are often grouped here too, because their logical shape is not a simple sequence.

Remember
struct Tree { int v; struct Tree *left, *right; };
/* tree : one parent, many children
   graph: any vertex can connect to any other */

Cheat Sheet

Keep these facts handy while you work with Non-linear Data Structures in C.

TopicDetail
TreeHierarchy, no cycles
GraphVertices and edges
HeapTree stored in an array
Hash tableKey-based lookup

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Static vs Dynamic Structures.

Example 1 — Count nodes and find the height of a tree

A small hand-built binary tree.

C
#include <stdio.h>

struct T { int v; struct T *l, *r; };

static int count(const struct T *t) {
    return t ? 1 + count(t->l) + count(t->r) : 0;
}
static int height(const struct T *t) {
    if (!t) return 0;
    int a = height(t->l), b = height(t->r);
    return 1 + (a > b ? a : b);
}

int main(void) {
    struct T n4 = {4, NULL, NULL}, n5 = {5, NULL, NULL}, n3 = {3, NULL, NULL};
    struct T n2 = {2, &n4, &n5};
    struct T n1 = {1, &n2, &n3};

    printf("nodes: %d\n", count(&n1));
    printf("height: %d\n", height(&n1));
    return 0;
}

How It Works

  1. Each node may have two children, so recursion mirrors the shape.
  2. The height is the number of levels, counting the root level.
  3. Longest path: 1 -> 2 -> 4 gives three levels.

Edge Cases & Pitfalls

Check these before relying on Non-linear Data Structures in real programs.

cycle

Cycles in graphs

Graph traversals need a visited set to avoid looping forever.

null

Forgetting empty children

Every recursive tree function needs a NULL base case.

Key Takeaways

  • One-to-many relationships.
  • Trees: no cycles.
  • Graphs: any connections.

One line: In a non-linear data structure one element may connect to several others.

Frequently Asked Questions

Structures where elements are not stored in a single sequence, such as trees and graphs.
A tree is a connected graph with no cycles and a single root. A general graph can have cycles and no root.
Continue with "C Static vs Dynamic Structures" next. Compare fixed-capacity structures with ones that grow at run time. The C Data Structures hub lists every topic in order.

Next: Static vs Dynamic Structures

Compare fixed-capacity structures with ones that grow at run time.

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