To process every node of a binary tree, we visit them in a definite order. There are three classic depth-first orders, defined recursively.

The Three Traversals

Preorder (Root, Left, Right): visit the root, then traverse the left subtree in preorder, then the right subtree in preorder. Inorder (Left, Root, Right): traverse the left subtree in inorder, visit the root, then traverse the right subtree in inorder. Postorder (Left, Right, Root): traverse the left subtree, then the right subtree, then visit the root. Each traversal visits every node once, so it takes O(n) time. The recursion uses stack space equal to the height of the tree.

Definition: Inorder traversal. A way of visiting the nodes of a binary tree in which the left subtree is visited first, then the root, then the right subtree.

Uses of the Traversals

Preorder is used to copy a tree and to write prefix expressions. Inorder of a binary search tree gives the keys in sorted order, and inorder of an expression tree gives the infix expression. Postorder is used to delete a tree, since children are freed before their parent, to compute folder sizes, and to give postfix expressions. Level-order traversal, which visits the nodes level by level using a queue, is the breadth-first traversal of a tree.

Expression Trees

In an expression tree, the leaves are operands and the internal nodes are operators. For (A + B) × C, the root is ×, its left child is + with children A and B, and its right child is C. Preorder gives × + A B C, inorder gives A + B × C (with brackets added as needed), and postorder gives A B + C ×.

Building a Tree from Traversals

A binary tree can be rebuilt uniquely from its inorder together with its preorder or postorder sequence. The first element of preorder (or the last of postorder) is the root, and its position in the inorder sequence splits the left and right subtrees.

Table 4.4: Traversal orders

Traversal Order Use
Preorder Root, Left, Right Copy tree, prefix form
Inorder Left, Root, Right Sorted BST keys, infix form
Postorder Left, Right, Root Delete tree, postfix form
Level order Level by level BFS of a tree

Example 4.4: Traversing a tree

Problem: For the tree with root A, children B and C, B's children D and E, and C's left child F, give the three traversals.

Solution:

  1. Preorder: A, then the left subtree B D E, then the right subtree C F: A B D E C F.
  2. Inorder: left subtree D B E, then A, then right subtree F C: D B E A F C.
  3. Postorder: D E B, then F C, then A: D E B F C A.

Result: Preorder A B D E C F, inorder D B E A F C, postorder D E B F C A.

#include <stdio.h>
#include <stdlib.h>
struct n { char d; struct n *l, *r; };
struct n *mk(char d, struct n *l, struct n *r) {
    struct n *p = malloc(sizeof *p); p->d = d; p->l = l; p->r = r;
    return p;
}
void pre(struct n *t) {
    if (t) { printf("%c ", t->d); pre(t->l); pre(t->r); }
}
void in(struct n *t) {
    if (t) { in(t->l); printf("%c ", t->d); in(t->r); }
}
void post(struct n *t) {
    if (t) { post(t->l); post(t->r); printf("%c ", t->d); }
}
int main(void) {
    struct n *t = mk('A', mk('B', mk('D', 0, 0), mk('E', 0, 0)),
                          mk('C', mk('F', 0, 0), 0));
    printf("Pre: "); pre(t); printf("\nIn: "); in(t);
    printf("\nPost: "); post(t); printf("\n");
    return 0;
}

Output

Pre: A B D E C F
In: D B E A F C
Post: D E B F C A

Common mistakes

  • Mixing up the position of the root in the three orders.
  • Forgetting the base case of the recursion for an empty subtree.
  • Expecting preorder and postorder alone to fix a unique tree.

Key takeaways

  • Preorder: Root, Left, Right. Inorder: Left, Root, Right. Postorder: Left, Right, Root.
  • Each traversal is O(n).
  • Inorder of a BST gives sorted order.
  • Inorder plus preorder (or postorder) rebuilds a binary tree.

Practice questions

  1. What is the order of postorder traversal?
  2. Which traversal of a BST gives sorted keys?
  3. Give the preorder of the expression tree for A + B.

Answers to the practice questions

  1. Left subtree, right subtree, then root.
  2. Inorder.
    • A B.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit IV