954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary Tree Display

Hi,

Does anyone know a function that prints out binary tree in this form

example :
................15
.........12.............17
.......10 14.........16 18


?

my Node class :

class Node
{
 double Node_Data;
 Node Left_Child;
 Node Right_Child;
 Node Parent;
}
c++_fem
Newbie Poster
24 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Are you required to display the tree like that? Because if not, it's much easier to rotate the tree by 90 degrees:

public void TreeStructure(Node root)
{
    TreeStructure(root, 0);
}

private void TreeStructure(Node root, int depth)
{
    if (root == null) {
        for (int i = 0; i < depth; i++)
            Console.Write('\t');

        Console.WriteLine('~'); // I use ~ to mean null
    }
    else {
        TreeStructure(root.Right_Child, depth + 1);

        for (int i = 0; i < depth; i++)
            Console.Write('\t');

        Console.WriteLine(root.Node_Data);

        TreeStructure(root.Left_Child, depth + 1);
    }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Unfortunately the form in which the tree should displayed is required

c++_fem
Newbie Poster
24 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Bummer. There's no function I'm aware of that does this for you; you'll have to work out the distances between nodes manually and basically draw the tree as if the console were a GUI.

I'd suggest searching google for people who've already done this and outlined the steps.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: