Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "dictionary.h"
- #define LENGTH 45
- // A Trie node
- // Function that returns a new Trie node
- struct Trie* emptyTrieNode()
- {
- struct Trie* node = (struct Trie*)malloc(sizeof(struct Trie));
- node->leaf = 0;
- for (int i = 0; i < LENGTH; i++)
- node->character[i] = NULL;
- return node;
- }
- // Iterative function to insert a string in Trie
- void addWord(struct Trie *dictionaryTrie, char* str)
- {
- // start from root node
- struct Trie* currentNode = dictionaryTrie;
- while (*str)
- {
- // create a new node if path doesn't exists
- if (currentNode->character[*str - 'a'] == NULL)
- currentNode->character[*str - 'a'] = emptyTrieNode();
- // go to next node
- currentNode = currentNode->character[*str - 'a'];
- // move to next character
- str++;
- }
- // mark currentNodeent node as leaf
- currentNode->leaf = 1;
- }
- // Iterative function to check a string in Trie. It returns 1
- // if the string is found in the Trie, else it returns 0
- bool check(struct Trie* dictionaryTrie, char* str)
- {
- // return 0 if Trie is empty
- if (dictionaryTrie == NULL)
- return false;
- struct Trie* currentNode = dictionaryTrie;
- while (*str)
- {
- // go to next node
- currentNode = currentNode->character[*str - 'a'];
- // if string is invalid (reached end of path in Trie)
- if (currentNode == NULL)
- return false;
- // move to next character
- str++;
- }
- // if currentNodeent node is a leaf and we have reached the
- // end of the string, return 1
- return currentNode->leaf;
- }
- void trieFree()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement