/
thread_bst.c
287 lines (250 loc) · 5.85 KB
/
thread_bst.c
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
//#include<alloc.h>
//#include<conio.h>
#define MAX 3000
struct treenode
{
struct treenode *leftchild;
int data;
struct treenode *rightchild;
};
typedef struct treenode tree;
typedef struct _thread_data_t
{
int amount; // the size of input array
int list[2000]; // the array to catch all the input numbers
int tid;
int search_num; // what number to search
int total; // how many numbers to search that you have
char *arg;
}thread_data_t;
//typedef struct output_file
tree *insert(tree *, int);
tree *delete(tree *, int);
tree *search(tree *, int);
void inorder(tree *);
tree *find_min(tree *);
tree *find_max(tree *);
void *thr_func(void *);
int main(int argc, char *argv[ ])
{
tree *bst = NULL;
int input[ 5000 ];
char input_num[MAX];
char *temp;
char *temp2;
int b[MAX];
int try[5] = {5,4,3,2,1};
int i=0, num, req;
int j = 0;
int count = 0;
int check = 0;
int input_size = 0;
int a[MAX] = {0};
int test;
char get_ch ;
// FILE pointer of reading and writing
// and thread_t
FILE *fp;
FILE *fw;
// check the arguments if it is exactly three
/*
if(argc != 3){
perror("parameter amount error!");
return 0;
}
*/
// open two files for read input and write output
fp = fopen(argv[1], "r");
//fw = fopen(argv[2], "a");
/*
while( (get_ch = fgetc(fp)) != EOF ){
printf("this character: %s\n",get_ch);
if( get_ch != )
count ++;
}
*/
while( fgets(input_num,MAX,fp) != NULL )
{
printf("this is count: %d\n", count);
temp = strtok(input_num," ");
while( temp != NULL){
printf("%s\n",temp);
//printf("test\n");
if(count == 0){
b[i] = atoi(temp);
//printf("what is b[%d] ? %d\n",i,b[i]);
i++;
check++;
}else{
input[j] = atoi(temp);
j++;
input_size++;
}
//printf("%s ",temp);
temp = strtok(NULL, " ");
}
//char temp[10];
count++;
}
i = 0;
j = 0;
// arr to catch the "to search numbers"
// the value of the array "arr" is the number to search in the bst
int arr[check];
//printf("\n\ncheck: %d\n",check);
for(j=0;j<check;++j){
arr[j] = b[j];
printf(" array arr value: %d \n",arr[j]);
}
printf("the total input numbers' size: %d\n",input_size);
for(i=0;i<input_size;i++)
printf("input[%d] , value: %d\n",i,input[i]);
// create thread according to the number of int's on the first line
pthread_t threads[check];
// thread_data_t type structture initialize
thread_data_t thr_data[check];
// check the thread ids
int check_thread[check];
for(i=0;i<check;++i){
thr_data[i].amount = input_size;
thr_data[i].tid = i;
thr_data[i].search_num = arr[i];
thr_data[i].arg = argv[2];
for(j=0;j<thr_data[i].amount;++j){
thr_data[i].list[j] = input[j];
}
if( (check_thread[i] = pthread_create(&threads[i], NULL, thr_func, &thr_data[i])) ){
fprintf(stderr,"error: pthread_create, thread: %d\n",check_thread[i]);
return EXIT_FAILURE;
}
}
// insert the array into the tree
// print out the data in the inorder fashion
//inorder(bst);
printf("\n");
// to open a file
// while to insert
pthread_exit(NULL);
fclose(fp);
fclose(fw);
return 0;
}
// the functnio that the threads need to do.
void *thr_func(void *arg)
{
thread_data_t *data = (thread_data_t *)arg;
//printf("hello thread, you are thread %d, value: %d\n",data->tid,data->search_num);
tree *node = NULL;
int k ;
// construct tree: insert all node into the tree
for( k = 0; k<data->amount; k++ ){
node = insert(node, data->list[k]);
}
//inorder(node);
// search tree:
int num = 0;
//int a = data->amount;
//int search_arr[data->] = {0};
for( k = 0 ; k < data->amount ; k++){
if( (data->list[k]) == (data->search_num) ){
if( (node = search(node, data->search_num))!= NULL){
num = num + 1;
}
}
}
data->total = num;
printf("number of same: %d\n", num);
FILE *fptr;
fptr = fopen(data->arg,"a");
fprintf(fptr,"search number: %d, the amount: %d\n", data->search_num, num);
pthread_exit("thread exit success\n");
}
tree *find_min( tree *node )
{
tree * current;
current = node;
while(current->leftchild != NULL)
current = current->rightchild;
return current;
}
tree *find_max(tree *node)
{
if( node == NULL)
return NULL;
if( node->rightchild)
return find_max(node->rightchild);
else
return node;
}
tree *insert( tree *node, int num)
{
if( node == NULL ) {
tree *temp;
temp = (tree *)malloc( sizeof( struct treenode ) );
temp->data = num;
temp->leftchild = NULL;
temp->rightchild = NULL;
return temp;
}
if( num < node->data ) // leftchild
node->leftchild = insert( node->leftchild , num );
else
node->rightchild = insert( node->rightchild , num );
return node;
}
tree *delete( tree *node, int num)
{
tree *temp;
if( node == NULL ){
printf("the tree is empty\n");
return node;
}else if(num < node->data){
node->leftchild = delete( node->leftchild , num);
}else if(num > node->data){
node->rightchild = delete( node->rightchild, num);
}else{
// if both children exists,
// use the smallest on the righthand child
if( node->leftchild && node->rightchild){
temp = find_min( node->rightchild );
node->data = temp->data;
node->rightchild = delete( node->rightchild, temp->data);
}else{
temp = node; //
if( node->leftchild == NULL){ // discard the leftchild
temp = node->rightchild;
return temp;
}
else if( node->rightchild == NULL){ // discard rightchild
temp = node->leftchild;
return temp;
}
}
}
return node;
}
tree *search( tree *node, int num)
{
int match = 0;
if( node == NULL){
return 0;
}else if( num < node->data ){
return search( node->leftchild, num);
}else if( num > node->data ){
return search ( node->rightchild, num);
}else
//match = match + 1;
return node;
}
void inorder( tree *bst)
{
if( bst != NULL){
inorder( bst->leftchild);
printf("%d ", bst->data);
inorder( bst->rightchild);
}
}