0Pricing
C Academy · Lesson

Traversals

In-order, pre-order, post-order.

What Is a Traversal?

A traversal is a systematic way to visit every node in a tree exactly once.

The three classic depth-first orders are in-order, pre-order, and post-order. They differ only in when the current node is processed relative to its subtrees.

In-Order Traversal

In-order visits the left subtree, then the node, then the right subtree.

For a BST this prints values in sorted ascending order, which makes it the most useful traversal for search trees.

void in_order(Node *root) {
    if (root == NULL) return;
    in_order(root->left);
    printf("%d ", root->value);
    in_order(root->right);
}

All lessons in this course

  1. Tree Nodes and Structure
  2. Inserting into a BST
  3. Traversals
  4. Searching and Freeing
← Back to C Academy