Printing Syntax Tree with different indentation at different levels?
Hey guys, I'm trying to print out my syntax tree using different amounts of indentation for each level in the tree. I'm not not quite sure how to achieve this in C. I've created a for loop and a char* that has " " assigned to it... but when I try to add the char* to itself for each increment in the level it just crashes the program. Anyways, here's what I got, let me know what you guys think I should do:
Code:
int synTreePreOrderWalk( TREENODE * ptree, int (*func)(), int level)
{
char *c = " ";
int i;
int iRet;
if( ptree) { // if current node not null
// perform action - quit walking if non-zero return value
for(i = 0; i < level; i++)
// *c += *c;
if( iRet = (*func)(ptree, level)) printf("%s" "%s\n", c, ptree->label);
// visit children
level++; // going to a new indentation level
if( iRet = synTreePreOrderWalk( ptree->left, func, level)) printf("%s" "%s\n", c, ptree->label);
if( iRet = synTreePreOrderWalk( ptree->right, func, level)) printf("%s" "%s\n", c, ptree->label);
// set level back to my level
level--; // visit siblings
//if( iRet = synTreePreOrderWalk( ptree->sibling, func, level)) return iRet;
}
return 0;
}