Created
March 15, 2018 23:21
-
-
Save andr1972/20936eb7bcc71e05cfaa58e3aff28eec to your computer and use it in GitHub Desktop.
Tree with posssiblity replacement subtree with other subtree,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory> | |
#include <string> | |
#include <vector> | |
#include <assert.h> | |
using namespace std; | |
class Node | |
{ | |
vector<shared_ptr<Node>> childs; | |
Node* parent = nullptr;//not shared_ptr! because of memory leaks of circular dependency | |
int level = 0; | |
public: | |
string name; | |
Node(string name) | |
{ | |
this->name = name; | |
} | |
~Node() | |
{ | |
printf("delete %s\n",name.c_str()); | |
} | |
shared_ptr<Node> cloneOne() | |
{ | |
shared_ptr<Node> result = make_shared<Node>(name+"a"); | |
return result; | |
} | |
shared_ptr<Node> cloneTree() | |
{ | |
shared_ptr<Node> newElem = cloneOne(); | |
for (size_t i = 0; i<childs.size(); i++) | |
{ | |
shared_ptr<Node> subElem = childs[i]->cloneTree(); | |
newElem->AddOneChild(subElem); | |
} | |
return newElem; | |
} | |
void erase() | |
{ | |
printf("erase from %s\n", name.c_str()); | |
childs.clear(); | |
} | |
void deleteChild(int index) | |
{ | |
printf("delete child %d from %s - %s\n", index, name.c_str(), childs[index]->name.c_str()); | |
childs.erase(childs.begin()+index); | |
} | |
void setLevel(int level) | |
{ | |
this->level = level; | |
for (size_t i = 0; i<childs.size(); i++) | |
childs[i]->setLevel(level+1); | |
} | |
void AddOneChild(shared_ptr<Node> node) | |
{ | |
childs.push_back(node); | |
node->parent = this; | |
} | |
void InsertOneChild(int index, shared_ptr<Node> node) | |
{ | |
childs.insert(childs.begin() + index, node); | |
node->parent = this; | |
} | |
void AddTree(shared_ptr<Node> subtree) | |
{ | |
shared_ptr<Node> clone = subtree->cloneTree(); | |
AddOneChild(clone); | |
clone->setLevel(level+1); | |
} | |
void InsertTree(int index, shared_ptr<Node> subtree) | |
{ | |
shared_ptr<Node> clone = subtree->cloneTree(); | |
InsertOneChild(index, clone); | |
clone->setLevel(level + 1); | |
} | |
void ReplaceChild(int index, shared_ptr<Node> subtree) | |
{ | |
shared_ptr<Node> clone = subtree->cloneTree(); | |
deleteChild(index); | |
InsertOneChild(index, clone); | |
clone->setLevel(level + 1); | |
} | |
shared_ptr<Node>& at(int index) | |
{ | |
return childs[index]; | |
} | |
void print() | |
{ | |
for (int i = 0; i<level; i++) | |
printf(" "); | |
printf("%s->",name.c_str()); | |
if (parent) printf("%s", parent->name.c_str()); | |
printf("\n"); | |
for (size_t i=0; i<childs.size(); i++) | |
childs[i]->print(); | |
} | |
}; | |
int main() | |
{ | |
shared_ptr<Node>root,rootB; | |
root = make_shared<Node>("1"); | |
root->AddTree(make_shared<Node>("2")); | |
root->AddTree(make_shared<Node>("3")); | |
root->at(0)->AddTree(make_shared<Node>("4")); | |
root->at(0)->AddTree(make_shared<Node>("5")); | |
root->at(1)->AddTree(make_shared<Node>("6")); | |
root->at(1)->AddTree(make_shared<Node>("7")); | |
root->print(); | |
root->ReplaceChild(0,root); | |
root->print(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment