2024-03-14 19:12:36 +00:00
|
|
|
#include <iostream>
|
2024-03-14 19:25:58 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
public:
|
|
|
|
virtual ~Node() = default;
|
|
|
|
virtual void print() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InnerNode : public Node {
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
std::vector<Node *> children_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InnerNode(std::string_view name) : name_(name) {}
|
|
|
|
|
|
|
|
void addNode(Node *node) { children_.push_back(node); }
|
|
|
|
|
|
|
|
void print() const override {
|
|
|
|
std::cout << name_ << " (inner)" << std::endl;
|
|
|
|
|
|
|
|
for (auto child : children_) {
|
|
|
|
child->print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Leaf : public Node {
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Leaf(std::string_view name) : name_(name) {}
|
|
|
|
|
|
|
|
void print() const override { std::cout << name_ << " (leaf)" << std::endl; }
|
|
|
|
};
|
2024-03-14 19:12:36 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
std::cout << "Composite" << std::endl;
|
|
|
|
|
2024-03-14 19:25:58 +00:00
|
|
|
InnerNode tree("root");
|
|
|
|
InnerNode left("left"), right("right");
|
|
|
|
Leaf leaf1("leaf 1"), leaf2("leaf 2"), leaf3("leaf 3"), leaf4("leaf 4");
|
|
|
|
|
|
|
|
tree.addNode(&left);
|
|
|
|
tree.addNode(&right);
|
|
|
|
|
|
|
|
left.addNode(&leaf1);
|
|
|
|
left.addNode(&leaf2);
|
|
|
|
|
|
|
|
right.addNode(&leaf3);
|
|
|
|
right.addNode(&leaf4);
|
|
|
|
|
|
|
|
tree.print();
|
|
|
|
|
2024-03-14 19:12:36 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|