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