Tree datastructures: Difference between revisions
Content added Content deleted
(Added C++ solution) |
m (C++ - use list instead of vector in nest_tree so that adding a child doesn't invalidate references to existing children) |
||
Line 42: | Line 42: | ||
<lang cpp>#include <iomanip> |
<lang cpp>#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
#include <list> |
|||
#include <string> |
#include <string> |
||
#include <vector> |
#include <vector> |
||
Line 64: | Line 65: | ||
return name_; |
return name_; |
||
} |
} |
||
const std:: |
const std::list<nest_tree>& children() const { |
||
return children_; |
return children_; |
||
} |
} |
||
Line 78: | Line 79: | ||
} |
} |
||
std::string name_; |
std::string name_; |
||
std:: |
std::list<nest_tree> children_; |
||
}; |
}; |
||
Line 120: | Line 121: | ||
nest_tree n("RosettaCode"); |
nest_tree n("RosettaCode"); |
||
auto& child1 = n.add_child("rocks"); |
auto& child1 = n.add_child("rocks"); |
||
⚫ | |||
child1.add_child("code"); |
child1.add_child("code"); |
||
child1.add_child("comparison"); |
child1.add_child("comparison"); |
||
child1.add_child("wiki"); |
child1.add_child("wiki"); |
||
⚫ | |||
child2.add_child("trolling"); |
child2.add_child("trolling"); |
||