回答編集履歴
2
守勢
test
CHANGED
@@ -56,11 +56,11 @@
|
|
56
56
|
|
57
57
|
else if (x < p->data)↲
|
58
58
|
|
59
|
-
search_tree(x, p->left);↲
|
59
|
+
return search_tree(x, p->left);↲
|
60
60
|
|
61
61
|
else↲
|
62
62
|
|
63
|
-
search_tree(x, p->right);↲
|
63
|
+
return search_tree(x, p->right);↲
|
64
64
|
|
65
65
|
}↲
|
66
66
|
|
@@ -238,11 +238,11 @@
|
|
238
238
|
|
239
239
|
else if (x < p->data)
|
240
240
|
|
241
|
-
search_tree(x, p->left);
|
241
|
+
return search_tree(x, p->left);
|
242
242
|
|
243
243
|
else
|
244
244
|
|
245
|
-
search_tree(x, p->right);
|
245
|
+
return search_tree(x, p->right);
|
246
246
|
|
247
247
|
}
|
248
248
|
|
1
追加
test
CHANGED
@@ -65,3 +65,203 @@
|
|
65
65
|
}↲
|
66
66
|
|
67
67
|
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
全文
|
72
|
+
|
73
|
+
```c
|
74
|
+
|
75
|
+
#include <stdio.h>
|
76
|
+
|
77
|
+
#include <stdlib.h>
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
struct node {
|
82
|
+
|
83
|
+
int data;
|
84
|
+
|
85
|
+
struct node *left;
|
86
|
+
|
87
|
+
struct node *right;
|
88
|
+
|
89
|
+
};
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
struct node *insert_data(int x, struct node *p);
|
94
|
+
|
95
|
+
int search_tree(int x, struct node *p);
|
96
|
+
|
97
|
+
void print_tree(struct node *p);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
int main(int argc, char *argv[]) {
|
102
|
+
|
103
|
+
FILE *fp;
|
104
|
+
|
105
|
+
int i, x;
|
106
|
+
|
107
|
+
struct node *root;
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
if (argc != 2) {
|
112
|
+
|
113
|
+
printf("missing file argument\n");
|
114
|
+
|
115
|
+
return 1;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
fp = fopen(argv[1], "r");
|
122
|
+
|
123
|
+
if (fp == NULL) {
|
124
|
+
|
125
|
+
printf("can't open %s\n", argv[1]);
|
126
|
+
|
127
|
+
return 1;
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
root = NULL;
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
for (i = 0; fscanf(fp, "%d", &x) != EOF; i++) {
|
138
|
+
|
139
|
+
root = insert_data(x, root);
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
print_tree(root);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
while (1) {
|
150
|
+
|
151
|
+
scanf("%d", &x);
|
152
|
+
|
153
|
+
if (x <= 0)
|
154
|
+
|
155
|
+
break;
|
156
|
+
|
157
|
+
if (search_tree(x, root) == 1)
|
158
|
+
|
159
|
+
printf("%d: Found\n", x);
|
160
|
+
|
161
|
+
else
|
162
|
+
|
163
|
+
printf("%d: Not found\n", x);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
fclose(fp);
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
return 0;
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
struct node *insert_data(int x, struct node *p) {
|
180
|
+
|
181
|
+
if (p == NULL) {
|
182
|
+
|
183
|
+
p = (struct node *) malloc(sizeof(struct node));
|
184
|
+
|
185
|
+
if (p == NULL) {
|
186
|
+
|
187
|
+
printf("Out of memory\n");
|
188
|
+
|
189
|
+
exit(1);
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
p->data = x;
|
194
|
+
|
195
|
+
p->left = NULL;
|
196
|
+
|
197
|
+
p->right = NULL;
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
return p;
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
if (x == p->data)
|
208
|
+
|
209
|
+
return p;
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
if (x < p->data)
|
214
|
+
|
215
|
+
p->left = insert_data(x, p->left);
|
216
|
+
|
217
|
+
else
|
218
|
+
|
219
|
+
p->right = insert_data(x, p->right);
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
return p;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
int search_tree(int x, struct node *p) { //自分で書いたのはここです
|
232
|
+
|
233
|
+
if (p == NULL) return 0;
|
234
|
+
|
235
|
+
if (x == p->data)
|
236
|
+
|
237
|
+
return 1;
|
238
|
+
|
239
|
+
else if (x < p->data)
|
240
|
+
|
241
|
+
search_tree(x, p->left);
|
242
|
+
|
243
|
+
else
|
244
|
+
|
245
|
+
search_tree(x, p->right);
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
void print_tree(struct node *p) {
|
252
|
+
|
253
|
+
if (p == NULL)
|
254
|
+
|
255
|
+
return;
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
print_tree(p->left);
|
260
|
+
|
261
|
+
printf("%d\n", p->data);
|
262
|
+
|
263
|
+
print_tree(p->right);
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
```
|