回答編集履歴
2
守勢
answer
CHANGED
@@ -27,9 +27,9 @@
|
|
27
27
|
if (x == p->data)↲
|
28
28
|
return 1;↲
|
29
29
|
else if (x < p->data)↲
|
30
|
-
search_tree(x, p->left);↲
|
30
|
+
return search_tree(x, p->left);↲
|
31
31
|
else↲
|
32
|
-
search_tree(x, p->right);↲
|
32
|
+
return search_tree(x, p->right);↲
|
33
33
|
}↲
|
34
34
|
```
|
35
35
|
|
@@ -118,9 +118,9 @@
|
|
118
118
|
if (x == p->data)
|
119
119
|
return 1;
|
120
120
|
else if (x < p->data)
|
121
|
-
search_tree(x, p->left);
|
121
|
+
return search_tree(x, p->left);
|
122
122
|
else
|
123
|
-
search_tree(x, p->right);
|
123
|
+
return search_tree(x, p->right);
|
124
124
|
}
|
125
125
|
|
126
126
|
void print_tree(struct node *p) {
|
1
追加
answer
CHANGED
@@ -31,4 +31,104 @@
|
|
31
31
|
else↲
|
32
32
|
search_tree(x, p->right);↲
|
33
33
|
}↲
|
34
|
+
```
|
35
|
+
|
36
|
+
全文
|
37
|
+
```c
|
38
|
+
#include <stdio.h>
|
39
|
+
#include <stdlib.h>
|
40
|
+
|
41
|
+
struct node {
|
42
|
+
int data;
|
43
|
+
struct node *left;
|
44
|
+
struct node *right;
|
45
|
+
};
|
46
|
+
|
47
|
+
struct node *insert_data(int x, struct node *p);
|
48
|
+
int search_tree(int x, struct node *p);
|
49
|
+
void print_tree(struct node *p);
|
50
|
+
|
51
|
+
int main(int argc, char *argv[]) {
|
52
|
+
FILE *fp;
|
53
|
+
int i, x;
|
54
|
+
struct node *root;
|
55
|
+
|
56
|
+
if (argc != 2) {
|
57
|
+
printf("missing file argument\n");
|
58
|
+
return 1;
|
59
|
+
}
|
60
|
+
|
61
|
+
fp = fopen(argv[1], "r");
|
62
|
+
if (fp == NULL) {
|
63
|
+
printf("can't open %s\n", argv[1]);
|
64
|
+
return 1;
|
65
|
+
}
|
66
|
+
|
67
|
+
root = NULL;
|
68
|
+
|
69
|
+
for (i = 0; fscanf(fp, "%d", &x) != EOF; i++) {
|
70
|
+
root = insert_data(x, root);
|
71
|
+
}
|
72
|
+
|
73
|
+
print_tree(root);
|
74
|
+
|
75
|
+
while (1) {
|
76
|
+
scanf("%d", &x);
|
77
|
+
if (x <= 0)
|
78
|
+
break;
|
79
|
+
if (search_tree(x, root) == 1)
|
80
|
+
printf("%d: Found\n", x);
|
81
|
+
else
|
82
|
+
printf("%d: Not found\n", x);
|
83
|
+
}
|
84
|
+
|
85
|
+
fclose(fp);
|
86
|
+
|
87
|
+
return 0;
|
88
|
+
}
|
89
|
+
|
90
|
+
struct node *insert_data(int x, struct node *p) {
|
91
|
+
if (p == NULL) {
|
92
|
+
p = (struct node *) malloc(sizeof(struct node));
|
93
|
+
if (p == NULL) {
|
94
|
+
printf("Out of memory\n");
|
95
|
+
exit(1);
|
96
|
+
}
|
97
|
+
p->data = x;
|
98
|
+
p->left = NULL;
|
99
|
+
p->right = NULL;
|
100
|
+
|
101
|
+
return p;
|
102
|
+
}
|
103
|
+
|
104
|
+
if (x == p->data)
|
105
|
+
return p;
|
106
|
+
|
107
|
+
if (x < p->data)
|
108
|
+
p->left = insert_data(x, p->left);
|
109
|
+
else
|
110
|
+
p->right = insert_data(x, p->right);
|
111
|
+
|
112
|
+
return p;
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
int search_tree(int x, struct node *p) { //自分で書いたのはここです
|
117
|
+
if (p == NULL) return 0;
|
118
|
+
if (x == p->data)
|
119
|
+
return 1;
|
120
|
+
else if (x < p->data)
|
121
|
+
search_tree(x, p->left);
|
122
|
+
else
|
123
|
+
search_tree(x, p->right);
|
124
|
+
}
|
125
|
+
|
126
|
+
void print_tree(struct node *p) {
|
127
|
+
if (p == NULL)
|
128
|
+
return;
|
129
|
+
|
130
|
+
print_tree(p->left);
|
131
|
+
printf("%d\n", p->data);
|
132
|
+
print_tree(p->right);
|
133
|
+
}
|
34
134
|
```
|