forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.28.cpp
204 lines (168 loc) · 5.5 KB
/
13.28.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <string>
// Valuelike `TreeNode`. When we copy a `TreeNode`, we copy all the subtree
// from current node.
class TreeNode {
public:
TreeNode() : value(), count(0), left(nullptr), right(nullptr) {}
TreeNode(const std::string &s, int c)
: value(s), count(c), left(nullptr), right(nullptr) {}
TreeNode(const TreeNode &);
~TreeNode();
TreeNode &operator=(const TreeNode &);
// insert
// remove
// tranverse
private:
std::string value;
int count;
TreeNode *left;
TreeNode *right;
// Note that the valid state for the `left` and `right` pointer is either
// `nullptr` or a subtree node. So that we must check these pointers every
// time before dereferencing them.
/* First version: helper funtion for copy constructor and destructor *
static void copyTree(TreeNode *, const TreeNode *);
static void destroyTree(TreeNode *);
/**/
};
/* First version: copy constructor and destructor *
void TreeNode::copyTree(TreeNode *lhs, const TreeNode *rhs) {
// Both lhs and rhs must NOT be nullptr
lhs->value = rhs->value;
lhs->count = rhs->count;
if (rhs->left) {
lhs->left = new TreeNode; // call default constructor of `TreeNode`
copyTree(lhs->left, rhs->left);
} else
lhs->left = nullptr;
if (rhs->right) {
lhs->right = new TreeNode; // call default constructor of `TreeNode`
copyTree(lhs->right, rhs->right);
} else
lhs->right = nullptr;
}
void TreeNode::destroyTree(TreeNode *n) {
// n can be a nullptr
if (!n) return;
destroyTree(n->left);
n->left = nullptr;
destroyTree(n->right);
n->right = nullptr;
delete n; // This statement will cause delete subtree multiple times if we
// forget to assign n->left and n->right to nullptr.
}
TreeNode::TreeNode(const TreeNode &n) {
copyTree(this, &n);
}
TreeNode::~TreeNode() {
destroyTree(left);
destroyTree(right);
}
/**/
/* Second version: without helper function */
TreeNode::TreeNode(const TreeNode &n)
: value(n.value), count(n.count), left(nullptr), right(nullptr) {
if (n.left)
left = new TreeNode(*n.left); // recursively call copy constructor
if (n.right)
right = new TreeNode(*n.right); // recursively call copy constructor
}
TreeNode::~TreeNode() {
delete left; // recursively call destructor on left subtree node
delete right; // recursively call destructor on right subtree node
// Note that when left or right is nullptr, delete will do nothing, so that
// the recursion will be stopped.
}
TreeNode &TreeNode::operator=(const TreeNode &n) {
value = n.value;
count = n.count;
TreeNode *tmp = nullptr;
if (n.left)
tmp = new TreeNode(*n.left); // recursively call copy constructor
delete left; // recursively call destructor
left = tmp;
tmp = nullptr;
if (n.right)
tmp = new TreeNode(*n.right); // recursively call copy constructor
delete right; // recursively call destructor
right = tmp;
return *this;
}
/**/
// Two versions of `BinStrTree` are defined here. The first version is
// valuelike version, while the second verison is pointerlike version. The
// class declarations of these two versions are the same except the pointerlike
// version adds a data member for reference count, while the defitions of
// copy-control members are different.
/* First Version: Valuelike `BinStrTree` *
// When we copy a `BinStrTree`, we copy all the subtree from `root` node.
class BinStrTree {
public:
BinStrTree(const TreeNode &r = TreeNode()) : root(new TreeNode(r)) {}
BinStrTree(const BinStrTree &);
~BinStrTree();
BinStrTree &operator=(const BinStrTree &);
private:
TreeNode *root;
// Note that the valid state for the `root` pointer is always a pointer to an
// existing tree node, and `nullptr` is not a valid state. So that we can
// dereference this pointer without checking if it's a null pointer in copy
// constructor and copy-assignment operator. (Class Invariant)
};
BinStrTree::BinStrTree(const BinStrTree &rhs)
: root(new TreeNode(*rhs.root)) {} // recursively call copy constructor
BinStrTree::~BinStrTree() {
delete root; // recursively call destructor of `TreeNode`
}
BinStrTree &BinStrTree::operator=(const BinStrTree &t) {
TreeNode *tmp = new TreeNode(*t.root);
delete root;
root = tmp;
return *this;
}
/**/
/* Second version: Pointerlike `BinStrTree` */
// When we copy a `BinStrTree`, the copyed object point to the same tree as the
// original object.
class BinStrTree {
public:
BinStrTree(const TreeNode &r = TreeNode())
: root(new TreeNode(r)), use(new std::size_t(1)) {}
BinStrTree(const BinStrTree &);
~BinStrTree();
BinStrTree &operator=(const BinStrTree &);
private:
TreeNode *root;
// Note that the valid state for the `root` pointer is always a pointer to an
// existing tree node, and `nullptr` is not a valid state. So that we can
// dereference this pointer without checking if it's a null pointer in copy
// constructor and copy-assignment operator. (Class Invariant)
std::size_t *use;
};
BinStrTree::BinStrTree(const BinStrTree &rhs)
: root(new TreeNode(*rhs.root)), use(rhs.use) { ++*use; }
// recursively call copy constructor
BinStrTree::~BinStrTree() {
if (--*use == 0) {
delete root; // recursively call destructor of `TreeNode`
delete use;
}
}
BinStrTree &BinStrTree::operator=(const BinStrTree &t) {
++*t.use;
if (--*use == 0) {
delete root;
delete use;
}
root = t.root;
use = t.use;
return *this;
}
/**/
int main() {
// TODO test class `TreeNode`
TreeNode node;
// TODO test class `BinStrTree`
BinStrTree bst;
return 0;
}