-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
235 lines (203 loc) · 4.43 KB
/
tree.cpp
File metadata and controls
235 lines (203 loc) · 4.43 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <string>
using namespace std;
class node {
public :
string name;
double score;
node *left, *right;
void set_data(string s, double n);
};
void node::set_data(string s, double n)
{
name = s;
score = n;
}
class my_tree {
public :
int node_count;
node *root;
my_tree();
int insert_root(node t);
int insert_left(string tname, node t);
int insert_right(string tname, node t);
double score_sum();
double score_average();
void print_data_inorder();
void print_data_preorder();
void print_data_postorder();
};
my_tree::my_tree()
{
node_count = 0;
root = NULL;
}
int my_tree::insert_root(node t)
{
if(root != NULL)
return 0;
node *p;
p = new node;
(*p) = t;
p->left = NULL;
p->right = NULL;
root = p;
node_count++;
return 1;
}
int node_insert_left(node *p, string tname, node tnode)
{
int result;
if(p == NULL)
return 0;
if(p->name == tname)
{
if(p->left != NULL)
return -1;
node *t;
t = new node;
(*t) = tnode;
t->left = NULL;
t->right = NULL;
p->left = t;
return 1;
}
else
{
result = node_insert_left(p->left, tname, tnode);
if(result != 0)
return result;
return node_insert_left(p->right, tname, tnode);
}
}
int my_tree::insert_left(string tname, node tnode)
{
int result;
result = node_insert_left(root, tname, tnode);
if(result == 1)
node_count++;
return result;
}
int node_insert_right(node *p, string tname, node tnode)
{
int result;
if(p == NULL)
return 0;
if(p->name == tname)
{
if(p->right != NULL)
return -1;
node *t;
t = new node;
(*t) = tnode;
t->left = NULL;
t->right = NULL;
p->right = t;
return 1;
}
else
{
result = node_insert_right(p->left, tname, tnode);
if(result != 0)
return result;
return node_insert_right(p->right, tname, tnode);
}
}
int my_tree::insert_right(string tname, node tnode)
{
int result;
result = node_insert_right(root, tname, tnode);
if(result == 1)
node_count++;
return result;
}
double sum_allnodes(node *p)
{
if(p == NULL)
return 0;
return sum_allnodes(p->left) + sum_allnodes(p->right) + p->score;
}
double my_tree::score_sum()
{
return sum_allnodes(root);
}
int count_allnodes(node *p)
{
if(p == NULL)
return 0;
return count_allnodes(p->left) + count_allnodes(p->right) + 1;
}
double average_allnodes(node *p)
{
if(p == NULL)
return 0;
int count;
count = count_allnodes(p);
return sum_allnodes(p) / count;
}
double my_tree::score_average()
{
return average_allnodes(root);
}
void inorder_print(node *p)
{
if(p == NULL)
return;
inorder_print(p->left);
cout << p->name << " : " << p->score << "\n";
inorder_print(p->right);
}
void my_tree::print_data_inorder()
{
inorder_print(root);
}
void preorder_print(node *p)
{
if(p == NULL)
return;
cout << p->name << " : " << p->score << "\n";
preorder_print(p->left);
preorder_print(p->right);
}
void my_tree::print_data_preorder()
{
preorder_print(root);
}
void postorder_print(node *p)
{
if(p == NULL)
return;
postorder_print(p->left);
postorder_print(p->right);
cout << p->name << " : " << p->score << "\n";
}
void my_tree::print_data_postorder()
{
postorder_print(root);
}
int main ()
{
my_tree thetree;
node tmp;
tmp.set_data("Kim", 8.1);
thetree.insert_root(tmp);
tmp.set_data("Lee", 6.5);
thetree.insert_left("Kim", tmp);
tmp.set_data("Park", 8.3);
thetree.insert_right("Kim", tmp);
tmp.set_data("Choi", 7.2);
thetree.insert_left("Lee", tmp);
tmp.set_data("Ryu", 9.0);
thetree.insert_right("Lee", tmp);
tmp.set_data("Cho", 7.7);
thetree.insert_right("Park", tmp);
cout << "Score Sum : " << thetree.score_sum() << "\n";
cout<< "Score Average : " << thetree.score_average() << "\n";
cout <<"\n Inorder Traversal Result \n";
thetree.print_data_inorder();
cout << "\n Preorder Traversal Result \n";
thetree.print_data_preorder();
cout << "\n Postorder Traversal Result \n";
thetree.print_data_postorder();
return 0;
}