Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'd like to print a given binary tree horizontally in C WITH LINKS between nodes.
I've done it without links , when i tried to do it with links it really got messed up... So if anyone can give me the solution that will be very helpful.

Thanx...

PS: More explanations in the images Click here to view & here's the structure i work with :

C++
typedef struct node{
         int val;            // value of the node
         struct node *left;  // left node
         struct node *right; // right node
      }node;


What I have tried:

Like i said i can draw the tree with no links between nodes with this code but i want links between nodes
C++
#define space 5

//secondary function
void draw_tree_hor2(node *tree, int distance)
{
    // stopping condition
    if (tree== NULL)
        return;

    // increase spacing
    distance += space;

    // start with right node
    draw_tree_hor2(tree->right, distance);

    // print root after spacing

    printf("\n");

    for (int i = space; i < distance; i++)
        printf(" ");

    printf("%d\n", tree->value);

    // go to left node
    draw_tree_hor2(tree->left, distance);
}

//primary fuction
void draw_tree_hor(node *tree)
{
   //initial distance is 0
	draw_tree_hor2(tree, 0);
}
Posted
Updated 12-Dec-16 12:39pm

1 solution

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

Not a full blow up solution, But guidelines:
You want to render a binary tree as a 2D drawing on console with root on left and leafs on right.
Fact that impact the drawing:
-Console drawing imply integer coordinates

Questions to answer in order to draw the tree:
- My tree can be the root alone, how do I draw it?
- A binary tree is not limited to 7 nodes, your program must handle more or less nodes.
- The binary tree can be not full, some parents can have only 1 child either on left or right.
- The tree can be not equilibrated, some leaf can be on different levels.
- presentation: you must be able to handle the minimum spacing between nodes horizontally and vertically.
- Using / and \ may be complicated to use as the orientation may not fit the drawing.
- You need to plan enough space between nodes everywhere to avoid collision of drawing lines and nodes.
- placing a new node can imply to move all parents nodes cascading to the root. If a non integer position arise, it will imply to move the sub-tree to correct the problem.

You will need to store nodes coordinates somewhere and probably an array of last used position per level

This graph editor will allow you to draw trees, it have the neat feature of spacing nodes to render your graph according to spacing rules.
yEd - Graph Editor[^]

Note: with a sheet and pencil draw the tree starting with root only and draw how the tree rendering evolve as you add nodes. Do it with different trees until you are at ease with the placing processes.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900