-
Notifications
You must be signed in to change notification settings - Fork 14
/
13.28.cpp
105 lines (92 loc) · 1.63 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
#include<iostream>
#include<string>
class TreeNode
{
public:
TreeNode(std::string s = std::string()) : value(s), count(new int(1)), left(nullptr), right(nullptr) { }
TreeNode(const TreeNode & tn) : value(tn.value), count(tn.count), left(tn.left), right(tn.right) { ++*count; }
~TreeNode();
TreeNode & operator=(const TreeNode & tn);
private:
void destory_subTree();
std::string value;
int * count;
TreeNode *left;
TreeNode *right;
};
void TreeNode::destory_subTree()
{
if(left != nullptr)
{
left->destory_subTree();
delete left;
left = nullptr;
}
if(right != nullptr)
{
right->destory_subTree();
delete right;
right = nullptr;
}
}
TreeNode::~TreeNode()
{
if(--*count == 0)
{
delete count;
destory_subTree();
}
}
TreeNode & TreeNode::operator=(const TreeNode & tn)
{
++*tn.count;
if(--*count == 0)
{
delete count;
destory_subTree();
}
value = tn.value;
count = tn.count;
left = tn.left;
right = tn.right;
return *this;
}
class BinStrTree
{
public:
BinStrTree(TreeNode * tn = nullptr) : root(tn), count(new int(1)) { }
BinStrTree(const BinStrTree & bst) : root(bst.root), count(bst.count) { ++*count; };
BinStrTree & operator=(const BinStrTree & bst);
~BinStrTree();
private:
TreeNode *root;
int * count;
};
BinStrTree::~BinStrTree()
{
if(--*count == 0)
{
delete root;
delete count;
}
}
BinStrTree & BinStrTree::operator=(const BinStrTree & bst)
{
++*bst.count;
if(--*count == 0)
{
delete root;
delete count;
}
root = bst.root;
count = bst.count;
return *this;
}
int main()
{
TreeNode tna("123");
TreeNode tnb = tna;
tnb = tnb;
BinStrTree bst(new TreeNode(tnb));
return 0;
}