Tree operations and traversals
Inorder, preorder, postorder and level order — four ways to visit every node, with recursive algorithms.
Syllabus says: “Tree Operations, Tree Traversals”
On this page · 12 sections
Look where the PARENT (N) sits: PRE = N L R (parent first), IN = L N R (parent in the middle), POST = L R N (parent last).
Imagine walking around the tree outline, starting left of the root. Shout the name when you pass a node on its LEFT side (preorder), UNDER it (inorder) or on its RIGHT side (postorder).
Preorder visits root, left subtree, right subtree; inorder visits left, root, right; postorder visits left, right, root; level order visits nodes level by level using a queue.
Pre = ആദ്യം, In = നടുവിൽ, Post = അവസാനം (parent എവിടെ എന്ന് നോക്കിയാൽ മതി).
What you will be able to do
- Write recursive algorithms for inorder, preorder and postorder (Nov 2025 Q13b, May 2026 Q13a).
- Find all traversals of a given tree.
- Do a level-order traversal using a queue.
Worked example
| Traversal | Rule | Result |
|---|---|---|
| Preorder | N L R | A B D E C F |
| Inorder | L N R | D B E A C F |
| Postorder | L R N | D E B F C A |
| Level order | level by level | A B C D E F |
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithms
Algorithm Inorder(t)
1. if t ≠ NULL then
2. Inorder(t→left)
3. visit(t) // print t→data
4. Inorder(t→right)
Algorithm Preorder(t)
1. if t ≠ NULL then
2. visit(t)
3. Preorder(t→left)
4. Preorder(t→right)
Algorithm Postorder(t)
1. if t ≠ NULL then
2. Postorder(t→left)
3. Postorder(t→right)
4. visit(t)
Algorithm LevelOrder(t)
1. if t = NULL then return
2. enqueue(t)
3. while queue not empty do
4. p ← dequeue(); visit(p)
5. if p→left ≠ NULL then enqueue(p→left)
6. if p→right ≠ NULL then enqueue(p→right)The same recursive pattern gives copy(t), equal(t1, t2), countNodes(t) = 1 + count(left) + count(right) and height(t) = 1 + max(height(left), height(right)).
C implementation
bst.ctested ✓ 200/200 cases/* Binary search tree — KTU PCCST303, Module 3 (KTU Nov 2025 Q13a, model Q14b)
* Left subtree < node < right subtree. Duplicates are ignored.
* Delete with two children: copy the inorder successor (smallest key of the right
* subtree) into the node, then delete the successor.
* Commands: I x | D x | S x | IN | PRE | POST | LEVEL
* After every command: optional result line, then "TREE: " + parenthesised form,
* e.g. 35(12(,30),47) — key(left,right), leaves written alone. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int key;
struct node *left, *right;
};
struct node *root = NULL;
struct node *newNode(int x) {
struct node *n = malloc(sizeof *n);
if (n == NULL) { printf("OUT OF MEMORY\n"); exit(1); }
n->key = x; n->left = n->right = NULL;
return n;
}
void insert(int x) {
struct node **p = &root;
while (*p != NULL) {
if (x == (*p)->key) { printf("duplicate ignored\n"); return; }
p = x < (*p)->key ? &(*p)->left : &(*p)->right;
}
*p = newNode(x);
}
void search(int x) {
struct node *p = root;
int comparisons = 0;
while (p != NULL) {
comparisons++;
if (x == p->key) { printf("found after %d comparison%s\n", comparisons, comparisons > 1 ? "s" : ""); return; }
p = x < p->key ? p->left : p->right;
}
printf("not found\n");
}
void deleteKey(int x) {
struct node **p = &root, *t;
while (*p != NULL && (*p)->key != x) p = x < (*p)->key ? &(*p)->left : &(*p)->right;
if (*p == NULL) { printf("not found\n"); return; }
t = *p;
if (t->left != NULL && t->right != NULL) { /* case 3: two children */
struct node **s = &t->right;
while ((*s)->left != NULL) s = &(*s)->left; /* inorder successor */
t->key = (*s)->key;
t = *s;
*s = t->right; /* successor has no left child */
} else *p = t->left != NULL ? t->left : t->right; /* cases 1 and 2 */
free(t);
printf("deleted %d\n", x);
}
void inorder(struct node *t) { if (t) { inorder(t->left); printf(" %d", t->key); inorder(t->right); } }
void preorder(struct node *t) { if (t) { printf(" %d", t->key); preorder(t->left); preorder(t->right); } }
void postorder(struct node *t) { if (t) { postorder(t->left); postorder(t->right); printf(" %d", t->key); } }
void levelorder(void) {
struct node *q[64];
int front = 0, rear = 0;
if (root) q[rear++] = root;
while (front < rear) {
struct node *t = q[front++];
printf(" %d", t->key);
if (t->left) q[rear++] = t->left;
if (t->right) q[rear++] = t->right;
}
}
void show(struct node *t) {
if (t == NULL) return;
printf("%d", t->key);
if (t->left || t->right) { printf("("); show(t->left); printf(","); show(t->right); printf(")"); }
}
void freeTree(struct node *t) { if (t) { freeTree(t->left); freeTree(t->right); free(t); } }
int main(void) {
char cmd[8];
int x;
while (scanf("%7s", cmd) == 1) {
if (strcmp(cmd, "I") == 0 && scanf("%d", &x) == 1) insert(x);
else if (strcmp(cmd, "D") == 0 && scanf("%d", &x) == 1) deleteKey(x);
else if (strcmp(cmd, "S") == 0 && scanf("%d", &x) == 1) search(x);
else if (strcmp(cmd, "IN") == 0) { printf("inorder:"); inorder(root); printf("\n"); }
else if (strcmp(cmd, "PRE") == 0) { printf("preorder:"); preorder(root); printf("\n"); }
else if (strcmp(cmd, "POST") == 0) { printf("postorder:"); postorder(root); printf("\n"); }
else if (strcmp(cmd, "LEVEL") == 0) { printf("levelorder:"); levelorder(); printf("\n"); }
else { printf("BAD COMMAND\n"); break; }
printf("TREE: "); show(root); printf("\n");
}
freeTree(root);
return 0;
}
Sample run · Nov 2025 Q13a · real output from gcc
Input (stdin)
I 35 I 12 I 47 I 36 I 52 I 30 I 25 I 49 IN D 30 D 47
Output (stdout)
TREE: 35 TREE: 35(12,) TREE: 35(12,47) TREE: 35(12,47(36,)) TREE: 35(12,47(36,52)) TREE: 35(12(,30),47(36,52)) TREE: 35(12(,30(25,)),47(36,52)) TREE: 35(12(,30(25,)),47(36,52(49,))) inorder: 12 25 30 35 36 47 49 52 TREE: 35(12(,30(25,)),47(36,52(49,))) deleted 30 TREE: 35(12(,25),47(36,52(49,))) deleted 47 TREE: 35(12(,25),49(36,52))
Complexity
Every node is visited exactly once: O(n) time. Recursion stack space O(h), where h is the height (O(log n) when balanced, O(n) when skewed). Level order uses a queue of up to O(n).
Common mistakes
- Visiting the right subtree before the left.
- Forgetting the empty-subtree base case.
- Treating a missing left child as if the right child were the left one.
Asked in KTU exams (2)
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Pre NLR, In LNR, Post LRN, Level order with a queue.
- Recursive 4-line algorithms; O(n).
- Inorder of a BST = sorted order.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §5.3 (Binary tree traversals)
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §12.1
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)