Add iteration and unique check

This commit is contained in:
hodasemi 2019-05-01 10:44:24 +02:00
parent f61fe57a40
commit f2a8d58b24
3 changed files with 113 additions and 32 deletions

View file

@ -2,7 +2,8 @@
#include "node.h" #include "node.h"
int main() { int main()
{
/* /*
Node* root = new Node("root"); Node* root = new Node("root");
Node* left_child = new Node("left child"); Node* left_child = new Node("left child");
@ -14,11 +15,26 @@ int main() {
delete root; delete root;
*/ */
/*
Node *auto_root = Node::create_complete_tree(2, 4); Node *auto_root = Node::create_complete_tree(2, 4);
std::cout << auto_root << std::endl; std::cout << auto_root << std::endl;
delete auto_root; delete auto_root;
*/
// cycle test
Node *root = new Node;
Node *child1 = new Node;
Node *child2 = new Node;
root->add_child(child1);
root->add_child(child2);
// add cycle
child1->add_child(root);
std::cout << root << std::endl;
return 0; return 0;
} }

View file

@ -64,8 +64,7 @@ void Node::add_child(Node* child)
void Node::print(std::ostream &str, uint32_t depth) const void Node::print(std::ostream &str, uint32_t depth) const
{ {
auto predicate = [&str](const Node* node, uint32_t depth) auto predicate = [&str](const Node *node, uint32_t depth) {
{
for (uint32_t i = 0; i < depth; i++) for (uint32_t i = 0; i < depth; i++)
{ {
str << "\t"; str << "\t";
@ -74,7 +73,7 @@ void Node::print(std::ostream& str, uint32_t depth) const
str << node->get_name() + "\n"; str << node->get_name() + "\n";
}; };
Node::traverse_tree(this, predicate); Node::traverse_tree(this, predicate, false);
} }
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)
@ -101,13 +100,74 @@ Node* Node::create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth)
return node; return node;
} }
void Node::traverse_tree(const Node* node, std::function<void(const Node*, uint32_t)> predicate, uint32_t depth) void Node::traverse_tree(const Node *node, std::function<void(const Node *, uint32_t)> predicate, bool recursive, uint32_t depth)
{ {
if (recursive)
{
std::set<const Node *> visited;
traverse_tree_recursive(node, predicate, visited, depth);
}
else
{
traverse_tree_iterative(node, predicate, depth);
}
}
void Node::traverse_tree_recursive(const Node *node, std::function<void(const Node *, uint32_t)> predicate, std::set<const Node *> &visited, uint32_t depth)
{
if (visited.find(node) != visited.end())
{
return;
}
predicate(node, depth); predicate(node, depth);
visited.insert(node);
for (Node *child : node->children) for (Node *child : node->children)
{ {
Node::traverse_tree(child, predicate, depth + 1); Node::traverse_tree_recursive(child, predicate, visited, depth + 1);
}
}
void Node::traverse_tree_iterative(const Node *node, std::function<void(const Node *, uint32_t)> predicate, uint32_t depth)
{
struct NodeInfo
{
const Node *node;
uint32_t depth;
};
std::set<const Node *> visited;
std::vector<NodeInfo> stack;
NodeInfo info = {.node = node, .depth = 0};
stack.emplace_back(info);
while (!stack.empty())
{
// get the last node and remove it from the stack
NodeInfo current = stack.back();
stack.pop_back();
// check if we already visited this node
if (visited.find(current.node) != visited.end())
{
continue;
}
// execute given function
predicate(current.node, current.depth);
// mark this node as visited
visited.insert(current.node);
// push children to the stack
for (auto child = current.node->children.rbegin(); child != current.node->children.rend(); child++)
{
NodeInfo info = {.node = *child, .depth = current.depth + 1};
stack.emplace_back(info);
}
} }
} }

View file

@ -6,8 +6,10 @@
#include <sstream> #include <sstream>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <set>
class Node { class Node
{
public: public:
Node(const std::string &name = ""); Node(const std::string &name = "");
@ -22,13 +24,16 @@ public:
void print(std::ostream &str = std::cout, uint32_t depth = 0) const; void print(std::ostream &str = std::cout, uint32_t depth = 0) const;
static Node *create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth); static Node *create_complete_tree(uint32_t nr_child_nodes, uint32_t tree_depth);
static void traverse_tree(const Node* node, std::function<void(const Node*, uint32_t)> predicate, uint32_t depth = 0); static void traverse_tree(const Node *node, std::function<void(const Node *, uint32_t)> predicate, bool recursive, uint32_t depth = 0);
private: private:
std::string name; std::string name;
std::vector<Node *> children; std::vector<Node *> children;
static uint32_t node_id; static uint32_t node_id;
static void traverse_tree_recursive(const Node *node, std::function<void(const Node *, uint32_t)> predicate, std::set<const Node *> &visited, uint32_t depth = 0);
static void traverse_tree_iterative(const Node *node, std::function<void(const Node *, uint32_t)> predicate, uint32_t depth = 0);
}; };
extern std::ostream &operator<<(std::ostream &os, const Node *node); extern std::ostream &operator<<(std::ostream &os, const Node *node);