forked from ChrisRyan98008/NwCpp-May2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_tree.cpp
97 lines (77 loc) · 2.7 KB
/
main_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
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include "util.h"
#include "Serialize.h"
using namespace Serialize;
class Node : public Serializable<Node>
{
using Base = Serializable;
public:
using shared_ptr = std::shared_ptr<Node>;
Node(shared_ptr pLeft = nullptr, shared_ptr pRight = nullptr)
: _pLeft(pLeft), _pRight(pRight)
{}
void Serialize(Archive& arc)
{
Base::Serialize(arc);
arc.Serialize(_pLeft);
arc.Serialize(_pRight);
}
template<typename ...Args> static auto make_shared(Args...args) { return std::make_shared<Node>(args...); }
protected:
shared_ptr _pLeft;
shared_ptr _pRight;
template<class Type> friend class Util::DrawTree;
virtual std::ostream& TextOut(std::ostream& os) const { return os; }
friend std::ostream& operator<<(std::ostream& os, const Node& node) { return node.TextOut(os); }
};
class Node2 : public Serializable<Node2, Node>
{
using Base = Serializable;
public:
using shared_ptr = std::shared_ptr<Node2>;
using unique_ptr = std::unique_ptr<Node2>;
Node2(int32 data = 0, shared_ptr pLeft = nullptr, shared_ptr pRight = nullptr)
: Base(pLeft, pRight), _data(data)
{}
void Serialize(Archive& arc)
{
Base::Serialize(arc);
arc.Serialize(_data);
}
template<typename ...Args> static auto make_shared(Args...args) { return std::make_shared<Node2>(args...); }
template<typename ...Args> static auto make_unique(Args...args) { return std::make_unique<Node2>(args...); }
protected:
int32 _data;
virtual std::ostream& TextOut(std::ostream& os) const
{
Base::TextOut(os);
return os << _data;
}
};
Node2::shared_ptr GenerateNode2Tree()
{
return Node2::make_shared(1,
Node2::make_shared(2,
Node2::make_shared(4,
Node2::make_shared(9),
nullptr),
Node2::make_shared(5,
Node2::make_shared(10,
nullptr,
Node2::make_shared(8)),
Node2::make_shared(11))),
Node2::make_shared(3,
Node2::make_shared(6,
Node2::make_shared(12),
Node2::make_shared(13)),
Node2::make_shared(7,
Node2::make_shared(14),
Node2::make_shared(15))));
}
int main()
{
Node2::shared_ptr p2Tree = GenerateNode2Tree();
std::cout << "Node2 Tree:\n";
std::cout << Util::DrawTree<decltype(p2Tree)>(p2Tree, true) << "\n";
return 0;
}