Tree Nodes and Structure
Model a node with pointers.
Tree Nodes and Structure is a free C Academy lesson on CoddyKit — lesson 1 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Binary Tree?
A binary tree is a hierarchical structure where each node holds a value and links to up to two children: a left child and a right child.
The topmost node is the root. Nodes with no children are leaves. This shape makes binary trees great for fast search, sorting, and recursive processing.
The Node Struct
In C we model a node with a struct that stores the data plus two self-referential pointers.
Each pointer points to another Node, or to NULL when there is no child on that side.
struct Node {
int value;
struct Node *left;
struct Node *right;
};Why Self-Referential Pointers
A node cannot contain another full node by value, because that would require infinite storage. Instead it holds pointers to its children.
Pointers are fixed-size, so the struct stays a known size while still chaining to other nodes on the heap.
struct Node {
int value;
struct Node *left; /* 8 bytes on 64-bit */
struct Node *right; /* 8 bytes on 64-bit */
};A typedef for Convenience
Typing struct Node everywhere is tedious. A typedef lets us write just Node.
The tag is still needed inside the struct because the type is not fully defined yet at that point.
typedef struct Node {
int value;
struct Node *left;
struct Node *right;
} Node;Allocating a Node
Nodes live on the heap, created with malloc. We set the value and initialize both child pointers to NULL.
Always check that malloc did not return NULL before using the memory.
Node *create_node(int value) {
Node *n = malloc(sizeof(Node));
if (n == NULL) return NULL;
n->value = value;
n->left = NULL;
n->right = NULL;
return n;
}Building a Tiny Tree by Hand
To understand the links, let us wire three nodes manually: a root with two children.
This program builds the tree and prints the values, then we would normally free it (covered later).
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node *left;
struct Node *right;
} Node;
Node *create_node(int v) {
Node *n = malloc(sizeof(Node));
n->value = v; n->left = NULL; n->right = NULL;
return n;
}
int main(void) {
Node *root = create_node(10);
root->left = create_node(5);
root->right = create_node(15);
printf("%d %d %d\n", root->left->value, root->value, root->right->value);
return 0;
}Reaching Grandchildren
You navigate the tree by chaining the arrow operator. root->left->right moves down to the left child, then to its right child.
Before following a pointer, make sure it is not NULL, or your program will crash.
/* root
* \
* right (15)
* \
* right->right (20)
*/
if (root->right != NULL && root->right->right != NULL)
printf("%d\n", root->right->right->value);Counting Nodes Recursively
Recursion fits trees naturally. To count nodes, an empty subtree has zero nodes; otherwise count this node plus both subtrees.
The NULL check is the base case that stops the recursion.
int count_nodes(Node *root) {
if (root == NULL) return 0;
return 1 + count_nodes(root->left)
+ count_nodes(root->right);
}Measuring Height
The height of a tree is the longest path from the root down to a leaf, measured in edges.
We take the larger of the two subtree heights and add one. An empty tree is given height -1 so a single node has height 0.
int height(Node *root) {
if (root == NULL) return -1;
int l = height(root->left);
int r = height(root->right);
return 1 + (l > r ? l : r);
}Identifying Leaves
A leaf is a node with no children: both left and right are NULL.
This tiny helper is useful in many traversal and counting routines.
int is_leaf(Node *n) {
return n != NULL && n->left == NULL && n->right == NULL;
}Putting Structure to Work
Here a small tree is built and we report its node count and height using the recursive helpers.
Notice how the helpers never assume a fixed shape; they work for any tree because recursion follows the actual pointers.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node { int value; struct Node *left, *right; } Node;
Node *nn(int v){ Node *n=malloc(sizeof(Node)); n->value=v; n->left=n->right=NULL; return n; }
int count(Node *r){ return r? 1+count(r->left)+count(r->right):0; }
int height(Node *r){ if(!r) return -1; int l=height(r->left),x=height(r->right); return 1+(l>x?l:x); }
int main(void){
Node *root = nn(10);
root->left = nn(5); root->right = nn(15);
root->left->left = nn(2);
printf("nodes=%d height=%d\n", count(root), height(root));
return 0;
}Quick Check
Test your understanding of node structure.
Recap
A binary tree node holds a value and two self-referential pointers (left, right), set to NULL when absent.
We allocate nodes with malloc, link them manually, and process them recursively. The NULL check is always the base case for counting, height, and leaf tests.
Frequently asked questions
Is the “Tree Nodes and Structure” lesson free?
Yes — the full text of “Tree Nodes and Structure” is free to read here on the web, and the C Academy course includes 4 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 “Tree Nodes and Structure”?
Model a node with pointers. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tree Nodes and Structure” 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
- Tree Nodes and Structure
- Inserting into a BST
- Traversals
- Searching and Freeing