MCS-208 · Unit 6

Trees

Section 6 of 8

Block 2 · Unit 6Trees14 min · Section 6 of 8

6.6Recursive Traversals: Preorder, Inorder, Postorder and Level Order

Covers IGNOU MCS-208 §6.4, 6.7, 6.7.1

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

TraversalOrderMnemonic
PreorderNode, left subtree, right subtreeNLR
InorderLeft subtree, node, right subtreeLNR
PostorderLeft subtree, right subtree, nodeLRN
Level orderLevel 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) time, and O(h)O(h) extra space for the recursion, where hh 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
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); } }

Check Your Progress 2: the tree of Figure 6.16

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.

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) time; recursion uses 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?

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?

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?

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
#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;
}
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?

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.

↑ ↓ move↵ openTab switch type