bachra04
asked on
Printing a tree
Hi All,
I need to do the following
I have a tree with the following structure
STree
{
STree* pchildTree;
STree* pNextTree;
const char* pszNodePath;
}
I need to print the whole tree (full path of each node) without using dynamic allocation.
I need an elegant solution.
Thanks
BT.
I need to do the following
I have a tree with the following structure
STree
{
STree* pchildTree;
STree* pNextTree;
const char* pszNodePath;
}
I need to print the whole tree (full path of each node) without using dynamic allocation.
I need an elegant solution.
Thanks
BT.
ASKER
Just a little remark
here
STree
{
STree* pchildTree;
STree* pNextTree;
const char* pszNodePath;
}
here pszNodePath does not contain the full path, it contains only the node name
so I need to concatenate to get the full path.
here
STree
{
STree* pchildTree;
STree* pNextTree;
const char* pszNodePath;
}
here pszNodePath does not contain the full path, it contains only the node name
so I need to concatenate to get the full path.
Then include that in the recursiion, e.g.
void print_tree(Stree* tree, string path) {
// print this node
string full_path = path + tree->pszNodePath;
cout << full_path << endl;
print_tree(tree->pChildTre e, full_path);
print_tree(tree->pNextTree , full_path);
}
If you add a recursion counter, you could even manage indentation.
void print_tree(Stree* tree, string path) {
// print this node
string full_path = path + tree->pszNodePath;
cout << full_path << endl;
print_tree(tree->pChildTre
print_tree(tree->pNextTree
}
If you add a recursion counter, you could even manage indentation.
ASKER
Hi JKr,
the following code
string full_path = path + tree->pszNodePath;
does not compile
the following code
string full_path = path + tree->pszNodePath;
does not compile
What errors do you get? Be sure to add
#include <string>
using namespace std;
#include <string>
using namespace std;
ASKER
I have #include <string.h> included and I cannot control that (It is a must to use these includes),
'string.h' and 'string' are two different things - just add the above.
ASKER
I cannot do that, This is a MUST to use only the already included headers files.
>> I cannot do that, This is a MUST to use only the already included headers files.
I thought that this was homework ... maybe jkr gave away too much ... re-read my post, and try to find a way to do the same a different way ... If you don't find anything, we'll be glad to help you further - just post the code you have uptill then ...
I thought that this was homework ... maybe jkr gave away too much ... re-read my post, and try to find a way to do the same a different way ... If you don't find anything, we'll be glad to help you further - just post the code you have uptill then ...
'Adding' does not mean that you aren't using the already included header files. You're still using them. Plus one more.
ASKER
Note that I develop embdded software where code size is very important
Including the string library will increase the code size.
This a high level decision and for me as developer I cannot control that
I must do that using teh functions availabes in string.h
I should use strcat function but this can be the source of the memory allocation
problem. I need to do it in a clean way.
Including the string library will increase the code size.
This a high level decision and for me as developer I cannot control that
I must do that using teh functions availabes in string.h
I should use strcat function but this can be the source of the memory allocation
problem. I need to do it in a clean way.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you JKr and Infinity for your swift reactions.
I already found a solution that is not bad.
I already found a solution that is not bad.
void print_tree(Stree* tree) {
// print this node
print_tree(tree->pChildTre
print_tree(tree->pNextTree
}
If pszNodePath already contains the path, then you can just use that - if not, you'll have to keep the path uptill now in the recursive function, and add to it (passing it to the next recursion step).
btw, what do you mean by "no dynamic allocation" ? Where would you need it (maybe for storing the path ?) and why don't you want to use it ?
Final remark : a recursive solution is only good for limited depth trees - otherwise you'll fast run out of stack space !!