-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cpp
87 lines (72 loc) · 1.69 KB
/
Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @file Tree.cpp
*/
#include <iostream>
#include <list>
#include <iomanip>
using namespace std;
template <typename T>
class Tree
{
public:
struct Node {
T value;
list<Node*> children;
Node(const T& value) : value(value) {};
};
explicit Tree(const T& value) {
root_ = new Node(value);
}
virtual ~Tree() {
ClearRecursive(root_);
cout << "delete : " << root_->value << endl;
delete root_;
}
Node* GetRoot() const {
return root_;
}
static Node* Append(Node *node, const T& value) {
Node* child = new Node(value);
node->children.push_back(child);
return child;
}
void Dump() const {
DumpRecursive(root_);
}
private:
Node* root_;
void ClearRecursive(Node* node) {
list<Node*>& children = node->children;
typename list<Node*>::iterator itr = children.begin();
for (; itr != children.end(); ++itr) {
ClearRecursive(*itr);
}
children.clear(); // リスト内の全ての要素を削除
// root nodeはdeleteしない
if (node != root_) {
cout << "delete : " << node->value << endl;
delete node;
}
}
void DumpRecursive(Node *node, int depth = 0) const {
cout << setw(depth*4) << " " << node->value << endl;
list<Node*>& children = node->children;
typename list<Node*>::iterator itr = children.begin();
for (; itr != children.end(); ++itr) {
DumpRecursive(*itr, depth + 1);
}
}
};
int main() {
Tree<string> tree("foo");
Tree<string>::Node* foo = tree.GetRoot();
Tree<string>::Node* bar = tree.Append(foo, "bar");
Tree<string>::Node* baz = tree.Append(foo, "baz");
tree.Append(bar, "bar.hpp");
tree.Append(bar, "bar.cpp");
tree.Append(baz, "baz.hpp");
tree.Append(baz, "baz.cpp");
tree.Append(foo, "README");
tree.Dump();
return 0;
}