-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshared-test-case.cpp
169 lines (108 loc) · 3.35 KB
/
shared-test-case.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
#include <memory>
#include <utility>
#include <queue>
#include <stack>
// TODO: Implement print_tree()
template<class Key> class Node; // fwd ref.
template<class Key> std::shared_ptr<Node<Key>> buildTree(Key k1, Key k2, Key k3); // fwd ref.
template<class Key> void print_tree(const Node<Key>& root); // fwd ref.
template<class Key> class Node {
friend std::shared_ptr<Node<Key>> buildTree<Key>(Key k1, Key k2, Key k3);
friend void print_tree<Key>(const Node<Key>&);
Key key;
std::shared_ptr<Node> left;
std::shared_ptr<Node> right;
public:
Node(Key k) : key{k} {}
Node(const Node<Key>& ) = default;
Node(Node<Key>&& ) = default;
Node<Key>& operator=(const Node<Key>&) = default;
Node<Key>& operator=(Node<Key>&&) = default;
Key get_key() { return key; }
~Node()
{
std::cout << "In ~Node<Key> with key = \n\t" << get_key() << "\n\t and this = " << this << '.' << std::endl;
}
};
template<class Key> std::shared_ptr<Node<Key>> buildTree(Key k1, Key k2, Key k3)
{
std::shared_ptr<Node<Key>> tree;
Key min, mid, max;
if (k1 < k2) {
min = k1;
max = k2;
} else {
min = k2;
max = k1;
}
if (k3 < min) {
mid = min;
min = k3;
} else if (k3 < max) {
mid = k3;
} else {
mid = max;
max = k3;
}
tree = std::make_shared<Node<Key>>(mid);
tree->left = std::make_shared<Node<Key>>(min);
tree->right = std::make_shared<Node<Key>>(max);
return tree;
}
template<class Key, class Functor> void levelOrderTraverse(const Node<Key>& root, Functor f)
{
if (root == nullptr) return;
// pair of: 1. Node pointer and 2. level of tree.
std::queue<std::pair<const Node<Key>*, int>> q;
auto level = 1;
q.push(std::make_pair(root.get(), level));
while (!q.empty()) {
std::pair<const Node<Key> *, int> pair_ = q.front();
const Node<Key> *current = pair_.first;
int tree_level = pair_.second;
f(current, tree_level); // For example: print out all the keys_values in current.
if (!current->isLeaf()) {
q.push(std::make_pair(current->left.get(), tree_level + 1));
q.push(std::make_pair(current->right.get(), tree_level + 1));
}
q.pop();
}
}
template<class Key> void print_tree(const Node<Key>& root)
{
// auto functor = [&](const Node *current, int level) {
}
/*
Object for testing trees implemented with shared_ptr's
*/
class Test {
int i;
public:
bool operator <(const Test& lhs) { return i < lhs.i; }
bool operator==(const Test& lhs) { return i == lhs.i; }
Test() : i{0} {}
Test(int in) : i{in} {}
Test(const Test& ) = default;
Test(Test&& ) = default;
Test& operator=(const Test&) = default;
Test& operator=(Test&&) = default;
friend std::ostream& operator<<(std::ostream& ostr, const Test& lhs)
{
ostr << "i = " << lhs.i << std::endl;
return ostr;
}
~Test()
{
std::cout << "In ~Test() with i = " << i << " and this = " << this << std::endl;
}
};
using namespace std;
int main(int argc, char** argv)
{
shared_ptr<Node<Test>> root = buildTree(Test(0), Test{1}, Test{2});
cout << "\nTree built." << endl;
shared_ptr<Node<Test>> root2 = root;
cout << "\nroot2 = root done. Doing 'return 0'." << endl;
return 0;
}