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. This is 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, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.
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.

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.
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;
}Binary Tree Traversal
Traversal methods:
- Inorder (LNR) - Left, Node, Right.
- Preorder (NLR) - Node, Left, Right.
- Postorder (LRN) - Left, Right, Node.
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;
}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.
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.
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.
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!

Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
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. 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. The C Academy course includes 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.
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
- Linked Lists
- Stacks and Queues
- Trees and Graphs