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 <set>
uint32_t Node::node_id = 0;
Node::Node(const std::string &name)
{
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->node_id;
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()
{
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;
}
std::cout << "leave ~node() of \"" << name << "\"" << std::endl;
std::cout << "leave ~node() of \"" << this->name << "\"" << std::endl;
}
std::string Node::get_name() const
{
return name;
return this->name;
}
void Node::set_name(const std::string &new_name)
{
name = new_name;
this->name = new_name;
}
int Node::get_nr_children() const
{
return children.size();
return this->children.size();
}
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 children[i];
return this->children[i];
}
void Node::add_child(Node *child)
{
children.emplace_back(child);
void Node::add_child(Node* child) {
this->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) {
for (uint32_t i = 0; i < depth; i++)
std::set<const Node*> set;
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)
@ -83,18 +96,16 @@ Node *Node::create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth)
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);
if (child == nullptr)
{
break;
return node;
}
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;
@ -177,3 +188,14 @@ std::ostream &operator<<(std::ostream &os, const Node *node)
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();
}