How to display all descendant Vectors in Family Tree

I am unsure how to go about solving this. I am almost certain solving this recursively is the way to go but I'm not quite sure how to go about it. I have a structure with a string and vector of said structure. The parent is the name and the vector are the children.
Structure Code:
1
2
3
4
5
6
7
8
9
struct TreeNode
{
    string data;
    vector<TreeNode *> children;
    TreeNode(string data)
    {
        this -> data = data;
    }
};


what i have which isnt much:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void ListDescendants(TreeNode * parentPtr)
{
    TreeNode *childPtr = nullptr;
    if(parentPtr -> children.size() > 0)
    {
        for(int i = 0; i < parentPtr -> children.size(); i++)
        {
            cout << parentPtr -> children[i] << endl;
            childPtr = parentPtr -> children[i];
        }
        ListDescendants(childPtr);
    }
    else if(parentPtr -> children.size() == 0)
    {
       cout << parentPtr -> data << " has no descendants. " << endl;
    }
}       {
            parentPtr = parentPtr -> children[i];
            ListDescendants(parentPtr);
        }
    }
    else if(parentPtr -> children.size() == 0)
    {
       cout << parentPtr -> data << " has no descendants. " << endl;
    }
}


I know i need a for loop but i cant seem to figure out the logic. I've been trying to draw pictures to help visualize it but to no avail. Can anyone help send me in the right direction?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct TreeNode
{
    // ...
    std::string data;
    std::vector< TreeNode* > children;
};

void list_node( const TreeNode* p, int depth = 0 )
{ if(p) std::cout << std::string( depth*2, ' ' ) << p->data << '\n' ; }

void list_subtree( const TreeNode* p, int depth = 0 )
{
    list_node( p, depth ) ;
    if(p) for( const TreeNode* pchild : p->children ) list_subtree( pchild, depth+1 ) ;
}

http://coliru.stacked-crooked.com/a/864327f8f53878c8
Wow that was a very thorough answer! Thank You! Could you help explain the depth part? I believe that was what was causing me issues in my logic. Thanks again!!
> Could you help explain the depth part?
> I believe that was what was causing me issues in my logic.

The depth is the depth of recursion (the level of the node in the sub-tree). Each time we descend one level down the tree, we pass the depth as current depth + 1
for( const TreeNode* pchild : p->children ) list_subtree( pchild, depth+1 ) ;
The depth of the node is then used to give the output a tree-like look, with the data of the node indented by the depth of the node; so that we get an output like this:
aaaa
  bbbb
    eeee
      pppp
      qqqq
      rrrr
        tttt
        uuuu
        vvvv
        wwww
      ssss
    ffff
    gggg
    hhhh
  cccc
    iiii
    jjjj
    kkkk
  dddd
    mmmm
    nnnn
    oooo

http://coliru.stacked-crooked.com/a/864327f8f53878c8

To generate a list all the nodes in the sub-tree, without any indentation to indicate the level, keeping track of the depth is not required.
1
2
3
4
5
6
7
8
void list_subtree( const TreeNode* p )
{
    if(p)
    {
        std::cout << p->data << '\n' ;
        for( const TreeNode* pchild : p->children ) list_subtree(pchild) ;
    }
}

In that case, we would get a flat list like this:
aaaa
bbbb
eeee
pppp
qqqq
rrrr
tttt
uuuu
vvvv
wwww
ssss
ffff
gggg
hhhh
cccc
iiii
jjjj
kkkk
dddd
mmmm
nnnn
oooo

http://coliru.stacked-crooked.com/a/dff47bcc7434f304
Ahhh that makes sense. Thank you for the clarification(:
Topic archived. No new replies allowed.