views:

264

answers:

2

I'm trying to print binary tree

void print_tree(Node * root,int level )
 {
    if (root!=NULL)  
    {  
        cout<< root->value << endl;
    }
    //...
}

How can I indent output in order to indent each value with level '-' chars.

+10  A: 

You can construct a string to contain a number of repitions of a character:

std::cout << std::string(level, '-') << root->value << std::endl;
Daniel Earwicker
Also use the '\t' char.
Aviral Dasgupta
@Aviraldg - read the question. The indent is to consist of '-' chars.
Daniel Earwicker
Also, please don't use '\t'. On most consoles this will render as an 8-space tab, which is horrendously wide.
Matthew Scharley
A: 

cout has special characters, below are two:

'\t' - tab
'\n' - new line

Hope it helped.

vinnybad