// Adds transformation (string1 => string2 : value) to the trie t int addToTrie(Trie *t, wchar_t *string1, int strLen1, wchar_t *string2, double value){ TrieNode *tmp; /* if the trie is empty ... */ if(t->firstNode == NULL){ t->firstNode = newTrieNode(*string1); tmp = t->firstNode; if((strLen1-1) == 0) tmp->replacement = newEndNode(string2, value); else addToTrieDepth(tmp, string1+1, strLen1-1, string2, value); return 0; } tmp = t->firstNode; TrieNode *prev; prev = NULL; /* find common prefix from the trie */ while(strLen1 > 0){ /* if the left side already exists, add replacement to its ending node */ if(strLen1 == 1 && tmp->label == *string1){ addEndNode(tmp,string2, value); return 0; } /* if the label matches, proceed to the next level in depth */ else if(tmp->label == *string1 && tmp->nextNode != NULL){ prev = tmp; tmp = tmp->nextNode; string1 = string1 + 1; strLen1--; } /* if we are at the end of known prefix, expand */ else if(tmp->label == *string1 && tmp->nextNode == NULL){ addToTrieDepth(tmp, string1+1,strLen1-1, string2, value); return 0; } /* if labels did not match, check the next node in the same depth level ...*/ else if(tmp->rightNode != NULL) tmp = tmp->rightNode; else break; } /* if common prefix was not found or was incomplete, a new branch must be started */ tmp->rightNode = newTrieNode(*string1); tmp->rightNode->prevNode = prev; tmp = tmp->rightNode; if(strLen1 == 1) addEndNode(tmp,string2, value); else addToTrieDepth(tmp, (string1+1),strLen1-1, string2, value); return 0; }
int main() { struct TrieNode *root = newTrieNode(); insert(root, "hello"); printf("%d\n", search(root, "hel")); free(root); return 0; }
//Driver function. int main() { /* Change third ipAddress for validation */ char ipAdd[][MAX] = {"107.108.11.123", "107.109.123.255", "74.125.200.106"}; char URL[][50] = {"www.samsung.com", "www.samsung.net", "www.google.in"}; int n = sizeof(ipAdd)/sizeof(ipAdd[0]); struct trieNode *root = newTrieNode(); // Inserts all the ip address and their corresponding // domain name after ip address validation. for (int i=0; i<n; i++) insert(root,ipAdd[i],URL[i]); // If reverse DNS look up succeeds print the domain // name along with DNS resolved. char ip[] = "107.108.11.123"; char *res_url = searchDNSCache(root, ip); if (res_url != NULL) printf("Reverse DNS look up resolved in cache:\n%s --> %s", ip, res_url); else printf("Reverse DNS look up not resolved in cache "); return 0; }
// This method inserts an ip address and the corresponding // domain name in the trie. The last node in Trie contains the URL. void insert(struct trieNode *root, char *ipAdd, char *URL) { // Length of the ip address int len = strlen(ipAdd); struct trieNode *pCrawl = root; // Traversing over the length of the ip address. for (int level=0; level<len; level++) { // Get index of child node from current character // in ipAdd[]. Index must be from 0 to 10 where // 0 to 9 is used for digits and 10 for dot int index = getIndex(ipAdd[level]); // Create a new child if not exist already if (!pCrawl->child[index]) pCrawl->child[index] = newTrieNode(); // Move to the child pCrawl = pCrawl->child[index]; } //Below needs to be carried out for the last node. //Save the corresponding URL of the ip address in the //last node of trie. pCrawl->isLeaf = true; pCrawl->URL = new char[strlen(URL) + 1]; strcpy(pCrawl->URL, URL); }
// Adds string1 to trie, while expanding only downwards (in depth) via nextNode links int addToTrieDepth(TrieNode *node, wchar_t *string1, int strLen1, wchar_t *string2, double value){ TrieNode *tmp; /* more than 1 letters to add */ if(strLen1 > 1){ node->nextNode = newTrieNode(*string1); node->nextNode->prevNode = node; return addToTrieDepth(node->nextNode, string1+1, strLen1-1, string2, value); } /* only 1 letter to add */ else{ tmp = newTrieNode(*string1); tmp->prevNode = node; node->nextNode = tmp; tmp->replacement = newEndNode(string2, value); } return 0; }
/** * Constructs and returns a trie given a list of words. */ static TrieNode* buildTrie(char* dict[], int dictLength) { TrieNode* root = newTrieNode(); int d; for(d = 0; d < dictLength; d++) { addTrieWord(root, dict[d]); } return root; }
void insert(struct TrieNode *root, char *word) { int i, length, index; length = strlen(word); if (length <= 0) return; struct TrieNode *current = root; for (i = 0; i < length; i++) { index = word[i] - 'a'; if (current->children[index] == NULL) { current->children[index] = newTrieNode(); } current = current->children[index]; } current->isWord = true; }
/** * Used by addTrieWord() to actually add a word to the trie, allocating any new * nodes required. */ static void addTrieWordRecurse(TrieNode* node, char* word) { int index = *word - FIRST_ASCII_CH; if(node->children[index] == NULL) { node->children[index] = newTrieNode(); } if(word[1] == '\0') { node->children[index]->isWord = TRUE; } else { addTrieWordRecurse(node->children[index], word + 1); } }
//-------------------------------------------------------------------------------------------------- // A utility function to insert a word to Trie void insert(struct TrieNode** root, char* word, int index) { if (*root == NULL) // Base case *root = newTrieNode(); if (*word != '\0') //Down - here we are calling insert as well as we are inserting insert( &( (*root)->child[tolower(*word) - 'a'] ), word+1, index ); //ch into child array. else // while recursion stack unwinding, it will go inside to else only once for last char '\0' { if ((*root)->isEnd) // Insert index of this word to end of index linked list { // going inside this means this word is already present in Trie DS IndexNode* pCrawl = (*root)->head; while( pCrawl->next ) pCrawl = pCrawl->next; // going till the last of the link. pCrawl->next = newIndexNode(index); } else // If Index list is empty { // We are first time adding this word into Trie DS. (*root)->isEnd = 1; (*root)->head = newIndexNode(index); } } }