I wrote this function :
void printDFS(pXml_Element root)
{
int i ;
printf("< %s > \n", root->data->name);
for ( i = 0 ; i < root->childrenList->num_of_items ; i++)
{
void* voidElem;
pXml_Element currElem;
voidElem = getStructDataAtIndex(root->childrenList, i);
if (!voidElem)
{
printf("error: couldn't get data.. \n");
return;
}
currElem = (pXml_Element)voidElem;
printDFS(currElem);
}
}
It runs like DFS search but I want to add spaces.
My DS is a tree where each node has children (I know the amount of them)
I run using deep search and printing my context but I don’t have spaces .
I want it to look like the XML format :
<node>
<n1>
<n2>
<o1>
<o2>
<n3>
like that.
I wrote this function as well :
void printSpace(int numOfspaces)
{
while(numOfspaces > 0)
{
printf(" ");
numOfspaces--;
}
}
but I still fail id doing that 🙁
can anyone help my with that> would love to get an explanation how to do it right…
thank you!
Basically, make a global or static variable to count the recursion depth, and increase it when you enter the function:
Then, indent each line with the appropiate number of spaces:
At the end of the function, decrease it again:
EDIT: a threadsafe alternative would be an additional “Depth” parameter; you would simply pass “Depth+1” in your recursive call, and pass “0” for the intial call.