mardi 27 novembre 2018

How to add elements to n array tree structure recursively if we know connection between parent and children?

It is given that A is root and its children are B, C, D and also we know that B has a child E. My question is how to insert elements recursively instead of adding element by element if we know connection between them?

class Node { 
public: 
string key; 
vector<Node*> child; 

// constructor 
Node(string data) 
{ 
    key = data; 
} 
}; 

Node* root = new Node("A"); 
(root->child).push_back(new Node("B")); 
(root->child).push_back(new Node("C")); 
(root->child).push_back(new Node("D"));  
(root->child[0]->child).push_back(new Node("E"));

Aucun commentaire:

Enregistrer un commentaire