printing it as it is
??
printing it as it is
??
I use
but its not separating them by levelsCode:void showtree(node *root){ if (root){ printf("%d\n ",root->data); if(root->left){ showtree(root->left); } if(root->right){ showtree(root->right); } }
??
You probably want to wrap ALL of the function in "if (root)", as you can not access root->left or root->right in any way if root is NULL.
To pass show a level, you may want to pass a level along to the function and then indent based on the level (by printing level number of spaces, for example).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
how to connect the level of a node
with \t and \n in printf
??
Like I said, you need to pass a level (and level + 1 when you recurse) in each call. You can then use varieties of spaces and such to show the level of each node by adding spaces (or tabs) according to level.
If you actually want to draw a complete tree with arrows and such, then that would require a lot more.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I did what you said.
I input 0 with the root and each time i call recursively i increase the level by 1.
but how to involve this number to the printf command?
Code:void showtree(node *root,level){ if (root){ printf("%d\n ",root->data); if(root->left){ showtree(root->left,level+1); } if(root->right){ showtree(root->right,level+1); } }
Last edited by transgalactic2; 11-19-2008 at 02:01 PM.
The simple method is to use a loop. More complicated version would be to use %*d, and pass level as the argument before data. %* means "use the next argument as width".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.