PageRenderTime 85ms CodeModel.GetById 20ms app.highlight 30ms RepoModel.GetById 20ms app.codeStats 0ms

/main.c

https://github.com/eslopez92/Binary_Search_Tree
C | 367 lines | 278 code | 74 blank | 15 comment | 99 complexity | ac3746761c7551ca2978fcf644362b43 MD5 | raw file
  1#include <stdio.h>
  2#include <stdlib.h>
  3
  4struct WORD
  5{
  6    int linearr[8];//a word can be in 8 lines maximum
  7    int linearr_index;//index of line array
  8    int flag;
  9    char word[10];//its actual word with max of 10 letters
 10    struct WORD *LEFT;
 11    struct WORD *RIGHT;
 12};
 13
 14static FILE *input,*output;//input and output file
 15static int char_index = 0;
 16static int line_index = 1;
 17static int itt = 0;
 18static int itter= 0;
 19int main()
 20{
 21
 22    struct WORD *root,*child,*trash;
 23    char *str;//the entire string of the function
 24    char temp_char;//temporary character
 25    int i = 0;
 26    int result;
 27
 28    input = fopen("testcase.txt","r");
 29    output = fopen("output.txt","w");
 30
 31    root = (struct WORD*)malloc(sizeof(struct WORD));
 32
 33    if (root == NULL)
 34    {
 35        printf("allocation error!\n");
 36        exit(0);
 37    }
 38
 39   	while(fscanf(input,"%c",&temp_char)!=EOF)
 40	{
 41        *(str + i) = temp_char;//assigns character to the string
 42        i++;
 43	}//end while
 44    i=0;
 45
 46    assign_word(root,str);//first word of root is first word of str
 47    //root -> LEFT -> linearr[0] = line_index;//first appears a line # ...
 48    child = root;
 49    root -> LEFT = NULL;
 50    root -> RIGHT = NULL;
 51    root ->linearr[0]=1;
 52    root->linearr_index = 0;//initalization
 53    root ->flag = 0;
 54    create_tree(root,child,str,trash);//creates binary tree
 55    //result = compare_words(root, str+36);
 56    //printf("result = %d",result);
 57    //while(*(root->word + i)!= NULL)
 58    //{
 59    //    printf("%c",*(root->word + i));
 60    //    i++;
 61    //}
 62
 63    print_tree(root,root);
 64    fclose(input);
 65    fclose(output);
 66    return 0;
 67}
 68
 69void create_tree(struct WORD *root,struct WORD *child,char *str,struct WORD *trash)
 70{
 71
 72    int check;
 73    check = compare_words(child,str+char_index);//compares the strings
 74    if (*(str + char_index) == '#')
 75        return;//keep returning until you get back to main
 76
 77    if (check == 2 )//node word > string word
 78    {
 79        if (child -> LEFT == NULL)//means this word hasnt been used yet
 80        {
 81            child -> LEFT = (struct WORD*)malloc(sizeof(struct WORD));
 82            child -> LEFT -> linearr[0] = line_index;//first appears a line # ...
 83            child->LEFT->linearr_index = 0;//initalization
 84            child->LEFT->flag = 0;
 85            assign_word(child->LEFT,str);//create new leaf node on the left
 86            itt++;
 87
 88            child = child -> LEFT;//making leaf's left and right node NULL
 89            child -> LEFT = NULL;
 90            //child ->LEFT->flag=1;
 91            child -> RIGHT = NULL;
 92            //child ->RIGHT->flag=1;
 93
 94            child = root;//go back to the root node
 95            create_tree(root,child,str,trash);
 96        }
 97        else
 98        {
 99            child = child -> LEFT;
100            create_tree(root,child,str,trash);
101        }
102
103    }
104
105    else if (check == 1)//node word < string word
106    {
107        if (child -> RIGHT == NULL)//means this word hasnt been used yet
108        {
109            child -> RIGHT = (struct WORD*)malloc(sizeof(struct WORD));
110            child -> RIGHT -> linearr[0] = line_index;//first appears a line # ...
111            child->RIGHT->linearr_index = 0;//initalization
112            child->RIGHT->flag = 0;
113            assign_word(child->RIGHT, str);//create new leaf node on the right
114            itt++;
115
116            child = child -> RIGHT;//making leaf's left and right node NULL
117            child -> LEFT = NULL;
118            //child ->LEFT->flag=1;
119            child -> RIGHT = NULL;
120            //child ->RIGHT->flag=1;
121
122            child = root;//go back to the root node
123            create_tree(root,child,str,trash);
124        }
125        else
126        {
127            child = child -> RIGHT;
128            create_tree(root,child,str,trash);
129        }
130    }
131
132    else if (check == 0)//node word == string word
133    {
134        trash = (struct WORD*)malloc(sizeof(struct WORD));//used to dump unecessary repeated roots
135        child->linearr_index++;//next element in the array
136        child->linearr[child->linearr_index]=line_index;//outputs that word's  line index to that array
137
138        assign_word(trash, str);//emptys the repeated word into the trash
139        itt++;
140        child = root;//go back to the root node
141        create_tree(root,child,str,trash);
142    }
143
144    else
145    {
146        printf("something went wrong!!!!");
147        exit(0);
148    }
149}
150
151void assign_word(struct WORD *wrd,char *str)//assigns strings word to structure's word value
152{
153    int i = 0;
154    while(*(str + char_index)!= 10 && *(str + char_index)!= '.' && *(str + char_index)!= ',' && *(str + char_index)!= ' ')//check to see if at punctuation mark
155        {
156          *(wrd->word + i) = *(str + char_index);
157          i++;
158          char_index++;//traversing through the string
159        }//end while
160
161        while(i<10)
162        {
163            *(wrd->word + i) = NULL;
164            i++;
165        }
166
167        if(*(str + char_index) != '#')//done to skip over punctuation marks
168        {
169            if (*(str + char_index) == '.')//right before line character
170            {
171                line_index++;//static increase
172                char_index++;//skip over period punctuation
173            }
174            char_index++;//skip over punctuation
175        }
176}
177
178int compare_words(struct WORD *wrd,char *str)//compares node's word with the word in the string
179{
180    int i = 0;
181    int keep_going = 1;
182    int counter = 0;//counts to see how many characters are the same
183    do
184    {
185        if (*(wrd->word + i) == *(str + i))
186        {
187            if (*(wrd->word + i + 1) == NULL && ( *(str + i + 1) == ','|| *(str + i + 1) == '.'|| *(str + i + 1) == ' '))//truly the same
188                return 0;//truly the same
189            else if(*(wrd->word + i + 1) == NULL && (*(str + i + 1) != ',' && *(str + i + 1) != '.' && *(str + i + 1) != ' '))
190                return 1;//1 means the node's word in higher than the string in a aplhabetical manner
191            else if(*(wrd->word + i + 1) != NULL && (*(str + i + 1) == ',' || *(str + i + 1) == '.' || *(str + i + 1) == ' '))
192                return 2;//2 means the string in higher than the node's word in a aplhabetical manner
193            //else
194                //the next characters are the same
195        }
196        else if(*(wrd->word + i) > *(str + i))
197            return 1;//1 means the node's word in higher than the string in a aplhabetical manner
198        else if(*(wrd->word + i) < *(str + i))
199            return 2;//2 means the string in higher than the node's word in a aplhabetical manner
200
201            i++;
202
203    } while(keep_going);
204
205    return 3;//means they are the same
206}
207
208void print_tree(struct WORD *child, struct WORD *root)
209{
210    int i = 0, j = 0;
211
212    itter++;
213    struct WORD *temp, *trash_ptr;
214//    trash = (struct WORD*)malloc(sizeof(struct WORD));//used to dump unecessary repeated roots
215
216    if ((child == root && child ->RIGHT->flag==1 && child ->flag != 1))//leaf node and print
217    {
218            while(*(child->word + i) != NULL)
219        {
220            fprintf(output,"%c",*(child->word + i));
221            i++;
222        }
223
224
225        if (*(child->word) != NULL)
226        {
227
228            for (j=0;j <= child->linearr_index;j++)
229                fprintf(output,"\t %d",child->linearr[j]);
230
231            fprintf(output,"\n\n");
232        }
233
234        child ->flag = 1;
235        temp = root;
236
237        print_tree(temp,root);
238
239    }
240    if ((child ->RIGHT==NULL && child ->LEFT==NULL))//leaf node and print
241    {
242            while(*(child->word + i) != NULL)
243        {
244           fprintf(output,"%c",*(child->word + i));
245            i++;
246        }
247
248        if (*(child->word) != NULL)
249              {
250                for (j=0;j <= child->linearr_index;j++)
251                    fprintf(output,"\t %d",child->linearr[j]);
252
253                fprintf(output,"\n\n");
254              }
255
256        child ->flag = 1;
257        temp = root;
258
259        print_tree(temp,root);
260
261    }
262
263    else if (child ->RIGHT!=NULL && child ->LEFT!=NULL)
264    {
265        if(child ->RIGHT->flag==1 && child ->LEFT->flag==1)
266        {
267            if(child->flag == 1)
268                return;
269
270                while(*(child->word + i) != NULL)
271            {
272                fprintf(output,"%c",*(child->word + i));
273                i++;
274            }//end while
275
276
277            if (*(child->word) != NULL)
278              {
279                  for (j=0;j <= child->linearr_index;j++)
280                    fprintf(output,"\t %d",child->linearr[j]);
281
282                fprintf(output,"\n\n");
283              }
284
285            child ->flag = 1;
286            temp = root;
287
288            print_tree(temp,root);
289        }//end if
290
291    }//end else if
292
293
294    if (child->RIGHT!= NULL)
295    {
296        if (child ->RIGHT->flag == 1 && child ->LEFT==NULL)
297        {
298                while(*(child->word + i) != NULL)
299                {
300                    fprintf(output,"%c",*(child->word + i));
301                    i++;
302                }//end while
303
304
305                if (*(child->word) != NULL)
306                  {
307                    for (j=0;j <= child->linearr_index;j++)
308                        fprintf(output,"\t %d",child->linearr[j]);
309
310                    fprintf(output,"\n\n");
311                  }
312
313                child ->flag = 1;
314                temp = root;
315
316                print_tree(temp,root);
317
318        }//end else if
319
320        if (child ->RIGHT->flag != 1)
321        {
322            child = child -> RIGHT;
323            print_tree(child,root);
324        }
325
326    }
327
328
329    if (child->LEFT != NULL)
330    {
331        if (child ->RIGHT==NULL && child ->LEFT-> flag==1)
332        {
333
334                while(*(child->word + i) != NULL)
335                {
336                    fprintf(output,"%c",*(child->word + i));
337                    i++;
338                }//end while
339
340
341                if (*(child->word) != NULL)
342                  {
343                    for (j=0;j <= child->linearr_index;j++)
344                        fprintf(output,"\t %d",child->linearr[j]);
345
346                    fprintf(output,"\n\n");
347                  }
348
349                child ->flag = 1;
350                temp = root;
351                print_tree(temp,root);
352
353
354        }//end if
355
356        if (child ->LEFT->flag != 1)
357        {
358            child = child -> LEFT;
359            print_tree(child,root);
360        }
361    }
362
363
364
365    return;
366    fprintf(output,"\n\n");
367}