diff --git a/exercise1/node.cpp b/exercise1/node.cpp index 7f4a0fe..cdacd26 100644 --- a/exercise1/node.cpp +++ b/exercise1/node.cpp @@ -1,79 +1,92 @@ #include "node.h" +#include uint32_t Node::node_id = 0; Node::Node(const std::string &name) { - node_id++; + ++this->node_id; + this->name = name; +} - if (name.empty()) - { - std::stringstream str_sm; - str_sm << node_id; - std::string node_id_str = str_sm.str(); +Node::Node() +{ + ++this->node_id; - this->name = "node" + node_id_str; - } - else - { - this->name = name; - } + 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(children.size())) + if (i < 0 || i >= static_cast(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 set; + this->print(stream, 0, set); +}; + +// std::string(times, " "); +void Node::print(std::ostream& stream, int level, std::set& set) const +{ + int i = 0; + int count = this->get_nr_children(); + + if (set.find(this) != set.end()) + { + stream << repeat(" ", level * 3) << this->get_name() << " (cycle)" << std::endl; + } + else + { + set.insert(this); + + stream << repeat(" ", level * 3) << this->get_name() << std::endl; + + for (; i < count; ++i) { - str << "\t"; + this->get_child(i)->print(stream, level + 1, set); } - - str << node->get_name() + "\n"; - }; - - Node::traverse_tree(this, predicate, false); + } } 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); + 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; @@ -176,4 +187,15 @@ std::ostream &operator<<(std::ostream &os, const Node *node) node->print(os); return os; -} \ No newline at end of file +} + +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(); +}