ECG/exercise1/node.h

41 lines
1.2 KiB
C
Raw Normal View History

2019-04-19 12:00:50 +00:00
#ifndef NODE_H
#define NODE_H
#include <vector>
#include <string>
#include <sstream>
#include <functional>
#include <iostream>
2019-05-01 08:44:24 +00:00
#include <set>
2019-04-19 12:00:50 +00:00
2019-05-01 08:44:24 +00:00
class Node
{
2019-04-19 12:00:50 +00:00
2019-05-01 08:44:24 +00:00
public:
Node(const std::string &name = "");
2019-04-19 12:00:50 +00:00
virtual ~Node();
std::string get_name() const;
2019-05-01 08:44:24 +00:00
void set_name(const std::string &new_name);
2019-04-19 12:00:50 +00:00
int get_nr_children() const;
2019-05-01 08:44:24 +00:00
Node *get_child(int i) const;
void add_child(Node *child);
2019-04-19 12:00:50 +00:00
2019-05-01 08:44:24 +00:00
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 void traverse_tree(const Node *node, std::function<void(const Node *, uint32_t)> predicate, bool recursive, uint32_t depth = 0);
2019-04-19 12:00:50 +00:00
2019-05-01 08:44:24 +00:00
private:
2019-04-19 12:00:50 +00:00
std::string name;
2019-05-01 08:44:24 +00:00
std::vector<Node *> children;
2019-04-19 12:00:50 +00:00
static uint32_t node_id;
2019-05-01 08:44:24 +00:00
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);
2019-04-19 12:00:50 +00:00
};
2019-05-01 08:44:24 +00:00
extern std::ostream &operator<<(std::ostream &os, const Node *node);
2019-04-19 12:00:50 +00:00
#endif // NODE_H