In order to traverse a tree you could use something like this:

For inorder traversal:

inorder(TreeNode* currentNode)
{
    if (currentNode) {
       inorder(currentNode->LeftChild);
       cout << currentNode->data;
       inorder(currentNode->RightChild);
    }
}

For preorder traversal:

preorder(TreeNode* currentNode)
{
    if (currentNode) {
        cout << currentNode->data;
        preorder(currentNode->LeftChild);
        preorder(currentNode->RightChild);
    }
}

For post order traversal:

postorder(TreeNode* currentNode)
{
    if (currentNode) {
        postorder(currentNode->LeftChild);
        postorder(currentNode->RightChild);
        cout << currentNode->data;
    }
}

For level order traversal:

LevelOrder(TreeNode* root)
{
   Queue q<TreeNode*>;
   TreeNode* currentNode = root;
       
    while (currentNode) {
        cout << currentNode->data;
        if (currentNode->LeftChild) q.Add(currentNode->LeftChild);
        if (currentNode->RightChild) q.Add(currentNode->RightChild);
        currentNode = q.Delete(); //q.Delete returns a node pointer
    }
}

The question is:

How can i make it a bit more graphical......
i mean that with the above methods the output is a list of numbers... is there anyway to modify this code so that i can have a nicer output?

PS: if you give example for one kind of traversal i will find the others by myself :)


Thanks again for your help....

Dani AI

Generated

The posts already give the traversal work (thanks ). To turn those traversals into something visual you have three practical options depending on how much work you want to do and where you want the output to appear:

  1. Emit a Graphviz DOT file and let Graphviz do the layout (fast, clean image/SVG).
  2. Produce a fixed-width-console ASCII layout (sideways tree or level-by-level) — no external tools but more code to position nodes.
  3. Render to a window or HTML/SVG (Qt, SFML, or a small HTML page) for interactive or styled output.

A minimal, practical approach is to adapt your traversal so it writes DOT. Once you have DOT you get a nicely laid-out image with one external command. Example emitter (C++ snippet):

#include <iostream>
#include <functional>

void emitDot(TreeNode* root, std::ostream& out = std::cout) {
    out << "digraph Tree {\n";
    int id = 0;
    std::function<int(TreeNode*)> walk = [&](TreeNode* n) -> int {
        if (!n) return -1;
        int my = ++id;
        out << "  n" << my << " [label=\"" << n->data << "\"];\n";
        if (n->LeftChild) {
            int l = walk(n->LeftChild);
            out << "  n" << my << " -> n" << l << ";\n";
        }
        if (n->RightChild) {
            int r = walk(n->RightChild);
            out << "  n" << my << " -> n" << r << ";\n";
        }
        return my;
    };
    walk(root);
    out << "}\n";
}

Save output to tree.dot and render with dot -Tpng tree.dot -o tree.png (or -Tsvg for vector output).

If you must stay in the console, compute an (x,y) for every node (x from an inorder-like numbering or by subtree widths, y = depth), build a 2D char buffer sized for max columns/rows, place node labels and simple connectors, then print lines. This matches ’s idea of plotting the tree on its side and can use ’s level-order output to help compute column slots.

Tips: use a monospaced font for console output, handle multi-digit labels by reserving column width, escape Graphviz labels that contain quotes, and avoid deep recursion on degenerate trees (use iterative traversal or increase stack). For a quick, nicest visual: DOT + Graphviz. For console-only: a two-pass position + buffer render. For interactive needs: export SVG or draw with a GUI toolkit.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

I don't think this is easy, the ones that I have seen that look half ok are the ones that plot the tree on its side.

The ones that work the best use a gdi. Drawing that in the console window is always going to be a pain in the ...

I am currently working on a printout for level order for my data structures class. I will post it up here for you.

#
inorder(TreeNode* currentNode)
#
{
#
if (currentNode) {
#
inorder(currentNode->LeftChild);
#
cout << currentNode->data;
#
inorder(currentNode->RightChild);
#
}
#
}

1.
#
2.
inorder(TreeNode* currentNode)
3.
#
4.
{
5.
#
6.
if (currentNode) {
7.
#
8.
inorder(currentNode->LeftChild);
9.
#
10.
cout << currentNode->data;
11.
#
12.
inorder(currentNode->RightChild);
13.
#}
14.
#
15.
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.