0Pricing
C Academy · Lesson

Trees and Graphs

Explore tree and graph structures for hierarchical and networked data representations.

Trees and Graphs is a free C Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Trees and Graphs in C

Trees and graphs are non-linear data structures used to represent hierarchical and networked data.

In this lesson, you will learn:

  • How trees and graphs are structured.
  • How to implement a binary tree in C.
  • How to represent graphs using adjacency lists and matrices.
Trees and Graphs — illustration 1

2

What is a Tree?

A tree is a hierarchical data structure consisting of nodes.

Key terms:

  • Root - The top node.
  • Parent and Child - Nodes connected directly.
  • Leaf - A node with no children.

3

Example: Binary Tree Node

In C, a binary tree node is defined using a struct with left and right child pointers.

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

struct Node {
    int data;
    struct Node *left, *right;
};

struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}

int main() {
    struct Node *root = createNode(10);
    return 0;
}

4

Binary Tree Traversal

Traversal methods:

  • Inorder (LNR) - Left, Node, Right.
  • Preorder (NLR) - Node, Left, Right.
  • Postorder (LRN) - Left, Right, Node.

5

Example: Inorder Traversal

This program performs an inorder traversal of a binary tree.

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

struct Node {
    int data;
    struct Node *left, *right;
};

void inorder(struct Node *root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
}

int main() {
    struct Node *root = malloc(sizeof(struct Node));
    root->data = 10;
    root->left = NULL;
    root->right = NULL;
    inorder(root);
    return 0;
}

6

What is a Graph?

A graph is a collection of nodes (vertices) connected by edges.

Graphs can be:

  • Directed - Edges have direction.
  • Undirected - Edges have no direction.

7

Graph Representation

Graphs can be represented using:

  • Adjacency Matrix - A 2D array representing connections.
  • Adjacency List - A list where each node points to its neighbors.

8

9

Graph Traversal

Common traversal methods:

  • Breadth-First Search (BFS) - Visits all neighbors before moving deeper.
  • Depth-First Search (DFS) - Explores as deep as possible before backtracking.

10

Summary

In this lesson, you learned:

  • How trees and graphs are structured.
  • How to perform tree traversals.
  • How graphs are represented and traversed.

This concludes the Data Structures in C section!

Trees and Graphs — illustration 10

Frequently asked questions

Is the “Trees and Graphs” lesson free?

Yes — the full text of “Trees and Graphs” is free to read here on the web, and the C Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Trees and Graphs”?

Explore tree and graph structures for hierarchical and networked data representations. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Trees and Graphs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Linked Lists
  2. Stacks and Queues
  3. Trees and Graphs
← Back to C Academy