/*------------------Trie Data Structure----------------------------------*/ /*-------------Implimented for search a word in dictionary---------------*/ /*-----character - 97 used for get the character from the ASCII value-----*/ #include #include #include #include #define ALPHABET_SIZE 26 /*--Node in the Trie--*/ typedef struct TrieNode { struct TrieNode *children[ALPHABET_SIZE]; char character; bool isEndOfWord; } TrieNode; /*--Create new node--*/ TrieNode *createTrieNode() { TrieNode *node; node = malloc(sizeof(TrieNode)); node->isEndOfWord = false; int i = 0; while(ichildren[i] = NULL; i++; } return node; } /*--Insert new word to Trie--*/ void insert(TrieNode *root,char *word) { /*----Addition of the word done by recurcively----*/ //Check wheather word character pointer is NULL if((strlen(word)-1) != 0) { char character = *word; if(root->children[character-97] == NULL) { TrieNode *node = NULL; node = createTrieNode(); node->character = character; root->children[character-97] = node; } word++; insert(root->children[character-97],word); } else { root->isEndOfWord = true; } return; } /*--Search a word in the Trie--*/ TrieNode *search( TrieNode *root, char *word) { TrieNode *temp; while(*word != '\0') { char character = *word; if(root->children[character - 97] != NULL) { temp = root->children[character-97]; word++; root = temp; } else { printf("No possible words!!\n"); return NULL; } } return root; } /*---Print a word in the array--*/ void printArray(char chars[], int len) { int i; for (i=0; icharacter; filledLen++; if (node->isEndOfWord) { printArray(prefix, filledLen); } int i ; for(i=0;ichildren[i], prefix, filledLen); } } /*--Travel through the Trie and return words from it--*/ void traverse(char prefix[], TrieNode *root) { TrieNode *temp = NULL; temp = search(root,prefix); int j=0; while(prefix[j]!='\0') { j++; } printPathsRecur(temp,prefix,j-1); } /*------Demonstrate purposes uses text file called dictionary -------*/ #define NUMBER_OF_WORDS (354935) #define INPUT_WORD_SIZE (100) /*----Get input from the user------*/ char *receiveInput(char *s) { scanf("%99s", s); return s; } int main() { //Read the file dictionary int word_count = 0; char* words[NUMBER_OF_WORDS]; FILE *fp = fopen("dictionary.txt", "r"); if (fp == 0) { fprintf(stderr, "Error while opening dictionary file"); exit(1); } words[word_count] = malloc(INPUT_WORD_SIZE); while (fgets(words[word_count], INPUT_WORD_SIZE, fp)) { word_count++; words[word_count] = malloc(INPUT_WORD_SIZE); } //Push the words in to Trie TrieNode *root = NULL; root = createTrieNode(); int i; for(i=0;i