Update node.cpp

This commit is contained in:
Gregor 2019-05-08 11:07:37 +00:00
parent 2be8580dce
commit e1c649cb34

View file

@ -1,79 +1,92 @@
#include "node.h" #include "node.h"
#include <set>
uint32_t Node::node_id = 0; uint32_t Node::node_id = 0;
Node::Node(const std::string &name) Node::Node(const std::string &name)
{ {
node_id++; ++this->node_id;
if (name.empty())
{
std::stringstream str_sm;
str_sm << node_id;
std::string node_id_str = str_sm.str();
this->name = "node" + node_id_str;
}
else
{
this->name = name; this->name = name;
} }
Node::Node()
{
++this->node_id;
std::stringstream str_sm;
str_sm << "node_";
str_sm << this->node_id;
this->name = str_sm.str();
} }
Node::~Node() Node::~Node()
{ {
std::cout << "enter ~node() of \"" << name << "\"" << std::endl; std::cout << "enter ~node() of \"" << this->name << "\"" << std::endl;
for (Node *child : children) for (Node *child : this->children)
{ {
delete child; delete child;
} }
std::cout << "leave ~node() of \"" << name << "\"" << std::endl; std::cout << "leave ~node() of \"" << this->name << "\"" << std::endl;
} }
std::string Node::get_name() const std::string Node::get_name() const
{ {
return name; return this->name;
} }
void Node::set_name(const std::string &new_name) void Node::set_name(const std::string &new_name)
{ {
name = new_name; this->name = new_name;
} }
int Node::get_nr_children() const int Node::get_nr_children() const
{ {
return children.size(); return this->children.size();
} }
Node *Node::get_child(int i) const Node *Node::get_child(int i) const
{ {
if (i < 0 || i >= static_cast<int>(children.size())) if (i < 0 || i >= static_cast<int>(this->children.size()))
{ {
return nullptr; return nullptr;
} }
return children[i]; return this->children[i];
} }
void Node::add_child(Node *child) void Node::add_child(Node* child) {
{ this->children.emplace_back(child);
children.emplace_back(child);
} }
void Node::print(std::ostream &str, uint32_t depth) const void Node::print(std::ostream& stream) const
{ {
auto predicate = [&str](const Node *node, uint32_t depth) { std::set<const Node*> set;
for (uint32_t i = 0; i < depth; i++) this->print(stream, 0, set);
};
// std::string(times, " ");
void Node::print(std::ostream& stream, int level, std::set<const Node*>& set) const
{
int i = 0;
int count = this->get_nr_children();
if (set.find(this) != set.end())
{ {
str << "\t"; stream << repeat(" ", level * 3) << this->get_name() << " (cycle)" << std::endl;
} }
else
{
set.insert(this);
str << node->get_name() + "\n"; stream << repeat(" ", level * 3) << this->get_name() << std::endl;
};
Node::traverse_tree(this, predicate, false); for (; i < count; ++i)
{
this->get_child(i)->print(stream, level + 1, set);
}
}
} }
Node *Node::create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth) Node *Node::create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth)
@ -83,18 +96,16 @@ Node *Node::create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth)
return nullptr; return nullptr;
} }
Node *node = new Node; Node* node = new Node();
for (uint32_t j = 0; j < nr_child_nodes; j++) if (tree_depth == 1)
{ {
Node *child = Node::create_complete_tree(nr_child_nodes, tree_depth - 1); return node;
if (child == nullptr)
{
break;
} }
node->children.emplace_back(child); for (uint32_t i = 0; i < nr_child_nodes; ++i)
{
node->add_child(create_complete_tree(nr_child_nodes, tree_depth - 1));
} }
return node; return node;
@ -177,3 +188,14 @@ std::ostream &operator<<(std::ostream &os, const Node *node)
return os; return os;
} }
std::string repeat(std::string value, int times)
{
std::stringstream str_sm;
for (int i = 0; i < times; ++i) {
str_sm << value;
}
return str_sm.str();
}