Advertisement
LeRou

Untitled

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