Advertisement
LeRou

Untitled

May 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. struct node* left;
  8. struct node* right;
  9. struct node* parent;
  10. char data[20];
  11. }node;
  12.  
  13. node *treeroot=NULL;
  14. char Entry[3000][20];
  15.  
  16. node* create_node(char* data)
  17. {
  18. node *new_node = (node*)malloc(sizeof(node));
  19. if(new_node == NULL)
  20. {
  21. fprintf (stderr, "OUT OF MEMORY!!! (create_node)\n");
  22. exit(1);
  23. }
  24. strcpy(new_node->data,data);
  25. new_node->left = NULL;
  26. new_node->right = NULL;
  27. new_node->parent = NULL;
  28. return new_node;
  29. }
  30.  
  31. int compare(char *left,char *right)
  32. {
  33. if(strcmp(left,right)>0)
  34. return 1;
  35. else if(strcmp(left,right)<0)
  36. return -1;
  37. return 0;
  38. }
  39.  
  40. node* insert_node(node *root, char* data)
  41. {
  42. if(root == NULL)
  43. {
  44. root = create_node(data);
  45. treeroot=root;
  46. }
  47. else
  48. {
  49. printf("trying to insert\n");
  50. int is_left = 0;
  51. int r = 0;
  52. node* cursor = root;
  53. node* prev = NULL;
  54.  
  55. while(cursor != NULL)
  56. {
  57. r = compare(data,cursor->data);
  58. prev = cursor;
  59. if(r < 0)
  60. {
  61. is_left = 1;
  62. cursor = cursor->left;
  63. }
  64. else if(r > 0)
  65. {
  66. is_left = 0;
  67. cursor = cursor->right;
  68. }
  69.  
  70. }
  71. if(is_left)
  72. prev->left = create_node(data);
  73. else
  74. prev->right = create_node(data);
  75. }
  76. return root;
  77. }
  78.  
  79. node* search(node *root,char *data)
  80. {
  81. if(root == NULL)
  82. return NULL;
  83.  
  84. int r;
  85. node* cursor = root;
  86. while(cursor != NULL)
  87. {
  88. r = compare(data,cursor->data);
  89. if(r < 0)
  90. cursor = cursor->left;
  91. else if(r > 0)
  92. cursor = cursor->right;
  93. else
  94. return cursor;
  95. }
  96. return cursor;
  97. }
  98.  
  99. void display_tree(node* nd)
  100. {
  101. if (nd == NULL)
  102. return;
  103. printf("%s",nd->data);
  104. if(nd->left != NULL)
  105. {
  106. node* temp = nd->left;
  107. printf("(Left kid:%s )",temp->data);
  108. }
  109. if(nd->right != NULL)
  110. {
  111. node* temp = nd->right;
  112. printf("(Right kid:%s)",temp->data);
  113. }
  114. printf("\n");
  115.  
  116. display_tree(nd->left);
  117. display_tree(nd->right);
  118. }
  119.  
  120. void presucc(node *root, node *succ, node *pre, char* key)
  121. {
  122. if(root==NULL)
  123. {
  124. return 0;
  125. }
  126. else{
  127. if(strcmp(root->data,key)==0)
  128. {
  129. if (root->left != NULL)
  130. {
  131. node* tmp = root->left;
  132. while (tmp->right)
  133. tmp = tmp->right;
  134. pre = tmp ;
  135. }
  136. if (root->right != NULL)
  137. {
  138. node* tmp = root->right ;
  139. while (tmp->left)
  140. tmp = tmp->left ;
  141. succ = tmp ;
  142. }
  143. }
  144. if (strcmp(root->data,key)>0)
  145. {
  146. succ = root ;
  147. root=root->left;
  148. presucc(root, pre, succ, key);
  149. }
  150. else
  151. {
  152. pre = root;
  153. root=root->right;
  154. presucc(root, pre, succ, key);
  155. }
  156. }
  157. }
  158.  
  159. node* sortedArrayToBST(int start, int end)
  160. {
  161. if (start > end)
  162. return NULL;
  163.  
  164. int mid = (start + end)/2;
  165. node *root = create_node(Entry[mid]);
  166.  
  167. root->left = sortedArrayToBST(start, mid-1);
  168. root->right = sortedArrayToBST(mid+1, end);
  169.  
  170. return root;
  171. }
  172.  
  173. void Spellcheck(char* Sentence)
  174. {
  175.  
  176. }
  177.  
  178. int main()
  179. {
  180. FILE *f;
  181. int i;
  182. char path[100];
  183. char Sentence[100];
  184. node* pre = NULL, *suc = NULL,*root;
  185.  
  186. printf("ENTER FILE PATH (INCLUDING FILENAME AND EXTENSION) \n");
  187. fgets(path,sizeof(path),stdin);
  188. path[strlen(path) - 1] = 0;
  189. f=fopen(path,"r");
  190. if(f==NULL)
  191. {
  192. printf("\n%s\" File NOT FOUND! PRESS ANY KEY TO CONTINUE ...",path);
  193. getch();
  194. exit(1);
  195. }
  196. else
  197. {
  198.  
  199. while (!feof(f))
  200. {
  201. for(i=0;i<3000;i++)
  202. {
  203. fscanf(f,"%s",&Entry[i]);
  204. fscanf(f,"\n");
  205. }
  206. }
  207. printf("trying");
  208. root=sortedArrayToBST(0,2999);
  209. display_tree(root);
  210. printf("DICTIONARY LOADED ...\n");
  211. fclose(f);
  212. printf("Please enter the sentence to be checked:\n");
  213. fgets(Sentence,sizeof(Sentence),stdin);
  214. Sentence[strlen(Sentence) - 1] = 0;
  215. Spellcheck(Sentence);
  216. getchar();
  217. }
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement