Printing a Binary Tree (in a Visual way) on Console

Hello, can anyone help me in writing a function which could visualize the Binary tree on the Console?
Like this,

1
2
3
4
5
6
       
       j    <-- root
     /   \
    f      k  
  /   \      \
 a     h      z  



I can do this in the horizontal way, using the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Tree::Display(int level, node *ptr = root)
{
	int i;
	if (ptr != NULL)
	{
		Display(level + 1, ptr->right);
		cout << endl;
		if (ptr == root)
			cout << "Root -> ";
		for (i = 0; i < level && ptr != root; i++)
			cout << "        ";
		cout << ptr->data;
		Display(level + 1, ptr->left);
	}
}


This displays the following result (for a random set of data)
It's a BST and as you can see, it is displaying the result in horizontal manner... However, could the same be done in vertical manner?
1
2
3
4
5
6
7
8
9
10
11
12
13
                                45
                        40
                                37
                30
                                27
                        25
                                22
Root -> 20
                                15
                        11
                10
                                8
                        5


Thanks for any help in advance!
Last edited on
Topic archived. No new replies allowed.