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?