Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define CHAR_TO_INDEX(c) ((int)c - (int)'a')//convertim un caracter intr-un index
  5. //structura pentru un nod din arbore
  6. typedef struct Trie
  7. {
  8. char data;
  9. int final_string;
  10. struct Trie *next[26];
  11. }Node;
  12. //structura pentru stiva
  13. typedef struct ELEM
  14. {
  15. Node *valoare;
  16. struct ELEM *next;
  17. }stackNode;
  18. //declaram functiile
  19. Node *createNode ();
  20. void insertNode ( Node *root, char *sir );
  21. int searchNode (Node *root, char *sir );
  22. int isLeaf ( Node * root );
  23. void push ( stackNode **top, Node *v );
  24. Node *pop ( stackNode **top );
  25. void DeleteStack ( stackNode **top );
  26. void sugestie ( Node *root, char *sir );
  27. int autocomplete ( Node *root, char *sir );
  28.  
  29. int main()
  30. {
  31. Node *root = NULL;
  32. root = createNode ();
  33.  
  34. insertNode ( root, "hello" );
  35. printf ( "%d\n", searchNode ( root, "hello" ) );
  36. insertNode ( root, "hel" );
  37. printf ( "%d\n", searchNode ( root, "hel" ) );
  38. insertNode ( root, "ana" );
  39. printf ( "%d\n", searchNode ( root, "ana" ) );
  40. insertNode ( root, "hell" );
  41. printf ( "%d\n", searchNode ( root, "hell" ) );
  42. insertNode ( root, "help" );
  43. printf ( "%d\n", searchNode ( root, "help" ) );
  44. insertNode ( root, "anabel" );
  45. printf ( "%d\n", searchNode ( root, "anabel" ) );
  46. insertNode ( root, "helps" );
  47. printf ( "%d\n", searchNode ( root, "helps" ) );
  48. insertNode ( root, "ce" );
  49. printf ( "%d\n", searchNode ( root, "ce" ) );
  50. insertNode ( root, "helping" );
  51. printf ( "%d\n", searchNode ( root, "helping" ) );
  52.  
  53. int comp = autocomplete ( root, "hel" );
  54. if ( comp == -1 )
  55. printf ( "Nu s-au gasit alte cuvinte cu acest prefix" );
  56. else if ( comp == 0)
  57. printf ( "Nu s-a gasit niciun cuvant cu acest prefix" );
  58. return 0;
  59.  
  60. }
  61.  
  62. Node *createNode ()
  63. {
  64. int i;
  65. Node *newNode = malloc ( sizeof (Node) );
  66. newNode -> final_string = 0;
  67. for ( i = 0; i < 26; i++ )
  68. newNode -> next[i] = NULL;
  69. return newNode;
  70. }
  71.  
  72. void insertNode ( Node *root, char *sir )
  73. {
  74. Node *current = root;
  75. while (*sir)
  76. {
  77. if ( current -> next [ *sir - 'a' ] == NULL )
  78. current -> next [ *sir - 'a' ] = createNode ();
  79. current = current -> next [ *sir - 'a' ];
  80. sir ++;
  81. }
  82. current -> final_string = 1;
  83. }
  84.  
  85. int searchNode (Node *root, char *sir )
  86. {
  87. if ( root == NULL )
  88. return 0;
  89.  
  90. Node *current = root;
  91. while (*sir)
  92. {
  93. current = current -> next [ *sir - 'a' ];
  94. if ( current == NULL )
  95. return 0;
  96. sir++;
  97. }
  98.  
  99. return current -> final_string;
  100. }
  101.  
  102. int isLeaf ( Node * root )
  103. {
  104. int i;
  105. for ( i = 0; i < 26; i++ )
  106. if ( root -> next[i])
  107. return 1;
  108. return 0;
  109. }
  110.  
  111. //adaugare element in stiva
  112. void push ( stackNode **top, Node *v )
  113. {
  114. stackNode *newNode = malloc ( sizeof (Node) );
  115. newNode -> valoare -> data = v -> data;
  116. newNode -> next = *top;
  117. *top = newNode;//adaugam doar in varful stivei
  118. }
  119. //scoatere element din stiva
  120. Node *pop ( stackNode **top )
  121. {
  122. if ( isEmpty ( *top ) )
  123. return NULL;//daca e goala,returnez NULL
  124. stackNode *temp = (*top);//stochez adresa varfului in temp
  125. Node *d = NULL;
  126. strcpy ( d, temp -> valoare );//stochez valoarea din varf intr_un auxiliar
  127. *top = (*top) -> next;//sterg elementul din varf
  128. free (temp);//eliberez memeoria ocupata de temp
  129. return d;
  130. }
  131. //stergere stiva
  132. void DeleteStack ( stackNode **top )
  133. {
  134. stackNode *topCopy = *top, *temp;
  135. while ( topCopy != NULL )
  136. {
  137. temp = topCopy;
  138. topCopy = topCopy -> next;
  139. printf ( "%s", temp -> valoare ->data );
  140. free (temp);
  141. }
  142. *top = NULL;
  143. }
  144.  
  145. void sugestie ( Node *root, char *sir)
  146. {
  147. sir = malloc ( 26 * sizeof (char) );
  148. stackNode *S = NULL;
  149. if ( root -> final_string )
  150. {
  151. printf ( "%s", sir );
  152. printf ("\n");
  153. }
  154.  
  155. if ( isLeaf (root) )
  156. return ;
  157.  
  158. int i;
  159. for ( i = 0; i < 26; i++ )
  160. {
  161. if ( root -> next[i] )
  162. {
  163. push ( &S, root );
  164. sugestie ( root -> next[i], sir );
  165. sir= pop (&S);
  166. }
  167. }
  168. DeleteStack (&S);
  169. }
  170.  
  171. int autocomplete ( Node *root, char *sir )
  172. {
  173. Node *temp = root;
  174. int i, n;
  175. sir = malloc ( 40 * sizeof (char) );
  176. n = strlen (sir) - 1;
  177. int index;
  178. for ( i = 0; i < n; i ++ )
  179. {
  180. index = CHAR_TO_INDEX ( sir[i] );
  181. if ( ! temp -> next[index] )
  182. return 0;
  183. temp = temp -> next[index];
  184. }
  185.  
  186. if ( temp -> final_string == 1 && isLeaf (temp) )
  187. {
  188. printf ( "%s\n", sir );
  189. return -1;
  190. }
  191.  
  192. char *prefix = malloc ( n * sizeof (char) );
  193. if ( isLeaf (temp) )
  194. {
  195. prefix = sir;
  196. sugestie ( temp, prefix );
  197. return 1;
  198. }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement