How to insert elements to n array tree structure recursively if we know connection between parent and...
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;
}
};
//main
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"));
c++ c++11 tree parent-child tree-structure
add a comment |
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;
}
};
//main
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"));
c++ c++11 tree parent-child tree-structure
add a comment |
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;
}
};
//main
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"));
c++ c++11 tree parent-child tree-structure
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;
}
};
//main
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"));
c++ c++11 tree parent-child tree-structure
c++ c++11 tree parent-child tree-structure
edited Nov 27 '18 at 16:21
helloworldman
asked Nov 27 '18 at 15:39
helloworldmanhelloworldman
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can move recursively over the tree and add you element when find the parent node.
Consider this function:
bool insert(string s, string t, Node * root) { // will return true is success
if (root->key == s) { // found the parent -> insert the new node
(root->child).push_back(new Node(t));
return true;
}
bool ans = false;
for( int i =0; i< (root->child).size();i++){
ans |= insert(s, t, root->child[i]); recursive call to all the children
}
return ans;
}
Now when using it in the main:
int main()
{
Node* root = new Node("A");
cout << "status adding B to A: " << insert("A", "B", root) << endl; // return true
cout << "status adding E to G: " << insert("G", "E", root) << endl; // return false
cout << "status adding E to B: " << insert("B", "E", root) << endl; // return true
return 0;
}
Hope that helps!
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503123%2fhow-to-insert-elements-to-n-array-tree-structure-recursively-if-we-know-connecti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can move recursively over the tree and add you element when find the parent node.
Consider this function:
bool insert(string s, string t, Node * root) { // will return true is success
if (root->key == s) { // found the parent -> insert the new node
(root->child).push_back(new Node(t));
return true;
}
bool ans = false;
for( int i =0; i< (root->child).size();i++){
ans |= insert(s, t, root->child[i]); recursive call to all the children
}
return ans;
}
Now when using it in the main:
int main()
{
Node* root = new Node("A");
cout << "status adding B to A: " << insert("A", "B", root) << endl; // return true
cout << "status adding E to G: " << insert("G", "E", root) << endl; // return false
cout << "status adding E to B: " << insert("B", "E", root) << endl; // return true
return 0;
}
Hope that helps!
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
add a comment |
You can move recursively over the tree and add you element when find the parent node.
Consider this function:
bool insert(string s, string t, Node * root) { // will return true is success
if (root->key == s) { // found the parent -> insert the new node
(root->child).push_back(new Node(t));
return true;
}
bool ans = false;
for( int i =0; i< (root->child).size();i++){
ans |= insert(s, t, root->child[i]); recursive call to all the children
}
return ans;
}
Now when using it in the main:
int main()
{
Node* root = new Node("A");
cout << "status adding B to A: " << insert("A", "B", root) << endl; // return true
cout << "status adding E to G: " << insert("G", "E", root) << endl; // return false
cout << "status adding E to B: " << insert("B", "E", root) << endl; // return true
return 0;
}
Hope that helps!
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
add a comment |
You can move recursively over the tree and add you element when find the parent node.
Consider this function:
bool insert(string s, string t, Node * root) { // will return true is success
if (root->key == s) { // found the parent -> insert the new node
(root->child).push_back(new Node(t));
return true;
}
bool ans = false;
for( int i =0; i< (root->child).size();i++){
ans |= insert(s, t, root->child[i]); recursive call to all the children
}
return ans;
}
Now when using it in the main:
int main()
{
Node* root = new Node("A");
cout << "status adding B to A: " << insert("A", "B", root) << endl; // return true
cout << "status adding E to G: " << insert("G", "E", root) << endl; // return false
cout << "status adding E to B: " << insert("B", "E", root) << endl; // return true
return 0;
}
Hope that helps!
You can move recursively over the tree and add you element when find the parent node.
Consider this function:
bool insert(string s, string t, Node * root) { // will return true is success
if (root->key == s) { // found the parent -> insert the new node
(root->child).push_back(new Node(t));
return true;
}
bool ans = false;
for( int i =0; i< (root->child).size();i++){
ans |= insert(s, t, root->child[i]); recursive call to all the children
}
return ans;
}
Now when using it in the main:
int main()
{
Node* root = new Node("A");
cout << "status adding B to A: " << insert("A", "B", root) << endl; // return true
cout << "status adding E to G: " << insert("G", "E", root) << endl; // return false
cout << "status adding E to B: " << insert("B", "E", root) << endl; // return true
return 0;
}
Hope that helps!
answered Nov 28 '18 at 6:34
dWinderdWinder
5,63131130
5,63131130
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
add a comment |
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
thanks you very much! you helped a lot!
– helloworldman
Nov 28 '18 at 8:04
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
by the way how to make that A has only one child B if we declared insert("A", "B", root) twice?
– helloworldman
Nov 28 '18 at 10:19
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
@helloworldman Is your data structure is tree? If so then duplicate keys not allow then best approach is to build a function that check is key exists in root and insert only is does not. If your data structure is graph which allow circles then it's different question
– dWinder
Nov 28 '18 at 10:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503123%2fhow-to-insert-elements-to-n-array-tree-structure-recursively-if-we-know-connecti%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown