-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathass6.cpp
More file actions
289 lines (226 loc) · 5.79 KB
/
ass6.cpp
File metadata and controls
289 lines (226 loc) · 5.79 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <iostream>
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();
int n_d2_nodes();
int n_d1_nodes();
int n_leaf_nodes();
};
int node_insert_left(node *p, string tname, node tnode);
int node_insert_right(node *p, string tname, node tnode);
double sum_allnodes(node *p);
void inorder_print(node *p);
void preorder_print(node *p);
void postorder_print(node *p);
int n_d2_nodes_count(node* p);
int n_d1_nodes_count(node* p);
int n_leaf_count(node* p);
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 my_tree::insert_left(string tname, node tnode){
int result = 0;
result = node_insert_left(root,tname,tnode);
if(result == 1)
node_count++;
return result;
}
int node_insert_left(node *p, string tname, node tnode){
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{
int result = 0;
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_right(string tname, node tnode){
int result = 0;
result = node_insert_right(root,tname,tnode);
if(result == 1)
node_count++;
return result;
}
int node_insert_right(node *p, string tname, node tnode){
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{
int result = 0;
result = node_insert_right(p->right,tname,tnode);
if(result != 0)
return result;
return node_insert_right(p->left,tname,tnode);
}
}
double my_tree::score_sum(){
return sum_allnodes(root);
}
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_average(){
double average = 0;
average = sum_allnodes(root);
average = average/node_count;
return average;
}
void my_tree::print_data_inorder(){
inorder_print(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_preorder(){
preorder_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_postorder(){
postorder_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";
}
int my_tree::n_d2_nodes(){
int count = 0;
count = n_d2_nodes_count(root);
return count;
}
int n_d2_nodes_count(node* p){
if(p == NULL)
return 0;
int count = 0;
if(p->left != NULL && p->right != NULL)
count = 1;
count += n_d2_nodes_count(p->left) + n_d2_nodes_count(p->right);
return count;
}
int my_tree::n_d1_nodes(){
int count = 0;
count = n_d1_nodes_count(root);
return count;
}
int n_d1_nodes_count(node* p){
if(p == NULL)
return 0;
int count = 0;
if((p->left != NULL && p->right == NULL)|| (p->left == NULL&& p -> right != NULL))
count = 1;
count += n_d1_nodes_count(p->left) + n_d1_nodes_count(p->right);
return count;
}
int my_tree::n_leaf_nodes(){
int count = 0;
count = n_leaf_count(root);
return count;
}
int n_leaf_count(node* p){
if(p == NULL)
return 0;
int count = 0;
count = n_leaf_count(p->left) + n_leaf_count(p->right);
if(p->left == NULL && p->right == NULL)
count ++;
return count;
}
int main()
{
my_tree thetree;
node tmp;
tmp.set_data("Kim", 81.1);
thetree.insert_root(tmp);
tmp.set_data("Lee", 86.2);
thetree.insert_left("Kim", tmp);
tmp.set_data("Park", 79.4);
thetree.insert_right("Kim", tmp);
tmp.set_data("Choi", 77.8);
thetree.insert_left("Lee", tmp);
tmp.set_data("Ryu", 92.2);
thetree.insert_right("Lee", tmp);
tmp.set_data("Cho", 68.6);
thetree.insert_right("Park", tmp);
tmp.set_data("Yang", 72.8);
thetree.insert_left("Ryu", tmp);
tmp.set_data("Jung", 62.3);
thetree.insert_left("Cho", tmp);
cout << "The number of degree-2 nodes : " << thetree.n_d2_nodes() << endl; // degree 2인 node 수
cout << "The number of degree-1 nodes : " << thetree.n_d1_nodes() << endl; // degree 1인 node 수
cout << "The number of leaf nodes : " << thetree.n_leaf_nodes() << endl; // leaf node 수
return 0;
}