C programming
binary tree
console output
data structures
coding tutorial

C How to draw a Binary Tree to the console

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Drawing a binary tree in a console is mostly a formatting problem. In plain C, the most practical answer is to print the tree sideways: right subtree first, then the current node, then the left subtree. That format is easy to implement, easy to debug, and far less fragile than trying to build perfect centered ASCII art.

Use a Sideways Tree Printer First

The sideways layout treats indentation as depth. Each recursive level adds spaces, so the structure becomes visible without complex coordinate calculations.

c
1#include <stdio.h>
2#include <stdlib.h>
3
4typedef struct Node {
5    int value;
6    struct Node *left;
7    struct Node *right;
8} Node;
9
10Node *make_node(int value) {
11    Node *node = malloc(sizeof(Node));
12    if (node == NULL) {
13        fprintf(stderr, "allocation failed\n");
14        exit(EXIT_FAILURE);
15    }
16    node->value = value;
17    node->left = NULL;
18    node->right = NULL;
19    return node;
20}
21
22void print_tree(Node *root, int depth) {
23    if (root == NULL) {
24        return;
25    }
26
27    print_tree(root->right, depth + 1);
28
29    for (int i = 0; i < depth; i++) {
30        printf("    ");
31    }
32    printf("%d\n", root->value);
33
34    print_tree(root->left, depth + 1);
35}

This traversal order places the right side above the parent and the left side below it. For binary search trees, that usually matches how developers already think about node order.

Build a Small Example Tree

Here is a complete example that creates a tree and prints it.

c
1void free_tree(Node *root) {
2    if (root == NULL) {
3        return;
4    }
5    free_tree(root->left);
6    free_tree(root->right);
7    free(root);
8}
9
10int main(void) {
11    Node *root = make_node(8);
12    root->left = make_node(3);
13    root->right = make_node(10);
14    root->left->left = make_node(1);
15    root->left->right = make_node(6);
16    root->right->right = make_node(14);
17
18    print_tree(root, 0);
19    free_tree(root);
20    return 0;
21}

Possible output:

text
1        14
2    10
38
4        6
5    3
6        1

It is rotated ninety degrees, but the hierarchy is clear immediately.

Add Labels if You Need More Structure

When you are debugging insertion or deletion, it can help to show whether a printed node came from the left or right branch.

c
1void print_tree_marked(Node *root, int depth, const char *marker) {
2    if (root == NULL) {
3        return;
4    }
5
6    print_tree_marked(root->right, depth + 1, "/");
7
8    for (int i = 0; i < depth; i++) {
9        printf("    ");
10    }
11    printf("%s%d\n", marker, root->value);
12
13    print_tree_marked(root->left, depth + 1, "\\");
14}

You can call it like this:

c
print_tree_marked(root, 0, "-");

That small addition makes parent-child direction easier to follow without turning the renderer into a full layout engine.

Why Top-Down ASCII Art Is Harder

A centered tree with connecting branches looks attractive, but it requires much more machinery. You need subtree widths, horizontal positions, connector rows, and logic for variable-width labels. Unbalanced trees make all of that trickier.

In other words, a pretty printer is not just "more indentation." It is a layout algorithm.

If the goal is debugging a data structure implementation, the sideways printer gives a much better complexity-to-value ratio. You can verify shape, depth, and ordering with a few lines of code instead of spending most of your time on rendering math.

Include Null Markers Only When Necessary

Some bugs involve missing children, so a compact sideways print can hide important details. If that matters, print a placeholder for null children at a limited depth.

c
1void print_tree_with_nulls(Node *root, int depth, int max_depth) {
2    if (depth > max_depth) {
3        return;
4    }
5
6    if (root == NULL) {
7        for (int i = 0; i < depth; i++) {
8            printf("    ");
9        }
10        printf(".\n");
11        return;
12    }
13
14    print_tree_with_nulls(root->right, depth + 1, max_depth);
15    for (int i = 0; i < depth; i++) {
16        printf("    ");
17    }
18    printf("%d\n", root->value);
19    print_tree_with_nulls(root->left, depth + 1, max_depth);
20}

This is useful for diagnosing structural errors without printing an infinite wall of placeholders.

Common Pitfalls

The most common mistake is forgetting the base case and recursing on NULL pointers, which causes crashes immediately.

Another issue is using too little indentation. If each depth level adds only one space, the structure becomes hard to read.

Developers also sometimes print an in-order traversal and expect it to look like a tree. Traversal order alone is not a visualization strategy.

Finally, do not ignore memory cleanup in quick demos. A tree printer is often used in data-structure exercises, which are exactly where leaks go unnoticed.

Summary

  • The simplest useful console visualization is a sideways recursive printer.
  • Print the right subtree first, then the node, then the left subtree.
  • Indentation represents tree depth and keeps the implementation small.
  • Add markers or limited null placeholders only when debugging needs them.
  • Full top-down ASCII tree layouts are possible, but they are much more complex than most C programs need.

Course illustration
Course illustration

All Rights Reserved.