Traversing a binary tree means visiting each node exactly once. The order of the visits matters: it decides whether an expression tree prints as prefix, infix or postfix, and whether a binary search tree prints in sorted order.
The three depth-first orders
Intuition
Walk round the outside of the tree, starting left of the root and keeping the tree on your right hand side. You pass each node three times: on its left, under it, and on its right. Writing the node on the first pass gives preorder, on the second pass inorder, and on the last pass postorder.
Traversal Order Mnemonic Preorder Node, left subtree, right subtree N LRInorder Left subtree, node, right subtree LN R Postorder Left subtree, right subtree, node LRN Level order Level by level, left to right (uses a queue) —
Each depth-first traversal is naturally recursive: to traverse a tree, handle the root and traverse its two subtrees in the same way. Each visits every node once: O ( n ) O(n) O ( n ) time, and O ( h ) O(h) O ( h ) extra space for the recursion, where h h h is the height.
Expression trees
In an expression tree every leaf is an operand and every internal node is an operator applied to its two subtrees. The three traversals give the three notations of Unit 4: preorder gives prefix, postorder gives postfix, and inorder (with brackets around each subtree) gives infix.
For the tree of Figure 6.8, + at the root with left subtree (4 / 2) * 7 and right subtree 3 - 1:
preorder: + * / 4 2 7 - 3 1
postorder: 4 2 / 7 * 3 1 - +
inorder (bracketed): ((4 / 2) * 7) + (3 - 1)
Recursive C functions
Each function first checks whether the (sub)tree is empty, so it works for any tree, including the empty one:
Recursive preorder, inorder and postorder Copy void preorder(const Node *t) { if (t) { visit(t); preorder(t->left); preorder(t->right); } }
void inorder(const Node *t) { if (t) { inorder(t->left); visit(t); inorder(t->right); } }
void postorder(const Node *t) { if (t) { postorder(t->left); postorder(t->right); visit(t); } }
Correction · Programs 6.2 to 6.4 (recursive traversals)
Study material says: Programs 6.2–6.4 (§6.7.1) check curr->left != NULL and curr->right != NULL before each recursive call but never check curr itself, declare the functions without a return type, and print with printf("%d", curr->value).
Correct version: Test the node itself at the start (if (curr == NULL) return;) and call the function on both children unconditionally; declare the functions void; print a separator, e.g. printf("%d ", curr->value).
Why: Called on an empty tree (curr == NULL), the study-material functions read curr->left and crash. Implicit int return types are not allowed since C99 (C17 §6.7.2 ¶2). Without a separator, values run together: 4, 5, 3 print as 453, which cannot be read back.
The tree: A has children B and C; B has children D and E; D has G and H; E has I and J; C has no children.
Example 6.12 Traversals and properties of Figure 6.16
Goal. Print the four traversals of the tree of Figure 6.16 and count its nodes, leaves, height and NULL links.
Program.
C · traversals.c Copy #include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node * left;
char value;
struct node * right;
} Node;
Node * make( char v, Node * l, Node * r)
{
Node * n = malloc( sizeof * n);
if (n == NULL ) {
fprintf(stderr, "out of memory \n " );
exit(EXIT_FAILURE);
}
n->value = v;
n->left = l;
n->right = r;
return n;
}
/* Each traversal checks the node itself, so an empty tree (NULL) is fine. */
void preorder( const Node * t)
{
if (t == NULL ) return ;
printf( "%c " , t->value); /* root first */
preorder(t->left);
preorder(t->right);
}
void inorder( const Node * t)
{
if (t == NULL ) return ;
inorder(t->left);
printf( "%c " , t->value); /* root in the middle */
inorder(t->right);
}
void postorder( const Node * t)
{
if (t == NULL ) return ;
postorder(t->left);
postorder(t->right);
printf( "%c " , t->value); /* root last */
}
void level_order( const Node * t) /* uses a queue of node pointers */
{
const Node * queue[ 64 ];
int front = 0 , rear = 0 ;
if (t != NULL )
queue[rear ++ ] = t;
while (front < rear) {
const Node * n = queue[front ++ ];
printf( "%c " , n->value);
if (n->left) queue[rear ++ ] = n->left;
if (n->right) queue[rear ++ ] = n->right;
}
}
int count( const Node * t) { return t ? 1 + count(t->left) + count(t->right) : 0 ; }
int leaves( const Node * t)
{
if (t == NULL ) return 0 ;
if (t->left == NULL && t->right == NULL ) return 1 ;
return leaves(t->left) + leaves(t->right);
}
int height( const Node * t) /* number of levels; empty tree = 0 */
{
if (t == NULL ) return 0 ;
int hl = height(t->left), hr = height(t->right);
return 1 + (hl > hr ? hl : hr);
}
int null_links( const Node * t)
{
if (t == NULL ) return 0 ;
return (t->left == NULL ) + (t->right == NULL ) + null_links(t->left) + null_links(t->right);
}
void free_tree(Node * t) /* postorder: children before parent */
{
if (t == NULL ) return ;
free_tree(t->left);
free_tree(t->right);
free(t);
}
int main( void )
{
/* Figure 6.16: A has children B and C; B has D and E;
D has G and H; E has I and J; C is a leaf. */
Node * root = make( 'A' ,
make( 'B' ,
make( 'D' , make( 'G' , NULL , NULL ), make( 'H' , NULL , NULL )),
make( 'E' , make( 'I' , NULL , NULL ), make( 'J' , NULL , NULL ))),
make( 'C' , NULL , NULL ));
printf( "preorder : " ); preorder(root); printf( " \n " );
printf( "inorder : " ); inorder(root); printf( " \n " );
printf( "postorder : " ); postorder(root); printf( " \n " );
printf( "level order: " ); level_order(root); printf( " \n " );
printf( "nodes = %d, leaves = %d, height (levels) = %d, NULL links = %d \n " ,
count(root), leaves(root), height(root), null_links(root));
free_tree(root);
printf( "empty tree preorder: " );
preorder( NULL );
printf( "(nothing printed, no crash) \n " );
return 0 ;
}
Run it.
Output Copy preorder : A B D G H E I J C
inorder : G D H B I E J A C
postorder : G H D I J E B C A
level order: A B C D E G H I J
nodes = 9, leaves = 5, height (levels) = 4, NULL links = 10
empty tree preorder: (nothing printed, no crash)
Answers to Check Your Progress 2, Q1. (a) leaves: C, G, H, I, J ; (b) sibling of J: I; (c) parent of G: D; (d) depth of the tree (number of levels): 4; (e) level of J: 4.
Result. Preorder A B D G H E I J C; inorder G D H B I E J A C; postorder G H D I J E B C A; level order A B C D E G H I J.
Key idea
Preorder is node-left-right, inorder left-node-right and postorder left-right-node; each is a three-line recursive function that must first check for an empty (sub)tree, and each takes O ( n ) O(n) O ( n ) time.
TEE Exam note
“Give the preorder, inorder, postorder (and level order) traversals of the tree” is in almost every paper. Write each order with commas, and check that each contains every node exactly once. For expression trees, link the traversals to prefix, infix and postfix notation. When writing code, include the NULL check.
Common mistake
Visiting the right subtree before the left. In all three standard traversals the left subtree is traversed before the right.
Common mistake
Forgetting that a node’s whole subtree is finished before moving on: in preorder of Figure 6.16, all of B’s subtree (B D G H E I J) comes before C.
Key takeaways
Preorder NLR, inorder LNR, postorder LRN; level order uses a queue.
Expression trees: preorder = prefix, inorder = infix (with brackets), postorder = postfix.
Recursive traversal functions must check if (t == NULL) return; first.
All traversals are O ( n ) O(n) O ( n ) time; recursion uses O ( h ) O(h) O ( h ) stack space.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice What is the preorder traversal of the tree with root 1, left child 2 (which has children 4 and 5) and right child 3?
A 1 2 4 5 3 B 4 2 5 1 3 C 4 5 2 3 1 D 1 2 3 4 5
Check answer
Show answer Answer: A. 1 2 4 5 3
Node, then the whole left subtree (2 4 5), then the right subtree (3). “1 2 3 4 5” is level order; “4 2 5 1 3” is inorder.
Question 2 · multiple choice For the same tree, what is the postorder traversal?
A 1 2 4 5 3 B 4 2 5 1 3 C 4 5 2 3 1 D 5 4 3 2 1
Check answer
Show answer Answer: C. 4 5 2 3 1
Left subtree (4 5 2), right subtree (3), then the root 1, which always comes last in postorder.
Question 3 · multiple choice Which traversal of an expression tree gives the postfix expression?
A Preorder B Inorder C Postorder D Level order
Check answer
Show answer Answer: C. Postorder
Postorder visits both operands before their operator. Preorder gives prefix.
Question 4 · multiple choice What does this program print?
C · q6-trav.c Copy #include <stdio.h>
#include <stddef.h>
typedef struct n {
struct n * l;
char v;
struct n * r;
} N;
void in( const N * t)
{
if (t == NULL ) return ;
in(t->l);
printf( "%c" , t->v);
in(t->r);
}
int main( void )
{
N a = { NULL , 'a' , NULL }, b = { NULL , 'b' , NULL }, c = { NULL , 'c' , NULL };
N mul = { & b, '*' , & c};
N plus = { & a, '+' , & mul};
in( & plus);
printf( " \n " );
return 0 ;
} A +a*bc B a+b*c C abc*+ D a*b+c
Check answer
Show answer Answer: B. a+b*c
The tree is + with left a and right * (b, c); inorder gives a+b*c. “+abc” is the preorder (prefix) and “abc +” the postorder (postfix).
Question 5 · multiple choice Postorder D E B F C A and inorder D B E A F C are given for a binary tree. What is the root?
A D B B C C D A
Check answer
Show answer Answer: D. A
The root is the last node in postorder. (In the inorder, D B E are then its left subtree and F C its right subtree.)
Question 6 · short answer
For the binary tree of Figure 6.16 (A: B, C; B: D, E; D: G, H; E: I, J), write the preorder, inorder, postorder and level-order traversals and list its leaves.
Show worked answer Preorder: A B D G H E I J C. Inorder: G D H B I E J A C. Postorder: G H D I J E B C A. Level order: A B C D E G H I J. Leaves (nodes without children): C, G, H, I, J.